本文整理了 RHCE 考试/个人实验中常用的 10 类核心服务配置,适配 CentOS Stream 9/10 系统(CentOS8及以下也适用,可能有部分不同)所有命令均经过验证,可直接复制使用。
前置准备:配置镜像源(必做) CentOS Stream 9/10 默认源可能访问缓慢,需先配置国内源: 推荐中科大源USTC Open Source Software Mirror
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 dnf clean all mkdir -p /etc/yum.repos.d/bakmv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/vi -o /etc/yum.repos.d/CentOS-Stream-BaseOS.repo 打开配置文件自己添加 baseurl=https://mirrors.ustc.edu.cn/centos-stream/9-stream/BaseOS/x86_64/os/ vi -o /etc/yum.repos.d/CentOS-Stream-AppStream.repo 打开配置文件自己添加 baseurl=https://mirrors.ustc.edu.cn/centos-stream/9-stream/AppStream/x86_64/os/ dnf makecache
1. FTP 服务(端口 20/21) 用于文件传输,核心包为 vsftpd。
1.1 安装与基础配置 1 2 3 4 5 dnf install -y vsftpd vi /etc/vsftpd/vsftpd.conf
关键配置项(修改/确认):
1 2 3 4 5 anonymous_enable =YES local_enable =YES write_enable =YES chroot_local_enable =YES anon_upload_enable =YES
1.2 SELinux 配置(关键) 1 2 setsebool -P tftp_home_dir 1 setsebool -P ftpd_full_access 1
1.3 防火墙配置 1 2 firewall-cmd --permanent --add-service=ftp firewall-cmd --reload
1.4 创建 FTP 用户(可选) 1 2 3 useradd ftpuser passwd ftpuser usermod -s /sbin/nologin ftpuser
1.5 启动服务 1 2 3 systemctl start vsftpd systemctl enable vsftpd systemctl status vsftpd
2. NFS 服务(端口 2049/111/20048) 网络文件共享,实现多主机共享存储。
2.1 服务端配置 (1)安装组件 1 dnf install -y nfs-utils rpcbind nfs4-acl-tools
(2)创建共享目录 1 2 3 mkdir -p /data/nfs_sharechown -R nobody:nobody /data/nfs_sharechmod -R 755 /data/nfs_share
(3)配置导出规则
添加规则(按需选择):
1 2 3 4 5 6 /data/nfs_share 192.168.0.0/24(rw,sync,no_subtree_check) /data/nfs_share 192.168.0.100(ro,sync,no_subtree_check) /data/nfs_share *(rw,sync,no_root_squash)
(4)生效配置并启动服务 1 2 3 exportfs -r systemctl enable --now nfs-server rpcbind systemctl status nfs-server
(5)防火墙配置 1 2 3 4 firewall-cmd --permanent --add-service=nfs firewall-cmd --permanent --add-service=rpc-bind firewall-cmd --permanent --add-service=mountd firewall-cmd --reload
(6)验证配置 1 2 exportfs -v showmount -e localhost
2.2 客户端配置 (1)安装客户端工具 1 dnf install -y nfs-utils
(2)手动挂载共享 1 2 3 4 mkdir -p /mnt/nfs_mountmount -t nfs 192.168.0.5:/data/nfs_share /mnt/nfs_mount df -h
(3)开机自动挂载(/etc/fstab)
添加:
1 192.168.0.5:/data/nfs_share /mnt/nfs_mount nfs defaults 0 0
测试挂载:
(4)自动挂载(AutoFS,可选) 1 2 dnf install -y autofs vi /etc/auto.master
添加:
创建自动挂载配置:
添加:
1 /mnt/nfs_mount -fstype =nfs 192.168 .0.5 :/data/nfs_share
启动服务:
1 systemctl enable --now autofs
3. DNS 服务(端口 53/UDP/TCP) 域名解析,核心包为 bind。
3.1 安装与基础配置 1 2 3 4 5 dnf install -y bind bind-utils vi /etc/named.conf
修改关键项:
1 2 listen-on port 53 { any allow-query { any
3.2 配置区域文件 (1)添加区域配置(在 named.conf 末尾) 1 2 3 4 zone "example.com" IN { type master file "/var/named/example.com.zone" }
(2)创建区域数据文件 1 2 3 4 5 6 7 8 9 10 11 12 13 cat > /var/named/example.com.zone << EOF \$TTL 86400 @ IN SOA ns.example.com. admin.example.com. ( 2026030701 ; 序列号 3600 ; 刷新时间 1800 ; 重试时间 604800 ; 过期时间 86400 ; 最小TTL ) @ IN NS ns.example.com. ns IN A 192.168.142.10 ; DNS 服务器 IP www IN A 192.168.142.10 ; www 解析 IP EOF
3.3 设置权限 1 2 chown root:named /var/named/example.com.zonechmod 644 /var/named/example.com.zone
3.4 验证配置 1 2 named-checkconf named-checkzone example.com /var/named/example.com.zone
3.5 启动服务与防火墙 1 2 3 4 systemctl start named systemctl enable named firewall-cmd --permanent --add-service=dns firewall-cmd --reload
3.6 测试解析 1 nslookup www.example.com 127.0.0.1
4. Samba 服务(Windows 共享文件) 实现 Linux 与 Windows 跨平台文件共享。
4.1 安装组件 1 dnf install -y samba samba-common samba-client
4.2 创建共享目录 1 2 3 mkdir -p /srv/samba/sharechmod -R 777 /srv/samba/sharechown -R nobody:nobody /srv/samba/share
4.3 配置 smb.conf 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 cp /etc/samba/smb.conf /etc/samba/smb.conf.bakcat > /etc/samba/smb.conf << EOF [global] workgroup = WORKGROUP server string = Samba Server %v netbios name = centos-samba security = user map to guest = Bad User [share] path = /srv/samba/share browseable = yes writable = yes guest ok = yes public = yes create mask = 0666 directory mask = 0777 EOF
4.4 验证配置
4.5 启动服务 1 2 systemctl enable --now smb nmb systemctl status smb
4.6 防火墙与 SELinux 1 2 3 4 5 6 7 firewall-cmd --permanent --add-service=samba firewall-cmd --reload setsebool -P samba_enable_home_dirs on chcon -t samba_share_t /srv/samba/share -R
4.7 创建 Samba 用户(可选) 1 2 useradd smbuser smbpasswd -a smbuser
4.8 访问测试
Windows :文件管理器输入 \\虚拟机IP
Linux 客户端 :mount -t cifs //虚拟机IP/share /mnt -o username=smbuser,password=你的密码
5. Chrony/NTP 服务(端口 123/UDP) 系统时间同步,替代传统 NTP。
5.1 服务端配置(提供时间同步) (1)安装 Chrony
(2)修改配置
添加/修改:
1 2 allow 192.168.0.0/24 local stratum 10
(3)启动服务与防火墙 1 2 3 systemctl enable --now chronyd firewall-cmd --permanent --add-service=ntp firewall-cmd --reload
5.2 客户端配置(同步服务端时间) (1)修改配置
注释原有 pool,添加服务端 IP:
1 2 server 192.168.0.100 iburst
(2)重启服务 1 systemctl restart chronyd
5.3 验证同步 1 2 chronyc sources -v date
6. HTTP/HTTPS 服务(端口 80/443) 搭建 Web 网站,核心包为 httpd。
6.1 安装组件 1 dnf install -y httpd mod_ssl
6.2 配置 HTTP(80 端口) (1)启动服务 1 2 systemctl enable --now httpd systemctl status httpd
(2)防火墙配置 1 2 3 firewall-cmd --permanent --add-service=http firewall-cmd --permanent --add-service=https firewall-cmd --reload
(3)测试网页 1 echo "<h1>我的 HTTP 网页</h1>" > /var/www/html/index.html
浏览器访问:http://虚拟机IP
6.3 配置 HTTPS(443 端口) (1)生成自签名证书 1 2 3 openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 \ -keyout /etc/pki/tls/private/httpd.key \ -out /etc/pki/tls/certs/httpd.crt
(一路回车即可)
(2)重启服务
(3)HTTPS 访问 浏览器访问:https://虚拟机IP(忽略安全提示)
6.4 SELinux 配置 1 2 chown -R apache:apache /var/www/htmlchmod -R 755 /var/www/html
6.5 常用验证命令 1 2 httpd -t ss -tulnp | grep httpd
7. DHCP 服务(端口 67/68/UDP) 自动分配 IP 地址给客户端。
7.1 安装组件 1 dnf install -y dhcp-server
7.2 复制模板配置(必做) 1 cp /usr/share/doc/dhcp-server/dhcpd.conf.example /etc/dhcp/dhcpd.conf
7.3 编辑 DHCP 配置
替换为以下内容(修改网段适配自己环境):
1 2 3 4 5 6 7 8 subnet 192.168.100.0 netmask 255.255.255.0 { range 192.168.100.100 192.168.100.200 option routers 192.168.100.2 option domain-name-servers 192.168.100.10 option subnet-mask 255.255.255.0 default-lease-time 600 max-lease-time 7200 }
7.4 指定监听网卡(可选)
添加:DHCPDARGS="网卡名"(如 ens33)
7.5 启动服务与防火墙 1 2 3 systemctl enable --now dhcpd firewall-cmd --permanent --add-service=dhcp firewall-cmd --reload
7.6 验证服务
7.7 客户端测试 Windows 执行:
1 2 ipconfig /releaseipconfig /renew
查看是否获取到 DHCP 分配的 IP。
8. iSCSI 服务(端口 3260/TCP) 将远程存储映射为本地硬盘。
8.1 服务端(Target)配置 (1)安装工具 1 2 dnf install -y targetcli systemctl enable --now target
(2)准备后端存储(LVM 示例) 1 2 3 pvcreate /dev/sdb vgcreate iscsi_vg /dev/sdb lvcreate -n iscsi_lv -L 100G iscsi_vg
(3)targetcli 交互配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 targetcli /backstores/block create iscsi_disk /dev/iscsi_vg/iscsi_lv /iscsi create iqn.2026-03.com.example:target01 cd /iscsi/iqn.2026-03.com.example:target01/tpg1/lunscreate /backstores/block/iscsi_disk cd ../aclscreate iqn.2026-03.com.example:initiator01 saveconfig exit
(4)防火墙配置 1 2 firewall-cmd --permanent --add-port=3260/tcp firewall-cmd --reload
8.2 客户端(Initiator)配置 (1)安装工具 1 2 dnf install -y iscsi-initiator-utils systemctl enable --now iscsid iscsi
(2)配置 Initiator IQN 1 2 echo "InitiatorName=iqn.2026-03.com.example:initiator01" > /etc/iscsi/initiatorname.iscsisystemctl restart iscsid iscsi
(3)发现并登录 Target 1 2 3 4 iscsiadm -m discovery -t st -p 192.168.1.100 iscsiadm -m node -L all
(4)使用存储 1 2 3 4 5 6 7 lsblk parted /dev/sdb mklabel gpt parted /dev/sdb mkpart primary 0% 100% mkfs.xfs /dev/sdb1 mkdir -p /mnt/iscsimount /dev/sdb1 /mnt/iscsi
(5)开机自动挂载(加 _netdev) 1 2 3 4 5 6 blkid /dev/sdb1 UUID=xxxx-xxxx /mnt/iscsi xfs defaults,_netdev 0 0 mount -a
8.3 常用管理命令 1 2 3 4 5 targetcli ls iscsiadm -m session -o show iscsiadm -m node -U all
9. Sendmail 服务(端口 25/465) 邮件发送与转发。
9.1 安装组件 1 dnf install -y sendmail sendmail-cf mailx
9.2 修改监听地址 1 vi /etc/mail/sendmail.mc
修改:
1 DAEMON_OPTIONS(`Port =smtp,Addr=0.0 .0.0 , Name=MTA')dnl # 监听所有 IP
9.3 重新生成配置 1 m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
9.4 配置本机域名 1 2 3 4 cat >> /etc/mail/local-host-names << EOF localhost localhost.localdomain EOF
9.5 启动服务与防火墙 1 2 3 systemctl enable --now sendmail sm-client firewall-cmd --permanent --add-service=smtp firewall-cmd --reload
9.6 测试发送邮件 1 2 echo "测试邮件内容" | mail -s "Sendmail 测试" rootmail
10. Ansible 批量管理 批量配置、管理多台服务器。
10.1 控制节点(管理端)配置 (1)安装 Ansible 1 2 dnf install -y ansible-core ansible --version
(2)生成 SSH 密钥(免密登录) 1 ssh-keygen -t rsa -b 2048
(3)拷贝密钥到被管理节点 1 2 ssh-copy-id root@192.168.142.20
(4)配置主机清单
添加:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 192.168.142.20 [web] 192.168.142.20 192.168.142.21 [all:vars] ansible_ssh_user =rootansible_ssh_port =22 ansible_become =yes ansible_become_method =sudoansible_become_user =root
(5)测试连通性
10.2 被管理节点(客户端)配置 无需安装 Ansible,仅需 2 步:
1 2 3 4 5 6 dnf install -y openssh-server systemctl enable --now sshd setenforce 0
10.3 常用 Ansible 命令 1 2 3 4 5 6 7 8 ansible all -m command -a "free -h" ansible web -m dnf -a "name=httpd state=installed" ansible web -m service -a "name=httpd state=started enabled=yes" ansible web -m copy -a "src=/root/test.txt dest=/tmp/test.txt"
10.4 验证配置 1 2 ansible all --list-hosts ansible all -m shell -a "hostname"