0

The centos Server and ubuntu host machine both has gedit installed. Root login to the server is blocked and only a user can login via SSH through rsa keys.

When I log in to the server with -X supplied as parameter, I can use gedit like:
gedit filename.txt &
and the file opens in gedit for me to edit and save. But if I do Sudo to edit files then I get following error:

sudo gedit filename.txt &

[1] 11039
[user@server ~]$ X11 connection rejected because of wrong authentication.

** (gedit:11040): WARNING **: Could not open X display
X11 connection rejected because of wrong authentication.
X11 connection rejected because of wrong authentication.
X11 connection rejected because of wrong authentication.

(gedit:11040): Gtk-WARNING **: cannot open display: localhost:10.0

[1]+  Exit 1                  sudo gedit filename.txt

How can I use Gedit in such situation.

2

You can use the sudoedit command instead.

EDITOR=gedit sudoedit filename.txt

what this does is take a copy of the file, then runs the editor as you, then if it detects the file has changed copy the results back.

In most cases has the same effect as sudo gedit filename.txt but it runs the editor unprivileged and so your X forwarding should still work.

  • thanks but if I have to edit other files not just one file in such case? – user34260 Sep 6 '16 at 13:43
  • You can do sudoedit file1 file2 file3 and it'll make copies of all three files, call $EDITOR tmpfile1 tmpfile2 tmpfile3 and then compare them for changes and copy back the changed ones. – Stephen Harris Sep 6 '16 at 13:50
1

Just to provide an explanation, when you run sudo it resets a lot of environment variables, including $HOME which is reset to root's HOME (/root probably). When you run your X11 program it needs an XAUTHORITY magic cookie to allows it to connect to the DISPLAY. The appropriate cookie will have been placed by ssh -X in ~/.Xauthority on the remote when you connected.

But after sudo your X11 program will look in /root/.Xauthority, and so get no cookie, or an old, stale, cookie.

Hence the wrong authentication messages you see. There are several solutions to preserve or restore $HOME after sudo, but for your particular case, it is wisest to use sudoedit as in the other answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy