Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-18-2012, 03:39 AM   #1
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Rep: Reputation: 41
Question 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:

scp user1@
ssh user1@
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.

Old 07-18-2012, 03:58 AM   #2
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,054

Rep: Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883
ssh user1@
su -c 'cp /tmp/file.txt ~' user2
Old 07-18-2012, 05:20 AM   #3
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Original Poster
Rep: Reputation: 41
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:
ssh user1@ '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.

Last edited by archieval; 07-18-2012 at 05:22 AM.
Old 07-18-2012, 06:41 AM   #4
LQ Guru
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,799

Rep: Reputation: 2552Reputation: 2552Reputation: 2552Reputation: 2552Reputation: 2552Reputation: 2552Reputation: 2552Reputation: 2552Reputation: 2552Reputation: 2552Reputation: 2552
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


remote session, ssh, sudo

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Kinit user1 to authenticate for ssh user2 MikeyCarter Linux - Software 0 03-01-2011 11:48 AM
Postfix-Squirrelmail: Mail sent to delivered to tsouvalasi Linux - Server 4 02-26-2010 08:15 AM
from Win XP, remotely execute a command on Linux using ssh lothario Linux - Software 3 06-21-2008 05:59 PM
URGENT: How to execute a Linux command remotely alix123 Programming 7 04-06-2006 03:00 AM
Using FTP to execute command line commands remotely steve_2010 Solaris / OpenSolaris 4 02-13-2004 08:44 AM

All times are GMT -5. The time now is 02:58 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration