Linux的SSH密钥认证快捷配置

本文适用:rhel5-9系列,同类系统(CentOS,AlmaLinux,RockyLinux等)、debian系(ubuntu)等也可参照
文档形成时期:2012-2024年
因系统版本不同,配置应略有差异,本文没有在细节上区分,但实践中发现均可配置成功

Linux客户端密钥认证配置

一键配置方式

配置和生成如下,可以一键拷贝到终端运行

# 修改加密码强度:
sed -i '/ServerKeyBits/c\ServerKeyBits 2048' /etc/ssh/sshd_config
service sshd reload # 或 systemctl reload sshd
# 配置修改:
# 修改/etc/ssh/ssh_config或拷贝一份儿到当前用户.ssh下
cp /etc/ssh/ssh_config ~/.ssh/config
sed -i '/HostbasedAuthentication/c\HostbasedAuthentication yes' ~/.ssh/config
sed -i '/ConnectTimeout/c\ConnectTimeout 7' ~/.ssh/config
sed -i '/IdentityFile \~\/\.ssh\/id_rsa/c\IdentityFile \~\/\.ssh\/id_rsa' ~/.ssh/config
# sed -i '/StrictHostKeyChecking/c\StrictHostKeyChecking no' ~/.ssh/config # 在某些全自动化场景下,不希望检测服务端公钥,便可配置该项
if ( ! grep -q "UserKnownHostsFile /dev/null" ~/.ssh/config ); then echo "UserKnownHostsFile /dev/null" >> ~/.ssh/config; fi 
# 参考:https://blog.csdn.net/yasaken/article/details/7348441
# 配置完成

# 生成密钥和公钥
# -b 指定加密位数,默认也是2048;-N是指定空密码;-f是指定密钥生成的文件,不能使用~这个的主目录符号,不识别;
# -C 只是指定评论,在公钥的末尾能看到,改动评论不影响密钥使用;
if [ ! -f ~/.ssh/id_rsa ]; then ssh-keygen -b 2048 -t rsa -f "/root/.ssh/id_rsa" -N "" -C "root@`ip addr show dev eth0 | grep "inet" | awk -F ' ' '{print $2}' | cut -d'/' -f 1 | head -1 2> /dev/null`"; else echo "This machine already has this rsa, do nothing"; fi

# 不判断,直接生成:
# ssh-keygen -b 2048 -t rsa -f "/root/.ssh/id_rsa" -N "" -C "root@localhost"

交互式生成方式

下面是交互式生成过程:
在SSH客户端生成公钥和私钥(不是在服务端)(生成的是用户密钥,有别于主机密钥)

ssh-keygen -t rsa

一路回车就行
Enter file in which to save the key (/root/.ssh/id_rsa):
回车,把key放到哪个位置及指定文件名,默认就行
Enter passphrase (empty for no passphrase):
给key加密,可设可不设,就是打开密钥和使用密钥的时候需要密码,如果设,必须4位以上,建议不要设
Enter same passphrase again:
在输入刚才的密码

查看生成的密钥对

ls ~/.ssh

id_rsa 密钥
id_rsa.pub 公钥

Linux服务端密钥认证配置

编辑 /etc/ssh/sshd_config 文件,打开密钥登录功能

sed -i '/#RSAAuthentication/c\RSAAuthentication yes' /etc/ssh/sshd_config
sed -i '/#PubkeyAuthentication/c\PubkeyAuthentication yes' /etc/ssh/sshd_config
sed -i '/#AuthorizedKeysFile/c\AuthorizedKeysFile      .ssh/authorized_keys' /etc/ssh/sshd_config
service sshd reload
# su - user # 如果是为普通用户生成,需要切换
mkdir ~/.ssh
touch ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
service sshd reload

vi ~/.ssh/authorized_keys
#追加ssh客户端的公钥,注意是一个一行

当你完成全部设置,并以密钥方式登录成功后,再禁用密码登录(可选):
PermitRootLogin yes 允许ROOT

PasswordAuthentication yes
改为
PasswordAuthentication no
检查配置:
sshd -t
最后,重载 SSH 服务:
service sshd reload #rh6或更低版本
systemctl reload sshd #rh7+
直接执行,网络不会断掉的。

vi ~/.ssh/authorized_keys
#把客户端的公钥放到服务端的 ~/.ssh/authorized_keys (不是客户端的哟)
cat ~/.ssh/id_rsa.pub
#拷贝内容过去也可以(公钥没有换行,只能是一行哟,注意)
或这样下发到客户端:
[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub -p 22 root@1.2.3.4

客户端登陆服务端

客户端首次密钥方式登陆服务端,需要加入服务端的公钥到客户端的~/.ssh/known_hosts,这个过程一般是自动提示,你只需输入yes

然后该客户端登陆该服务端就可以不需要密码验证了,当然密码认证和密钥认证可以同时开启,然后由客户端的连接方式决定。

无提示登陆示例

ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ConnectTimeout=7 -o HostbasedAuthentication=yes -o IdentityFile=~/.ssh/id_rsa -p 22 root@remoteip "{ ifconfig; }"

SSH双向密钥登陆

如果把服务端和客户端角色换一下,再按以上操作步骤实施一遍,即可实现双向密钥认证登陆。

  • 16
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沉思的归零者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值