0

This question already has an answer here:

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?

marked as duplicate by Community Nov 9 '18 at 13:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

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.

1

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.

1

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.