LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 07-18-2012, 03:39 AM   #1
archieval
Member
 
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:

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!
 
Old 07-18-2012, 03:58 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
ssh user1@10.22.10.111
su -c 'cp /tmp/file.txt ~' user2
 
Old 07-18-2012, 05:20 AM   #3
archieval
Member
 
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:
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.

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

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


Reply

Tags
remote session, ssh, sudo



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 user1@mydomain.com delivered to user2@mydomain.com 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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:36 PM.

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