learning_record_doc/ubuntu/设置SSH密钥登录.md
2022-02-28 23:07:00 +08:00

53 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

### 一、为root账户设置ssh密钥登录
**1、进入 root 用户,进入 / root**
```shell
su root
cd /root
```
**2、新建文件夹**
`mkdir /root/.ssh`
**3、生成密钥密钥会自动保存在 / root/.ssh 目录里。**
`ssh-keygen -t rsa`
再连摁三下回车
**4、进入. ssh 文件夹里面有两个文件id_rsa 是私钥需要自己下载到本地id_rsa.pub 是公钥,该名成 authorizedkeys**
```shell
cd /root/.ssh
mv id_rsa.pub authorized_kyes
```
**5、设置文件和文件夹权限**
```shell
chmod 600 /root/.ssh/authorized_keys
chmod 700 /root/.ssh
```
**6、修改 / etc/ssh/sshd_config 文件,并重启服务**
`vim /etc/ssh/sshd_config`
修改下面两项
```
PasswordAuthentication: no // 禁止使用基于口令认证的方式登陆
PubkeyAuthentication: yes // 允许使用基于密钥认证的方式登陆
```
重启ssh服务使配置生效
`service sshd restart`
之后登录时选择密钥Publickey登录导入生成的私钥id_rsa。
### 二、为普通账户设置ssh密钥登录
在要设置的账户目录下进行上面的2-6操作 例cd /home/user