我们昨天已经实现了root账户通过密码使用ssh登录Arch Linux系统,但是通过密码登录是不安全的,容易被暴力破解,今天我们的目标是配置ssh公钥和私钥,并限制只能使用私钥登录Arch Linux。
实现今天的目标主要有三个步骤,接下来将按照下边的三个步骤进行操作:
- 在Win10的机器上生成公钥和私钥
- 将生成的公钥上传到Arch Linux
- 配置sshd禁止密码登录
生成公钥和私钥
我的Win10电脑上已经安装过git
的客户端,客户端中包含了OpenSSH相关的命令行工具,所以直接使用git-bash
生成ssh key
,具体操作如下:
# 生成时一路enter,中间有输入保存的目录、文件名称、密码,都是使用的默认配置(默认目录、空密码)
$ ssh-keygen
Generating public/private ed25519 key pair.
Enter file in which to save the key (/c/Users/danny/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/danny/.ssh/id_ed25519
Your public key has been saved in /c/Users/danny/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:kRtOw8i3Pc6aWVyP6ZDmpvAmMS7cjjeqp3/9JP7FyWc danny@DESKTOP-SSJG5N5
The key's randomart image is:
+--[ED25519 256]--+
| |
| . o . |
| o O |
| + B |
| S o . |
| o + * = |
| . o.+. X B E |
| +.B+oX.+ o |
| o=+*.+B=+ . |
+----[SHA256]-----+
上传公钥到服务器
上一步生成了两个文件: id_ed25519
和id_ed25519.pub
,其中后缀为.pub
的文件就是公钥,接下来我们将这个文件上传到Arch Linux系统。
第一种方式,使用OpenSSH自带的命令行工具ssh-copy-id
:
# 省略了上传的文件和服务器端口参数,如果上传文件和端口不是默认的,可以使用完成的命令:
# ssh-copy-id -i /c/Users/danny/.ssh/id_ed25519.pub -p 22 root@172.19.143.111
$ ssh-copy-id root@172.19.143.111
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/c/Users/danny/.ssh/id_ed25519.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.19.143.111's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@172.19.143.111'"
and check to make sure that only the key(s) you wanted were added.
第二种方式,使用scp
命令行将公钥文件上传到服务器root用户home目录,然后ssh
命令登录Arch Linux,将上传的文件拼接到.ssh/authorized_keys,如果目录或者文件不存在,则需要手动创建。
# 上传公钥到服务器,注意最后的英文冒号,一定要有
$ scp /c/Users/danny/.ssh/id_ed25519.pub root@172.19.143.111:
id_ed25519.pub 100% 103 52.0KB/s 00:00
# 将文件追加到authorized_keys,
# 因为我这里有,所以不需要创建目录和文件
# 如果没有需要创建`.ssh`目录、authorized_keys文件
# 最后删除上传的公钥文件
$ ssh -P 22 root@172.19.143.111
Last login: Fri May 10 23:25:23 2024 from 172.19.128.1
[root@archlinux ~]# ls -al
total 32
drwxr-x--- 5 root root 4096 May 10 23:32 .
drwxr-xr-x 17 root root 4096 May 1 22:22 ..
-rw------- 1 root root 3742 May 10 23:32 .bash_history
drwx------ 3 root root 4096 May 9 22:13 .cache
drwx------ 3 root root 4096 May 1 22:22 .gnupg
-rw-r--r-- 1 root root 103 May 10 23:32 id_ed25519.pub
drwx------ 2 root root 4096 May 10 23:08 .ssh
-rw------- 1 root root 3479 May 10 21:50 .viminfo
[root@archlinux ~]# cat id_ed25519.pub >> .ssh/authorized_keys
[root@archlinux ~]# rm id_ed25519.pub
到这里我们已经上传公钥文件成功了。
修改配置文件
上边已经上传公钥成功了,接下来要修改配置文件,限制密码登录。在这之前的我们先测试一下:
# 登陆时未输入密码,登录成功
$ ssh -P 22 root@172.19.143.111
Last login: Fri May 10 23:35:01 2024 from 172.19.128.1
[root@archlinux ~]#
以上说明,我们已经可以使用公钥验证登录了,可以放心大胆的禁止密码登录方式了。
# vim打开配置文件
vim /etc/ssh/sshd_config
# 将PermitRootLogin 修改为 prohibit-password
# 将PasswordAuthentication 修改为 no
修改配置文件后,验证配置是否正确,之后在进行重启sshd
服务
# 验证配置文件是否正确,未输出信息表明配置正确
[root@archlinux ~]# sshd -t
# 重启sshd服务
[root@archlinux ~]# systemctl restart sshd.service
最后,在Win10的git
客户端使用ssh验证:
$ ssh -P 22 root@172.19.143.111
Last login: Fri May 10 23:43:58 2024 from 172.19.128.1
[root@archlinux ~]#
登录成功。
总结,使用root用户并用密码方式登录服务器,对于服务起来说容易遭到暴力破解密码攻击,所以尽量使用ssh key的方式。在这里主要用到ssh-keygen
、ssh-copy-id
或者scp
、ssh
、sshd
,以及sshd_config配置文件中的PermitRootLogin
、PasswordAuthentication
参数。
网友评论