You should never use normal sudo to start graphical applications as
root. You should use gksudo (kdesudo on Kubuntu) to run such programs.
gksudo sets HOME=/root, and copies .Xauthority to a tmp directory.
This prevents files in your home directory becoming owned by root.
Please note that this is primarily about configuration files. If you run Nautilus as root, even with gksu/gksudo, and you create a file or folder anywhere with it (including in your home directory), that file or folder will be owned by root. But if you run Nautilus (or most other graphical applications) as root with sudo, they may save their configuration files in your home directory (rather than root's home directory). Those configuration files may be owned by root and inaccessible when you're not running as root, which can severely mess up your settings, and may even keep some applications from working altogether.
The solution, once you have made this mistake, is to find the configuration files and delete them or chown them back to belonging your non-root user. Many such files start with a . or are contained in a directory that starts with a .. Some are located inside the .config folder in your home directory. To see files and folders that start with a . in Nautilus, press Ctrl+H (this shows hidden files.) To see them with ls, use the -a (or -A) flag.
To find if there are files not owned by you in your home directory, you can use the following command in a terminal:
find $HOME -not -user $USER -exec ls -lad {} \;
which will list all files under the home directory not owned by the user.
If you start a graphical application with sudo you can mess up the ownership of your files which can cause your apps to break. Never do that. Check the Community Help:
You should never use normal sudo to
start graphical applications as Root.
You should use gksudo (kdesudo on
Kubuntu) to run such programs. gksudo
sets HOME=~root, and copies
.Xauthority to a tmp directory. This
prevents files in your home directory
becoming owned by Root. (AFAICT, this
is all that's special about the
environment of the started process
with gksudo vs. sudo).
In Ubuntu (17.10) 17.10.1 and probably future versions of Ubuntu, gksu and gksudo are deprecated. They work in Xorg but not Wayland. But you can use sudo -H to run GUI application programs without damage to your configuration files. Do not use plain sudo.
If you are running Wayland (the new graphical system that replaces Xorg), you need a command with xhost too.
xhost +si:localuser:root # if Wayland
sudo -H nautilus --no-desktop
sudo by default preserves your $HOME variable. For example, if you run sudo firefox you will run with root privileges but your configuration. If you change settings in this situation, you will end up with parts of your configuration owned by root instead of your user id.
Found in the file generated by the terminal command 'info gksudo':
gksu is a front-end to su and gksudo is a front-end to sudo. Their primary purpose is to run graphical commands that need root without the need to run an X terminal emulator and using su directly.
I have found that, in many cases, the 'info' files have helpful descriptions as well as useful information on options. I would strongly suggest that learning about any command first start with accessing the 'info' file on that command.
For the beginner, in the terminal, type or paste the command info with the command name in which you are interested as a parameter. The format is info [command_name] (remember not to include the command prompt or the brackets). Suggested terminal entries for the beginner are:
Taken from here:
Please note that this is primarily about configuration files. If you run Nautilus as
root
, even withgksu
/gksudo
, and you create a file or folder anywhere with it (including in your home directory), that file or folder will be owned byroot
. But if you run Nautilus (or most other graphical applications) asroot
withsudo
, they may save their configuration files in your home directory (rather thanroot
's home directory). Those configuration files may be owned byroot
and inaccessible when you're not running as root, which can severely mess up your settings, and may even keep some applications from working altogether.The solution, once you have made this mistake, is to find the configuration files and delete them or
chown
them back to belonging your non-root
user. Many such files start with a.
or are contained in a directory that starts with a.
. Some are located inside the.config
folder in your home directory. To see files and folders that start with a.
in Nautilus, press Ctrl+H (this shows hidden files.) To see them withls
, use the-a
(or-A
) flag.To find if there are files not owned by you in your home directory, you can use the following command in a terminal:
which will list all files under the home directory not owned by the user.
Did you know there is a Nautilus add on called nautilus-gksu which adds an "open as administrator" to nautilus' right click menu?
Note: Not available in Ubuntu 12.04 and onward.
If you start a graphical application with sudo you can mess up the ownership of your files which can cause your apps to break. Never do that. Check the Community Help:
sudo -H GUI-application-program
In Ubuntu (17.10) 17.10.1 and probably future versions of Ubuntu,
gksu
andgksudo
are deprecated. They work in Xorg but not Wayland. But you can usesudo -H
to run GUI application programs without damage to your configuration files. Do not use plainsudo
.If you are running Wayland (the new graphical system that replaces Xorg), you need a command with
xhost
too.See the following link for more details,
Why don't gksu/gksudo or launching a graphical application with sudo work with Wayland?
sudo by default preserves your $HOME variable. For example, if you run
sudo firefox
you will run with root privileges but your configuration. If you change settings in this situation, you will end up with parts of your configuration owned by root instead of your user id.Found in the file generated by the terminal command 'info gksudo':
I have found that, in many cases, the 'info' files have helpful descriptions as well as useful information on options. I would strongly suggest that learning about any command first start with accessing the 'info' file on that command.
For the beginner, in the terminal, type or paste the command info with the command name in which you are interested as a parameter. The format is
info [command_name]
(remember not to include the command prompt or the brackets). Suggested terminal entries for the beginner are:If you choose gksudo nautilus, You ask your password graphically. With sudo, you ask it in a terminal