Linux: how to run commands on a remote server

In some cases, we need run commands on a remote server.

Get disk information
Get all running process

Syntax for running commands on a remote server:

ssh [USER@][HOSTNAME-OR-IP] command

Full options:

[root@tutorialspots ~]# ssh
usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
           [-D [bind_address:]port] [-E log_file] [-e escape_char]
           [-F configfile] [-I pkcs11] [-i identity_file]
           [-J [user@]host[:port]] [-L address] [-l login_name] [-m mac_spec]
           [-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address]
           [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]]
           [user@]hostname [command]

Example 1: Get disk information

ssh df -h


[root@tutorialspots ~]# ssh df -h
The authenticity of host ' (' can't be established.
ECDSA key fingerprint is SHA256:yxg/X3UwKkrQWI8LoQdmerXPgr6IMM2mc2XxRpPpslk.
ECDSA key fingerprint is MD5:86:09:6d:c1:05:1f:e0:e7:04:6c:6f:33:8d:7a:9e:33.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '' (ECDSA) to the list of known hosts.'s password:
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs         16G     0   16G   0% /dev
tmpfs            16G     0   16G   0% /dev/shm
tmpfs            16G  556K   16G   1% /run
tmpfs            16G     0   16G   0% /sys/fs/cgroup
/dev/md2        5.4T  1.6T  3.6T  31% /
/dev/md1        487M  148M  314M  32% /boot
tmpfs           3.2G     0  3.2G   0% /run/user/0

To skip Strict HostKey Checking:

ssh -o StrictHostKeyChecking=no df -h

Example 2: check load average

[root@tutorialspots ~]# ssh cat /proc/loadavg's password:
0.75 0.52 0.58 1/296 15713

If you want to use password in command line, you should install sshpass:

[root@tutorialspots ~]# yum install -y sshpass
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                     |  13 kB     00:00
 * base:
 * epel:
 * extras:
 * updates:
base                                                     | 3.6 kB     00:00
epel                                                     | 4.7 kB     00:00
extras                                                   | 2.9 kB     00:00
ius                                                      | 1.3 kB     00:00
nodesource                                               | 2.5 kB     00:00
updates                                                  | 2.9 kB     00:00
(1/3): updates/7/x86_64/primary_db                         | 2.9 MB   00:00
(2/3): epel/x86_64/primary_db                              | 6.8 MB   00:00
(3/3): epel/x86_64/updateinfo                              | 1.0 MB   00:00
Resolving Dependencies
--> Running transaction check
---> Package sshpass.x86_64 0:1.06-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

 Package           Arch             Version              Repository        Size
 sshpass           x86_64           1.06-2.el7           extras            21 k

Transaction Summary
Install  1 Package

Total download size: 21 k
Installed size: 38 k
Downloading packages:
sshpass-1.06-2.el7.x86_64.rpm                              |  21 kB   00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : sshpass-1.06-2.el7.x86_64                                    1/1
  Verifying  : sshpass-1.06-2.el7.x86_64                                    1/1

  sshpass.x86_64 0:1.06-2.el7


Example 3: use password in command line

sshpass -p yourpassword ssh -o StrictHostKeyChecking=no root@ip "free -m"


[root@tutorialspots ~]# sshpass -p yourpassword ssh -o StrictHostKeyChecking=no 'free -m'
              total        used        free      shared  buff/cache   available
Mem:          31923         888         471           0       30563       30643
Swap:         32733           5       32728

To ignore this warning:

Warning: Permanently added '' (ECDSA) to the list of known hosts.

Follow Example 4:
Example 4:

ssh -o LogLevel=quiet user@ip_or_hostname

Full example:

sshpass -p yourpass ssh -o StrictHostKeyChecking=no -o LogLevel=quiet root@ "cat /root/ok/okdead.txt" > /root/ok/ok2.txt

Read more: Linux: how to install and use sshpass

Leave a Reply