Linux 安全加固与常见服务配置(一):DHCP服务配置和DNS域名解析
是一个网络协议,用于自动为网络中的设备分配IP地址及相关配置参数,使设备能够快速、自动地加入网络。DNS(Domain Name System,域名系统)是用于将域名解析为 IP 地址的服务系统。在网络通信中,主机之间实际依靠 IP 地址进行访问,但人类更容易记住文字域名,DNS 就是实现“域名 ↔ IP 地址”转换的系统。DNS 工作在 应用层,使用 UDP 53 端口,也可使用 TCP(如区域
SELinux 加固 Linux 安全
控制端口 SELinux 上下文
端口 SELinux 上下文
# 查看端口标记 [root@server ~ 09:43:07]# semanage port -l | grep http_port_t http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000 pegasus_http_port_t tcp 5988 # 添加端口标记 [root@server ~ 09:43:27]# semanage port -a -t http_port_t -p tcp 18020 [root@server ~ 09:44:22]# semanage port -l | grep 18020 http_port_t tcp 18020, 80, 81, 443, 488, 8008, 8009, 8443, 9000 # 修改网络端口标记 [root@server ~ 09:44:30]# semanage port -m -t ssh_port_t -p tcp 18020 [root@server ~ 09:45:05]# semanage port -l | grep 18020 ssh_port_t tcp 18020, 22 # 删除端口标记 [root@server ~ 09:45:08]# semanage port -d -t ssh_port_t -p tcp 18020 [root@server ~ 09:45:29]# semanage port -l | grep 18020
示例:配置 web 站点监听端口
[root@server ~ 09:47:49]# vim /etc/httpd/conf/httpd.conf # 修改原先的 Listen 80 为 Listen 18020 Listen 18020 # 重启服务 [root@server ~ 09:48:40]# systemctl restart httpd Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details. [root@server ~ 09:49:27]# systemctl status httpd.service |cat |grep Per 8月 06 09:49:27 server.yuxb.cloud httpd[1913]: (13)Permission denied: AH00072: make_sock: could not bind to address [::]:18020 8月 06 09:49:27 server.yuxb.cloud httpd[1913]: (13)Permission denied: AH00072: make_sock: could not bind to address 0.0.0.0:18020 # 配置端口 [root@server ~ 09:49:49]# semanage port -a -t http_port_t -p tcp 18020 [root@server ~ 09:50:19]# semanage port -l | grep 18020 http_port_t tcp 18020, 80, 81, 443, 488, 8008, 8009, 8443, 9000 # 只查看改动标签 [root@server ~ 09:50:22]# semanage port -lC SELinux 端口类型 协议 端口号 http_port_t tcp 18020 # 重启服务并访问 [root@server ~ 09:50:34]# systemctl restart httpd # 访问验证 [root@server ~ 09:51:10]# curl http://server.yuxb.cloud:18020/
控制 SELinux 布尔值
SELinux 布尔值
# 查看SELinux布尔值 [root@server ~ 10:12:09]# getsebool httpd_enable_homedirs httpd_enable_homedirs --> off [root@server ~ 10:12:35]# getsebool -a |grep httpd_enable_homedirs httpd_enable_homedirs --> off # 设置SELinux布尔值 [root@server ~ 10:12:44]# setsebool Usage: setsebool [ -NPV ] boolean value | bool1=val1 bool2=val2... # 启用value为1或者on或者true # 禁用value为0或者off或者false [root@server ~ 10:20:34]# setsebool httpd_enable_homedirs=1或者on或者true [root@server ~ 10:21:27]# setsebool httpd_enable_homedirs=0或者off或者false # 持久化设置布尔值,使用-P选项修改将写入SELinux数据库。 [root@server ~ 10:21:29]# setsebool httpd_enable_homedirs=1 [root@server ~ 10:22:27]# setsebool -P httpd_enable_homedirs=1 # semanage boolean 命令管理 SELinux 布尔值 [root@server ~ 10:22:37]# semanage boolean Usage: semanage boolean [-h] [-n] [-N] [-S STORE] [ --extract | --deleteall | --list -C | --modify ( --on | --off ) boolean ] # 查看 SELinux 布尔值 [root@server ~ 10:23:23]# semanage boolean -l SELinux 布尔值 状态 默认 描述 privoxy_connect_any (开 , 开) Allow privoxy to connect any smartmon_3ware (关 , 关) Allow smartmon to 3ware mpd_enable_homedirs (关 , 关) Allow mpd to enable homedirs xdm_sysadm_login (关 , 关) Allow xdm to sysadm login xen_use_nfs (关 , 关) Allow xen to use nfs mozilla_read_content (关 , 关) Allow mozilla to read content ssh_chroot_rw_homedirs (关 , 关) Allow ssh to chroot rw homedirs mount_anyfile (开 , 开) Allow mount to anyfile cron_userdomain_transition (开 , 开) Allow cron to userdomain transition xdm_write_home (关 , 关) Allow xdm to write home openvpn_can_network_connect (开 , 开) Allow openvpn to can network connect xserver_execmem (关 , 关) Allow xserver to execmem ...... # 只查看非默认的 SELinux 布尔值 [root@server ~ 10:24:35]# semanage boolean -l -C SELinux 布尔值 状态 默认 描述 httpd_enable_homedirs (开 , 开) Allow httpd to enable homedirs # 设置 SELinux 布尔值 # 相当于setsebool -P [root@server ~ 10:27:09]# semanage boolean -m --on use_nfs_home_dirs [root@server ~ 10:28:38]# semanage boolean -m --off use_nfs_home_dirs # 删除原先自定义的 SELinux 布尔值 [root@server ~ 10:28:51]# semanage boolean -D
DHCP 服务器
DHCP 服务介绍
是一个网络协议,用于自动为网络中的设备分配IP地址及相关配置参数,使设备能够快速、自动地加入网络。
DHCP 的核心作用
在局域网中,每台计算机都需要一个唯一的 IP 地址才能通信。手动设置每台设备的 IP 地址既繁琐又容易出错,而 DHCP 可以自动完成以下配置:
-
IP 地址
-
子网掩码
-
默认网关
-
DNS 服务器
-
租期(Lease Time)
配置 DHCP 服务器
dhcpd服务使用/etc/dhcp/dhcpd.conf配置文件。 dhcp软件包提供/usr/share/doc/dhcp-*/dhcpd.conf.example 配置文件示例。
[root@server ~ 11:28:08]# yum install -y dhcp [root@server ~ 10:44:53]# cat /etc/dhcp/dhcpd.conf # # DHCP Server Configuration file. # see /usr/share/doc/dhcp*/dhcpd.conf.example # see dhcpd.conf(5) man page # [root@server ~ 10:45:00]# cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf cp:是否覆盖"/etc/dhcp/dhcpd.conf"? yes [root@server ~ 10:45:37]# [root@server ~ 10:45:42]# vim /etc/dhcp/dhcpd.conf [root@server ~ 10:56:19]# systemctl restart dhcpd
典型的DHCP配置示例:
# A slightly different configuration for an internal subnet.
subnet 10.1.8.0 netmask 255.255.255.0 {
range 10.1.8.101 10.1.8.200;
option domain-name-servers 223.5.5.5, 223.6.6.6;
option domain-name "yuxb.cloud";
option routers 10.1.8.1;
option broadcast-address 10.1.8.2;
default-lease-time 600;
max-lease-time 7200;
}
# Fixed IP addresses can also be specified for hosts. These addresses
# should not also be listed as being available for dynamic assignment.
# Hosts for which fixed IP addresses have been specified can boot using
# BOOTP or DHCP. Hosts for which no fixed address is specified can only
# be booted with DHCP, unless there is an address range on the subnet
# to which a BOOTP client is connected which has the dynamic-bootp flag
# set.
host fantasia {
hardware ethernet 08:00:07:26:c0:a5;
fixed-address fantasia.fugue.com;
}
[root@client ~ 12:39:21]# ip -br a
lo UNKNOWN 127.0.0.1/8 ::1/128
ens33 UP 10.1.8.11/24 fe80::20c:29ff:fe8e:12ed/64
ens36 UP 10.1.8.101/24 fe80::e87:444a:b00d:b5b6/64
[root@client ~ 12:39:24]# nmcli device show ens36
GENERAL.DEVICE: ens36
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 00:0C:29:8E:12:F7
GENERAL.MTU: 1500
GENERAL.STATE: 100(已连接)
GENERAL.CONNECTION: ens36-dhcp
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/3
WIRED-PROPERTIES.CARRIER: 开
IP4.ADDRESS[1]: 10.1.8.101/24
IP4.GATEWAY: 10.1.8.1
IP4.ROUTE[1]: dst = 0.0.0.0/0, nh = 10.1.8.1, mt = 101
IP4.ROUTE[2]: dst = 10.1.8.0/24, nh = 0.0.0.0, mt = 101
IP4.DNS[1]: 223.5.5.5
IP4.DNS[2]: 223.6.6.6
IP4.DOMAIN[1]: yuxb.cloud
IP6.ADDRESS[1]: fe80::e87:444a:b00d:b5b6/64
[root@client ~ 12:39:43]# ip -br link
lo UNKNOWN 00:00:00:00:00:00 <LOOPBACK,UP,LOWER_UP>
ens33 UP 00:0c:29:8e:12:ed <BROADCAST,MULTICAST,UP,LOWER_UP>
ens36 UP 00:0c:29:8e:12:f7 <BROADCAST,MULTICAST,UP,LOWER_UP>
[root@client ~ 12:40:19]#
[root@client ~ 12:41:57]#
[root@client ~ 12:41:57]# ip -br a
lo UNKNOWN 127.0.0.1/8 ::1/128
ens33 UP 10.1.8.11/24 fe80::20c:29ff:fe8e:12ed/64
ens36 UP 10.1.8.101/24 fe80::e87:444a:b00d:b5b6/64
[root@client ~ 12:42:01]# nmcli connection up ens36-dhcp
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)
[root@client ~ 12:42:11]# ip -br a
lo UNKNOWN 127.0.0.1/8 ::1/128
ens33 UP 10.1.8.11/24 fe80::20c:29ff:fe8e:12ed/64
ens36 UP 10.1.8.88/24 fe80::e87:444a:b00d:b5b6/64
[root@server ~ 13:30:37]# cat /etc/dhcp/dhcpd.conf
# A slightly different configuration for an internal subnet.
subnet 10.1.8.0 netmask 255.255.255.0 {
range 10.1.8.101 10.1.8.200;
option domain-name-servers 223.5.5.5, 223.6.6.6;
option domain-name "yuxb.cloud";
option routers 10.1.8.1;
option broadcast-address 10.1.8.2;
default-lease-time 600;
max-lease-time 7200;
}
# Fixed IP addresses can also be specified for hosts. These addresses
# should not also be listed as being available for dynamic assignment.
# Hosts for which fixed IP addresses have been specified can boot using
# BOOTP or DHCP. Hosts for which no fixed address is specified can only
# be booted with DHCP, unless there is an address range on the subnet
# to which a BOOTP client is connected which has the dynamic-bootp flag
# set.
host client {
hardware ethernet 00:0c:29:8e:12:f7;
fixed-address 10.1.8.88;
}
[root@server ~ 12:42:41]# ls /var/lib/dhcpd/
dhcpd6.leases dhcpd.leases dhcpd.leases~
[root@server ~ 12:42:49]# ls /var/lib/dhcpd/dhcpd.leases
/var/lib/dhcpd/dhcpd.leases
[root@server ~ 12:43:05]# less /var/lib/dhcpd/dhcpd.leases
DNS 服务器
DNS 服务介绍
DNS(Domain Name System,域名系统)是用于将域名解析为 IP 地址的服务系统。
在网络通信中,主机之间实际依靠 IP 地址进行访问,但人类更容易记住文字域名,DNS 就是实现“域名 ↔ IP 地址”转换的系统。
DNS 工作在 应用层,使用 UDP 53 端口,也可使用 TCP(如区域传输时)。
DNS 查询
DNS 查询分为以下几种类型:
| 查询方式 | 描述 |
|---|---|
| 递归查询 | 客户端(通常是浏览器或操作系统)请求本地 DNS 服务器解析域名,服务器负责帮它找出最终结果。 |
| 迭代查询 | DNS 服务器之间交互时,若不知道答案,会告诉客户端“你去问某某服务器”,而不会帮它查到底。 |
| 正向解析 | 从域名 → 找 IP 地址(A 或 AAAA 记录)。 |
| 反向解析 | 从 IP 地址 → 找域名(PTR 记录)。 |
DNS 资源记录
资源记录(Resource Record)是 DNS 数据库中的基本数据单位,用于描述域名与各种信息之间的关系。
| 类型 | 名称 | 作用说明 |
|---|---|---|
| A | 地址记录 | 将主机名映射到 IPv4 地址 |
| AAAA | 地址记录 | 将主机名映射到 IPv6 地址 |
| CNAME | 别名记录 | 定义某个域名是另一个域名的别名 |
| MX | 邮件交换记录 | 指定负责接收邮件的服务器地址 |
| NS | 域名服务器 | 指定某域名由哪个 DNS 服务器解析 |
| PTR | 反向记录 | 将 IP 地址映射为主机名 |
| SOA | 起始授权记录 | 定义域的基本属性(权威信息) |
| TXT | 文本记录 | 存储任意文本,如 SPF、防伪标识等 |
主机和资源记录
⼀个主机,无论是客户端还是服务器,都具有以下 DNS 资源记录:
-
⼀个或多个A或AAAA记录
-
用于将其IP地址反向映射到名称的PTR记录
-
⼀个或多个CNAME记录(可选)
DNS zone 还具有以下资源记录:
-
唯一的 SOA 记录
-
每个权威名称服务器的 NS 记录
-
⼀个或多个MX记录(可选)
-
用于在域中查找服务的⼀个或多个SRV记录(可选)
配置权威名称服务器
权威名称服务器架构
什么是权威名称服务器?
权威名称服务器 是 DNS 系统中的核心角色,负责一个或多个域(zone)的最终解析权,它可以:
-
保存域名 → IP 的真实记录(A、CNAME 等)
-
对该域的查询给出权威答案
-
不会再向其他服务器转发请求
权威 DNS 服务器的基本组成
主服务器(Primary / Master)
-
也叫“权威源”。
-
管理员手动编写 zone 文件,保存所有资源记录。
-
是域的配置和数据中心。
-
通常使用
type master配置。
从服务器(Secondary / Slave)
-
通过 zone transfer(区域传送) 从主服务器同步数据。
-
不直接编辑 zone 文件,只读并响应查询。
-
起到容灾备份、负载均衡作用。
-
通常使用
type slave配置。
安装 BIND
[root@server ~ 13:40:14]# yum install -y bind bind-utils
配置 BIND DNS 服务器
定义地址匹配列表
在/etc/named.conf文件的开头,可以使用acl指令定义地址匹配列表。 acl指令不是用于控制客户端对服务器的访问,而是使用它们来定义IP地址和网络列表。
它们提供别名,可以与访问控制指令和其他配置选项一起使用,并使更新配置文件更加容易。
条目可以是完整的IP地址或网络,用尾点(10.1.8.)或CIDR表示法(192.168.0/24或2001:db8::/32)表示,也可以使用先前定义的地址匹配列表的名称。
配置 named 侦听的IP地址
我们可以在/etc/named.conf文件options块中指定许多全局设置。listen-on和listen-on-v6指令,指定了命名监听的接口和端口。
-
listen-on选项采用以分号分隔的IPv4地址列表。
-
listen-on-v6使用IPv6地址。
# 翻译官告诉自己在哪些网卡上听请求(127.0.0.1 是本机,10.1.8.10 是内网IP)。
[root@server ~ 14:33:38]# vim /etc/named.conf
options {
listen-on port 53 { 127.0.0.1;10.1.8.10; };
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { localhost;10.1.8.0/24; };
配置 zone
# 告诉翻译官负责哪些域名(yuxb.cloud)和反向域名(IP翻名字的区域)。
[root@server ~ 14:36:34]# vim /etc/named.conf
# 最后添加如下内容
zone "yuxb.cloud" IN {
type master;
file "yuxb.cloud.zone";
};
zone "8.1.10.in-addr.arpa" IN {
type master;
file "10.1.8.zone";
};
配置说明:
-
type,指定服务器角色。
-
file,指定相对路径名。 相对路径由 options 块中的 directory 指令设置。
创建区域文件
# 写好一张词典,告诉翻译官“yuxb.cloud”是什么地址,谁是邮件服务器,别名怎么指向,等等。 [root@server ~ 14:54:46]# cd /var/named/ [root@server named 14:56:18]# ls data dynamic named.ca named.empty named.localhost named.loopback slaves [root@server named 14:56:28]# touch yuxb.cloud.zone 10.1.8.zone [root@server named 14:56:57]# ls 10.1.8.zone dynamic named.empty named.loopback yuxb.cloud.zone data named.ca named.localhost slaves [root@server named 14:56:59]# chmod 640 yuxb.cloud.zone 10.1.8.zone [root@server named 14:57:34]# chgrp named yuxb.cloud.zone 10.1.8.zone [root@server named 14:57:50]# ll named yuxb.cloud.zone 10.1.8.zone ls: 无法访问named: 没有那个文件或目录 -rw-r----- 1 root named 0 8月 6 14:56 10.1.8.zone -rw-r----- 1 root named 0 8月 6 14:56 yuxb.cloud.zone
添加记录
正向记录,将名称映射到IP地址和其他记录。该区域文件必须具有:
-
SOA记录。
-
每个公用名称服务器的NS记录。
-
该区域的其他A,AAAA,CNAME,MX,SRV和TXT记录。
示例:yuxb.cloud域
[root@server named 15:04:17]# vim /var/named/yuxb.cloud.zone $TTL 3600 @ IN SOA dns.yuxb.cloud. root.yuxb.cloud. ( 42 ; serial 3H ; secondary refresh 15M ; secondary retry 1W ; secondary timeout 15M ; minimum cache TTL for negative answers ) IN NS dns.yuxb.cloud. dns IN A 10.1.8.10 server IN A 10.1.8.10 student IN CNAME client.yuxb.cloud. client IN A 10.1.8.11 www 30 IN A 10.1.8.200 @ IN MX 10 mail.yuxb.cloud. mail IN A 10.1.8.253
示例:8.1.10.inaddr.arpa区域
[root@server named 15:09:23]# vim 10.1.8.zone $TTL 3600 @ IN SOA dns.yuxb.cloud. root.yuxb.cloud. ( 42 ; serial 3H ; secondary refresh 15M ; secondary retry 1W ; secondary timeout 15M ; minimum cache TTL for negative answers ) IN NS dns.yuxb.cloud. 10 IN PTR server.yuxb.cloud. 10 IN PTR dns.yuxb.cloud. 11 IN PTR client.yuxb.cloud. 11 IN PTR student.yuxb.cloud. 200 IN PTR www.yuxb.cloud. 253 IN PTR mail.yuxb.cloud.
运行 BIND 和 客户端测试
# 启动服务 [root@server named 15:14:20]# systemctl enable named --now [root@client ~ 15:29:46]# nmcli connection delete ens36-dhcp 成功删除连接 "ens36-dhcp" (e0d06ed6-eaa6-49fd-93a3-a755c7dd51ce)。 # 测试 # 方式1:配置客户端 DNS [root@client ~ 15:30:08]# nmcli connection modify ens33 ipv4.dns 10.1.8.10 [root@client ~ 15:30:35]# nmcli connection up ens33 连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/7) [root@client ~ 15:30:41]# cat /etc/resolv.conf # Generated by NetworkManager search yuxb.cloud nameserver 10.1.8.10 [root@client ~ 15:36:56]# ping student PING client.yuxb.cloud (10.1.8.11) 56(84) bytes of data. 64 bytes from client.yuxb.cloud (10.1.8.11): icmp_seq=1 ttl=64 time=0.009 ms 64 bytes from client.yuxb.cloud (10.1.8.11): icmp_seq=2 ttl=64 time=0.031 ms # 方式2:dig工具 [root@client ~ 16:12:38]# yum install -y bind-utils [root@client ~ 16:15:18]# dig @10.1.8.10 student.yuxb.cloud. ; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.16 <<>> @10.1.8.10 student.yuxb.cloud. ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 62303 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 1, ADDITIONAL: 2 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;student.yuxb.cloud. IN A ;; ANSWER SECTION: student.yuxb.cloud. 3600 IN CNAME client.yuxb.cloud. client.yuxb.cloud. 3600 IN A 10.1.8.11 ;; AUTHORITY SECTION: yuxb.cloud. 3600 IN NS dns.yuxb.cloud. ;; ADDITIONAL SECTION: dns.yuxb.cloud. 3600 IN A 10.1.8.10 ;; Query time: 0 msec ;; SERVER: 10.1.8.10#53(10.1.8.10) ;; WHEN: 三 8月 06 16:16:39 CST 2025 ;; MSG SIZE rcvd: 118
额外实践:阿里云域名解析与 Apache 网站部署
操作环境
首先要有一个自己的域名,我的是yuxb.cloud 还要有公网服务器 远程工具xshell 默认端口 网站根目录 /www/yuxb.cloud
操作步骤
1. 域名解析配置(阿里云)
登录 阿里云控制台,添加如下解析记录:
| 主机记录 | 类型 | 记录值 |
|---|---|---|
| www | A | xxx |
| @ | A | xxx |
2. 使用 Xshell 连接服务器
3. 创建网站目录与首页文件
cd /www mkdir yuxb.cloud echo 'Welcome to yuxb site' > yuxb.cloud/index.html
/www/yuxb.cloud/是你的网站目录,index.html是首页文件
4. 配置 Apache 虚拟主机
进入配置目录:
cd /etc/httpd/conf.d/ vim vhost-yuxb.cloud.conf
内容如下:
<VirtualHost *:80> DocumentRoot "/www/yuxb.cloud" ServerName www.yuxb.cloud <Directory "/www/yuxb.cloud"> AllowOverride None # Allow open access: Require all granted </Directory> </VirtualHost>
5. 重启 Apache 服务
systemctl restart httpd
6. 浏览器访问测试
访问地址: www.yuxb.cloud:xxx
更多推荐

所有评论(0)