06-SSH远程连接服务
SSH远程连接服务
1. SSH基本概述
- SSH是一个安全协议,在进行数据传输时,会对数据包进行加密处理,加密后在进行数据传输。确保了数据传输安全。那SSH服务主要功能有哪些呢?
- 提供远程连接服务器的服务
- 对传输的数据进行加密
2. ssh 服务与 telnet
2.1 ssh 服务与telnet 区别
- ssh 服务会对传输数据进行加密, 监听在本地 22/tcp端口, ssh 服务默认支持root 用户登录
- telnet服务不对数据进行加密, 监听在本地23/tcp 端口, telnet 默认不支持root 用户登录
| 服务连接方式 | 服务数据传输 | 服务监听端口 | 服务登陆用户 | 登陆场景 |
|---|---|---|---|---|
| ssh | 加密 | 22/tcp | 默认支持root用户登陆 | 连接Linux操作系统 |
| telnet | 明文 | 23/tcp | 不支持root用户登陆 | 网络工程师用来连接交换机路由器 |
2.2 telnet服务连接与抓包
1. 安装并启动telnet服务
-
root@SSH 16:35:35 ~ # yum -y install telnet-server root@SSH 16:36:39 ~ # systemctl start telnet.socket
2. 使用普通用户进行telnet登录
-
root@SSH 16:36:39 ~ # useradd oldboy root@SSH 16:36:39 ~ # echo "1"| passwd --stdin oldboy
3. 测试22端口并用本地windows连接telnet
-
#.测试端口 [c:\~]$ telnet 10.0.0.200 22 Connecting to 10.0.0.200:22... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. SSH-2.0-OpenSSH_7.4 #.telnet连接10,0,0,200 [c:\~]$ telnet 10.0.0.200 Connecting to 10.0.0.200:23... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. SSH login: oldboy Password: Last login: Sun Jun 12 23:47:59 from 10.0.0.1 oldboy@SSH 16:40:58 ~ $ # 已连接到10.0.0.200的oldboy用户
4. 用 wireshark 抓包 VMnet8 的telnet服务

- 右键-追踪流-tcp流
![]()
2.3 ssh服务连接与抓包
1. 用本地windows 连接ssh
-
[c:\~]$ ssh 10.0.0.200 Connecting to 10.0.0.200:22... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. Last login: Mon Jun 13 16:56:03 2022 from localhost oldboy@SSH 17:25:55 ~ $ # 已连接到10.0.0.200的oldboy用户
2. 用wireshark 抓包VMnet8 的ssh服务

