Method of running commands on remote Linux system through SSH
- 2021-07-01 08:34:18
- OfStack
Sometimes we may need to run a few commands on a remote machine. If it is only an occasional operation, you can log on to a remote system and execute commands directly to achieve this goal. But it's a little annoying to do it every time. In this case, is there a better way to get rid of this troublesome operation?
Yes, you can perform these operations from your local system without logging in to a remote system. Is there any good in this? No doubt about it. This will save you a lot of good time.
How did this come about? SSH allows you to run commands on it without logging in to a remote computer.
The general syntax is as follows:
$ ssh [ User name ]@[ Remote host name or IP] [ Command or script ]
1) How to run commands on a remote Linux system via SSH
The following example allows the user to run the df command on a remote Linux machine via ssh.
$ ssh daygeek@CentOS7.2daygeek.com df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 27G 4.4G 23G 17% /
devtmpfs 903M 0 903M 0% /dev
tmpfs 920M 0 920M 0% /dev/shm
tmpfs 920M 9.3M 910M 2% /run
tmpfs 920M 0 920M 0% /sys/fs/cgroup
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 184M 8.0K 184M 1% /run/user/42
tmpfs 184M 0 184M 0% /run/user/1000
2) How to run multiple commands on a remote Linux system via SSH
The following example allows the user to run multiple commands at once on a remote Linux machine via ssh.
Run the uptime command and the free command simultaneously on the remote Linux system.
$ ssh daygeek@CentOS7.2daygeek.com "uptime && free -m"
23:05:10 up 10 min, 0 users, load average: 0.00, 0.03, 0.03
total used free shared buffers cached
Mem: 1878 432 1445 1 100 134
-/+ buffers/cache: 197 1680
Swap: 3071 0 3071
3) How to run commands with sudo permissions on a remote Linux system via SSH
The following example allows the user to run the fdisk command with sudo privileges on a remote Linux machine via ssh.
Ordinary users are not allowed to execute commands provided in the system binary (/usr/sbin/) directory. The user needs root permission to run it.
So you need root permissions to run the fdisk command on the Linux system. The which command returns the full executable path of the given command.
$ which fdisk
/usr/sbin/fdisk
$ ssh -t daygeek@CentOS7.2daygeek.com "sudo fdisk -l"
[sudo] password for daygeek:
Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000bf685
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 62914559 30407680 8e Linux LVM
Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/mapper/centos-root: 29.0 GB, 28982640640 bytes, 56606720 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Connection to centos7.2daygeek.com closed.
4) How to run service control commands with sudo permissions on a remote Linux system via SSH
The following example allows a user to run service control commands with sudo privileges on a remote Linux machine via ssh.
$ ssh -t daygeek@CentOS7.2daygeek.com "sudo systemctl restart httpd"
[sudo] password for daygeek:
Connection to centos7.2daygeek.com closed.
5) How to run commands on a remote Linux system over a non-standard port SSH
The following example allows the user to run the hostnamectl command over ssh on a remote Linux machine that uses a non-standard port.
$ ssh -p 2200 daygeek@CentOS7.2daygeek.com hostnamectl
Static hostname: Ubuntu18.2daygeek.com
Icon name: computer-vm
Chassis: vm
Machine ID: 27f6c2febda84dc881f28fd145077187
Boot ID: bbeccdf932be41ddb5deae9e5f15183d
Virtualization: oracle
Operating System: Ubuntu 18.04.2 LTS
Kernel: Linux 4.15.0-60-generic
Architecture: x86-64
6) How to save the output of the remote system to the local system
The following example allows the user to run the top command on a remote Linux machine via ssh and save the output to the local system.
$ ssh daygeek@CentOS7.2daygeek.com "top -bc | head -n 35" > /tmp/top-output.txt
cat /tmp/top-output.txt
top - 01:13:11 up 18 min, 1 user, load average: 0.01, 0.05, 0.10
Tasks: 168 total, 1 running, 167 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 6.2 sy, 0.0 ni, 93.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1882300 total, 1176324 free, 342392 used, 363584 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 1348140 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
4943 daygeek 20 0 162052 2248 1612 R 10.0 0.1 0:00.07 top -bc
1 root 20 0 128276 6936 4204 S 0.0 0.4 0:03.08 /usr/lib/sy+
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 [kthreadd]
3 root 20 0 0 0 0 S 0.0 0.0 0:00.25 [ksoftirqd/+
4 root 20 0 0 0 0 S 0.0 0.0 0:00.00 [kworker/0:+
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kworker/0:+
7 root rt 0 0 0 0 S 0.0 0.0 0:00.00 [migration/+
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 [rcu_bh]
9 root 20 0 0 0 0 S 0.0 0.0 0:00.77 [rcu_sched]
10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [lru-add-dr+
11 root rt 0 0 0 0 S 0.0 0.0 0:00.01 [watchdog/0]
13 root 20 0 0 0 0 S 0.0 0.0 0:00.00 [kdevtmpfs]
14 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [netns]
15 root 20 0 0 0 0 S 0.0 0.0 0:00.00 [khungtaskd]
16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [writeback]
17 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kintegrity+
18 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [bioset]
19 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [bioset]
20 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [bioset]
Or you can run multiple commands on a remote system using the following format:
$ ssh daygeek@CentOS7.2daygeek.com << EOF
hostnamectl
free -m
grep daygeek /etc/passwd
EOF
The output of the above command is as follows:
Pseudo-terminal will not be allocated because stdin is not a terminal.
Static hostname: CentOS7.2daygeek.com
Icon name: computer-vm
Chassis: vm
Machine ID: 002f47b82af248f5be1d67b67e03514c
Boot ID: dca9a1ba06374d7d96678f9461752482
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-957.el7.x86_64
Architecture: x86-64
total used free shared buff/cache available
Mem: 1838 335 1146 11 355 1314
Swap: 2047 0 2047
daygeek:x:1000:1000:2daygeek:/home/daygeek:/bin/bash
7) How to run a native Bash script on a remote system
The following example allows the user to run the local bash script remote-test. sh on a remote Linux machine via ssh.
Create an shell script and execute it.
$ vi /tmp/remote-test.sh
#!/bin/bash
#Name: remote-test.sh
#--------------------
uptime
free -m
df -h
uname -a
hostnamectl
The output of the above command is as follows:
$ ssh daygeek@CentOS7.2daygeek.com df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 27G 4.4G 23G 17% /
devtmpfs 903M 0 903M 0% /dev
tmpfs 920M 0 920M 0% /dev/shm
tmpfs 920M 9.3M 910M 2% /run
tmpfs 920M 0 920M 0% /sys/fs/cgroup
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 184M 8.0K 184M 1% /run/user/42
tmpfs 184M 0 184M 0% /run/user/1000
0
Or you can use pipes. If you think the output is not very good-looking, make some modifications to make it more elegant.
$ ssh daygeek@CentOS7.2daygeek.com df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 27G 4.4G 23G 17% /
devtmpfs 903M 0 903M 0% /dev
tmpfs 920M 0 920M 0% /dev/shm
tmpfs 920M 9.3M 910M 2% /run
tmpfs 920M 0 920M 0% /sys/fs/cgroup
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 184M 8.0K 184M 1% /run/user/42
tmpfs 184M 0 184M 0% /run/user/1000
1
The output of the above script is as follows:
$ ssh daygeek@CentOS7.2daygeek.com df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 27G 4.4G 23G 17% /
devtmpfs 903M 0 903M 0% /dev
tmpfs 920M 0 920M 0% /dev/shm
tmpfs 920M 9.3M 910M 2% /run
tmpfs 920M 0 920M 0% /sys/fs/cgroup
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 184M 8.0K 184M 1% /run/user/42
tmpfs 184M 0 184M 0% /run/user/1000
2
8) How to run multiple instructions on multiple remote systems at the same time
The following bash script allows users to run multiple instructions on multiple remote systems at the same time. It is implemented using a simple for loop.
To do this, you can try the PSSH command or the ClusterShell command or the DSH command.
$ vi /tmp/multiple-host.sh
for host in CentOS7.2daygeek.com CentOS6.2daygeek.com
do
ssh daygeek@${host} "uname -a;uptime;date;w"
done
The output of the above script is as follows:
$ ssh daygeek@CentOS7.2daygeek.com df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 27G 4.4G 23G 17% /
devtmpfs 903M 0 903M 0% /dev
tmpfs 920M 0 920M 0% /dev/shm
tmpfs 920M 9.3M 910M 2% /run
tmpfs 920M 0 920M 0% /sys/fs/cgroup
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 184M 8.0K 184M 1% /run/user/42
tmpfs 184M 0 184M 0% /run/user/1000
4
9) How to add a password using the sshpass command
If you find it troublesome to enter your password every time, I suggest you choose one of the following methods to solve this problem according to your needs.
If you do this frequently, I suggest you set up password-free authentication because it is a standard and permanent solution.
If you only perform these tasks a few times a month, I recommend using the sshpass tool. Just provide your password with the-p parameter option.
$ ssh daygeek@CentOS7.2daygeek.com df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 27G 4.4G 23G 17% /
devtmpfs 903M 0 903M 0% /dev
tmpfs 920M 0 920M 0% /dev/shm
tmpfs 920M 9.3M 910M 2% /run
tmpfs 920M 0 920M 0% /sys/fs/cgroup
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 184M 8.0K 184M 1% /run/user/42
tmpfs 184M 0 184M 0% /run/user/1000
5
Summarize