I do the following many times a day

I login to a server via ssh as user1,
I then do su - to become root,
then I do su --login user2 to become user2

Is there a way to tell su to go to user2 directly from user1?

Note:
user21 does not have a password. user2 is named different at different times. (apache, liferay, solr, ftp, ...)

Note2:
user1 is not trusted with sudo it's a general purpose account used mainly to access logs. It doesn't even have a password, access granted only with a sshkey only.

Note3:
NO solutions that means any modification of the remote computer allowed. I have 30+ servers that I admin.

1 If you are curious it's the user an application runs as (tomcat, apache, jetty or similar)

link|improve this question

1  
Try su - -c "su - user2" – Keith Apr 21 '11 at 8:19
@Keith - You should post that as an answer as it is precisely what I'm looking for. – Nifle Apr 21 '11 at 10:38
feedback

3 Answers

up vote 2 down vote accepted

Ok. :-)

This will work:

su -c "su - user2"

I think that's a little bit faster.

link|improve this answer
feedback

You could use 'sudo' instead of 'su'.

Allow user1 to run the shell command (/bin/bash?) as user2 (in the sudoers file - edit with visudo):

user1 ALL=user2 /bin/bash

Then you can (as user1) do:

$ sudo -u user2 /bin/bash
link|improve this answer
Should have mentioned that sudo isn't an option. user1 is a user many people have access to (it's used to inspect logs mostly). I don't want them to have access to user2 – Nifle Apr 21 '11 at 10:37
feedback

I'm pretty sure you can perform:

sudo su user2

All you're doing is moving to root to have the privilege to access that account, you can skip a step but will still need to provide root credentials.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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