-
右键-追踪流-tcp流
![]()
-
发现用wireshark 抓包VMnet8 的ssh服务的tcp流时乱码的,因为ssh在传输时对数据进行了加密
3. SSH相关命令
- SSH有客户端与服务端,我们将这种模式称为C/S架构,ssh客户端支持Windows、Linux、Mac等平台。
- 在ssh客户端中包含 ssh|slogin远程登陆、scp远程拷贝、sftp文件传输、ssh-copy-id秘钥分发等应用程序。
3.1 ssh远程登陆
1. windows登录linux 【用Xshell】
-
语法结构
- ssh 10.0.0.200
- ssh oldboy@10.0.0.200
- ssh oldboy@10.0.0.200 22
-
# 直接连接 默认是22端口 会弹出xshell的输入用户名和密码对话框 [c:\~]$ ssh 10.0.0.200 Connecting to 10.0.0.200:22... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. Last login: Mon Jun 13 17:09:47 2022 from 10.0.0.1 root@SSH 17:34:09 ~ # # 指定用户连接 指定用户名方式远程连接 [c:\~]$ ssh oldboy@10.0.0.200 Connecting to 10.0.0.200:22... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. Last login: Mon Jun 13 17:25:55 2022 from 10.0.0.1 # 指定端口连接 [c:\~]$ ssh oldboy@10.0.0.200 22 Connecting to 10.0.0.200:22... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. Last login: Mon Jun 13 17:41:26 2022 from 10.0.0.1 oldboy@SSH 17:43:08 ~ $
2. linux登录linux
-
语法结构
- ssh 10.0.0.7
- ssh root@10.0.0.7
- ssh -p22 root@10.0.0.200
-
# 默认远程连接,端口默认22,用户默认以当前登陆的系统用户为远程连接用户,如果当前200登陆是root则验证 10.0.0.7的root用户身份密码 root@SSH 17:47:04 ~ # ssh 10.0.0.7 root@10.0.0.7's password: Last login: Mon Jun 13 17:35:58 2022 from 10.0.0.200 root@web01 17:47:24 ~ # # 指定用户名连接 root@SSH 17:48:35 ~ # ssh root@10.0.0.7 root@10.0.0.7's password: Last login: Mon Jun 13 17:47:24 2022 from 10.0.0.200 # 指定端口远程连接 -p22 root@SSH 17:49:16 ~ # ssh -p22 root@10.0.0.7 root@10.0.0.7's password: Last login: Mon Jun 13 17:48:48 2022 from 10.0.0.200 root@web01 17:49:25 ~ # --------------------------------------------------------------------------------------------- # 不登录用户远程操作 1.远程查看网卡信息 root@SSH 09:30:50 ~ # ssh 10.0.0.7 'ifconfig eth0' eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.7 netmask 255.255.255.0 broadcast 10.0.0.255 inet6 fe80::20c:29ff:fe91:912e prefixlen 64 scopeid 0x20<link> ether 00:0c:29:91:91:2e txqueuelen 1000 (Ethernet) RX packets 55995 bytes 46944102 (44.7 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 48589 bytes 63677200 (60.7 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 2.远程查看ip地址 root@SSH 09:36:36 ~ # ssh 10.0.0.7 'hostname -I' 10.0.0.7 172.16.1.7 3.查看远程服务磁盘的信息 root@SSH 22:05:08 ~ # ssh 10.0.0.7 'df -h' Filesystem Size Used Avail Use% Mounted on devtmpfs 476M 0 476M 0% /dev tmpfs 487M 0 487M 0% /dev/shm tmpfs 487M 7.7M 479M 2% /run tmpfs 487M 0 487M 0% /sys/fs/cgroup /dev/sda3 18G 2.1G 16G 12% / /dev/sda1 197M 110M 88M 56% /boot tmpfs 98M 0 98M 0% /run/user/0
3. 两种连接方式的区别
# windows连接linux
ssh 10.0.0.31 默认22端口 用户密码和密码都要在对话框输入
ssh root@10.0.0.31 22 指定用户名和端口的方式远程连接
# Linux连接Linux
ssh 10.0.0.31 默认以当前系统登陆用户身份进行远程连接验证 默认端口22
ssh -p22 root@10.0.0.31 指定端口和密码方式进行远程连接
3.2 scp命令
1. scp含义
- scp是基于ssh服务的一种linux主机之间的远程拷贝命令
2. scp和rsync的区别
- rsync 增量备份 推送目录加/ 表示目录下面的内容 不加/ 表示目录本身
- scp 全量备份 加/和不加/ 都表示目录本身
3. scp的相关参数和语法结构
-
参数 含义 -P 指定端口,默认22端口可不写 -r 表示递归拷贝目录 -p 表示在拷贝文件前后保持文件或目录属性不变 -l 限制传输使用带宽(默认kb) -
# 从远端拷贝到当前 scp -P22 -rp root@10.0.0.61:/tmp/oldboy /opt/ 全量拷贝远端服务器10.0.0.61的root用户/tmp/oldboy的内容到本地的/opt/目录下 # 从当前拷贝到远端 scp -P22 -rp /tmp/oldboy root@10.0.0.61:/tmp 全量拷贝当前/tmp/oldboy的内容到远端服务器10.0.0.61的root用户的/tmp目录下 # 限速拷贝 scp -rp -l 8096 /opt/1.txt root@10.0.0.61:/tmp 将本地的/opt/1.txt拷贝到远端服务器的/tmp目录下 限速1MB/S # 限速为8096kb,换算为MB,要除以 8096/8=1024KB=1MB
4. 案例
-
# 将远端服务器拷贝到本地 1.将远程10.0.0.7服务器/etc/passwd文件拉取到本地/root/目录下 root@SSH 18:19:40 ~ # scp root@10.0.0.7:/etc/passwd /root/ root@10.0.0.7's password: 1 passwd 100% 1162 1.6MB/s 00:00 root@SSH 18:23:17 ~ # ll total 4 -rw-r--r-- 1 root root 1162 Jun 13 18:23 passwd 2.将远程10.0.0.7服务器/root/oldboy目录及目录下的文件拉取到本地/root/目录下 root@SSH 18:23:55 ~ # scp -rp root@10.0.0.7:/root/oldboy /root/ # -r是递归拷贝,可以拷贝目录 root@10.0.0.7's password: 1 1.txt 100% 0 0.0KB/s 00:00 2.txt 100% 0 0.0KB/s 00:00 3.txt 100% 0 0.0KB/s 00:00 root@SSH 18:24:18 ~ # ll total 4 drwxr-xr-x 2 root root 45 Jun 13 18:24 oldboy 3.将远程10.0.0.7服务器/etc/下的passwd和hosts文件打包拉取到本地/root/目录下,指定22端口 root@web01 18:53:08 ~ # tar zcfP ETC.tar.gz /etc/passwd /etc/hosts root@web01 18:53:16 ~ # ll total 4 -rw-r--r-- 1 root root 696 Jun 13 18:53 ETC.tar.gz root@SSH 18:55:05 ~ # scp -P22 -rp root@10.0.0.7:/root/ETC.tar.gz /root root@10.0.0.7's password: ETC.tar.gz 100% 696 618.6KB/s 00:00 root@SSH 18:55:09 ~ # ll total 4 -rw-r--r-- 1 root root 696 Jun 13 18:53 ETC.tar.gz root@SSH 18:55:28 ~ # tar xfP ETC.tar.gz root@SSH 18:55:56 ~ # ll total 4 -rw-r--r-- 1 root root 696 Jun 13 18:53 ETC.tar.gz root@SSH 18:56:06 ~ # ll etc total 8 -rw-r--r-- 1 root root 158 Jun 7 2013 hosts -rw-r--r-- 1 root root 1162 Jun 11 16:45 passwd # 已拷贝完成 --------------------------------------------------------------------------------------------- # 将本地拷贝到远端服务器 1.将本地/root/yong.txt推送至远端服务器10.0.0.7的/root目录,使用对端的root用户 root@SSH 18:33:54 ~ # scp young.txt root@10.0.0.7:/root root@10.0.0.7's password: young.txt 100% 0 0.0KB/s 00:00 root@web01 18:13:19 ~ # ll total 0 -rw-r--r-- 1 root root 0 Jun 13 18:34 young.txt # 已拷贝完成 2.将本地/root/haha目录及目录下的文件推送至远端服务器10.0.0.7的/root目录,使用对端的root用户 root@SSH 18:34:07 ~ # scp -r haha root@10.0.0.7:/root root@10.0.0.7's password: a.txt 100% 0 0.0KB/s 00:00 b.txt 100% 0 0.0KB/s 00:00 c.txt 100% 0 0.0KB/s 00:00 root@web01 18:34:12 ~ # ll total 0 drwxr-xr-x 2 root root 45 Jun 13 18:37 haha # 已拷贝完成 3.将本地/etc下的passwd和hosts文件推送至远端服务器10.0.0.7的/root目录,指定22端口 root@SSH 18:56:13 ~ # scp -P22 -rp /etc/passwd /etc/hosts root@10.0.0.7:/root root@10.0.0.7's password: passwd 100% 1205 1.1MB/s 00:00 hosts 100% 158 274.0KB/s 00:00 root@web01 18:53:18 ~ # ll total 12 -rw-r--r-- 1 root root 158 Jun 7 2013 hosts -rw-r--r-- 1 root root 1205 Jun 12 23:28 passwd # 已拷贝完成 ---------------------------------------------------------------------------------------------- # 限速拷贝 1.不限速 root@SSH 18:34:07 ~ # scp /opt/1.txt root@172.16.1.31:/tmp root@172.16.1.31 password: test 100% 656MB '83.9MB/s' 00:07 # 限速为8096kb,换算为MB,要除以 8096/8=1024KB=1MB 2.限速 root@SSH 18:34:07 ~ # scp -rp -l 8096 /opt/1.txt root@172.16.1.31:/tmp root@172.16.1.31s password: test 7% 48MB '1.0MB/s' 09:45
5. 结论
-
1.scp通过ssh协议加密方式进行文件或目录拷贝。 2.scp连接时的用户作为为拷贝文件或目录的权限。 3.scp支持数据推送和拉取,每次都是全量拷贝,效率较低。
4. SSH秘钥远程登录
4.1 密钥登录原理
- 默认情况下,通过ssh客户端命令登陆远程服务器,需要提供远程系统上的帐号与密码,但为了降低密码泄露的机率和提高登陆的方便性,建议使用密钥验证方式。
![]()
4.2 语法结构
1. 生成密钥对:ssh-keygen
在服务器上生成非对称密钥,使用-t指定密钥类型,使用-C指定用户邮箱
[root@nfs ~]# ssh-keygen -t rsa -C 3437369785@qq.com
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #保存的位置
Enter passphrase (empty for no passphrase): #输入密码没有则为空
Enter same passphrase again: #再次输入密码
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is: #关键指纹是
SHA256:RIWiZ0QM+LkTNsRgg9Bke0ABkMMa9/xSdvXG8lK+oZM 3437369785@qq.com
The key's randomart image is:
+---[RSA 2048]----+
|BBX=.+. .o. |
|=+++o +.. . |
|.+.=.+ ... o |
|. .X =.. . = |
| . X .S * |
| + . . + |
| o + o |
| E . |
| . |
+----[SHA256]-----+
-
ssh-keygen命令 用于为“ssh”生成、管理和转换认证密钥,它支持RSA和DSA两种认证密。 SSH的密钥默认保留在 ~/.ssh 目录中。 如果没有 ~/.ssh 目录,ssh-keygen命令会使用正确的权限创建一个。 # 命令实例: ssh-keygen [-q] [-b bits] [-t dsa | ecdsa | ed25519 | rsa | rsa1] [-N new_passphrase] [-C comment] [-f output_keyfile] -b:指定密钥长度; -t:指定要创建的密钥类型。 -N:提供一个新密语 -e:读取openssh的私钥或者公钥文件; -C:添加注释; -f:指定用来保存密钥的路径; -i:读取未加密的ssh-v2兼容的私钥/公钥文件,然后在标准输出设备上显示openssh兼容的私钥/公钥; -l:显示公钥文件的指纹数据; -P:提供密语; -q:安静输出; # 生成密钥对: 1).ssh-keygen -m PEM -t rsa -b 4096 ssh-keygen 命令默认在 ~/.ssh 目录中生成 4096 位 SSH RSA 公钥和私钥文件。 如果当前位置存在 SSH 密钥对,这些文件将被覆盖。 2).ssh-keygen -t rsa -C 'rumenz@qq.com' -f ~/.ssh/github_id_rsa ssh-keygen 在指定存放文件时输入一个新的文件名 3).使用ssh-kengen会在~/.ssh/目录下生成两个文件,不指定文件名和密钥类型的时候,默认生成的两个文件是 id_rsa 第一个是私钥文件 id_rsa.pub 第二个是公钥文件 # 案例: 生成密钥对,类型为rsb,长度为4096,保存的地址为.ssh/id_rsa [root@m01 ~]# ssh-keygen -t rsa -b 4096 -f .ssh/id_rsa -P ''
2. 分发公钥:ssh-copy-id
-
# 命令示例: ssh-copy-id [-i [identity_file]] [user@]machine ssh-copy-id -i ~/.ssh/id_rsa.pub user@server ssh-copy-id #命令 -i #指定下发公钥的路径,一般都在加目录下的.ssh隐藏文件 [user@] #以什么用户身份进行公钥分发(root),如果不输入,表示以当前系统用户身份分发公钥 machine #下发公钥至那台服务器, 填写远程主机IP地址 # 分发秘钥: 将A服务器的公钥写入B服务器~/.ssh/authorized_keys文件中 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.17.0.2
4.3 linux对linux远程密钥登录
-
Linux通过秘钥连接Linux服务器:10.0.0.200 远程连接10.0.0.7
-
1.在10.0.0.200上生成密钥 # 创建一把锁和钥匙 【使用-t指定密钥类型 使用-C指定用户邮箱,可省略】 root@SSH 19:09:43 ~ # ssh-keygen -t rsa -C 503472795@qq.com Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:n2j38ZH9aAcqn7y9t6DmsjilCa7uIQo5nrmu2ZF8ejw 503472795@qq.com The key's randomart image is: +---[RSA 2048]----+ | | | | | | | | | S | | o . . + . .o | |+.+oo . * + .oo..| |++++E. =.o.+o=oo+| |***=.. ...==*o==o| +----[SHA256]-----+ ---------------------------------------------------------------------------------------------- 2.查看密钥生成文件 # 默认在登录用户的家目录 root@SSH 19:36:23 ~ # ll -a total 44 dr-xr-x---. 4 root root 155 Jun 13 19:36 . dr-xr-xr-x. 20 root root 285 Jun 11 13:30 .. drwx------ 2 root root 57 Jun 13 19:34 .ssh root@SSH 19:36:26 ~ # ll .ssh total 12 -rw------- 1 root root 1679 Jun 13 19:34 id_rsa # 私钥 -rw-r--r-- 1 root root 398 Jun 13 19:34 id_rsa.pub # 公钥 -rw-r--r-- 1 root root 170 Jun 13 17:35 known_hosts ---------------------------------------------------------------------------------------------- 3.将公钥发送到10.0.0.7服务器,默认发送到登录用户的家目录 root@SSH 19:36:38 ~ # ssh-copy-id -i .ssh/id_rsa.pub root@10.0.0.7 # 登录身份可指定,不写默认 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@10.0.0.7's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@10.0.0.7'" and check to make sure that only the key(s) you wanted were added. # 到10.0.0.7服务器root用户的家目录下查看 root@web01 19:45:10 ~ # ll .ssh total 8 -rw------- 1 root root 398 Jun 13 19:44 authorized_keys # 密码文件权限600 -rw-r--r-- 1 root root 170 Jun 13 18:09 known_hosts root@web01 19:45:16 ~ # cat .ssh/authorized_keys ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCsJLh+qCBe0EvnIgHx6H78NN1N8fFs2g8zHbgaQ9KFons3bFq1ynkRXj1t31u6biLJY8ycWXNxG3LL1BFfau26BFDLWZwFprir3UJ2UyPO+AyGz9BundkFRZ3UiZmziHgMF4AxJGuWDmKesjH81Jpbc1HmQueNapV3ZA8m6LHo6jBRwrUsc0NRJJ285Y+D8N75QKNStrdYWVBFcUbHOux1kDnBw9T3KN57ZkG8/cm7Vr83EbAB5EusXfvDhonxbR7eqGa1vtlu1VNEu2dsfa3fNjUQ2w2QtqfzCVzPIYjESV/VLEoFOxYs210aArj22lgtj1WZSjKuYplLXjwwG9Y7 503472795@qq.com 4.远程登陆测试 不需要输入用户名和密码 免密连接 root@SSH 19:44:29 ~ # ssh 10.0.0.7 Last login: Mon Jun 13 17:49:25 2022 from 10.0.0.200 root@web01 19:47:30 ~ # ---------------------------------------------------------------------------------------------- # 在第三步中,我们将200生成的公钥匙发送给了7,如果不想发送的话,我们可以执行如下操作 1).在主机7 root用户的家目录创建隐藏目录.ssh,并在该目录下创建一个密码文件,写入公钥的密码内容 root@web01 20:43:55 ~ # mkdir .ssh root@web01 20:44:07 ~ # vim .ssh/authorized_keys ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCsJLh+qCBe0EvnIgHx6H78NN1N8fFs2g8zHbgaQ9KFons3bFq1ynkRXj1t31u6biLJY8ycWXNxG3LL1BFfau26BFDLWZwFprir3UJ2UyPO+AyGz9BundkFRZ3UiZmziHgMF4AxJGuWDmKesjH81Jpbc1HmQueNapV3ZA8m6LHo6jBRwrUsc0NRJJ285Y+D8N75QKNStrdYWVBFcUbHOux1kDnBw9T3KN57ZkG8/cm7Vr83EbAB5EusXfvDhonxbR7eqGa1vtlu1VNEu2dsfa3fNjUQ2w2QtqfzCVzPIYjESV/VLEoFOxYs210aArj22lgtj1WZSjKuYplLXjwwG9Y7 503472795@qq.com root@web01 20:45:10 ~ # ll .ssh total 4 -rw-r--r-- 1 root root 398 Jun 13 20:45 authorized_keys 2)修改密码文件的权限为600 root@web01 20:47:20 ~ # chmod 600 .ssh/authorized_keys root@web01 20:48:28 ~ # ll .ssh total 4 -rw------- 1 root root 398 Jun 13 20:45 authorized_keys 3)测试远程登录 root@SSH 20:43:51 ~ # ssh 10.0.0.7 Last login: Mon Jun 13 20:42:39 2022 from 10.0.0.200 root@web01 20:48:03 ~ #
4.4 windows远程连接linux跳板机 然后跳板机再连接web01和web02
-
用户通过Windows客户端连接跳板机免密码登录,跳板机连接后端无外网的Linux主机实现免密登录,架构图如下
-
实践多用户登陆一台服务器无密码
-
实践单用户登陆多台服务器免密码
-
角色 外网IP 内网IP 主机名 Windows 192.168.1.102 xshell 跳板机 10.0.0.200 172.16.1.200 SSH linux服务器A 172.16.1.7 web01 inux服务器B 172.16.1.8 web02

