There has been a lot of confusion about running graphical applications in Linux using the sudo command, so this tutorial will attempt to clear up some of it. Running GUI programs using sudo is fine, for the most part. However, in some cases it might cause problems. Traditionally some Linux users would recommend the use gksudo instead, but this has been removed in more recent versions of Ubuntu.
The problem with using plain sudu is that you are running the program as your own user with elevated privileges, which means that your $home directory will still be your own, while you will be running the program with root privileges. Obviously, it is not hard to imagine that this could cause problems in some cases.
Running graphical applications with root privileges is not really a bad thing, in fact, you will likely find yourself in many situations where it will be the most convenient thing to do. It is very common run the file manager (Nautilus in Ubuntu) as root when installing programs and performing system tasks, because it is usually faster and easier than typing commands in the terminal.
Running GUI programs as root
Since gksudo is no longer available, what should you use instead? The answer is simple. If you are often running certain graphical programs with sudo then you may want to consider adding the option to the shortcut (.desktop) file of the program. But to just launch the program as root, you can use:
sudo -i [program name]
The -i ensures that the program will be run as root in the root environment, including using the $home directory of root rather than the environment of your user.
If you have more work to do from the terminal, simply type the command once:
Then you do not have to type sudo in front of subsequent commands. I.e.
gedit <file_path> nautilus
You should make sure to close any applications and terminal windows with root access once you are done using them, but this is likely most relevant for system administrators, and if you are worried that someone with physical access to the system might take advantage of the situation.
Dangers in using plain sudo
As mentioned earlier, the problem with using plain sudo is that you will be running the application with your own users environment. The problem can be seen when checking the $HOME and $PATH variables after running the different variants of sudo. I.e.:
k@DreamMachine:~$ sudo -i [sudo] password for k: root@DreamMachine:~# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/snap/bin root@DreamMachine:~# echo $HOME /root root@DreamMachine:~#
In this case the $HOME path was correctly set to /root which is what we want for GUI applications. This avoids the problem where some programs might change the ownership of your own users files to root, and thereby potentially ruining something.