Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm wondering if there is a way to execute a script/commands from the the local machine without making any modifications as root on the remote machine?

Some background

I'm trying to setup some tasks via Capistrano and I require sudo access. There are about 30+ servers and for me to manually update /etc/sudoers will be painful so I was wondering if there is a method to update this file remotely?

share|improve this question
You can use Fabric that has remote interaction. You can do the same with no interaction in parallel using fabric.contrib.files.sed method. –  lcipriani Apr 29 '14 at 8:20

1 Answer 1

You can run local scripts remotely by executing bash on the remote system and feeding it your script

$ ssh user@host 'bash -s' < script.sh


To execute commands that require using sudo on a remote machine use ssh's -t option and pass the commands to ssh. The -t option allocates a psuedo tty and enables user interaction with the commands ran by ssh, such as entering a password for sudo

$ ssh user@host -t 'sudo foo'

To modify a file using this method sed is recommended over a redirect > because shell redirection does not allow for writing files when using sudo. Additionally, all variables in the sed command need to be escaped when they are passed to ssh.

$ ssh user@host -t 'sudo sed -i "\$a text to insert" /path/to/file'

To automate the whole thing:

SERVERS=( server1 server2 server3 )

for HOST in ${SERVERS[@]}; do 
    ssh user@${HOST} -t 'sudo sed -i "\$a text to insert" /path/to/file'

    if [[ $? -ne 0 ]]; then
        echo "ERROR: $HOST did not complete"
        echo "$HOST complete"
share|improve this answer
Thanks for the script and all but it doesn't really answer my question. If I wish modify the /etc/sudoers file due to root permissions it will fail. –  aspiringCodeArtisan Apr 30 '14 at 0:05
@aspiringCodeArtisan see edits to the answer –  Creek Apr 30 '14 at 2:02
Hi, even that prompts for a sudo password which from what I understand is set in /etc/sudoers –  aspiringCodeArtisan Apr 30 '14 at 6:08
@aspiringCodeArtisan If your account doesn't have sudo rights on these machines, then to modify /etc/sudoers you're going to have to login via ssh as root. So the command would be ssh root@host -t 'sed -i "\$a text to insert" /path/to/file' –  Creek Apr 30 '14 at 9:30
root ssh login is disabled by default so its not an option. I think I may be asking for the impossible. –  aspiringCodeArtisan Apr 30 '14 at 23:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.