1.windows通过秘钥连接Linux服务器:windows远程连接10.0.0.200


-
1.xshell中生成公钥匙 【xshell--->工具--->新建用户秘钥生成向导】 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArddrpItNjF2jI1tPinAP5Zij2+nwFNnBw+s42G/fzREAB0yKWr/ukNR64psMAp5JbvzW5gZiGgWlCt4oAThAIc9I7n3M7aY8VaeC7503mP0ehwQXTHQc78JtF/eUHosLKABjjFNxdeu0dN495qffaiokMNxKBMIy/y7bJBhUULmehovHk12ezaQHMrG3qoHfV0T87/5vXQ5Oft17kfPtdRxR56LBMGIa4oehiccRv0A6y+tusoeX3XgM3R9E3kspTrbLguLLJJ9InL+hj7Tmwz2VqBAsk68ot/E9+aDjiVKo6T2r41wj4rS5t+56T5yG6wnjdhC3UBcqXmtjgwjD2w== 2.在200的主机中修改修改ssh服务配置文件/etc/ssh/sshd_config,并重启服务【限制root登陆,限制密码认证(只能通过秘钥方式进行验证)】 PermitRootLogin no # 关闭root登陆 PasswordAuthentication no # 关闭密码验证 root@SSH 21:52:03 ~ # systemctl restart sshd 3.用oldboy用户登录200主机,在oldboy用户的家目录创建.ssh目录,并创建authorized_keys密码文件,写入windows公钥匙 oldboy@SSH 22:00:48 ~ $ mkdir .ssh oldboy@SSH 22:00:54 ~ $ vim .ssh/authorized_keys 4.修改.ssh目录权限为700,修改密码文件的权限为600 oldboy@SSH 22:04:08 ~ $ chmod 600 .ssh/authorized_keys oldboy@SSH 22:05:18 ~ $ chmod 755 .ssh 5.测试windows用oldboy用户登录200 [c:\~]$ ssh 10.0.0.200 Connecting to 10.0.0.200:22... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. Last login: Mon Jun 13 21:59:35 2022 from 10.0.0.1 oldboy@SSH 22:05:52 ~ $ # 成功登录
2. linux远程连接linux-200连接7和8
-
1.首先将200主机的oldboy用户提权到root用户 oldboy@SSH 22:30:19 ~ $ su - root Password: Last login: Mon Jun 13 22:29:59 CST 2022 from 10.0.0.1 on pts/0 root@SSH 22:32:31 ~ # ---------------------------------------------------------------------------------------------- 2.在root用户家目录下创建密匙文件 root@SSH 22:32:31 ~ # ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:A1a21jj0tQ6buOu1tD5cPmnme8K7m7963xGcXKkPqWM root@SSH The key's randomart image is: +---[RSA 2048]----+ | + . | | + = . . .| | o = + . ..| | . o o = = o| | S o .+ = | | o o o .| | ..oE . o | | ++oX.o o| | .o.+=O%ooo| +----[SHA256]-----+ ---------------------------------------------------------------------------------------------- 3.分别以oldboy用户身份将公钥匙发送给172.16.1.7和172.16.1.8 # 公钥匙发送给172.16.1.7 root@SSH 22:33:23 ~ # ssh-copy-id -i .ssh/id_rsa.pub root@10.0.0.7 /bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub" The authenticity of host '10.0.0.7 (10.0.0.7)' can't be established. ECDSA key fingerprint is SHA256:LbUlfXUR6bxgVQ4caeFc77yXbxJtNMHdElVbXZV3ybg. ECDSA key fingerprint is MD5:e8:cf:9d:e8:43:9b:86:2d:52:a4:b9:4d:3b:d7:13:2d. Are you sure you want to continue connecting (yes/no)? yes /bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@10.0.0.7's password: 1 Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@10.0.0.7'" and check to make sure that only the key(s) you wanted were added. # 公钥匙发送给172.16.1.8 root@SSH 22:35:53 ~ # ssh-copy-id -i .ssh/id_rsa.pub root@10.0.0.8 /bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub" The authenticity of host '10.0.0.8 (10.0.0.8)' can't be established. ECDSA key fingerprint is SHA256:LbUlfXUR6bxgVQ4caeFc77yXbxJtNMHdElVbXZV3ybg. ECDSA key fingerprint is MD5:e8:cf:9d:e8:43:9b:86:2d:52:a4:b9:4d:3b:d7:13:2d. Are you sure you want to continue connecting (yes/no)? yes /bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@10.0.0.8's password: 1 Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@10.0.0.8'" and check to make sure that only the key(s) you wanted were added. ---------------------------------------------------------------------------------------------- 4.测试是否可以再200主机上远程登录172.16.1.7和172.16.1.8 # 远程登录172.16.1.7 root@SSH 22:39:56 ~ # ssh 172.16.1.7 Last login: Mon Jun 13 22:39:38 2022 from 172.16.1.200 root@web01 22:39:58 ~ # exit logout Connection to 172.16.1.7 closed. # 远程登录172.16.1.8 root@SSH 22:40:08 ~ # ssh 172.16.1.8 Last login: Mon Jun 13 22:39:50 2022 from 172.16.1.200 root@web02 22:40:11 ~ # exit logout Connection to 172.16.1.8 closed. ---------------------------------------------------------------------------------------------- # 补充 为什么不用oldboy用户身份远程登录并分发密钥
1.远程登录的服务器中没有创建普通用户oldboy,所以无法以oldboy身份连接
2.如果以oldboy身份远程登录到服务器7和8上,那么需要再oldboy的家目录/home/oldboy目录下创建.ssh一场目录
并创建密码文件,此时需要修改文件和目录的属主属组为oldboy,相对比较繁琐。
- ##### 到此,windows可以连接跳板机,再连接后端服务器172.16.1.7 和 172.16.1.8
#### 3. 注意事项总结
- ```bash
1. 检查秘钥是否正确
2. .ssh/authorized_keys 这个验证文件名称必须正确的
3. 检查文件权限是否600
4. 检查目录的权限700
5. 检查/root目录的权限为550 属主属组root
5. SSH安全优化 /etc/ssh/sshd_config
-
SSH作为远程连接服务,通常我们需要考虑到该服务的安全,所以需要对该服务进行安全方面的配置。
-
1.更改远程连接登陆的端口 # 默认端口22 2.禁止ROOT管理员直接登录 # 用普通用户身份登录 3.密码认证方式改为密钥认证 # 禁止密码登录,用密钥的形式登录 4.重要服务不使用公网IP地址 # 用局域网ip防止黑客攻击 5.使用防火墙限制来源IP地址 # 只放行局域网ip,防止黑客工具 ---------------------------------------------------------------------------------------------- # 更改完端口后需要重启服务 root@SSH 22:45:35 ~ # systemctl reload sshd -
SSH服务登录防护需进行如下配置调整,先对如下参数进行了解
-
Port 6666 # 变更SSH服务远程连接端口 PermitRootLogin no # 禁止root用户直接远程登录 PasswordAuthentication no # 禁止使用密码直接远程登录 UseDNS no # 禁止ssh进行dns反向解析,影响ssh连接效率参数 GSSAPIAuthentication no # 禁止GSS认证,减少连接时产生的延迟 -
将如下具体配置添加至/etc/ssh/sshd_config文件中,参数需根据实际情况进行调整
-
###SSH### #Port 6666 #PasswordAuthentication no #PermitRootLogin no GSSAPIAuthentication no UseDNS no ###END###
6. Xshell脚本补充
-
编辑脚本:oldboy用户登录200主机,可以直接登录到172.16.1.7

-
[c:\~]$ Connecting to 10.0.0.200:22... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. Last login: Mon Jun 13 23:01:29 2022 from 10.0.0.1 oldboy@SSH 23:05:01 ~ $ su - root Password: Last login: Mon Jun 13 23:01:44 CST 2022 on pts/1 root@SSH 23:05:01 ~ # ssh 172.16.1.7 Last login: Mon Jun 13 23:02:05 2022 from 172.16.1.200 root@web01 23:05:01 ~ #
7. sshpass命令【未作免密钥】
7.1 使用范围和基础优化
-
应用范围:可以在命令行直接使用密码来进行远程连接和远程拉取文件。
-
使用前提:对于未连接过的主机。而又不输入yes进行确认,需要进行sshd服务的优化
-
# yum -y install sshpass # vim /etc/ssh/ssh_config StrictHostKeyChecking no # 禁止严格的主机密钥检查 # vim /etc/ssh/sshd_config GSSAPIAuthentication no # 禁止GSS认证,减少连接时产生的延迟 UseDNS no # 禁止ssh进行dns反向解析,影响ssh连接效率参数 # systemctl restart sshd
7.2 语法结构
-
# 语法结构: sshpass [-f|-d|-p|-e] [-hV] command parameters[命令参数] -f # 从文件中获取要使用的密码 -d # 使用数字作为获取密码的文件描述符 -p # 提供密码作为参数 -e # 密码作为env变量传递 # 不带参数- password将从stdin中获取
7.3 使用案例
1. 直接通过密码远程连接某台主机
-
1.远程连接登录 root@docker01 ~ # sshpass -p 1 ssh root@10.0.0.105 Last login: Wed Sep 14 11:44:16 2022 from 10.0.0.104 root@docker02 ~ # 2.远程执行命令 root@docker01 ~ # sshpass -p 1 ssh root@10.0.0.105 "free -h" total used free shared buff/cache available Mem: 3.8G 440M 1.4G 11M 2.0G 3.2G Swap: 2.0G 0B 2.0G
2. 远程连接指定ssh的端口(当远程主机不是默认的22端口时候)
-
sshpass -p 1 ssh -p 2222 root@10.0.0.105
3. 从密码文件读取文件内容作为密码去远程连接主机
-
sshpass -f secret.txt ssh root@10.0.0.105
4. 从远程主机上拉取文件到本地
-
sshpass -p 1 scp root@10.0.0.105:/root/test /tmp/
5. sshpass不可以直接接带特殊字符意义的密码,需要转义或者用小括号,或者指定文件的方式来连接都可以规避这个问题
7.4 编写一键分发密钥脚本
-
1.编写脚本 cat distribute_key.sh #!/bin/bash # 作者: song # 时间: 2022-04-30 # 描述: Distribute the public key # 定义的变量 dir="$HOME/.ssh" pass="1" # 01. 生成密钥对(Generating a key pair) if [ -d $dir ] then rm -f $dir/* fi echo "开始生成密钥对……" ssh-keygen -t rsa -b 4096 -f $dir/id_rsa -P '' &>/dev/null [ $? -eq 0 ] && echo "密钥对已生成" || echo "密钥对生成失败" # 02. 检查是否安装 sshpass rpm -qa|grep sshpass &>/dev/null if [ $? -ne 0 ] then echo "检测发现未安装 sshpass, 正在安装……" yum install sshpass -y &>/dev/null fi # 03. 分发公钥 echo "开始分发公钥……" for ip in `cat ip.txt` do sshpass -p$pass ssh-copy-id -i $dir/id_rsa.pub -o StrictHostKeyChecking=no root@$ip &>/dev/null [ $? -eq 0 ] && echo "$ip 已分发成功" || echo "$ip 分发失败" done # 需要分发密钥的主机ip可以写到一个文件内 cat ip.txt 10.0.0.105 10.0.0.151 10.0.0.152 10.0.0.153
企业面试题
下列服务,分别使用的哪个端口
ftp 21
dns 53
ssh 22
telnet 23
mysql 3306
http 80
https 443




浙公网安备 33010602011771号