This is quite a common task for Linux system administrators, when there is need for execution of some command or a local Bash script from a one Linux server or Unix server on another remote Linux machine over SSH.In this article you will find the examples of how to run remote ssh command in Linux to Show result locally.
Basic syntax to run remote ssh command in Linux
$ ssh USER@HOST 'command'
Here:
ssh –> is the protocol or command being used for making connection with remote server.
USER –> is the username on remote server.
HOST –> is the remote host on which you want to execute remote command.
Command –> is the command that you want to execute on remote host.
Examples:
- Lets say you want to have the mount point details of the remote server.
[root@rhel1 ~]# ssh lradmin@172.31.7.221 df Filesystem 1K-blocks Used Available Use% Mounted on /dev/xvda2 10473452 1599732 8873720 16% / devtmpfs 489456 0 489456 0% /dev tmpfs 507736 0 507736 0% /dev/shm tmpfs 507736 13000 494736 3% /run tmpfs 507736 0 507736 0% /sys/fs/cgroup tmpfs 101548 0 101548 0% /run/user/1000 tmpfs 101548 0 101548 0% /run/user/0 [root@rhel1 ~]#
In the above example system will prompt you for the password of your name on remote host. Since we have configured password less ssh between two servers , system performed it without password prompt. In case you want to configure password less ssh between two server please read this article.
- In case you want to have result of uptime command on the remote host.
[root@rhel1 ~]# ssh lradmin@172.31.7.221 uptime 00:31:26 up 52 min, 2 users, load average: 0.00, 0.01, 0.04 [root@rhel1 ~]#
- Even you can reboot the server remotely.
[root@rhel1 ~]#ssh root@172.31.7.221 reboot
Run 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.
[root@rhel1 ~]# ssh lradmin@172.31.7.221 'systemctl stop httpd'
Since in the above example command consists of more than one word you need to enclose entire command in quotes ” ‘ “.
- Now to start it again this httpd service use below command.
[root@rhel1 ~]# ssh lradmin@172.31.7.221 'systemctl start httpd'
- Lets check the status of httpd service remotely using below command:
[root@rhel1 ~]# ssh lradmin@172.31.7.221 '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 Tue 2017-06-20 00:49:33 EDT; 56s ago Docs: man:httpd(8) man:apachectl(8) Main PID: 2313 (httpd) Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec" CGroup: /system.slice/httpd.service ├─2313 /usr/sbin/httpd -DFOREGROUND ├─2314 /usr/sbin/httpd -DFOREGROUND ├─2315 /usr/sbin/httpd -DFOREGROUND ├─2316 /usr/sbin/httpd -DFOREGROUND ├─2317 /usr/sbin/httpd -DFOREGROUND └─2318 /usr/sbin/httpd -DFOREGROUND Jun 20 00:49:33 ip-172-31-7-221.ap-south-1.compute.internal systemd[1]: Starting The Apache HTTP Server... Jun 20 00:49:33 ip-172-31-7-221.ap-south-1.compute.internal systemd[1]: Started The Apache HTTP Server. [root@rhel1 ~]#
How to get output in the local Linux file by executing command on remote host.
In case you want to have the output of command executed on the remote host kindly follow below method.
[root@rhel1 ~]# ssh lradmin@172.31.7.221 'free -m' > /tmp/memory.txt [root@rhel1 ~]# cat /tmp/memory.txt total used free shared buff/cache available Mem: 991 81 489 12 420 745 Swap: 0 0 0 [root@rhel1 ~]#
Here in the above example we have executed memory utilisation check command on the remote host and redirected the output of the same to local file “/tmp/memory.txt”. Once done you can check the contents of the file using cat command.
Get your free copy of shell scripting guide in PDF format!!!!
Please download “Shell Scripting: Expert Recipes for Linux, Bash and more.” for free in PDF format.
Download This Book: Click Here!!
View Comments (2)
Thanks for the article, is there any way I can receive an email whenever you publish a new update?
You can check "Subscribe to our mailing list" in the right middle corner for the same..