SSH公钥免密登录
·
Dayup
·
约 2 分钟
·
技术
/
Linux 目录
一、在Windows上生成公钥对
ssh-keygen -t rsa -b 4096
二、拷贝id_rsa.pub到Linux
scp C:\Users\username\.ssh\id_rsa.pub root@ip:/~/.ssh
三、Linux中添加公钥到authorized_keys文件
cd
cat ~/.ssh/id_rsa.pub >> authorized_keys
四、Linux中开启公钥认证登录功能
- 修改
sshd_config 配置文件,将配置项 PubkeyAuthentication no 改为 PubkeyAuthentication yes
vim /etc/ssh/sshd_config
五、测试连接
ssh root@ip
六、更懒一点
- 在windows下的用户文件夹.ssh下,新建无后缀文件
config ,并输入以下内容,注意自行替换
- IdentityFile可省略,默认是用户文件夹下
.ssh 下的 id_rsa
Host <myhostname>
HostName <ip_address>
Port 22
User root
IdentityFile <私钥地址>
- 连接只需要cmd中输入
ssh <myhostname>
七、ssh其他修改
vim /etc/ssh/sshd_config
#不允许root直接登录
找到 # PermitRootLogin yes
改为 PermitRootLogin no
#修改ssh端口
找到 # Port 22
改为 Port xx
#使环境变量立即生效
systemctl restart sshd.service
/etc/rc.d/init.d/sshd restart
或者
systemctl restart sshd.service
← 返回技术