# ssh

# 使用密码登录

编辑当前账号~/.ssh/authorized_keys文件,添加公钥。

# 允许 Root 用户名密码登录

vim /etc/ssh/sshd_config

PermitRootLogin yes
PasswordAuthentication yes
1
2
3
4

# 禁止使用密码登录

vim /etc/ssh/sshd_config

PasswordAuthentication on
1
2
3

# 发现SSH登录失败记录

grep "Failed password" /var/log/auth.log
1

# 配置密钥登陆

  1. 现在本机生成密钥对,输入以下命令:
[root@host ~]$ ssh-keygen  <== 建立密钥对
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): <== 按 Enter
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): <== 输入密钥锁码,或直接按 Enter 留空
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:
0f:d3:e7:1a:1c:bd:5c:03:f1:19:f1:22:df:9b:cc:08 root@host
1
2
3
4
5
6
7
8
9
10

密钥锁码在使用私钥时必须输入,这样就可以保护私钥不被盗用。当然,也可以留空,实现无密码登录。

现在,在 root 用户的家目录中生成了一个 .ssh 的隐藏目录,内含两个密钥文件。id_rsa 为私钥,id_rsa.pub 为公钥。

  1. 在服务器上安装公钥

方式一:把共钥放到authorized_keys文件中

cat id_rsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh
1
2
3

方式二:使用ssh-copy-id命令自动拷贝id_rsa.pub公钥到authorized_keys

ssh-copy-id root@master01
1
  1. 设置 SSH,打开密钥登录功能

编辑 /etc/ssh/sshd_config 文件,进行如下设置:

RSAAuthentication yes
PubkeyAuthentication yes
1
2

另外,请留意 root 用户能否通过 SSH 登录:

PermitRootLogin yes
1

当你完成全部设置,并以密钥方式登录成功后,再禁用密码登录:

PasswordAuthentication no
1

# 配置远程登陆清单

  1. .ssh文件夹中创建config文件,内容如下:
host *
ControlMaster auto
ControlPath ~/.ssh/master-%r@%h:%p
Host bihell
    HostName 堡垒机地址
    Port 端口
    User 用户名
    IdentityFile ~/.ssh/id_rsa
    ServerAliveInterval 80
1
2
3
4
5
6
7
8
9

2.直接ssh刚才配置文件中的Host名字即可 ssh bihell

更新时间: 9/17/2021, 6:28:43 PM