How to run commands on a remote Linux system using SSH
This article introduces how to use SSH to run commands in a remote Linux system, the content is very detailed, interested friends can refer to, hope to be helpful to you.
The general syntax is as follows:
$ssh [user name] @ [remote hostname or IP] [command or script]
1) how to run commands on a remote Linux system through SSH
The following example allows the user to run the df command on a remote Linux machine through 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 0903M 0% / devtmpfs 920M 0920M 0% / dev/shm tmpfs 920M 9.3M 910m 2% / run tmpfs 920M 0920M 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 through SSH
The following example allows the user to run more than one command at a time on a remote Linux machine through ssh.
Run both the uptime command and the free command 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 1100 134-/ + buffers/cache: 197 1680 Swap: 3071 0 3071
3) how to run a command with sudo permission on a remote Linux system through SSH
The following example allows a user to run a fdisk command with sudo privileges on a remote Linux machine through 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 permission to run fdisk commands on Linux systems. The which command returns the full executable path of a 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 size 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 GB 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 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 size O size (minimum/optimal): 512 bytes / 512 bytes Connection to centos7.2daygeek.com closed.
4) how to run a service control command with sudo authority on a remote Linux system through SSH
The following example allows users to run service control commands with sudo privileges on a remote Linux machine through 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 through non-standard port SSH
The following example allows users to run hostnamectl commands through ssh on remote Linux machines that use non-standard ports.
$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 top commands on a remote Linux machine through ssh and save the output to the local system.
$ssh daygeek@CentOS7.2daygeek.com "top- bc | head-n 35" > / tmp/top-output.txtcat / tmp/top-output.txt top- 01:13:11 up 18 min, 1 user, load average: 0.01,0.05,0.10 Tasks: 168total, 1 running, 167sleeping, 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 200 162052 2248 1612 R 10.0 0.1 daygeek 00.07 top-bc 1 root 20 128276 6936 4204 S 0.0 0.4 root 20 00 00 S 0.0 0.00: 00.00 [kthreadd] 3 root 20 00 S 0.0 0.00: 0.0.25 [ Ksoftirqd/+ 4 root 20 00 00 S 0.0 0.00: 00.00 [kworker/0:+ 5 root 0-20 000 S 0.0 0.00: 00.00 [kworker/0:+ 7 root rt 00 00 S 0.0 0.00: 00.00 [migration/+ 8 root 20 00 S 0.0 0.00: 00.00 [rcu_bh ] 9 root 20 00 00 S 0.0 0.00: 00.77 [rcu_sched] 10 root 0-20 000 S 0.0 0.00: 00.00 [lru-add-dr+ 11 root rt 00 00 S 0.0 0.00: 00.01 [watchdog/0] 13 root 20 00 S 0.0 0.00 [kdevtmpfs] 14 root 0-20 000 S 0.0 0.00: 00.00 [netns] 15 root 20 00 00 S 0.0 0.00: 00.00 [khungtaskd] 16 root 0-20 000 S 0.0 0.00: 00.00 [writeback] 17 root 0-20 000 S 0.0 0.00: 00.00 [kintegrity+ 18 root 0-20 000 S 0 .00.00: 00.00 [bioset] 19 root 0-20 000 S 0.0 0.00: 00.00 [bioset] 20 root 0-20 000 S 0.0 0.00: 00.00 [bioset]
Or you can run multiple commands on a remote system using the following format:
$ssh daygeek@CentOS7.2daygeek.com