You can run a command on a remote system with SSH easily and securely. This is helpful if you just want to run a simple command on a remote Linux system and have the output displayed on your local terminal.

You will have to enter your password unless you set up SSH Keys. Configuring SSH Keys will allow you to securely login without typing a password. If you want to run a command with elevated privileges you will have either allow sudo without a password, use a pseudo-terminal, enable root SSH login. Enabling root login via SSH is strongly discouraged.

Run Simple Command via SSH

The syntax for running a remote command is fairly straightforward:

ssh [email protected] 'command'

Examples:

Check if a package is installed on remote system:

$ ssh [email protected] 'rpm -qa | grep dmidecode'
dmidecode-3.1-2.el7.x86_64
python-dmidecode-3.12.2-3.el7.x86_64

Check users logged on to remote system:

$ ssh [email protected] 'who'
savona pts/0 2019-03-08 11:42 (putor)

This will work with pretty much any command you have access to run, excluding programs that require X11 Forwarding.

Execute Command on Remote System using Sudo

If you want to run commands with sudo on a remote host you have several options which depend on the sudo configuration of the remote system.

Run sudo with Password Required

If you are required to enter a password when using sudo on the remote system, you will need to use the -t (force pseudo-tty).

$ ssh -t [email protected]st "command"

Example:

Let’s run dmidecode and get the BIOS information from a remote system.

$ ssh -t [email protected] "sudo dmidecode | egrep -i '(vendor|version)'"
[sudo] password for savona:
Vendor: American Megatrends Inc.
Version: 1.04
Version: V1.0
Version: 1.0
Version: To Be Filled By O.E.M.
Version: Intel(R) Core(TM) i5-3470S CPU @ 2.90GHz
Connection to fenrir closed.

NOTE: I used double quotes to encase the command since single quotes were part of the actual command.

Run sudo with No Password Required

This is as simple as adding sudo to the beginning of a command. The -t option is no longer needed here because we do not need to enter a password.

$ ssh [email protected] 'sudo command'

Example:

Let’s install clusterssh on the remote host:

$ ssh [email protected] 'sudo yum install clusterssh'
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
base: mirror.mobap.edu
epel: mirror.umd.edu
extras: mirror.ash.fastserv.com
rpmfusion-free-updates: fr2.rpmfind.net
rpmfusion-nonfree-updates: fr2.rpmfind.net
updates: mirror.cc.columbia.edu
Resolving Dependencies
--> Running transaction check
---> Package clusterssh.noarch 0:4.02.03-2.el7 will be installed
--> Processing Dependency: perl(Locale::Maketext) >= 1.01 for package: clusterssh-4.02.03-2.el7.noarch
--> Processing Dependency: perl(Tk) >= 800.022 for package: clusterssh-4.02.03-2.el7.noarch
...OUTPUT TRUNCATED...

Conclusion

This is a simple quick-tip that can save you a lot of time. If you have multiple servers, you can script this with a loop to retrieve information from each host, assuming you have SSH Keys configured.

Resources / Links: