Appearance
29. 端口与服务映射
本章节指导在 RHEL 8.10(主机名 ZSLinux)上使用 SecureCRT 配置和检查端口与服务映射,配合 SecureFX 传输日志文件。内容涵盖端口检查(ss, netstat, nmap)、服务与端口映射、防火墙配置,明确默认端口使用服务名方式(--add-service)和自定义端口使用端口开放方式(--add-port),以验证服务端口为例,全面实用,适合初学者快速掌握和运维人员日常维护。所有操作在 ZSLinux 环境中测试,确保实验一致性。
29.1 前提条件
- RHEL 8.10 已安装(参考第 1 章),主机名设置为
ZSLinux. - 使用 SecureCRT 登录(SSH2 协议,端口 2222,参考第 6 章).
- 使用 SecureFX 传输文件(SFTP 协议).
- 具有 root 或 sudo 权限.
- 系统已订阅并启用 RHEL 和 EPEL 仓库(参考第 4 和 7 章).
- 防火墙和 SELinux 启用(参考第 13 章).
- 网络连接正常,时间同步配置完成(参考第 3 章).
29.2 端口与服务管理
29.2.1 检查端口映射
- 安装检查工具:bash
dnf install -y nmap net-tools # 安装 nmap 和 netstat 工具 - 查看标准端口映射:bash
cat /etc/services | grep ssh # 显示 SSH 服务的标准端口(22/tcp) cat /etc/services | grep http # 显示 HTTP 服务的标准端口(80/tcp) - 检查开放端口:bash
ss -tuln # 显示 TCP/UDP 监听端口 netstat -tuln # 备选方法:显示监听端口 nmap localhost # 扫描本地主机开放端口
29.2.2 常见服务端口
以下为常见服务的标准端口及其防火墙配置方式:
| 服务 | 端口 | 防火墙命令 |
|---|---|---|
| SSH | 22/tcp | firewall-cmd --permanent --add-service=ssh |
| HTTP | 80/tcp | firewall-cmd --permanent --add-service=http |
| HTTPS | 443/tcp | firewall-cmd --permanent --add-service=https |
| FTP | 20/tcp, 21/tcp | firewall-cmd --permanent --add-service=ftp |
| DNS | 53/tcp, 53/udp | firewall-cmd --permanent --add-service=dns |
| SMTP | 25/tcp | firewall-cmd --permanent --add-service=smtp |
| POP3 | 110/tcp | firewall-cmd --permanent --add-service=pop3 |
| IMAP | 143/tcp | firewall-cmd --permanent --add-service=imap |
| NFS | 2049/tcp, 2049/udp | firewall-cmd --permanent --add-service=nfs |
| Samba | 445/tcp, 139/tcp | firewall-cmd --permanent --add-service=samba |
| MySQL/MariaDB | 3306/tcp | firewall-cmd --permanent --add-service=mysql |
| PostgreSQL | 5432/tcp | firewall-cmd --permanent --add-service=postgresql |
| RDP | 3389/tcp | firewall-cmd --permanent --add-service=rdp |
默认端口配置(服务名方式):
- 当服务使用标准端口时,使用服务名方式,只需一条命令加上
reload:bashfirewall-cmd --permanent --add-service=ssh # 允许 SSH(22/tcp) firewall-cmd --reload # 应用防火墙规则 - 说明:
firewalld服务定义(如/usr/lib/firewalld/services/ssh.xml)已包含标准端口,--add-service自动启用相应端口,无需手动指定端口。
- 当服务使用标准端口时,使用服务名方式,只需一条命令加上
自定义端口配置(按端口开放而不是服务名):
- 当服务使用非标准端口(如 SSH 使用 2222/tcp),需使用端口开放方式:bash
firewall-cmd --permanent --add-port=2222/tcp # 允许自定义 SSH 端口 firewall-cmd --reload # 应用防火墙规则 - 若 SELinux 启用(如
ZSLinux环境),需额外配置 SELinux 上下文:bashsemanage port -a -t ssh_port_t -p tcp 2222 # 为自定义 SSH 端口设置 SELinux 上下文 - 说明:
- 使用
--add-port而不是--add-service,因为服务名方式(如--add-service=ssh)仅支持默认端口(22/tcp)。 - SELinux 配置(
semanage)是额外的必要步骤,视为第二条命令。 - 替代方法:可修改服务定义后继续使用服务名方式:bash
cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/ssh-custom.xml # 复制服务定义 vim /etc/firewalld/services/ssh-custom.xml # 修改端口为 2222 # 修改内容示例: <service> <short>SSH-Custom</short> <description>Custom SSH on port 2222</description> <port protocol="tcp" port="2222"/> </service> firewall-cmd --permanent --add-service=ssh-custom # 允许自定义 SSH 服务 firewall-cmd --reload # 应用规则 - 这种方法仍为一条
firewall-cmd命令,但需额外配置文件修改。
- 使用
- 当服务使用非标准端口(如 SSH 使用 2222/tcp),需使用端口开放方式:
29.3 验证与故障排查
- 验证端口状态:bash
ss -tuln | grep 2222 # 检查自定义 SSH 端口(2222/tcp) firewall-cmd --list-services # 查看允许的服务 firewall-cmd --list-ports # 查看允许的自定义端口 nmap -p 80,443,2222 localhost # 扫描特定端口 - 验证服务运行:bash
systemctl status sshd # 检查 SSH 服务状态 systemctl status httpd # 检查 HTTP 服务状态 - 验证 SELinux 上下文:bash
semanage port -l | grep ssh # 验证 SSH 端口上下文 - 测试 SSH 连接:bash
ssh -p 2222 testuser@ZSLinux # 测试 SSH 连接(参考第 6 章) - 故障排查:
- 端口未开放:检查服务状态(
systemctl status <service>)或防火墙(firewall-cmd --list-ports)。 - 连接失败:验证 SELinux 上下文(
semanage port -l)或查看日志(journalctl -u <service>)。 - nmap 无结果:确认
nmap安装(dnf install -y nmap)或网络连通性(ping localhost)。 - 生成 SOS 报告:
sosreport --only-plugins=networking,firewalld用于诊断。
- 端口未开放:检查服务状态(
29.4 实践任务
- 使用 SecureCRT 登录
ZSLinux,检查 SSH 端口映射(2222/tcp)。 - 使用
ss或nmap扫描 HTTP 和 HTTPS 端口。 - 配置防火墙以支持 MySQL 和 PostgreSQL 服务(默认端口,服务名方式)。
- 为自定义 SSH 端口(2222/tcp)配置防火墙(端口开放方式)和 SELinux 上下文。
29.5 自测问题
- 问题:如何查找 SSH 标准端口?
- 答案:
cat /etc/services | grep ssh
- 答案:
- 问题:如何检查当前开放的 TCP 端口?
- 答案:
ss -tuln
- 答案:
- 问题:如何为自定义 SSH 端口(2222)配置防火墙和 SELinux?
- 答案:
firewall-cmd --permanent --add-port=2222/tcp和semanage port -a -t ssh_port_t -p tcp 2222,然后firewall-cmd --reload
- 答案:
