19

I have ubuntu 12.04 running in a virtual machine on virtual box. Its existence is mainly as a 'toy' to play about with Ubuntu, so for this reason I wanted the GUI (is that what 'Dash' is in ubuntu 12.04? Anyway...)

One thing I am currently using this machine for is a LAMP server. I use 'gedit' to edit my files, but because I haven't correctly set permissions yet I just open it with sudo in terminal.

Just now, to try to simplify this process I created a shell script on the desktop with a very simple job - sudo gedit

I can't help thinking there must be an even more obvious way to open gedit as root from the GUI than this. So is there?

Edit: I just 'improved' my script by replacing sudo with gksudo. Now I don't have to have a terminal window open while gedit is open. So I guess this is quite convenient for now :) It would be good, for the sake of knowledge, to know if this is anything close to how the more seasoned ubuntu users would do this type of thing.

Braiam
  • 66,947
  • 30
  • 177
  • 264
MrVimes
  • 375
  • 1
  • 4
  • 12
  • 2
    Yes, `gksudo` (or more commonly known as `gksu`) it what most people use. Lots of reading material: http://askubuntu.com/questions/270006/why-user-should-never-use-normal-sudo-to-start-graphical-application http://askubuntu.com/questions/197772/what-are-the-differences-between-sudo-su-visudo-chroot-and-gksu http://askubuntu.com/questions/284306/why-is-gksu-no-longer-installed-by-default-in-13-04 http://askubuntu.com/questions/11760/what-is-the-difference-between-gksudo-nautilus-and-sudo-nautilus/171971#171971 – Seth Jul 13 '13 at 23:06
  • 2
    Seth, would you be willing to post your reply as an answer? This looks resolved. – Travis G. Jul 13 '13 at 23:14
  • _"Now I don't have to have a terminal window open while gedit is open."_ <- You may already know about this, but if you append `&` to a command [as shown here](http://askubuntu.com/a/319885/23496), you allow the shell script's terminal window to close immediately when `gedit` is launched instead of waiting for it to close. This trick should work with `gksudo` without problems. – IQAndreas Jul 14 '13 at 12:26

7 Answers7

19

Use gksudo gedit.

gksudo is the equivalent of sudo for graphical programs, which are not safe to run using sudo.

Lucio
  • 18,648
  • 31
  • 107
  • 190
Taymon
  • 733
  • 2
  • 8
  • 19
  • 4
    Problem is, `gksu` is not installed by default in Ubuntu 13.04 anymore. I would really love to see an answer for those on 13.04+. The suggested alternative is `pkexec`, but it just doesn't work =/. – Alaa Ali Jul 14 '13 at 13:15
  • 1
    @Alaa But it's still in the repository, so a quick `sudo apt-get install gksu` should install it for you. – IQAndreas Jul 14 '13 at 21:15
  • 1
    If you want an explanation to why it is not recommended to use `sudo`: [What is the difference between “gksudo nautilus” and “sudo nautilus”?](http://askubuntu.com/a/11766/23496) – IQAndreas Jul 15 '13 at 02:05
  • 5
    @IQAndreas, I know it's in the repositories, but it's just inconvenient, if I'm answering a new user and want him to edit a file as root, to tell him "first, run `sudo apt-get install gksu`". Right now, it's a dilemma, at least for me. I can't tell him to "use `pkexec`", because it doesn't work, and it's too risky to `sudo -i` then `gedit`. Also, I know the difference between `gksudo` and `sudo` =), what you might have linked me to instead, is this: [Why is gksu no longer installed by default in 13.04?](http://askubuntu.com/q/284306/52726). – Alaa Ali Jul 15 '13 at 05:31
  • @Alaa Valid point regarding `apt-get`. Actually, my second comment (the one with the link) was directed at the answer you gave, and not a continuation of the discussion in the comments (I figured the poster may want to know the reason why it is "not safe to run using sudo"). – IQAndreas Jul 15 '13 at 06:05
  • @IQAndreas, ooops, my bad, apologies. – Alaa Ali Jul 15 '13 at 06:10
  • ubuntu 14.04: No protocol specified (gksudo:5835): Gtk-WARNING **: cannot open display: :0 – neoDev Jan 28 '17 at 10:27
7

I find myself running gedit as root quite frequently, so I added the option to the right-click menu in Unity (it uses the same command as the selected answer, gksu gedit).

You will need to edit a *.desktop file in order to add the menu option, and you have two choices:

  • /usr/share/applications/gedit.desktop (requires root access to edit) - The menu option will be added for all users. Note that you may need to re-edit this file, adding the option if an update to gedit occurrs that overwrites your changes.
  • ~/.local/share/applications/gedit.desktop (If you can't find it there, make a duplicate of the one found in /usr/share/applications/, and save it to this new location) - The menu option will only be available for the specified user, and is considered "better practice". Note that if you already have it docked to your Unity bar, you will need to choose Unlock from Launcher on your existing GEdit icon, then re-add your "custom" version. These changes should stay even if GEdit gets an update.

Edit the text file gedit.desktop, adding the following block of code (you can use the existing two blocks as a template):

[Desktop Action RootWindow]
Name=Open a Root Window
Exec=gksu gedit
OnlyShowIn=Unity;

Next, add the new action to the list (should be at around line 19 of the text file):

Actions=Window;Document;RootWindow;

Now right-clicking the text editor window in Unity brings up this handy new option:

Ubuntu Unity: Open Gedit as Root

IQAndreas
  • 3,108
  • 3
  • 26
  • 35
4

Use admin:// in front of the file name which brings up the standard GUI password prompt for your respective WM/shell.

So for example, if you wanted to edit your repos, instead of typing sudo gedit /etc/apt/sources.list you would type gedit admin:///etc/apt/sources.list. It works for basically all GUI programs, not just Gedit.

Brandon Giesing
  • 169
  • 1
  • 3
  • This answer is the correct way to do it now. The accepted answer was correct for previous versions of Ubuntu (prior to 17.04, I think). – Enterprise Nov 19 '18 at 15:59
  • When I use admin:// in Ubuntu Desktop 22.04, to access a file (e.g. "admin:///etc/default" followed by selecting "grub", Text Editor requires me to enter a password up to three times. I must type it twice at the Open dialog. Sometimes, Text Editor asks a third time when saving the file. Is there a better way to open files with root privileges? – Zian Choy Apr 17 '23 at 22:34
2

gksudo gedit works in 16.04 LTS, but in in 17.10 with Wayland gksudo does not work.

sudodus
  • 45,126
  • 5
  • 87
  • 151
0

You can open Nautilus as root, gksu nautilus, now any text file you open from that window will open in Gedit as root.

user154126
  • 305
  • 1
  • 10
0

gksudo is apparently not available on newer Ubuntu versions and gksu wasn't working for me, but this answer was very helpful in my case (Ubuntu 17.04).

Use visudo to add the following line to your sudo config file:

Defaults env_keep="XAUTHORIZATION XAUTHORITY TZ PS2 PS1 PATH LS_COLORS KRB5CCNAME HOSTNAME HOME DISPLAY COLORS"`
jtpereyda
  • 2,035
  • 3
  • 20
  • 21
0

EDITOR=gedit sudo -e is recommended.

muru
  • 193,181
  • 53
  • 473
  • 722
funicorn
  • 3,726
  • 2
  • 19
  • 16