用密钥直接登录服务器

由于经常登录服务器每次都要输入密码感觉很麻烦,而且密码的方式安全性也不高;所以决定使用sshkey的方式登录。

在centos7上使用密钥登录

产生密钥

1
2
3
4
5
6
7
cd ~
mkdir .ssh
chmod 700 .ssh
touch authorized_keys
chmod 700 authorized_keys
ssh-keygen -t rsa
cat id_rsa.pub >> authorized_keys //保存私钥

配置sshd

打开/etc/ssh/sshd_config 文件,确认以下配置打开

1
2
3
4
5
6
7
8
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
HostKey /etc/ssh/ssh_host_rsa_key
# 也可以顺便禁用密码登录
ChallengeResponseAuthentication no
PasswordAuthentication no
UsePAM no

重启ssh服务

1
2
service sshd restart
source ~/.bash_profile

本地设置

在本地电脑下载服务器上生成的id_rsa文件,保存~/.ssh路径;此时只要sshroot@server_ip就可以免密码登录了,非常方便。

当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器