ssh密钥登录详细教程

今天朋友的服务器IP被爆露了,它的SSH端口一直在被爆力破解,因此,它想改成ssh密钥登录,因此,就分享出来给大家,详细的步骤如下:

 

一、修改SSH配置文件

修改ssh配置文件:/etc/ssh/sshd_config

[root@localhost ~]# vim /etc/ssh/sshd_config

 

二、设置SSH配置文件里面的主要参数

设置主要ssh配置参数,这些都是重点关注的地方,其它的完全可以用ssh配置文件默认的参数。

 

如果有不会的,重点的设置可以参考我的配置文件,如下:

# The default requires explicit activation of protocol 1
#Protocol 2

# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key

# Authentication:

#LoginGraceTime 2m
#PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

RSAAuthentication yes
PubkeyAuthentication yes

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile      .ssh/authorized_keys

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication no

ssh_host_rsa_key:指的是以RSA加密的私钥文件。

ssh_host_ecdsa_key:指的是以ECDSA加密的私钥文件。

PermitRootLogin:默认值是yes,允许root用户通过SSH登录,如果没有需要,这里可以不用管,具体可参考:禁止root用户远程登录,我这里就先不禁止了。

RSAAuthentication:默认屏蔽,RSA密钥认证,由于这里我们想设置为ssh密钥登录,这里必须设置为yes。

PubkeyAuthentication:默认屏蔽,公钥认证,由于这里我们想设置为ssh密钥登录,这里必须设置为yes。

AuthorizedKeysFile:放公钥文件的地方。

PasswordAuthentication:值是yes,允许SSH用密码登录。由于这里我们想设置为ssh密钥登录,因此,需要将PasswordAuthentication设置为no。

 

三、密钥生成(制作”私钥+公钥“密钥对)

如果你的服务器上面已经有了私钥和公钥,这个步骤可以跳过,没必要再重新生成一次了,除非你想更换密钥。

 

例如:

去 /etc/ssh/ 路径,发现有很多私钥和公钥存在的。

密钥生成时会产生所需要的两把 Keys ,分别是私钥 (id_rsa) 与公钥 (id_rsa.pub)。

私钥 (id_rsa) 与公钥 (id_rsa.pub)

 

以下是生成私钥的命令,也就是在服务器上制作密钥对。

为了让大家学习,我就多写了几个命令,让大家看下各有什么与众不同?我这主要用的是:ecdsa加密的和rsa加密的两条命令。

[root@localhost ~]# ssh-keygen -t dsa /etc/ssh/ssh_host_dsa_key
[root@localhost ~]# ssh-keygen -t ecdsa /etc/ssh/ssh_host_ecdsa_key
[root@localhost ~]# ssh-keygen -t ed25519 /etc/ssh/ssh_host_ed25519_key
[root@localhost ~]# ssh-keygen -t rsa /etc/ssh/ssh_host_rsa_key
[root@localhost ~]# ssh-keygen -t rsa1 /etc/ssh/ssh_host_rsa1_key

 

这里如果有不懂的,可以看一下制作密钥对的原理:

在服务器上制作密钥对。

首先用密码登录到你打算使用密钥登录的账户,然后执行以下命令:

[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

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

现在,在 root 用户的家目录中生成了一个 .ssh 的隐藏目录,内含两个密钥文件:

id_rsa 为私钥

id_rsa.pub 为公钥

 

四、复制公钥到 authorized_keys

就是上面配置密钥公钥文件的参数:一般是指 /etc/ssh/authorized_keys

AuthorizedKeysFile .ssh/authorized_keys

 

1、键入以下命令,在服务器上安装公钥:

[root@host ~]$ cd /etc/ssh/
[root@host ssh]$ cat ssh_host_ecdsa_key.pub >> authorized_keys

 

2、为了确保连接成功,请保证以下文件权限正确:

[root@host .ssh]$ chmod 600 authorized_keys
[root@host .ssh]$ chmod 700 ~/.ssh

 

五、重启SSH服务

重启SSH服务,让ssh配置文件生效。

[root@localhost ]# systemctl restart sshd

[root@localhost ]# /etc/rc.d/init.d/sshd restart

 

六、 将私钥下载到客户端,然后SSH密钥登陆

使用 WinSCP、SFTP 等工具将私钥文件 ssh_host_ecdsa_key 下载到客户端机器上。

sz命令也可以将私钥文件下载到客户端机器上。

 

1、这里以PuTTY为例子,然后打开 PuTTYGen,单击 Actions 中的 Load 按钮,载入你刚才下载到的私钥文件。如果你刚才设置了密钥锁码,这时则需要输入。

2、载入成功后,PuTTYGen 会显示密钥相关的信息。在 Key comment 中键入对密钥的说明信息,然后单击 Save private key 按钮即可将私钥文件存放为 PuTTY 能使用的格式。

3、从今以后,当你使用 PuTTY 登录时,可以在左侧的 Connection -> SSH -> Auth 中的 Private key file for authentication: 处选择你的私钥文件,然后即可登录了,过程中只需输入密钥锁码即可。

 

当然了,你也可以用命令的方式去SSH密钥登陆:

ssh -i ssh_host_dsa_key root@ip
ssh -i ssh_host_ecdsa_key root@ip
ssh -i ssh_host_ed25519_key root@ip
ssh -i ssh_host_rsa_key root@ip
ssh -i ssh_host_rsa1_key root@ip

 

如果你没有禁用密码登陆,还可以用账号密码的方式登陆:

ssh root@xxx.xxx.xxx.xxx
    A+
发布日期:2021年03月27日 19:30:58  所属分类:Linux
最后更新时间:2022-11-05 06:48:04
标签:
付杰
  • ¥ 298.0元
  • 市场价:498.0元
  • ¥ 1999.9元
  • 市场价:8999元
  • ¥ 29.0元
  • 市场价:99.0元
  • ¥ 159.0元
  • 市场价:599.0元

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论:2   其中:访客  0   博主  0

  1. 头像 jax 0

    1、键入以下命令,在服务器上安装公钥:

    [root@host ~]$ cd /etc/ssh/
    [root@host ssh]$ cat ssh_host_ecdsa_key >> authorized_keys

    这里少了.pub
    cat ssh_host_ecdsa_key.pub >> authorized_keys

    • 付杰 付杰

      @jax 感谢纠正!