Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What's the easiest way to have a system script (running as root) execute remote commands over ssh?

I've written some scripts that execute commands remotely via ssh, and they work great when I run them as myself, as I've set up ssh-agent and keys for passwordless login. I'd like to call these when my laptop docks and undocks. I've been successful at running arbitrary scripts when docking/undocking, but since the ACPI event scripts run as root, trying to run my ssh script fails during authentication.

I tried using sudo with the -u and -i flags to simulate running the script as my user, e.g.:

sudo -u redmoskito -i /home/redmoskito/bin/remote_command

which successfully finds my private key and tries to use it, but the ssh-agent credentials are still missing, so it still needs my passphrase.

share|improve this question
possible duplicate of SSH: execute sudo command – Sathya Jun 14 '10 at 18:18
@Sathya The user in that post is trying to execute a remote command as root on the server, whereas I'm trying to execute a remote command as root on the client. – redmoskito Jul 3 '10 at 17:32
up vote 1 down vote accepted

Nothing wrong with the sudo part. To pick up an existing ssh agent, see these answers:

share|improve this answer

Did you try using su instead of sudo?

su - redmoskito -c "/home/redmoskito/bin/remote_command"


edit your script to use: ssh -l redmoskito (host) (command) (assuming that redmoskito is the same username on remote machine) Then on the remote machine's redmoskito home directory $HOME/.ssh/authorized_keys, put in the contents of local machine's root $HOME.ssh/ after creating a blank passphrase as root on local machine with ssh-keygen -t rsa Make sure $HOME or $HOME/.ssh or any of the files in $HOME/.ssh are not group or other writeable if your sshd_config file has StrictModes=yes

If none of this works, let me know. I will duplicate the problem on my machines.

share|improve this answer
That would certainly solve the issue, as it circumvents the "ssh agent needs authentication" issue, but blank passphrase is not an option for me. – redmoskito Jul 3 '10 at 17:33

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.