Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the specific method of running Linux commands remotely using SSH

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Use SSH to run Linux commands remotely, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

SSH is the basic tool for remote connection under Linux, but it would be a waste if you only use it to log in! SSH command is an artifact to complete remote operations, ah, with which we can automate a lot of remote operations. Let's share the specific methods of using SSH to run Linux commands remotely.

SSH allows you to run commands on a remote computer without logging in to it. 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-hFilesystem Size Used Avail Use% Mounted on/dev/mapper/centos-root 27G 4.4G 23G 17% / devtmpfs 903M 0903M 0% / devtmpfs 920M 0920m 0% / dev/shmtmpfs 920M 9.3M 910m 2% / runtmpfs 920M 0% 920m 0% / sys/fs/cgroup/dev/sda1 1014M 179M 836M 18% / boottmpfs 184M 8.0K 184m 1% / run/user/42tmpfs 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 cachedMem: 1878 432 1445 1 100 Muhammad + buffers/cache: 197 1680Swap: 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 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x000bf685Device Boot Start End Blocks Id System/dev/sda1 * 2048 2099199 1048576 83 Linux/dev/sda2 2099200 62914559 304076808e Linux LVMDisk / dev/sdb: 10.7 GB, 10737418240 bytes 20971520 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk / dev/mapper/centos-root: 29.0 GB, 28982640640 bytes, 56606720 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk / dev/mapper/centos-swap: 2147 MB, 2147 MB, 2147 483648 bytes 4194304 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesConnection to centos7.2daygeek.com closed.

4. How to run the service control command with sudo authority on the 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 a 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.txttop-01:13:11 up 18 min, 1 user, load average: 0.01,0.05, 0.10Tasks: 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 stKiB Mem: 1882300 total, 1176324 free, 342392 used, 363584 buff/cacheKiB Swap: 2097148 total, 2097148 free, 0 used. 1348140 avail MemPID 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 MEM TIME+ COMMAND 03.08 / usr/lib/sy+ 2 root 20 00 00 S 0.0 0.00 [kthreadd] 3 root 20 00 00 S 0.0 0.00: 00.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.00. 00: 00.00 [lru-add-dr+ 11 root rt 00 00 S 0.0 0.00: 00.01 [watchdog/0] 13 root 20 00 00 S 0.0 0.00: 00.00 [kdevtmpfs] 14 root 0-20 000 S 0.0 0.00: 00.00 [netns] 15 Root 20 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 0 S 0.0 0.00: 00.00 [bioset] 19 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

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 availableMem: 1838 335 1146 11 355 1314Swap: 2047 0 2047daygeek:x:1000:1000:2daygeek:/home/daygeek:/bin/bash

7. How to run local Bash scripts on remote systems

The following example allows the user to run the local bash script remote-test.sh on a remote Linux machine through ssh.

Create a shell script and execute it.

$vi / tmpgamma remotelytest.shallows ahostnamectl. Bingray bashcards name: remote-test.sh#-uptimefree-mdf-huname-ahostnamectl

The output of the above command is as follows:

$ssh daygeek@CentOS7.2daygeek.com 'bash-s' # 1 SMP Thu Nov 8 23:39:32 UTC 2018 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

Or you can use pipes. If you think the output doesn't look good, make some changes to make it more elegant.

$vi / tmpCompact RemoteMustMuth1.shemeasure binder Basiple name: remote-test.shecho "- System Uptime----" uptimeecho-e "\ n" echo "- Memory Usage- -- "free-mecho-e"\ n "echo"-Disk Usage--- "df-hecho-e"\ n "echo"-Kernel Version--- "uname-aecho-e"\ n "echo"-HostName Info---- "hostnamectlecho"- -"

The output of the above script is as follows:

$cat / tmp/remote-test.sh | ssh daygeek@CentOS7.2daygeek.comPseudo-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 availableMem: 1838 376 1063 11 398 1253Swap: 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 / devtmpfs 920M 0920m 0% / dev/shmtmpfs 920M 9.3M 910m 2% / runtmpfs 920M 0920M 0% / sys/fs/cgroup/dev/sda1 1014M 179M 836M 18% / boottmpfs 184M 12K 184m 1% / run/user/42tmpfs 184m 0 184m 0% / run/user/1000tmpfs 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

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. Using a simple for loop implementation.

To do this, you can try the PSSH command or ClusterShell command or DSH command.

$vi / tmp/multiple-host.shfor host in CentOS7.2daygeek.com CentOS6.2daygeek.comdo ssh daygeek@$ {host} "uname-a _ uptime _ date _ exchange _ w" done

The output of the above script is as follows:

$sh multiple-host.shLinux CentOS7.2daygeek.com 3.10.0-957.el7.x86_64 # 1 SMP Thu Nov 8 23:39:32 UTC 2018 x86 "64 GNU/Linux 01:33:57 up 39 min, 1 user, load average: 0.07,0.06, 0.06Wed Sep 25 01:33:57 CDT 2019 01:33:57 up 39 min, 1 user, load average: 0.07,0.06 0.06USER TTY FROM daygeek@CentOS7.2daygeek.com IDLE JCPU PCPU WHATdaygeek pts/0 192.168.1.6 01:08 23:25 0.06s-bashLinux CentOS6.2daygeek.com 2.6.32-754.el6.x86_64 # 1 SMP Tue Jun 19 21:26:04 UTC 2018 x86 "64 GNU/Linux 23:33:58 up 39 min, 0 users, load average: 0.00,0.00 0.00Tue Sep 24 23:33:58 MST 2019 23:33:58 up 39 min, 0 users, load average: 0.00, 0.00, 0.00USER TTY FROM daygeek@CentOS7.2daygeek.com IDLE JCPU PCPU WHAT

9. How to add a password using the sshpass command

If you find it troublesome to enter a password every time, I suggest you choose one of the following methods to solve the problem depending on your needs.

If you often do something like this, I recommend that you set up password-free authentication, as it is a standard and permanent solution.

If you only perform these tasks a few times a month, I recommend you use the sshpass tool. Just use the-p parameter option to provide your password.

$sshpass-p 'enter your password here' ssh-p 2200 daygeek@CentOS7.2daygeek.com ip A1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 link/loopback 0000 scope host valid_lft forever preferred_lft forever2: eth0: mtu 1500 Qdisc pfifo_fast state UP group default qlen 1000 link/ether 08VOG 00VO 27F brd ff:ff:ff:ff:ff:ff inet 192.168.1.12Accord 24 brd 192.168.1.12Accord 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 what is the Linux system Linux is a free-to-use and free-spread UNIX-like operating system Is a POSIX-based multi-user, multi-tasking, multi-threaded and multi-CPU operating system, using Linux to run major Unix tools, applications and network protocols.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report