Sometimes I see in many websites' tutorials the use of sudo -H + command
for example sudo -H gedit /etc/apt/sources.list
. However I know that sudo gedit /etc/apt/sources.list
can do the work.
So anyone please can tell me what is the difference between sudo -H
and sudo
?

- 6,880
-
3See Why should users never use normal sudo to start graphical applications? – steeldriver Nov 09 '18 at 13:08
3 Answers
The difference comes when e.g. gedit
writes a preference file or something similar to $HOME
when it is closed. Many programs do that.
When you run
sudo gedit ...
then $HOME
is still set to your home directory (say /home/singrium
) and gedit
will write its settings there but the preferences file will then be owned by root
. This might make it difficult to run gedit
as your user afterwards because then gedit
cannot write its settings due to a lack of permissions.
When you instead run
sudo -H gedit ...
then $HOME
will be set to root
's home directory (usually /root
) and gedit
will write its preferences file there without affecting your account.

- 13,335
sudo -H
is the same as sudo --set-home
-H, --set-home Request that the security policy set the HOME environment variable to the home directory specified by the target user's password database entry. Depending on the policy, this may be the default behavior.
It avoids the user's home folder permissions to be changed to root.
From man sudo
-H' The -H (HOME) option requests that the security policy set the HOME environment variable to the home directory of the target user (root by default) as specified by the password database. Depending on the policy, this may be the default behavior.
-H gedit
in your case will set the $HOME
variable to point to root
(by default) instead of being your user home dir.

- 1,048