RHCE的SSH配置文件及实验
一、ssh的配置文件
# 所有ssh_host开头的文件都是 ssh主机密钥 总共有3对,分时rsa ecdsa ed5519三种非对称加密算法产生的密钥对
[root@server ssh]# ls
moduli
ssh_host_ecdsa_key.pub
ssh_config # ssh客户端主配置文件
ssh_host_ed25519_key
ssh_config.d # ssh客户端的子配置文件目录(子配置的优先级高于主配置)
ssh_host_ed25519_key.pub
sshd_config # ssh服务端主配置文件
ssh_host_rsa_key
sshd_config.d # ssh服务端的子配置文件目录(子配置的优先级高于主配置)
ssh_host_rsa_key.pub
ssh_host_ecdsa_key
1.sshd_config
注意:大多数的服务在修改完配置文件之后都需要重新将服务启动。
# 在世界范围内有一个关于计算机的共识:
# 计算机的0-1024端口是固定的给某些特定的程序使用
# 例:明文web服务他的端口号(http):80 加密web服务端口(https):443
# 远程加密链接协议ssh:22 telnet:23
21 #Port 22 ssh的专用服务端口
22 #AddressFamily any
23 #ListenAddress 0.0.0.0 ssh监听所有ipv4
24 #ListenAddress :: ssh监听所有ipv6
25
# 下面这三个文件是 ssh主机私钥保存位置
26 #HostKey /etc/ssh/ssh_host_rsa_key
27 #HostKey /etc/ssh/ssh_host_ecdsa_key
28 #HostKey /etc/ssh/ssh_host_ed25519_key
39 #LoginGraceTime 2m 登录时输入密码市场不能超过2分钟
40 #PermitRootLogin prohibit-password #禁止root使用密码登录
49 AuthorizedKeysFile .ssh/authorized_keys # 客户端使用免密登录时,必须将自己的公钥保存在这个文件中,否则无法免密登录
65 #PasswordAuthentication yes 是否允许使用密码登录
66 #PermitEmptyPasswords no 是否允许空密码登陆
110 #ClientAliveInterval 100 # 发送激活包的时间间隔
111 #ClientAliveCountMax 3 # 发送几次激活包
二、实验
1.修改ssh服务器端的端口号 33
22 Port 33
2.拒绝root账户远程登录 (不仅仅要修改掉主配置文件,子配置文件的修改才是关键)
# 主配置文件的路径:/etc/sshd/sshd_config
# 子配置文件的路径:/etc/sshd/sshd_config.d/01-permitrootlogin.conf
permitRootLogin yes
3.允许特定账户进行ssh登录,其它账户无法登录
# 在sshd主配置中第一行写下如下内容
AllowUsers redhat #白名单
4.虚拟机之间免密登陆
# 客户端生成非对称加密的密钥对
[root@client ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
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:
SHA256:UJYMV5EgzGVuiFoDjSj/sDN7vkaP9gqgnr1/HmFU5fQ root@client
The key's randomart image is:
+---[RSA 3072]----+
| ..o oo+B=+= |
|o ....oO+ + . |
|.. + o.o . E |
| oo ..o |
|. .+ oS |
|..+ o . . |
|. .= o . |
|. +.= ... |
| o B*=+. |
+----[SHA256]-----+
#[root@client .ssh]# ls
# id_rsa 登录密钥的私钥 id_rsa.pub 登录密钥公钥 known_hosts 记录了服务器的主机公钥 known_hosts.old
# 将生成的登录密钥对中公钥发送给服务器
[root@client .ssh]# ssh-copy-id root@172.25.254.100
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.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.25.254.100's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@172.25.254.100'"
and check to make sure that only the key(s) you wanted were added.
# 此时就是免密登陆
[root@client .ssh]# ssh root@172.25.254.100
Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
Last failed login: Mon Oct 27 19:24:41 CST 2025 from 172.25.254.200 on ssh:notty
There were 8 failed login attempts since the last successful login.
Last login: Mon Oct 27 19:12:21 2025 from 172.25.254.200
三、Xshell 免密登陆
1.使用xshell生成密钥对 (在标题栏->工具->找到新建用户密钥引导)
2.一路下一步生成密钥,但是在公钥生成页将公钥复制
3.使用密码登录登录到远程服务器,在要登录的用户的家目录中.ssh/authorized_keys文件中写入刚刚的复制的公钥
4.重启一下服务端的sshd
5.客户端尝试远程免密登陆
