公钥生成
ssh-keygen
命令专门是用来生成密钥的。
常用的选项:
- -t 用来指定密钥类型(dsa | ecdsa | ed25519 | rsa | rsa1);
- -P 用来指定密码
- -f 用来指定生成的密钥文件名
- -C 用来添加注释
一般不用设置密码
将公钥部署到服务器
ssh-copy-id
将你的公共密钥填充到一个远程机器上的authorized_keys文件中
ssh-copy-id [-i [identity_file]] [user@]machine
identity_file就是公钥文件,默认是~/.ssh/id_rsa.pub
当然也可以登录服务器直接修改文件
#在本机上执行此命令,上传公钥
scp -P your_port id_rsa.pub user@hostname:/tmp
#在服务器上执行此命令,追加到authorized_keys
cd /tmp && cat id_rsa.pub >> ~/.ssh/authorized_keys
#更改权限
chmod 600 ~/.ssh/authorized_keys
修改配置
- 修改
/etc/ssh/sshd_config
文件
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys // 默认的
- 重启sshd
service sshd restart
或者systemctl restart sshd.service
如果想关闭密码登录:
PasswordAuthentication No