东莞商城网站建设,网站的ico图标做多大,佛山建设,wordpress分类目录页面本文适用#xff1a;rhel5-9系列#xff0c;同类系统(CentOS,AlmaLinux,RockyLinux等)、debian系(ubuntu)等也可参照 文档形成时期#xff1a;2012-2024年 因系统版本不同#xff0c;配置应略有差异#xff0c;本文没有在细节上区分#xff0c;但实践中发现均可配置成功 …本文适用rhel5-9系列同类系统(CentOS,AlmaLinux,RockyLinux等)、debian系(ubuntu)等也可参照 文档形成时期2012-2024年 因系统版本不同配置应略有差异本文没有在细节上区分但实践中发现均可配置成功 文章目录 Linux客户端密钥认证配置一键配置方式交互式生成方式查看生成的密钥对 Linux服务端密钥认证配置客户端登陆服务端无提示登陆示例 SSH双向密钥登陆 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 rootip 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 rootlocalhost交互式生成方式
下面是交互式生成过程 在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 ~/.sshid_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 reloadvi ~/.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 #拷贝内容过去也可以公钥没有换行只能是一行哟注意 或这样下发到客户端 [rootlocalhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub -p 22 root1.2.3.4
客户端登陆服务端
客户端首次密钥方式登陆服务端需要加入服务端的公钥到客户端的~/.ssh/known_hosts这个过程一般是自动提示你只需输入yes
然后该客户端登陆该服务端就可以不需要密码验证了当然密码认证和密钥认证可以同时开启然后由客户端的连接方式决定。
无提示登陆示例
ssh -q -o StrictHostKeyCheckingno -o UserKnownHostsFile/dev/null -o ConnectTimeout7 -o HostbasedAuthenticationyes -o IdentityFile~/.ssh/id_rsa -p 22 rootremoteip { ifconfig; }SSH双向密钥登陆
如果把服务端和客户端角色换一下再按以上操作步骤实施一遍即可实现双向密钥认证登陆。