How to run remotely SSH commands in Linux and show the result locally

This is a fairly common task for Linux system administrators when there is a need to execute some command or local bash script from one Linux server or Unix server to another remote Linux machine using SSH. In this article, you will find examples of how to run remote SSH commands in Linux and display the result locally.

Basic syntax for running a remote SSH command on Linux

$ ssh [email protected] 'command'

Where:

  • SSH -> this is a protocol or command used to establish a connection with a remote server.
  • USER -> this is the username on the remote server.
  • HOST -> this is the remote host where you want to execute the remote command.
  • Command -> this is the command you want to execute on the remote host.

Examples:

  • Let’s say you want to know the details of the mount point of a remote server.
[[email protected] ~]# ssh [email protected] df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/xvda2      10473456 1599732   8873740  16% /
devtmpfs          483256       0    489456   0% /dev
tmpfs             547132       0    502766   0% /dev/shm
tmpfs             547132   13000    492766   3% /run
tmpfs             547132       0    502766   0% /sys/fs/cgroup
tmpfs             111244       0    103558   0% /run/user/1000
tmpfs             111244       0    103558   0% /run/user/0
[[email protected] ~]#

In the above example, the system will ask you for your name password on the remote host. Since we configured an SSH password between the two servers, the system worked without a password. In case you want to set up an SSH password between two servers, please read this article.

  • In case you want to know the result of the uninterrupted operation command on the remote host.
[[email protected] ~]# ssh [email protected] uptime
00:31:26 up 52 min,  2 users,  load average: 0.00, 0.01, 0.03
[[email protected] ~]#

  • Even you can overload the server remotely.
[[email protected] ~]#ssh [email protected] reboot

Run a remote SSH command in Linux to stop or start any process or service.

  • In case you want to stop the HTTPd process on the remote host.
[[email protected] ~]# ssh [email protected] 'systemctl stop httpd'

Since in the above example the command consists of more than one word, you must enclose the entire command in quotation marks “‘”

  • Now to start it again the HTTPD service use below command.
[[email protected] ~]# ssh [email protected] 'systemctl start httpd'

  • Lets you check the status of the HTTPd service remotely with the following command:
[[email protected] ~]# ssh [email protected] 'systemctl status httpd'
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2017-06-212 95:15:21 EDT; 43s ago
     Docs: man:httpd(8)
           man:apachectl(8)
Main PID: 2121 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─1911 /usr/sbin/httpd -DFOREGROUND
           ├─1912 /usr/sbin/httpd -DFOREGROUND
           ├─1913 /usr/sbin/httpd -DFOREGROUND
           ├─1914 /usr/sbin/httpd -DFOREGROUND
           ├─1915 /usr/sbin/httpd -DFOREGROUND
           └─1916 /usr/sbin/httpd -DFOREGROUND
 
Jun 22 09:15:21 ip-213-159-209-228.ap-south-1.compute.internal systemd[1]: Starting The Apache HTTP Server...
Jun 22 09:15:21 ip-213-159-209-228.ap-south-1.compute.internal systemd[1]: Started The Apache HTTP Server.
[[email protected] ~]#

To get output to a local Linux file, run the command on the remote host.

In case you want to get the output of the executed command on the remote host, you need to follow the next method.

[[email protected] ~]# ssh [email protected] 'free -m' > /tmp/memory.txt
[[email protected] ~]# cat /tmp/memory.txt
              total        used        free      shared  buff/cache   available
Mem:            991          95         352          13         450         685
Swap:             0           0           0
[[email protected] ~]#

Here, in the above example, we ran the memory usage command on the remote host and redirected the output to the local file “/tmp/memory.txt”. After that, you can check the contents of the file using the cat command.

Sidebar