Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Faster User Switching of a different kind UNIX
Many people may be familiar with the su UNIX command and its sibling sudo. This allows a user to do stuff as the "root user." However, the su command can also be used to change the identity of a user (and thus have access to a different set of files) when running Terminal, or a *term in X11.

In my particular case, I have the original account I created when I first isntalled OS X, and I recently created an acount for programming and web serving. These accounts belong to different UNIX "groups," so when I think up a solution to a programming problem while surfing the web in my original account, I can't simply open a Terminal and cd to the other account. However, from the same terminal, I can do the following:
 % su other_account_name
I will be prompted for the password to the other account, and thus logged in as the web server/programming user. What's more, since I've stuck with the original prompt that Mac OS X has set up, it lets me know that I'm the web server/programming user. Typing exit at any point brings me back to my original account.
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[5,537 views]  

Faster User Switching of a different kind | 12 comments | Create New Account
Click here to return to the 'Faster User Switching of a different kind' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Faster User Switching of a different kind
Authored by: MacOSXAddict on Mar 16, '04 10:46:17AM

If I rember my *NIX correctly, SU stands for "S"witch "U"ser. Where as SUDO stands for "S"uper "U"ser DO (as in do something as the root/wheel/super user). But it has been years since my last *NIX class...

Bob

---
Bob



[ Reply to This | # ]
Faster User Switching of a different kind
Authored by: TvE on Mar 16, '04 12:38:06PM

Hmm - is'nt SUDO Substitute User DO?
It's not necessarily a super user that one is switching to



[ Reply to This | # ]
sudo
Authored by: sjk on Mar 16, '04 02:37:31PM
Visit the Sudo site for more info. And it's pronounced "soo-doo", not "soo-doe". :-)

[ Reply to This | # ]
Faster User Switching of a different kind
Authored by: MordEth on Mar 16, '04 11:07:38AM

actually, with the proper permissions, it's entirely possible for you to cd into the other account...you'd just still be writing files as you (and not the other account), and if you wanted the other account to own the files, you could do "chown -R user ~user" as root (either via su or sudo). i mirror my girlfriend's webpage and help her work on it in more or less this manner, since i setup a group that we're both in, and i generally write files from Photoshop directly into her web directory. [unfortunately for this hint, Photoshop writes as the logged in user.]

another note...if you actually have different environment files for the other account (the names of these files would vary depending on which shell you're using, but if you're using 10.3's default bash, they'd be ~/.bash_profile, ~/.bashrc, and ~/.bash_logout, plus anything you call from those files), you'd want to use "su - user", rather than "su user". without the -, su keeps your environment, not the user's that you're switching to. a good example: "su - user" will put you in their home directory, while "su user" will keep you in whichever dir you happen to be in when you use the command. this is useful to note if you take advantage of the alias built-in (command).

so one can do things like:

alias 'chweb'='chmod 604 *.html *.php* *.phtml *.xml *.jpg *.png *.gif *.css *.txt *.ssi *.pdf *.doc *.rtf *.mp3 *.mov *.swf *.js 2> /dev/null'
alias 'www'='cd ~/web_directory'

and be able to set this sort of thing per account. i use a horrific number of aliases, so i break them into files by category, and it's easy to source them with ~/.bash_profile or ~/.bashrc like so:

[ -r alias_file ] && source alias_file

(if it can read your alias file, it sources [or loads] it.)

hopefully this helps anyone that's interested in using this hint.



[ Reply to This | # ]
Another option is login
Authored by: googoo on Mar 16, '04 01:40:23PM

You can also use the login command in a terminal session:

% login other_username

You will be prompted for the user's password, and the terminal session will switch to the other user's account. The difference between login and su is that login does not return to the original account after exiting. (The process just ends as if the other user logged out of a regular terminal session.)

-Mark

[ Reply to This | # ]

Duh
Authored by: SOX on Mar 16, '04 02:23:06PM
This is a hint? Sigh, pardon my unix user arrogance.

anyhow, This sort of solution to your problem is sort of lame anyhow. Two more proper solutions are to greate a new group and add all the shared users to it. Then
chmod g+wrX -R ~user1 ~user2 ~user3 ....
now you will be able to have free terminal level acces to the other users. Interestingly, for reasons I have not figured out yet the finder may still block you from accessing these. Anyone know how to fix that???

The other solution is to create a folder in /scratch/shared for each of the users and use this as a common place to exchage files. Again the group permissions need to be set.

and as long as you want to do uncool tricks with sudo and su, then note an even more powerful trick is to type
sudo su other_user
this way you only need your own admin password to switch to anyone else, not their password. Again dont do this as a permenant solution.

[ Reply to This | # ]

sudo -u
Authored by: makaio on Mar 16, '04 07:47:37PM
On a similar note, you can also use:


sudo -u username command

Assuming you're an administrator, this will let you run a single command as another user. And if you always need to run a command as another user, you can add something like:


alias "command"="sudo -u username command"

to your .bashrc file and it will automatically run this command as the other user.

And finally, for more sudo fun than anyone could possibly handle, check out the sudo and sudoers man pages.

[ Reply to This | # ]

sudo -u
Authored by: jms1 on Mar 18, '04 12:50:57AM

actually it would be the ".tcshrc" file... OSX uses tcsh as the default login shell, not bash.

the line in .tcshrc would look something like this:

alias blah "su -c 'command goes here' userid"



[ Reply to This | # ]
Faster User Switching of a different kind
Authored by: datajedi on Mar 17, '04 07:47:59PM

If anbody knows how to use su and then run a .app, say TextEdit, as another user, please let me know. Before Panther I would get errors for everybody except root, now it launches it under my original UID.

Best I can do is use X11, or fast user switching, but fast user switching changes the whole desktop.



[ Reply to This | # ]
Faster User Switching of a different kind
Authored by: SOX on Mar 18, '04 12:32:24AM

For some applications the follwoing will work:
open the package contents, find the actual executable, and run this with su.
It wont work for all apps.



[ Reply to This | # ]
Faster User Switching of a different kind
Authored by: jms1 on Mar 18, '04 01:03:57AM
it would look somthing like this:
su userid -c "/Applications/TextEdit.app/Contents/MacOS/TextEdit &"
you will be prompted for the other user's password, and then the program will start running as the other user.

also, something i noticed while trying this... i come from a unix background and am used to typing "su -c 'command' userid"... for some reason, the "su" command in OSX requires the "-c" option to come AFTER the userid. unix hackers beware...

[ Reply to This | # ]

Faster User Switching of a different kind
Authored by: elmimmo on Mar 19, '04 03:49:28AM

Look it up in this hinton why it does not work in all apps and how to do it with the ones that does not work. I wrote this hint before 10.3, and at the time you could only launch applications as a different user if that user was root, because the new user would not have permissions to use the original user's window manager (so no gui allowed, only cli). So, in other words, you could only do sudo the_command_line_of_said_hint_without_ending_'&' and not sudo -user username the_command_line_of_said_hint_without_ending_'&'. I do not know if this has changed in 10.3, since my computer is set with only one account.



[ Reply to This | # ]