Linux- passwordless ssh from system (root) script

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.

Answer

Nothing wrong with the sudo part. To pick up an existing ssh agent, see these answers: Sharing the same `ssh-agent` among multiple login sessions

Attribution
Source : Link , Question Author : redmoskito , Answer Author : Community

Leave a Comment