LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Access remotely as user1 and execute command as user2 (http://www.linuxquestions.org/questions/programming-9/access-remotely-as-user1-and-execute-command-as-user2-4175417344/)

archieval 07-18-2012 03:39 AM

Access remotely as user1 and execute command as user2
 
Hello everyone!

Is it possible to execute a command remotely but the username used to login is not the user that will execute the command?

A sequence something like:

Code:

scp user1@10.22.10.111:/tmp/file.txt
ssh user1@10.22.10.111
sudo su - user2
cp /tmp/file.txt /home/user2/

I have the password of user1 and user2 but user2 is not allowed to access the computer remotely. Only user1 can do that but user1 cannot copy a file to the folders of user2. I am not allowed to change the permissions of that folder in user2.

Thanks!

NevemTeve 07-18-2012 03:58 AM

Code:

ssh user1@10.22.10.111
su -c 'cp /tmp/file.txt ~' user2


archieval 07-18-2012 05:20 AM

Thanks for the reply! When I login to that computer, and I execute "sudo su - user2", it does not prompt for password.

In your command, that would require password of user1, and then enter the remote computer. What I had in mind is a set of commands that I can put in a script file and just execute that file and when it returns, the prompt is still on my local computer.

I have tried:
Code:

ssh user1@10.22.10.111 'sudo su - user2 && cp /tmp/file.txt /home/user2/'
but doesn't work and I ended up in the remote computers terminal logged as user2. and when I exit, i got the file permission failed error.

pan64 07-18-2012 06:41 AM

In your example && will be executed after the sudo (sudo gives you the prompt, and after exiting cp will be executed), so you need to use another syntax:
sudo su - user2 -c '<your cp command>'
probably works


All times are GMT -5. The time now is 03:40 AM.