Linux配置SSH免密登录“ssh-keygen”的基本⽤法
⽬录
1 什么是SSH
2 配置SSH免密登录
2.1 安装必需的软件
2.2 ssh-keygen创建公钥-私钥对
2.3 ssh-copy-id把A的公钥发送给B
2.4 在A服务器上免密登录B服务器
3 扩展说明
3.2 ⽂件权限
3.3 ⽂件的编辑和查看
1 什么是SSH
引⽤百度百科的说明:
SSH 为 Secure Shell的缩写,由 IETF 的⽹络⼩组(Network Working Group)所制定;它是建⽴在应⽤层基础上的安全协议。
SSH 是⽬前较可靠,专为远程登录会话和其他⽹络服务提供安全性的协议。利⽤ SSH 协议可以有效防⽌远程管理过程中的信息泄露问题。
SSH最初是UNIX系统上的⼀个程序,后来⼜迅速扩展到其他操作平台。
为了在不同平台/⽹络主机之间的通信安全, 很多时候我们都要通过ssh进⾏认证. ssh认证⽅式主要有2种:
①基于⼝令的安全认证: 每次登录的时候都要输⼊⽤户名和密码, 由于要在⽹络上传输密码, 可能存在中间⼈攻击的风险;
②基于密钥的安全认证: 配置完成后就可以实现免密登录, 这种⽅式更加安全 —— 不需要在⽹络上传递⼝令, 只需要传输⼀次公钥. 常见的git的ssh⽅式就是通过公钥进⾏认证的.
2 配置SSH免密登录
说明: 这⾥演⽰所⽤的服务器操作系统是Cent OS 7. 我们的⽬标是:
A服务器(172.16.22.131) 能免密登录 B服务器 (172.16.22.132).
注意: ssh连接是单向的, A能免密登录B, 并不能同时实现B能免密登录A.
2.1 安装必需的软件
在操作之前, 先确保所需要的软件已经正常安装.
这⾥我们需要安装ssh-keygen和ssh-copy-id, 安装⽅式如下:
# 安装ssh-keygen, 需要确保服务器可以联⽹. 博主这⾥已经安装完成, 所以没有做任何事.
[root@localhost ~]# yum install -y ssh-keygen
Loaded plugins: fastestmirror, langpacks
base              | 3.6 kB 00:00:00
epel              | 3.6 kB 00:00:00
extras              | 2.9 kB 00:00:00
updates              | 2.9 kB 00:00:00
Loading mirror speeds from cached hostfile
No package ssh-keygen available.
Error: Nothing to do
# 安装ssh-copy-id
[root@localhost ~]# yum install -y ssh-copy-id
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
No package ssh-copy-id available.
Error: Nothing to do
2.2 ssh-keygen创建公钥-私钥对
(1) 在指定⽬录下⽣成rsa密钥, 并指定注释为“shoufeng”, 实现⽰例:
[root@localhost ~]# ssh-keygen -t rsa -f ~/.ssh/id_rsa -C "shoufeng"
#        ~密钥类型 ~密钥⽂件路径及名称 ~ 备注信息
Generating public/private rsa key pair.
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:
9a:e3:94:b9:69:c8:e9:68:4b:dc:fa:43:25:7f:53:f1 shoufeng
The key's randomart image is:
+--[ RSA 2048]----+
|    |
|  .  |
|  o  |
| . . . E |
|  + S.  |
| . .. .=o  |
| oo.oB. .  |
| ..o=o.+  |
| .++oo+  |
+-----------------+
注意: 密钥的⽂件名称必须是id_xxx, 这⾥的xxx就是-t参数指定的密钥类型. ⽐如密钥类型是rsa, 那么密钥⽂件名就必须是id_rsa.
(2) ssh-keygen常⽤参数说明:
-t: 密钥类型, 可以选择 dsa | ecdsa | ed25519 | rsa;
-f: 密钥⽬录位置, 默认为当前⽤户home路径下的.ssh隐藏⽬录, 也就是~/.ssh/, 同时默认密钥⽂件名以id_rsa开头. 如果是root⽤户, 则在/root/.ssh/id_rsa, 若为其他⽤户, 则在/home/username/.ssh/id_rsa; -C: 指定此密钥的备注信息, 需要配置多个免密登录时, 建议携带;
-N: 指定此密钥对的密码, 如果指定此参数, 则命令执⾏过程中就不会出现交互确认密码的信息了.
举例说明: 同时指定⽬录位置、密码、注释信息, 就不需要输⼊回车键即可完成创建:
ssh-keygen -t rsa -f ~/.ssh/id_rsa -N shoufeng -C shoufeng
(3) 前往~/.ssh/⽬录下查看⽣成的⽂件:
# ⽣成的⽂件以test_rsa开头, test_rsa是私钥, test_rsa.pub是公钥:
[root@localhost .ssh]# ls
test_rsa test_rsa.pub
# 通过cat命令查看公钥⽂件:
[root@localhost .ssh]# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC2JpLMqgeg9jB9ZztOCw0WMS8hdVpFxthqG1vOQTOji/cp0+8RUZl3P6NtzqfHbs0iTcY0ypIJGgx4eXyipfLvilV2bSxRINCVV73VnydVYl5gLHsrgOx+372Wovlanq7Mxq06qAONjuRD0c64xqdJFKb1Ov
# 可以看到最后有⼀个注释内容shoufeng
2.3 ssh-copy-id把A的公钥发送给B
默认⽤法是: ssh-copy-id root@172.16.22.132, ssh-copy-id命令连接远程服务器时的默认端⼝是22, 当然可以指定⽂件、远程主机的IP、⽤户和端⼝:
# 指定要拷贝的本地⽂件、远程主机的IP+⽤户名+端⼝号:
linux系统登录
[root@localhost .ssh]# ssh-copy-id -i ~/.ssh/id_rsa.pub -p 22 root@172.16.22.132
/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@172.16.22.132's password: # 输⼊密码后, 将拷贝公钥
Number of key(s) added: 1
Now try logging into the machine, with: "ssh -p '22' 'root@172.16.22.132'"
and check to make sure that only the key(s) you wanted were added.
2.4 在A服务器上免密登录B服务器
[root@localhost .ssh]# ssh root@172.16.22.132
Last login: Fri Jun 14 08:46:04 2019 from 192.168.34.16 # 登录成功
3 扩展说明
3.1 其他⽅式发送公钥⽂件
上述2.3步骤是通过ssh-copy-id⼯具发送公钥⽂件的, 当然我们也可以通过其他⽅式实现:
(1) 将A的公钥⽂件发给B:
通过scp命令将A服务器的公钥⽂件发送到B服务器的⽤户⽬录下, 因为还没有配置成功免密登录, 所以期间需要输⼊B服务器对应⽤户的密码:
[root@localhost .ssh]# scp id_rsa.pub root@172.16.22.132:/root/.ssh
root@172.16.22.132's password:
id_rsa.pub          100% 390  0.4KB/s 00:00
(2) 在B上创建authorized_keys⽂件:
[root@localhost .ssh]# cd /root/.ssh/
[root@localhost .ssh]# ls
id_rsa.pub
# 通过A服务器的公钥⽣成"authorized_keys"⽂件:
[root@localhost .ssh]# cat id_rsa.pub >> authorized_keys
[root@localhost .ssh]# cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC2JpLMqgeg9jB9ZztOCw0WMS8hdVpFxthqG1vOQTOji/cp0+8RUZl3P6NtzqfHbs0iTcY0ypIJGgx4eXyipfLvilV2bSxRINCVV73VnydVYl5gLHsrgOx+372Wovlanq7Mxq06qAONjuRD0c64xqdJFKb1OvS/nyKaOr9注意: 上述重定向时使⽤>>进⾏追加, 不要⽤>, 那会清空原有内容.
3.2 ⽂件权限
为了让私钥⽂件和公钥⽂件能够在认证中起作⽤, 需要确保权限的正确性:
①对于.ssh⽬录以及其内部的公钥、私钥⽂件, 当前⽤户⾄少要有执⾏权限, 其他⽤户最多只能有执⾏权限.
②不要图省事设置成777权限: 太⼤的权限不安全, ⽽且数字签名也不⽀持这种权限策略.
③对普通⽤户, 建议设置成600权限: chmod 600 authorized_keys id_rsa id_rsa.pub;
④对root⽤户, 建议设置成644权限: chmod 644 authorized_keys id_rsa id_rsa.pub.
3.3 ⽂件的编辑和查看
在Liunx环境下, 如果要查看、复制私钥、公钥, 以及authorized_keys等⽂件, 不要使⽤vim等编辑器打开, 因为它会产⽣不必要的回车;
应该通过cat、more、less等查看命令把内容打印到终端上, 再作查看、复制等操作.
总结
以上所述是⼩编给⼤家介绍的Linux 配置SSH免密登录 “ssh-keygen”的基本⽤法 ,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对⽹站的⽀持!
如果你觉得本⽂对你有帮助,欢迎转载,烦请注明出处,谢谢!