LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-25-2011, 08:58 AM   #1
carwe
LQ Newbie
 
Registered: Aug 2011
Posts: 17

Rep: Reputation: Disabled
Pass command to su


Hi,

To log in from host1 to host2, I do the following (someone else set up this, I don't control these machines):

Code:
[user1@host1] sudo su - user2
[user2@host1] ssh host2
[user2@host2]
...so, as I understand it, my user account user1 at host1 is allowed to sudo and change the user to user2, and user2 is allowed to ssh to host2 without having to enter a password. That is simply some set-up some user admin of the systems used to give me access to host2 through my personal user account user1 at host1.

I'd like to script this - so, I want to create a script that can be executed by user1 at host1 to get directly logged in to host2.

I thought a scriptfile at host1 for user user1 with this line in it would do it:
Code:
sudo su - -c "ssh host2" user2
...since it should run command "ssh host2" as the new user. But when I execute the above line as user1 at host1, I get the following:
Code:
[sudo] password for user1: (entering password)
Sorry, user user1 is not allowed to execute '/bin/su - -c echo foo user2' as root on host1.
...so it seems like that doesn't do what I need - or, this method won't work since I don't have sudo permissions to add the -c argument to su.

Any idea how I can achieve what I want - write a script in order to log into host2 as user1 from host1 as user1 with a script?

Thanks,
Carl
 
Old 09-25-2011, 05:47 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Try changing the order and see if that works:

Code:
sudo su - user2 -c "ssh host2"
 
Old 09-25-2011, 07:28 PM   #3
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ Reply

Hi Carl,

As I can understand what you want to do is to be able to ssh from host1 to host2 using user1. You do not want to login to host2 machine as user2.

From what I can see from the output is that the previous admin has configured key based authentication for user2 to be able to login to host2 without entering any password.

You can configure user1 the same way as user2 is configure using the steps mentioned in the following document: http://www.linuxproblem.org/art_9.html

I hope this helps.
 
Old 09-26-2011, 04:36 PM   #4
carwe
LQ Newbie
 
Registered: Aug 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by kbp View Post
Try changing the order and see if that works:

Code:
sudo su - user2 -c "ssh host2"
Thanks for the reply. I tried your suggestion, but I get back the same thing as before:

Code:
[user1@host1 ~]$ sudo su - user2 -c "ssh host2"
[sudo] password for user1: 
Sorry, user user1 is not allowed to execute '/bin/su - user2 -c ssh host2' as root on host1.
 
Old 09-26-2011, 04:45 PM   #5
carwe
LQ Newbie
 
Registered: Aug 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
Quote:
Hi Carl,

As I can understand what you want to do is to be able to ssh from host1 to host2 using user1. You do not want to login to host2 machine as user2.
*Yes* that is what I want do do actually, I was a bit unclear in my last line in my original post. This is what I want to do: as user1 at host1, I want to be able to run a script which when finished will leave me at host2 logged in as user2 (currently, user2 is the only allowed user at host2).

I know about key based verification and use it quite a lot. I have a chain of servers I must go through in order to reach host1 and I want to be able to fully atomate the process of getting into also host2 (and be logged in as user2 there) - so that's the part that this post is about.

Quote:
From what I can see from the output is that the previous admin has configured key based authentication for user2 to be able to login to host2 without entering any password.

You can configure user1 the same way as user2 is configure using the steps mentioned in the following document: http://www.linuxproblem.org/art_9.html

I hope this helps.
So, this won't work unfortunately since user2 is the only user that can log on to host2.
 
Old 09-27-2011, 04:44 PM   #6
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ Reply

You can put the script this way:

Code:
sudo su - user2
ssh user2@host2
Give it a try and see if it works.
 
Old 09-27-2011, 07:32 PM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,704

Rep: Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897
Actually
Code:
[user1@host1] sudo su - user2
[user2@host1] ssh host2
[user2@host2]
and
Quote:

sudo su - user2
ssh user2@host2
are the same since username@ is optional so if not provided ssh will automatically use the current USERNAME variable.

Quote:
sudo su - user2 -c "ssh host2"
I agree that user1 is not allowed to run the above command. To see what you can run try;
sudo -l

Don't think expect will work so unless your admin wants to set up keys for user1 you are stuck as is.
 
  


Reply

Tags
su, 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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Pass Command Result to Awk BarataPT Programming 7 04-15-2011 09:23 AM
Pass carriage return to a command say_hi_ravi Linux - Newbie 8 02-19-2011 03:17 AM
[SOLVED] how to pass argument from command prompt smritisingh03 Linux - Newbie 3 01-08-2011 09:18 AM
How to pass the result of a command to another command (like grep) desb01 Programming 4 06-25-2009 12:09 PM
Can you su and enter pass in same command Onemessedupjedi Slackware 3 11-19-2003 09:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:49 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