2

What's the difference between su - and bash -l?

I have this problem:

:/etc/profile.d # su -
Cannot open display "default display"
Directory: /root
Thu Nov 28 11:23:57 CET 2013

Now I try to find out why this message occurs.

:~ # logout
:/etc/profile.d # bash -x -l
+ test -f /proc/mounts
+ case "`/bin/ls -l /proc/$$/exe`" in
++ /bin/ls -l /proc/17167/exe
+ is=bash
+ read -t 1 a r
...

In the second output I have no sign of the "cannot open display" message. So what's the difference?

2

The first command logs you in as root; the option - means, from the su Manpage:

-l, --login Provide an environment similar to what the user would expect had the user logged in directly.

The second command, bash -l, does not log you in as root. It is true that the -l option means (again from the bash Manpage):

-l Make bash act as if it had been invoked as a login shell

However, this refers to a different user, in this case yourself instead of root. Thus, the bash -l command maintains the MIT magic cookie which allows control of your X session, while the su -c is not capable of doing that and, since you do not have the appropriate MIT magic cookie, you cannot take control of the X session.

If you want to get around this problem, insert this statement

  export XAUTHORITY=/home/your_name/.Xauthority

in the file /root/.bashrc, and you will be able to open GUIs as root even when you are su'ing instead of using a proper login shell.

| improve this answer | |
  • Sure, su and bash are not the same thing ;-) I now figured out what was happening by adding set -x to /etc/profile. It turned out a script that belonged in /etc/X11/xinit/xinitrc.d/ was present in /etc/profile.d/ – Marki Nov 28 '13 at 15:54
  • BTW the source of the message was a command that was in one of bash's startup sripts (bashrc or profile) whereas it should have been in xinitrc, duh. – Marki Jan 26 '14 at 3:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.