LinuxQuestions.org
Review your favorite Linux distribution.
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 05-05-2008, 01:24 AM   #1
kutty_prasad
Member
 
Registered: Sep 2006
Location: Bangalore,India
Distribution: RHEL,Fedora, CentOS, Slackware 12 & wrestling with LFS
Posts: 86

Rep: Reputation: 16
ssh and sudo


Hi All,

I have sudo access on few machines and I need to perform few things as root user. My question is, how can I remotely execute sudo via ssh. Below is an example:

ssh remote1.host.com "cat filename" <--This works fine.

ssh remote1.host.com "sudo echo newline >> filename" <-- trouble. Permision denied error.

Is there any option to fix this?

Thanks,
Prasad.
 
Old 05-05-2008, 07:43 AM   #2
Brian1
LQ Guru
 
Registered: Jan 2003
Location: Seymour, Indiana
Distribution: Distribution: RHEL 5 with Pieces of this and that. Kernel 2.6.23.1, KDE 3.5.8 and KDE 4.0 beta, Plu
Posts: 5,700

Rep: Reputation: 65
Does using sudo require the use of a password?
Is echo one of the commands that can be done with sudo or are all commands allowed through sudo?

Brian
 
Old 05-05-2008, 07:55 AM   #3
harsshal
Member
 
Registered: Jul 2006
Location: New York, NY
Distribution: redhat,ubuntu,RHEL,fedora,centOS
Posts: 105

Rep: Reputation: 15
how are you logged in your machine?
"ssh server.com" by default logs in with the account with which you are logged in.If you are logged in as "user1" it would be user1@server.com.So are you logged in as superdoer while doing ssh?

Last edited by harsshal; 05-07-2008 at 12:55 AM.
 
Old 05-06-2008, 11:48 PM   #4
kutty_prasad
Member
 
Registered: Sep 2006
Location: Bangalore,India
Distribution: RHEL,Fedora, CentOS, Slackware 12 & wrestling with LFS
Posts: 86

Original Poster
Rep: Reputation: 16
For Brain1: Yes, sudo will ask me for password and it allows me to use all the commands.


For Harsshal: I login as normal, since I don't have root password, I have to use sudo.

Thanks.
 
Old 05-07-2008, 12:10 PM   #5
danboland
Member
 
Registered: Feb 2006
Location: NJ, USA
Distribution: Debian
Posts: 72

Rep: Reputation: 17
i've never had to chain commands over ssh. Ussually i just put them in a shell script and run that through ssh,

like create a file called commands.sh with this:

Code:
#!/bin/sh
sudo echo newline >> filename
and then run that through ssh:
ssh remote1.host.com "commands.sh"


Also, make sure there is no restrictions set up for this sue in the sudousers file
 
Old 05-07-2008, 08:13 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
If sudo expects a passwd (the normal arrangement), then you'll have to supply it somehow.
If you need to automate this, then create a remote script as per danboland, and research the 'expect' tool, which is designed to handle situations like this.
Of course this'll mean storing the clear-text passwd on the remote box; possibly not a good idea...
 
Old 05-08-2008, 03:58 PM   #7
danboland
Member
 
Registered: Feb 2006
Location: NJ, USA
Distribution: Debian
Posts: 72

Rep: Reputation: 17
Actually i just tested something like what you where doing, using sudo:
Code:
dev# ssh dan@host.com "sudo ls -l /root/"
dan@host.com's password:
Password:*********
as you can see it asked for my password twice, once for the ssh and once for the sudo. (the sudo was in plain text on teh screen which is very bad...)

But the issue is probably not the ssh and sudo command them selfs then since that is working.

So then i tried to do:
Code:
dev# ssh dan@host.com "sudo echo newline > filename"
dan@host.com's password:
Password:*********
and it created the file in my local directory call 'filename' with the text "newline"

However the file was owned by me, and root like it should of been, so then i did this:

Code:
dev# ssh -dan@host.com "sudo sh -c echo newline2 > /root/filename"
dan@host.com's password:
bash: /root/filename: Permission denied
as you can see, got your error. after reading the sudo man pages i see that if you want to do file redirection you have to do sudo a little differently and i came up with this command:

Code:
dev# ssh dan@host.com "sudo sh -c 'echo newline > /root/filename'"
dan@host.com's password:
Password:*******
This created the file in the root directory, as owned by root.



Dan

Last edited by danboland; 05-13-2008 at 11:12 PM.
 
Old 05-13-2008, 03:46 AM   #8
kutty_prasad
Member
 
Registered: Sep 2006
Location: Bangalore,India
Distribution: RHEL,Fedora, CentOS, Slackware 12 & wrestling with LFS
Posts: 86

Original Poster
Rep: Reputation: 16
Thanks a lot Dan.

I'll try out this method when I get a chance again.

Thanks,
 
  


Reply



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
LXer: The Ultimate Sudo FAQ — To Sudo Or Not To Sudo? LXer Syndicated Linux News 13 04-13-2013 01:36 AM
"sudo -u user ssh server" fails to connect oudoubah Linux - Security 3 06-21-2007 06:45 AM
commande execution with sudo(on ssh) yousmg Linux - Security 4 06-14-2007 11:04 AM
Sudo and ssh X11 fowarding Mike_the_Man Linux - General 1 05-03-2006 11:04 PM
sudo ethereal with ssh adamwenner Linux - Software 0 09-10-2004 02:21 PM

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

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

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