7.13. Executing Commands Without Opening a Remote Shell
Problem
You have a single command to run on the remote machine, and you think it would be nice to be able to just run it without logging in and opening a remote shell, running the command, and then logging out. After all, is it not true that laziness is a virtue for network admins?
Solution
And, you shall have what you want because OpenSSH can do this. This example shows how to restart Postfix:
$ ssh mailadmin@limberger.alrac.net sudo /etc/init.d/postfix restartThis shows how to open a quick game of Kpoker, which requires X Windows:
$ ssh -X 192.168.1.10 /usr/games/kpokerYou'll be asked for a password, but you'll still save one whole step.
Discussion
You have to use sudo when you need root privileges with this command, not su, because you can't use su without first opening a remote shell. This is also a handy way to script remote commands.
And yes, laziness is a virtue, if it leads to increased efficiency and streamlined methods of getting jobs done.
See Also
man 1 ssh
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access