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
?
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
then
$HOME
is still set to your home directory (say/home/singrium
) andgedit
will write its settings there but the preferences file will then be owned byroot
. This might make it difficult to rungedit
as your user afterwards because thengedit
cannot write its settings due to a lack of permissions.When you instead run
then
$HOME
will be set toroot
's home directory (usually/root
) andgedit
will write its preferences file there without affecting your account.sudo -H
is the same assudo --set-home
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 toroot
(by default) instead of being your user home dir.