下面的例子允许用户通过 ssh 在远程 Linux 机器上运行带有 sudo 权限的服务控制命令。
$ ssh -t daygeek@CentOS7.2daygeek.com "sudo systemctl restart httpd" -
[sudo] password for daygeek: Connection to centos7.2daygeek.com closed.
5) 如何通过非标准端口 SSH 在远程 Linux 系统上运行命令
下面的例子允许用户通过 ssh 在使用了非标准端口的远程 Linux 机器上运行 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) 如何将远程系统的输出保存到本地系统
下面的例子允许用户通过 ssh 在远程 Linux 机器上运行 top 命令,并将输出保存到本地系统。
$ 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]
(编辑:ASP站长网)
|