PDA

View Full Version : Difference Between "su" and "su -"



dfelker
30th September 2005, 05:38 PM
Can someone explain the main differences between "su" and "su -"

I saw this in a thread on this board in regards to getting ifconfig to work.

Thanks!

niko123456
30th September 2005, 06:05 PM
not a great deal really.

'su' is used to switch users, and by default 'su' goes straight to root.

'su -' is used to switch to root, and also puts you in the root directory.

but you can really use both interchangeable. mostly these days I use 'su -c "make install"' or su -c "command needing root".

anyway, try it out and see. you'll notice 'su -' takes you away from your current directory..

RedFedora
30th September 2005, 06:06 PM
Taken from the su manual page:

-, -l, --login
make the shell a login shell

As far as I can tell, using the "-" causes two differences.
1. When I use "-" the PATH for the new user is loaded from their
own init files. Effectively, the user has logged in and loaded their
personal settings. With a simple "su" (no -) you're just changing
the permissions and home directory.

2. I notice when I login with "su -" and then run "exit", my terminal clears.
When just using "su" the screen doesn't clear, as if to start a new session.

To sum up, as I understand it, the "-" option does a full login of the
target user, rather than just changing your permissions and home
directory to match those of the target user.

Urkburk
30th September 2005, 07:58 PM
From: http://www.fedorafaq.org/basics/#root


Q: What is root? How do I "become root?"
A: "root" is the name of the user who can control everything on the entire computer. We call this the "super user."

To become root, first you must open a terminal. Then, you type:

su -

Note that the "-" is very important -- it sets things so that you have easy access to all the commands on the computer.

Then, you have to type in your root password. You set this when you installed Fedora.

If you're using the "open a terminal" method where you make the whole screen into a terminal, then you just need to put "root" as your "Login:" name, and then enter the correct password.

rega451
1st October 2005, 01:46 AM
Piecing all these "bits of wisdom" together it really seems like it comes down to this...If you are the only registered user on the system then using 'su' is sufficient. If you have other registered users and you want your root level changes to be seen by them (global) then use 'su -' . If on the other hand you have other registered users and you do not want your changes seen by them (local) then use 'su' instead. In any case, all changes made using 'su' or 'su -' will be seen at the root (administrator) level. Does that sound about right?

nick.stumpos
1st October 2005, 01:53 AM
not really by using "su -" you are opening a login shell which actually changes the $PATH variable among others to the values assigned for root. thus if you need root privilages to run a program in roots path i.e. modprobe you would use "su -" however if you need root priviliages to edit a file in your pwd it may be simpler to use just "su" as it doesn't change to roots home ditectory. However if you know the exact paths to all programs you wish to run i.e. /sbin/modprobe than it really becomes a matter of preference.

rega451
1st October 2005, 03:47 AM
Then it would seem using 'su' is more practical especially when you are compiling a package for installation since most users do a './configure' and 'make' as a normal user then switch to root level to do the 'make install'. If you use 'su -' you will be redirected to the root directory and have to path back to the working directory to do a 'make install'. However, if you just use 'su' you will stay in the working directory and won't have to path back into the working directory from root. Hmmm. Maybe 'su -' isn't as important as some of these articles seem to say it is. In fact, 'su -' can sometimes be downright inconvenient as far as I can tell. It seems to be useful only if you need to run something from the root directory. And that is even a moot issue if your user path is the same as your root path. Personally, I have never had a problem using just 'su' to do anything at root level.

Urkburk
1st October 2005, 04:22 PM
Then it would seem using 'su' is more practical especially when you are compiling a package for installation since most users do a './configure' and 'make' as a normal user then switch to root level to do the 'make install'. If you use 'su -' you will be redirected to the root directory and have to path back to the working directory to do a 'make install'. However, if you just use 'su' you will stay in the working directory and won't have to path back into the working directory from root. Hmmm. Maybe 'su -' isn't as important as some of these articles seem to say it is. In fact, 'su -' can sometimes be downright inconvenient as far as I can tell. It seems to be useful only if you need to run something from the root directory. And that is even a moot issue if your user path is the same as your root path. Personally, I have never had a problem using just 'su' to do anything at root level.
But... I must be able to change somewhere so it won't take me to /root every time I do 'su -'?

rega451
1st October 2005, 09:10 PM
But... I must be able to change somewhere so it won't take me to /root every time I do 'su -'?

That's part of the point of my last post. Using 'su -' will switch your directory to root, 'su' (without the '-') leaves you in the directory you are working in. Frankly, between all the discussions and articles on the subject, that seems to be the only thing it really does....IMHO ;)

sailor
1st October 2005, 10:30 PM
su - ....my understanding is that it keeps the path of the user. This way you will not have to type in the whole path name to a command.

rega451
1st October 2005, 11:06 PM
Using 'su -' switches to the root directory and uses roots path associations. As an example, if you use 'su' (without the '-') you will stay in the current directory and use the user path associations. So, if you just use 'su' and try to run say, modprobe and you don't have '/sbin' in your user path you will get a "Command not found" message if you just type 'modprobe'. Now if you use 'su -', you will switch to root and use roots path associations and you can just type 'modprobe' and it will execute. The root path usually has associations to '/sbin' (where modprobe lives), '/usr/sbin' and some other directories that are usually only associated with administrative (root) functions and they are not found in most user paths unless you add them as I did. So, if you haven't added them to your user path and use 'su' and want to run modprobe you would type '/sbin/modprobe'. If 'su -' does more than this I haven't seen any evidence of it.

Firewing1
2nd October 2005, 12:37 AM
if you really need to use su - with out switching the dir, do this:

pwd
su -
cd [copy-paste dir given in step 1]
Firewing1