Jump to content

- - - - -

su + environment

Posted by notKlaatu , 04 September 2008 · 201 views

Everyone knows that$ su fubar<password>switches user over to, in this case, a user named fubar.However, this doesn't bring along with it fubar's user environment; so, for instance, if you have su'd over to fubar and an app you are using needs to write a file into /home/fubar/.kde file, then that app will not be able to do it because you are fubar but you are not in fubar's user environment.OK, so to switch over to fubar + fubar's world, you must do this:# su - fubar<password>Note the - (dash) between the su and the fubar Now you can do everything the user fubar would normally be able to do without any unexpected permission errors.(I have to thank Popey for that tip; he saved my life with it while I was flirting with postfix. I was amazed I'd never encountered that seemingly basic yet vital distinction in all the beginning UNIX books and courses I've taken........)

Dirk Chestnut
Sep 14 2008 06:38 PM
Indeed, *most* material I've read also shows the example with the dash, but never explains its purpose. Usually what happens is you learn you don't need the dash, so start omitting it, unaware that you're not pulling in that user's environment variables when you do so.

This is mostly an issue when you su from a regular user to root, a la "su -", on a system that doesn't include /sbin/ and /usr/sbin in a regular user's PATH by default. Just using a plain "su" becomes an exercise in frustration when you're switching to root to be able to run some sys admin commands typically located in one of these two directories.

Why did the creators of "su" decide to use an oddball switch like "-" for this purpose? I haven't a clue. With most other commands, supplying a "-" explicitly tells that command to read from STDIN. This is useful for supplying arguments to a switch on one command with the output from another (ex: "find . -name 'expressions.txt' | grep -f - file.txt" - which means search for a file called expressions.txt, and have the grep command use it as a list of regular expressions to execute on the file file.txt).
  • Report
thanks for the info. very cool.
  • Report

November 2015

29 30     

Recent Entries

Recent Comments

Search My Blog

BinRev is hosted by the great people at Lunarpages!