设置服务器使用ssh秘钥登录

·

1.生成密钥对

使用命令生成密钥:

ssh-keygen

输入命令之后按提示进行操作,提示示例如下:

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #这里输入生成私钥文件位置

Created directory '/root/.ssh'.

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:

......

The key's randomart image is:

......

2.服务器上配制公钥

将公钥文件上传至服务器 ~/.ssh/目录下(谁用就上传到谁的家目录下),并改名为authorized_keys

cd .ssh mcd .ssh

mv  id_rsa.pub authorized_keys 或者 cat  id_rsa.pub  >> authorized_keys

chmod 600 authorized_keys #修改公钥为只有属主有读写权限(安全考虑)

chmod 700 ~/.ssh #修改.SSH目录为只有属主有读、写、执行权限(安全考虑)

3.修改服务器上SSH配制

修改ssh配置文件

vim /etc/ssh/sshd_config

添加如下内容(添加或者取消注释均可):

RSAAuthentication yes#使用RSA加密模式公钥
PubkeyAuthentication yes #开启公钥验证
AuthorizedKeysFile .ssh/authorized_keys #指定公钥文件

关闭ssh密码登录(如果想同时使用秘钥和密码登录,可忽略)

用密码登录远程服务器终归有风险,有可能被黑客给爆掉,所以最好禁止使用SSH+密码登录远程服务器。

修改如下内容:

PasswordAuthentication yes –> PasswordAuthentication no #关闭密码登录

重启ssh

# debain 系列使用如下了命令 
/etc/init.d/ssh restart 
# CentOS系列使用如下命令 
service sshd restart

操作完成!简单明了

Comments

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注