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?

  • 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
  • If you have 30 servers you're managing I highly recommend SaltStack Sep 29 '16 at 21:10

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"
  • 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. 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 Apr 30 '14 at 6:08
  • 1
    @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. Apr 30 '14 at 23:56

I found the tee command to be helpful in avoiding the sudo limitation on file redirection. Using sed proved frustrating due to character escaping requirements.

This is the command I used to remotely append the hosts for all machines in my cluster to /etc/hosts:

for i in {1..10}; do ssh ubuntu@10.1.1.$i -t "echo ' dev-1 dev-4 dev-3 dev-2 dev-6 dev-8 dev-5 dev-10 dev-9 dev-7' | sudo tee -a /etc/hosts >/dev/null"

The output for each iteration looks like this:

0+1 records in
0+1 records out
255 bytes (255 B) copied, 4.1338e-05 s, 6.2 MB/s

This SU answer was pivotal in building my final solution: https://superuser.com/a/1026359/587485


If you need to run multiple commands or redirects you should use the following syntax:

ssh server.com "sudo sh -c 'ps aux | grep jav > /root/1.txt'"

You would have to already have some sort of root level access, either via direct root passwd or your user id sudo rule setup prior to attempting to modify the sudoers file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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