通过 SSH 在远程 Linux 系统上运行命令的方法
我们有时可能需要在远程机器上运行一些命令。如果只是偶尔进行的操作,要实现这个目的,可以登录到远程系统上直接执行命令。但是每次都这么做的话,就有点烦人了。既然如此,有没有摆脱这种麻烦操作的更佳方案?
是的,你可以从你本地系统上执行这些操作,而不用登录到远程系统上。这有什么好处吗?毫无疑问。这会为你节省很多好时光。
这是怎么实现的?SSH允许你无需登录到远程计算机就可以在它上面运行命令。
通用语法如下所示:
$ssh[用户名]@[远程主机名或IP][命令或脚本]
1)如何通过SSH在远程Linux系统上运行命令
下面的例子允许用户通过ssh在远程Linux机器上运行df命令。
$sshdaygeek@CentOS7.2daygeek.comdf-h FilesystemSizeUsedAvailUse%Mountedon /dev/mapper/centos-root27G4.4G23G17%/ devtmpfs903M0903M0%/dev tmpfs920M0920M0%/dev/shm tmpfs920M9.3M910M2%/run tmpfs920M0920M0%/sys/fs/cgroup /dev/sda11014M179M836M18%/boot tmpfs184M8.0K184M1%/run/user/42 tmpfs184M0184M0%/run/user/1000
2)如何通过SSH在远程Linux系统上运行多条命令
下面的例子允许用户通过ssh在远程Linux机器上一次运行多条命令。
同时在远程Linux系统上运行uptime命令和free命令。
$sshdaygeek@CentOS7.2daygeek.com"uptime&&free-m" 23:05:10up10min,0users,loadaverage:0.00,0.03,0.03 totalusedfreesharedbufferscached Mem:187843214451100134 -/+buffers/cache:1971680 Swap:307103071
3)如何通过SSH在远程Linux系统上运行带sudo权限的命令
下面的例子允许用户通过ssh在远程Linux机器上运行带有sudo权限的fdisk命令。
普通用户不允许执行系统二进制(/usr/sbin/)目录下提供的命令。用户需要root权限来运行它。
所以你需要root权限,好在Linux系统上运行fdisk命令。which命令返回给定命令的完整可执行路径。
$whichfdisk /usr/sbin/fdisk $ssh-tdaygeek@CentOS7.2daygeek.com"sudofdisk-l" [sudo]passwordfordaygeek: Disk/dev/sda:32.2GB,32212254720bytes,62914560sectors Units=sectorsof1*512=512bytes Sectorsize(logical/physical):512bytes/512bytes I/Osize(minimum/optimal):512bytes/512bytes Disklabeltype:dos Diskidentifier:0x000bf685 DeviceBootStartEndBlocksIdSystem /dev/sda1*20482099199104857683Linux /dev/sda2209920062914559304076808eLinuxLVM Disk/dev/sdb:10.7GB,10737418240bytes,20971520sectors Units=sectorsof1*512=512bytes Sectorsize(logical/physical):512bytes/512bytes I/Osize(minimum/optimal):512bytes/512bytes Disk/dev/mapper/centos-root:29.0GB,28982640640bytes,56606720sectors Units=sectorsof1*512=512bytes Sectorsize(logical/physical):512bytes/512bytes I/Osize(minimum/optimal):512bytes/512bytes Disk/dev/mapper/centos-swap:2147MB,2147483648bytes,4194304sectors Units=sectorsof1*512=512bytes Sectorsize(logical/physical):512bytes/512bytes I/Osize(minimum/optimal):512bytes/512bytes Connectiontocentos7.2daygeek.comclosed.
4)如何通过SSH在远程Linux系统上运行带sudo权限的服务控制命令
下面的例子允许用户通过ssh在远程Linux机器上运行带有sudo权限的服务控制命令。
$ssh-tdaygeek@CentOS7.2daygeek.com"sudosystemctlrestarthttpd" [sudo]passwordfordaygeek: Connectiontocentos7.2daygeek.comclosed.
5)如何通过非标准端口SSH在远程Linux系统上运行命令
下面的例子允许用户通过ssh在使用了非标准端口的远程Linux机器上运行hostnamectl命令。
$ssh-p2200daygeek@CentOS7.2daygeek.comhostnamectl Statichostname:Ubuntu18.2daygeek.com Iconname:computer-vm Chassis:vm MachineID:27f6c2febda84dc881f28fd145077187 BootID:bbeccdf932be41ddb5deae9e5f15183d Virtualization:oracle OperatingSystem:Ubuntu18.04.2LTS Kernel:Linux4.15.0-60-generic Architecture:x86-64
6)如何将远程系统的输出保存到本地系统
下面的例子允许用户通过ssh在远程Linux机器上运行top命令,并将输出保存到本地系统。
$sshdaygeek@CentOS7.2daygeek.com"top-bc|head-n35">/tmp/top-output.txt cat/tmp/top-output.txt top-01:13:11up18min,1user,loadaverage:0.01,0.05,0.10 Tasks:168total,1running,167sleeping,0stopped,0zombie %Cpu(s):0.0us,6.2sy,0.0ni,93.8id,0.0wa,0.0hi,0.0si,0.0st KiBMem:1882300total,1176324free,342392used,363584buff/cache KiBSwap:2097148total,2097148free,0used.1348140availMem PIDUSERPRNIVIRTRESSHRS%CPU%MEMTIME+COMMAND 4943daygeek20016205222481612R10.00.10:00.07top-bc 1root20012827669364204S0.00.40:03.08/usr/lib/sy+ 2root200000S0.00.00:00.00[kthreadd] 3root200000S0.00.00:00.25[ksoftirqd/+ 4root200000S0.00.00:00.00[kworker/0:+ 5root0-20000S0.00.00:00.00[kworker/0:+ 7rootrt0000S0.00.00:00.00[migration/+ 8root200000S0.00.00:00.00[rcu_bh] 9root200000S0.00.00:00.77[rcu_sched] 10root0-20000S0.00.00:00.00[lru-add-dr+ 11rootrt0000S0.00.00:00.01[watchdog/0] 13root200000S0.00.00:00.00[kdevtmpfs] 14root0-20000S0.00.00:00.00[netns] 15root200000S0.00.00:00.00[khungtaskd] 16root0-20000S0.00.00:00.00[writeback] 17root0-20000S0.00.00:00.00[kintegrity+ 18root0-20000S0.00.00:00.00[bioset] 19root0-20000S0.00.00:00.00[bioset] 20root0-20000S0.00.00:00.00[bioset]
或者你也可以使用以下格式在远程系统上运行多条命令:
$sshdaygeek@CentOS7.2daygeek.com<上面命令的输出如下:
Pseudo-terminalwillnotbeallocatedbecausestdinisnotaterminal. Statichostname:CentOS7.2daygeek.com Iconname:computer-vm Chassis:vm MachineID:002f47b82af248f5be1d67b67e03514c BootID:dca9a1ba06374d7d96678f9461752482 Virtualization:kvm OperatingSystem:CentOSLinux7(Core) CPEOSName:cpe:/o:centos:centos:7 Kernel:Linux3.10.0-957.el7.x86_64 Architecture:x86-64 totalusedfreesharedbuff/cacheavailable Mem:18383351146113551314 Swap:204702047 daygeek:x:1000:1000:2daygeek:/home/daygeek:/bin/bash7)如何在远程系统上运行本地Bash脚本
下面的例子允许用户通过ssh在远程Linux机器上运行本地bash脚本remote-test.sh。
创建一个shell脚本并执行它。
$vi/tmp/remote-test.sh #!/bin/bash #Name:remote-test.sh #-------------------- uptime free-m df-h uname-a hostnamectl上面命令的输出如下:
$sshdaygeek@CentOS7.2daygeek.com'bash-s'或者也可以使用管道。如果你觉得输出不太好看,再做点修改让它更优雅些。
$vi/tmp/remote-test-1.sh #!/bin/bash #Name:remote-test.sh echo"---------SystemUptime--------------------------------------------" uptime echo-e"\n" echo"---------MemoryUsage---------------------------------------------" free-m echo-e"\n" echo"---------DiskUsage-----------------------------------------------" df-h echo-e"\n" echo"---------KernelVersion-------------------------------------------" uname-a echo-e"\n" echo"---------HostNameInfo--------------------------------------------" hostnamectl echo"------------------------------------------------------------------"上面脚本的输出如下:
$cat/tmp/remote-test.sh|sshdaygeek@CentOS7.2daygeek.com Pseudo-terminalwillnotbeallocatedbecausestdinisnotaterminal. ---------SystemUptime-------------------------------------------- 03:14:09up2:19,1user,loadaverage:0.00,0.01,0.05 ---------MemoryUsage--------------------------------------------- totalusedfreesharedbuff/cacheavailable Mem:18383761063113981253 Swap:204702047 ---------DiskUsage----------------------------------------------- FilesystemSizeUsedAvailUse%Mountedon /dev/mapper/centos-root27G4.4G23G17%/ devtmpfs903M0903M0%/dev tmpfs920M0920M0%/dev/shm tmpfs920M9.3M910M2%/run tmpfs920M0920M0%/sys/fs/cgroup /dev/sda11014M179M836M18%/boot tmpfs184M12K184M1%/run/user/42 tmpfs184M0184M0%/run/user/1000 tmpfs184M0184M0%/run/user/0 ---------KernelVersion------------------------------------------- LinuxCentOS7.2daygeek.com3.10.0-957.el7.x86_64#1SMPThuNov823:39:32UTC2018x86_64x86_64x86_64GNU/Linux ---------HostNameInfo-------------------------------------------- Statichostname:CentOS7.2daygeek.com Iconname:computer-vm Chassis:vm MachineID:002f47b82af248f5be1d67b67e03514c BootID:dca9a1ba06374d7d96678f9461752482 Virtualization:kvm OperatingSystem:CentOSLinux7(Core) CPEOSName:cpe:/o:centos:centos:7 Kernel:Linux3.10.0-957.el7.x86_64 Architecture:x86-648)如何同时在多个远程系统上运行多条指令
下面的bash脚本允许用户同时在多个远程系统上运行多条指令。使用简单的for循环实现。
为了实现这个目的,你可以尝试PSSH命令或ClusterShell命令或DSH命令。
$vi/tmp/multiple-host.sh forhostinCentOS7.2daygeek.comCentOS6.2daygeek.com do sshdaygeek@${host}"uname-a;uptime;date;w" done上面脚本的输出如下:
$shmultiple-host.sh LinuxCentOS7.2daygeek.com3.10.0-957.el7.x86_64#1SMPThuNov823:39:32UTC2018x86_64x86_64x86_64GNU/Linux 01:33:57up39min,1user,loadaverage:0.07,0.06,0.06 WedSep2501:33:57CDT2019 01:33:57up39min,1user,loadaverage:0.07,0.06,0.06 USERTTYFROMdaygeek@CentOS7.2daygeek.comIDLEJCPUPCPUWHAT daygeekpts/0192.168.1.601:0823:250.06s0.06s-bash LinuxCentOS6.2daygeek.com2.6.32-754.el6.x86_64#1SMPTueJun1921:26:04UTC2018x86_64x86_64x86_64GNU/Linux 23:33:58up39min,0users,loadaverage:0.00,0.00,0.00 TueSep2423:33:58MST2019 23:33:58up39min,0users,loadaverage:0.00,0.00,0.00 USERTTYFROMdaygeek@CentOS7.2daygeek.comIDLEJCPUPCPUWHAT9)如何使用sshpass命令添加一个密码
如果你觉得每次输入密码很麻烦,我建议你视你的需求选择以下方法中的一项来解决这个问题。
如果你经常进行类似的操作,我建议你设置免密码认证,因为它是标准且永久的解决方案。
如果你一个月只是执行几次这些任务,我推荐你使用sshpass工具。只需要使用-p参数选项提供你的密码即可。
$sshpass-p'在这里输入你的密码'ssh-p2200daygeek@CentOS7.2daygeek.comipa 1:lo:mtu65536qdiscnoqueuestateUNKNOWNgroupdefaultqlen1 link/loopback00:00:00:00:00:00brd00:00:00:00:00:00 inet127.0.0.1/8scopehostlo valid_lftforeverpreferred_lftforever inet6::1/128scopehost valid_lftforeverpreferred_lftforever 2:eth0:mtu1500qdiscpfifo_faststateUPgroupdefaultqlen1000 link/ether08:00:27:18:90:7fbrdff:ff:ff:ff:ff:ff inet192.168.1.12/24brd192.168.1.255scopeglobaldynamiceth0 valid_lft86145secpreferred_lft86145sec inet6fe80::a00:27ff:fe18:907f/64scopelinktentativedadfailed valid_lftforeverpreferred_lftforever总结
以上所述是小编给大家介绍的通过SSH在远程Linux系统上运行命令的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!