10.使用ssh命令通过不同的用户名远程访问系统
要使用不同的用户名远程访问系统,我们可以使用带有选项 -l 的 ssh 命令。
请参阅下面的输出。
[root@pc1 Desktop]# ssh -l root 192.168.0.106 # Take remote using different username root@192.168.0.106's password: Last login: Fri Mar 10 08:54:59 2017 from 192.168.0.107
7. 递归地将数据从远程系统复制到本地系统
要递归复制目录及其所有内容,我们可以使用带有选项 -r 的 scp 命令。
请参阅下面的示例输出。
这里我复制一个目录例如:data 从远程系统例如:pc2 到本地系统例如:pc1.
[root@pc1 ~]# scp -r root@192.168.0.106:/data /root/Desktop/ # Copy directory Recursively root@192.168.0.106's password: database.txt 100% 0 0.0KB/s 00:00 [root@pc1 ~]# cd /root/Desktop/ [root@pc1 Desktop]# ls data [root@pc1 Desktop]# ls data/ database.txt [root@pc1 Desktop]#
11. 使用 scp 命令复制隐藏文件和目录。
要复制隐藏文件和目录,我们可以使用带有选项 -rp 的 ssh 命令。
请记住,在使用 scp 命令复制隐藏文件和目录期间,始终将 . (点)在路径的末尾,例如“scp -rp /root/.”。
[root@pc1 ~]# scp -rp /root/. root@192.168.0.106:/hidden # Copy hidden files and directories using scp command root@192.168.0.106's password: .gtk-bookmarks 100% 107 0.1KB/s 00:00 .bashrc 100% 176 0.2KB/s 00:00 .bash_logout 100% 18 0.0KB/s 00:00 .bash_profile 100% 176 0.2KB/s 00:00 database.txt 100% 0 0.0KB/s 00:00 .bash_history 100% 3327 3.3KB/s 00:00 database.txt 100% 0 0.0KB/s 00:00 profiles.ini 100% 104 0.1KB/s 00:00 . . . home-2f9509b7.log 100% 32KB 32.0KB/s 00:00 addressbook.db 100% 12KB 12.0KB/s 00:00 addressbook.db.summary 100% 86 0.1KB/s 00:00 .ICEauthority 100% 1244 1.2KB/s 00:00 [root@pc1 ~]# ssh 192.168.0.106 root@192.168.0.106's password: Last login: Fri Mar 10 08:56:14 2017 from 192.168.0.107 [root@pc2 ~]# ls -a /hidden/ . data .gnote Pictures .. database.txt .gnupg Public anaconda-ks.cfg .dbus .gtk-bookmarks .pulse apps Desktop .gvfs .pulse-cookie .bash_history Documents .ICEauthority .ssh .bash_logout Downloads install.log .tcshrc .bash_profile .esd_auth install.log.syslog Templates .bashrc .gconf .local text.txt .cache .gconfd .mozilla Videos .config .gnome2 Music .cshrc .gnome2_private .nautilus
SSH 是一种代表 Secure Shell 的协议,它是一种客户端命令工具,用于安全地进行 Linux 系统的远程会话。
它使用对称和非对称加密方法通过网络传输数据。
SSH 是一种强烈推荐用于远程连接服务器的工具,因为它是连接任何服务器的最安全方式。
SSH 替代了以前使用的不安全的远程工具,如 telnet。
SSH 的端口号是 22.
SSH 的版本是 SSH Protocol Version 1 & SSH Protocol Version 2 也称为 SSH-1 和 SSH-2 。
SSH-1 和 SSH-2 的区别在于 SSH-2 支持公钥证书,而 SSH-1 不支持。
scp 是一个命令行实用程序,用于通过网络安全地将数据从一个 Linux 系统复制到另一个系统。
像 SSH 一样,SCP 也使用端口号 22 通过网络连接和传输数据。
SCP 是一个很好的替代和推荐工具,可以对抗不安全的数据传输协议,如 FTP、Telnet,并且它通过网络传输数据是高度安全的,因为它使用 SSH 协议来安全地传输数据。
因此,让我们通过示例来看看一些重要的 ssh 命令和 scp 命令。
我的测试环境中有两个 Linux 系统例如:pc1 和 pc2,在下面找到系统详细信息。
实验环境
电脑1:
Computer Name – pc1 IP Address – 192.168.0.105
电脑2:
Computer Name – pc2 IP Address – 192.168.0.106
9. 使用 SSH 命令在远程系统上同时连接和执行命令
我们可以使用服务器的远程会话并同时运行命令。
例如,这里我远程控制系统 pc2 并执行一个命令,例如:ifconfig。
请参阅下面的示例输出。
[root@pc1 ~]# ssh 192.168.0.106 "ifconfig" # take remote and run a command simultaneously root@192.168.0.106's password: eth0 Link encap:Ethernet HWaddr 00:0C:29:01:77:E7 inet addr:192.168.0.106 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe01:77e7/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:9870 errors:0 dropped:0 overruns:0 frame:0 TX packets:4845 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:11182999 (10.6 MiB) TX bytes:393576 (384.3 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:18 errors:0 dropped:0 overruns:0 frame:0 TX packets:18 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:1058 (1.0 KiB) TX bytes:1058 (1.0 KiB)
1. 使用 ssh 命令从 PC1 远程获取 PC2
众所周知,ssh用于安全地获取其他Linux系统的远程控制台,所以让我们从pc1中获取pc2的远程控制台,我们可以使用下面的ssh命令。
语法 :
# ssh username@IP Address of the Remote Computer
[root@pc1 ~]# ssh root@192.168.0.106 # Take Remote of PC2 from PC1 root@192.168.0.106's password: Last login: Thu Mar 9 09:14:34 2017 from 192.168.0.107
远程获取成功,确认检查远程系统的主机名和 IP 地址,如下所示。
[root@pc2 ~]# hostname pc2 [root@pc2 ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:01:77:E7 inet addr:192.168.0.106 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe01:77e7/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:9688 errors:0 dropped:0 overruns:0 frame:0 TX packets:4695 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:11157837 (10.6 MiB) TX bytes:370245 (361.5 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:16 errors:0 dropped:0 overruns:0 frame:0 TX packets:16 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:960 (960.0 b) TX bytes:960 (960.0 b)
3. 从远程控制台注销
要从已经使用的远程控制台注销,请使用命令 logout。
请参阅下面的示例输出。
[root@pc2 ~]# logout # Logout from already taken remote console Connection to 192.168.0.106 closed.
4.检查安装的ssh包的版本
要检查当前安装的 ssh 软件包版本,请使用带有选项 -V 的 ssh 命令。
请参阅下面的示例输出。
[root@pc1 ~]# ssh -V # check the currently installed ssh package version OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Nov 2013
8. 使用 scp 命令将数据从本地系统递归复制到远程系统
要将目录及其所有内容从本地系统递归复制到远程系统,我们可以使用带有选项 -r 的 scp 命令。
请参阅下面的示例输出。
[root@pc1 ~]# ls apps/ test1.txt test2.txt test3.txt test4.txt test5.txt [root@pc1 ~]# scp -r apps/ root@192.168.0.106:/root # Copy directory Recursively from Local System to Remote System root@192.168.0.106's password: test2.txt 100% 0 0.0KB/s 00:00 test1.txt 100% 0 0.0KB/s 00:00 test4.txt 100% 0 0.0KB/s 00:00 test5.txt 100% 0 0.0KB/s 00:00 test3.txt 100% 0 0.0KB/s 00:00
6. 使用 SCP 命令从远程系统下载/复制数据
要将数据从远程系统复制到本地系统,我们可以使用以下命令。
在这里,我将数据从 pc2 复制到 pc1.
[root@pc1 ~]# scp root@192.168.0.106:/data/database.txt /root/ # Copy Data from Remote System to Local using scp Command root@192.168.0.106's password: database.txt 100% 0 0.0KB/s 00:00 [root@pc1 ~]# ls anaconda-ks.cfg Desktop install.log Pictures text.txt data Documents install.log.syslog Public Videos database.txt Downloads Music Templates
5. 使用 scp 命令通过网络将数据从一个 Linux 系统复制到另一个
要将数据从一个 linux 系统复制到另一个系统,我们可以使用 scp 命令。
在这里,我将数据例如:touch.txt 文件从 pc1 复制到 pc2.
请参阅下面的示例输出。
[root@pc1 ~]# touch text.txt # Create a Sample text File [root@pc1 ~]# ls anaconda-ks.cfg Documents install.log.syslog Public Videos data Downloads Music Templates Desktop install.log Pictures text.txt [root@pc1 ~]# scp text.txt root@192.168.0.106:/root/Desktop/ # Copy data from Local System to Remote System root@192.168.0.106's password: text.txt 100% 0 0.0KB/s 00:00
2.使用当前登录的用户名获取系统的远程控制台
我们可以使用当前登录的用户名远程控制系统。
例如,在 pc1 中,我使用用户名“root”登录,我想使用相同的用户名进行 pc2 机器的远程会话,例如:“root”,然后我们可以使用以下命令。
[root@pc1 ~]# ssh 192.168.0.106 # Take Remote using currently logged in username root@192.168.0.106's password: Last login: Thu Mar 9 09:08:54 2017 from 192.168.0.107 [root@pc2 ~]# hostname pc2