這篇文章給大家介紹使用SSH怎么在遠(yuǎn)程 Linux 系統(tǒng)中運(yùn)行命令,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供烏什網(wǎng)站建設(shè)、烏什做網(wǎng)站、烏什網(wǎng)站設(shè)計(jì)、烏什網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、烏什企業(yè)網(wǎng)站模板建站服務(wù),10年烏什做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
通用語法如下所示:
$ ssh [用戶名]@[遠(yuǎn)程主機(jī)名或 IP] [命令或腳本]
1) 如何通過 SSH 在遠(yuǎn)程 Linux 系統(tǒng)上運(yùn)行命令
下面的例子允許用戶通過 ssh 在遠(yuǎn)程 Linux 機(jī)器上運(yùn)行 df 命令。
$ 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) 如何通過 SSH 在遠(yuǎn)程 Linux 系統(tǒng)上運(yùn)行多條命令
下面的例子允許用戶通過 ssh 在遠(yuǎn)程 Linux 機(jī)器上一次運(yùn)行多條命令。
同時(shí)在遠(yuǎn)程 Linux 系統(tǒng)上運(yùn)行 uptime 命令和 free 命令。
$ 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) 如何通過 SSH 在遠(yuǎn)程 Linux 系統(tǒng)上運(yùn)行帶 sudo 權(quán)限的命令
下面的例子允許用戶通過 ssh 在遠(yuǎn)程 Linux 機(jī)器上運(yùn)行帶有 sudo 權(quán)限 的 fdisk 命令。
普通用戶不允許執(zhí)行系統(tǒng)二進(jìn)制(/usr/sbin/)目錄下提供的命令。用戶需要 root 權(quán)限來運(yùn)行它。
所以你需要 root 權(quán)限,好在 Linux 系統(tǒng)上運(yùn)行 fdisk 命令。which 命令返回給定命令的完整可執(zhí)行路徑。
$ 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) 如何通過 SSH 在遠(yuǎn)程 Linux 系統(tǒng)上運(yùn)行帶 sudo 權(quán)限的服務(wù)控制命令
下面的例子允許用戶通過 ssh 在遠(yuǎn)程 Linux 機(jī)器上運(yùn)行帶有 sudo 權(quán)限的服務(wù)控制命令。
$ ssh -t daygeek@CentOS7.2daygeek.com "sudo systemctl restart httpd" [sudo] password for daygeek: Connection to centos7.2daygeek.com closed.
5) 如何通過非標(biāo)準(zhǔn)端口 SSH 在遠(yuǎn)程 Linux 系統(tǒng)上運(yùn)行命令
下面的例子允許用戶通過 ssh 在使用了非標(biāo)準(zhǔn)端口的遠(yuǎn)程 Linux 機(jī)器上運(yùn)行 hostnamectl 命令。
$ 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) 如何將遠(yuǎn)程系統(tǒng)的輸出保存到本地系統(tǒng)
下面的例子允許用戶通過 ssh 在遠(yuǎn)程 Linux 機(jī)器上運(yùn)行 top 命令,并將輸出保存到本地系統(tǒng)。
$ 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]
或者你也可以使用以下格式在遠(yuǎn)程系統(tǒng)上運(yùn)行多條命令:
$ ssh daygeek@CentOS7.2daygeek.com << EOF hostnamectl free -m grep daygeek /etc/passwd EOF
上面命令的輸出如下:
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) 如何在遠(yuǎn)程系統(tǒng)上運(yùn)行本地 Bash 腳本
下面的例子允許用戶通過 ssh 在遠(yuǎn)程 Linux 機(jī)器上運(yùn)行本地 bash 腳本 remote-test.sh。
創(chuàng)建一個(gè) shell 腳本并執(zhí)行它。
$ vi /tmp/remote-test.sh #!/bin/bash #Name: remote-test.sh #-------------------- uptime free -m df -h uname -a hostnamectl
上面命令的輸出如下:
$ ssh daygeek@CentOS7.2daygeek.com 'bash -s' < /tmp/remote-test.sh 01:17:09 up 22 min, 1 user, load average: 0.00, 0.02, 0.08 total used free shared buff/cache available Mem: 1838 333 1148 11 355 1316 Swap: 2047 0 2047 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 12K 184M 1% /run/user/42 tmpfs 184M 0 184M 0% /run/user/1000 Linux CentOS7.2daygeek.com 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux 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
或者也可以使用管道。如果你覺得輸出不太好看,再做點(diǎn)修改讓它更優(yōu)雅些。
$ vi /tmp/remote-test-1.sh #!/bin/bash #Name: remote-test.sh echo "---------System Uptime--------------------------------------------" uptime echo -e "\n" echo "---------Memory Usage---------------------------------------------" free -m echo -e "\n" echo "---------Disk Usage-----------------------------------------------" df -h echo -e "\n" echo "---------Kernel Version-------------------------------------------" uname -a echo -e "\n" echo "---------HostName Info--------------------------------------------" hostnamectl echo "------------------------------------------------------------------"
上面腳本的輸出如下:
$ cat /tmp/remote-test.sh | ssh daygeek@CentOS7.2daygeek.com Pseudo-terminal will not be allocated because stdin is not a terminal. ---------System Uptime-------------------------------------------- 03:14:09 up 2:19, 1 user, load average: 0.00, 0.01, 0.05 ---------Memory Usage--------------------------------------------- total used free shared buff/cache available Mem: 1838 376 1063 11 398 1253 Swap: 2047 0 2047 ---------Disk Usage----------------------------------------------- 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 12K 184M 1% /run/user/42 tmpfs 184M 0 184M 0% /run/user/1000 tmpfs 184M 0 184M 0% /run/user/0 ---------Kernel Version------------------------------------------- Linux CentOS7.2daygeek.com 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux ---------HostName Info-------------------------------------------- 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
8) 如何同時(shí)在多個(gè)遠(yuǎn)程系統(tǒng)上運(yùn)行多條指令
下面的 bash 腳本允許用戶同時(shí)在多個(gè)遠(yuǎn)程系統(tǒng)上運(yùn)行多條指令。使用簡(jiǎn)單的 for 循環(huán)實(shí)現(xiàn)。
為了實(shí)現(xiàn)這個(gè)目的,你可以嘗試 PSSH 命令 或 ClusterShell 命令 或 DSH 命令。
$ vi /tmp/multiple-host.sh for host in CentOS7.2daygeek.com CentOS6.2daygeek.com do ssh daygeek@${host} "uname -a;uptime;date;w" done
上面腳本的輸出如下:
$ sh multiple-host.sh Linux CentOS7.2daygeek.com 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux 01:33:57 up 39 min, 1 user, load average: 0.07, 0.06, 0.06 Wed Sep 25 01:33:57 CDT 2019 01:33:57 up 39 min, 1 user, load average: 0.07, 0.06, 0.06 USER TTY FROM daygeek@CentOS7.2daygeek.com IDLE JCPU PCPU WHAT daygeek pts/0 192.168.1.6 01:08 23:25 0.06s 0.06s -bash Linux CentOS6.2daygeek.com 2.6.32-754.el6.x86_64 #1 SMP Tue Jun 19 21:26:04 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux 23:33:58 up 39 min, 0 users, load average: 0.00, 0.00, 0.00 Tue Sep 24 23:33:58 MST 2019 23:33:58 up 39 min, 0 users, load average: 0.00, 0.00, 0.00 USER TTY FROM daygeek@CentOS7.2daygeek.com IDLE JCPU PCPU WHAT
9) 如何使用 sshpass 命令添加一個(gè)密碼
如果你覺得每次輸入密碼很麻煩,我建議你視你的需求選擇以下方法中的一項(xiàng)來解決這個(gè)問題。
如果你經(jīng)常進(jìn)行類似的操作,我建議你設(shè)置 免密碼認(rèn)證,因?yàn)樗菢?biāo)準(zhǔn)且永久的解決方案。
如果你一個(gè)月只是執(zhí)行幾次這些任務(wù),我推薦你使用 sshpass 工具。只需要使用 -p 參數(shù)選項(xiàng)提供你的密碼即可。
$ sshpass -p '在這里輸入你的密碼' ssh -p 2200 daygeek@CentOS7.2daygeek.com ip a 1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:00:27:18:90:7f brd ff:ff:ff:ff:ff:ff inet 192.168.1.12/24 brd 192.168.1.255 scope global dynamic eth0 valid_lft 86145sec preferred_lft 86145sec inet6 fe80::a00:27ff:fe18:907f/64 scope link tentative dadfailed valid_lft forever preferred_lft forever
關(guān)于使用SSH怎么在遠(yuǎn)程 Linux 系統(tǒng)中運(yùn)行命令就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。