|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1. Red Hat Enterprise Linux系统基础与高级配置
Red Hat Enterprise Linux (RHEL) 是全球领先的企业级Linux发行版,广泛应用于企业服务器环境。作为系统管理员,掌握RHEL的高级配置对于确保服务器稳定运行至关重要。
1.1 系统安装与初始化配置
RHEL的安装可以通过多种方式进行,包括传统DVD安装、网络安装(PXE)和自动化安装(Kickstart)。
Kickstart是一种自动化安装方法,可以大大简化多台服务器的部署过程。以下是一个基本的Kickstart配置文件示例:
- #version=RHEL8
- install
- cdrom
- lang en_US.UTF-8
- keyboard us
- network --bootproto=dhcp --device=eth0 --onboot=on
- rootpw --iscrypted $6$longhashedpasswordstring
- firewall --enabled --ssh
- authconfig --enableshadow --passalgo=sha512
- selinux --enforcing
- timezone --utc America/New_York
- bootloader --location=mbr --drive-name=sda
- clearpart --all --initlabel
- part /boot --fstype=xfs --size=500
- part pv.01 --size=1 --grow
- volgroup vg_root pv.01
- logvol / --fstype=xfs --name=lv_root --vgname=vg_root --size=1 --grow
- reboot
- %packages
- @core
- @base
- %end
- %post
- #!/bin/bash
- echo "Custom post-installation script"
- # Add custom configurations here
- %end
复制代码
1.2 系统服务管理
RHEL 7及以后版本使用systemd作为系统和服务管理器。以下是一些常用的systemd命令:
- # 启动服务
- systemctl start httpd
- # 停止服务
- systemctl stop httpd
- # 重启服务
- systemctl restart httpd
- # 重新加载服务配置
- systemctl reload httpd
- # 设置服务开机自启
- systemctl enable httpd
- # 禁用服务开机自启
- systemctl disable httpd
- # 查看服务状态
- systemctl status httpd
- # 查看所有服务的状态
- systemctl list-units --type=service --all
- # 查看服务日志
- journalctl -u httpd
复制代码
1.3 内核参数调优
通过修改/etc/sysctl.conf文件或使用sysctl命令可以调整内核参数,优化系统性能。以下是一些常用的内核参数优化:
- # 增加文件描述符限制
- echo "fs.file-max = 100000" >> /etc/sysctl.conf
- # 优化网络参数
- cat >> /etc/sysctl.conf << EOF
- # TCP优化
- net.ipv4.tcp_fin_timeout = 30
- net.ipv4.tcp_keepalive_time = 1200
- net.ipv4.tcp_max_syn_backlog = 8192
- net.ipv4.tcp_max_tw_buckets = 5000
- net.ipv4.tcp_tw_reuse = 1
- net.ipv4.tcp_tw_recycle = 1
- # 网络队列优化
- net.core.netdev_max_backlog = 65535
- net.core.somaxconn = 65535
- # 内存优化
- vm.swappiness = 10
- vm.dirty_ratio = 60
- vm.dirty_background_ratio = 2
- EOF
- # 应用配置
- sysctl -p
复制代码
1.4 文件系统高级配置
RHEL支持多种文件系统,包括XFS、ext4等。以下是XFS文件系统的高级配置示例:
- # 创建XFS文件系统
- mkfs.xfs -f /dev/sdb1
- # 挂载XFS文件系统并指定选项
- mount -o noatime,nodiratime,largeio,inode64,swalloc /dev/sdb1 /data
- # 永久挂载配置
- echo "/dev/sdb1 /data xfs defaults,noatime,nodiratime,largeio,inode64,swalloc 0 0" >> /etc/fstab
- # 调整XFS文件系统参数
- xfs_admin -L "DataVolume" /dev/sdb1
- # 扩展XFS文件系统(如果使用LVM)
- lvextend -L +10G /dev/vg_root/lv_data
- xfs_growfs /data
复制代码
2. 企业服务器性能优化
性能优化是系统管理员的核心职责之一。本节将详细介绍如何优化RHEL服务器的性能。
2.1 CPU性能优化
CPU亲和性可以绑定进程到特定的CPU核心,减少缓存失效和上下文切换开销:
- # 查看CPU核心信息
- lscpu
- # 查看当前进程的CPU亲和性
- taskset -p <PID>
- # 设置进程的CPU亲和性
- taskset -cp 0,1,2,3 <PID>
- # 启动程序时指定CPU亲和性
- taskset -c 0,1,2,3 <command>
- # 使用numactl控制NUMA系统上的内存和CPU分配
- numactl --cpubind=0 --membind=0 <command>
复制代码
通过调整CPU频率可以平衡性能和能耗:
- # 安装cpufreq工具
- yum install cpupowerutils
- # 查看CPU频率驱动信息
- cpupower frequency-info
- # 设置CPU频率调节策略
- cpupower frequency-set -g performance
- # 查看可用的调节策略
- ls /sys/devices/system/cpu/cpufreq/policy0/scaling_available_governors
- # 设置CPU频率上限和下限
- cpupower frequency-set -u 3.0GHz
- cpupower frequency-set -d 1.2GHz
复制代码
2.2 内存性能优化
- # 查看内存使用情况
- free -h
- cat /proc/meminfo
- # 查看进程内存使用情况
- ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head
- # 使用smem工具查看更详细的内存使用情况
- yum install smem
- smem -k -p
- # 查看内存映射信息
- pmap <PID>
复制代码- # 创建交换文件
- dd if=/dev/zero of=/swapfile bs=1M count=2048
- chmod 600 /swapfile
- mkswap /swapfile
- swapon /swapfile
- # 永久启用交换文件
- echo "/swapfile swap swap defaults 0 0" >> /etc/fstab
- # 调整swappiness参数(值越小,越倾向于使用物理内存)
- echo "vm.swappiness = 10" >> /etc/sysctl.conf
- sysctl -p
- # 查看交换空间使用情况
- swapon -s
- cat /proc/swaps
复制代码
2.3 磁盘I/O性能优化
- # 查看当前I/O调度器
- cat /sys/block/sda/queue/scheduler
- # 临时更改I/O调度器
- echo deadline > /sys/block/sda/queue/scheduler
- # 永久更改I/O调度器(通过udev规则)
- echo "ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="deadline"" > /etc/udev/rules.d/60-io-scheduler.rules
- udevadm control --reload-rules
- udevadm trigger
复制代码- # 安装fio工具
- yum install fio
- # 随机读测试
- fio --name=randread --ioengine=libaio --iodepth=16 --rw=randread --bs=4k --direct=1 --size=1G --numjobs=4 --runtime=60 --group_reporting
- # 随机写测试
- fio --name=randwrite --ioengine=libaio --iodepth=16 --rw=randwrite --bs=4k --direct=1 --size=1G --numjobs=4 --runtime=60 --group_reporting
- # 混合随机读写测试
- fio --name=randrw --ioengine=libaio --iodepth=16 --rw=randrw --rwmixread=70 --bs=4k --direct=1 --size=1G --numjobs=4 --runtime=60 --group_reporting
复制代码
2.4 网络性能优化
- # 查看网络接口信息
- ethtool eth0
- # 启用网络接口的多队列支持
- ethtool -L eth0 combined 8
- # 调整网络接口的队列长度
- ifconfig eth0 txqueuelen 10000
- # 启用网络接口的卸载功能
- ethtool -K eth0 gso on
- ethtool -K eth0 tso on
- ethtool -K eth0 lro on
- ethtool -K eth0 gro on
复制代码- # 增加本地端口范围
- echo "net.ipv4.ip_local_port_range = 1024 65535" >> /etc/sysctl.conf
- # 优化TCP连接参数
- echo "net.ipv4.tcp_rmem = 4096 87380 16777216" >> /etc/sysctl.conf
- echo "net.ipv4.tcp_wmem = 4096 65536 16777216" >> /etc/sysctl.conf
- echo "net.core.rmem_max = 16777216" >> /etc/sysctl.conf
- echo "net.core.wmem_max = 16777216" >> /etc/sysctl.conf
- # 应用配置
- sysctl -p
复制代码
3. 安全加固
安全加固是保护企业服务器免受攻击的关键步骤。本节将介绍如何加强RHEL服务器的安全性。
3.1 系统访问控制
- # 创建新用户并设置强密码
- useradd -m -s /bin/bash admin
- passwd admin
- # 限制sudo访问
- visudo
- # 添加以下行,允许admin用户以root权限执行所有命令
- admin ALL=(ALL) ALL
- # 或者更严格的限制,只允许执行特定命令
- admin ALL=(ALL) /usr/bin/systemctl, /usr/bin/rpm, /usr/bin/yum
- # 禁用root用户SSH登录
- echo "PermitRootLogin no" >> /etc/ssh/sshd_config
- systemctl restart sshd
- # 设置密码策略
- yum install libpwquality
- echo "minlen = 12" >> /etc/security/pwquality.conf
- echo "minclass = 3" >> /etc/security/pwquality.conf
- echo "dcredit = -1" >> /etc/security/pwquality.conf
- echo "ucredit = -1" >> /etc/security/pwquality.conf
- echo "lcredit = -1" >> /etc/security/pwquality.conf
- echo "ocredit = -1" >> /etc/security/pwquality.conf
复制代码- # 备份SSH配置文件
- cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
- # 修改SSH配置
- cat > /etc/ssh/sshd_config << EOF
- Port 2222
- Protocol 2
- PermitRootLogin no
- MaxAuthTries 3
- MaxSessions 3
- PubkeyAuthentication yes
- PasswordAuthentication yes
- PermitEmptyPasswords no
- ChallengeResponseAuthentication no
- UsePAM yes
- X11Forwarding no
- PrintMotd no
- AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
- AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
- AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
- AcceptEnv XMODIFIERS
- Subsystem sftp /usr/libexec/openssh/sftp-server
- EOF
- # 重启SSH服务
- systemctl restart sshd
复制代码
3.2 防火墙配置
RHEL 7及以后版本使用firewalld作为默认防火墙管理工具:
- # 启动并启用firewalld
- systemctl start firewalld
- systemctl enable firewalld
- # 查看防火墙状态
- firewall-cmd --state
- # 查看默认区域
- firewall-cmd --get-default-zone
- # 查看活动区域
- firewall-cmd --get-active-zones
- # 查看当前区域的规则
- firewall-cmd --list-all
- # 开放端口
- firewall-cmd --permanent --add-port=80/tcp
- firewall-cmd --permanent --add-port=443/tcp
- # 开放服务
- firewall-cmd --permanent --add-service=http
- firewall-cmd --permanent --add-service=https
- # 重新加载防火墙规则
- firewall-cmd --reload
- # 端口转发
- firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=8080
- # 丰富的规则
- firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="ssh" accept'
- firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.0/8" reject'
复制代码
3.3 SELinux配置
SELinux是RHEL中的强制访问控制(MAC)系统,提供额外的安全层:
- # 查看SELinux状态
- sestatus
- # 临时设置SELinux为 enforcing模式
- setenforce 1
- # 永久设置SELinux为 enforcing模式
- sed -i 's/SELINUX=disabled/SELINUX=enforcing/g' /etc/selinux/config
- # 查看SELinux布尔值
- getsebool -a
- # 设置SELinux布尔值
- setsebool -P httpd_can_network_connect on
- # 查看文件和进程的SELinux上下文
- ls -Z
- ps -Z
- # 修改文件和目录的SELinux上下文
- chcon -t httpd_sys_content_t /var/www/html/*
- restorecon -Rv /var/www/html
- # 查看SELinux日志
- ausearch -m avc -ts recent
- sealert -a /var/log/audit/audit.log
复制代码
3.4 系统审计
审计系统可以记录系统上的活动,帮助检测和调查安全事件:
- # 安装审计工具
- yum install audit
- # 启动并启用auditd服务
- systemctl start auditd
- systemctl enable auditd
- # 查看审计规则
- auditctl -l
- # 添加审计规则
- auditctl -w /etc/passwd -p wa -k passwd_changes
- auditctl -w /etc/selinux/ -p wa -k selinux_changes
- auditctl -w /var/log/audit/ -p wa -k audit_log_changes
- # 永久保存审计规则
- echo "-w /etc/passwd -p wa -k passwd_changes" >> /etc/audit/rules.d/audit.rules
- echo "-w /etc/selinux/ -p wa -k selinux_changes" >> /etc/audit/rules.d/audit.rules
- echo "-w /var/log/audit/ -p wa -k audit_log_changes" >> /etc/audit/rules.d/audit.rules
- # 查看审计日志
- ausearch -k passwd_changes
- ausearch -m avc -ts recent
- aureport -x
- # 生成审计报告
- aureport -m
- aureport -l
- aureport -au
复制代码
4. 网络配置
网络配置是系统管理员的重要职责之一。本节将详细介绍如何在RHEL中配置和管理网络。
4.1 基本网络配置
- # 查看网络连接
- nmcli connection show
- # 查看活动连接
- nmcli connection show --active
- # 查看网络设备状态
- nmcli device status
- # 创建新的以太网连接
- nmcli connection add type ethernet ifname eth0 con-name eth0-static
- # 配置静态IP地址
- nmcli connection modify eth0-static ipv4.addresses 192.168.1.100/24
- nmcli connection modify eth0-static ipv4.gateway 192.168.1.1
- nmcli connection modify eth0-static ipv4.dns "8.8.8.8 8.8.4.4"
- nmcli connection modify eth0-static ipv4.method manual
- # 启用连接
- nmcli connection up eth0-static
- # 配置动态IP地址
- nmcli connection modify eth0-static ipv4.method auto
- # 禁用IPv6
- nmcli connection modify eth0-static ipv6.method ignore
复制代码- # 配置以太网接口
- cat > /etc/sysconfig/network-scripts/ifcfg-eth0 << EOF
- TYPE=Ethernet
- BOOTPROTO=static
- DEFROUTE=yes
- PEERDNS=yes
- PEERROUTES=yes
- IPV4_FAILURE_FATAL=no
- IPV6INIT=no
- NAME=eth0
- DEVICE=eth0
- ONBOOT=yes
- IPADDR=192.168.1.100
- PREFIX=24
- GATEWAY=192.168.1.1
- DNS1=8.8.8.8
- DNS2=8.8.4.4
- EOF
- # 重启网络服务
- systemctl restart network
- # 配置DNS
- cat > /etc/resolv.conf << EOF
- nameserver 8.8.8.8
- nameserver 8.8.4.4
- search example.com
- EOF
复制代码
4.2 高级网络配置
- # 安装绑定工具
- yum install teamd
- # 创建绑定接口
- nmcli connection add type bond ifname bond0 con-name bond0 mode 802.3ad
- nmcli connection modify bond0 ipv4.addresses 192.168.1.200/24
- nmcli connection modify bond0 ipv4.gateway 192.168.1.1
- nmcli connection modify bond0 ipv4.dns "8.8.8.8 8.8.4.4"
- nmcli connection modify bond0 ipv4.method manual
- # 添加从接口到绑定
- nmcli connection add type ethernet ifname eth1 slave-type bond master bond0
- nmcli connection add type ethernet ifname eth2 slave-type bond master bond0
- # 启用绑定接口
- nmcli connection up bond0
- # 查看绑定状态
- cat /proc/net/bonding/bond0
复制代码- # 创建VLAN接口
- nmcli connection add type vlan ifname eth0.100 dev eth0 id 100 con-name vlan100
- nmcli connection modify vlan100 ipv4.addresses 192.168.100.100/24
- nmcli connection modify vlan100 ipv4.gateway 192.168.100.1
- nmcli connection modify vlan100 ipv4.method manual
- # 启用VLAN接口
- nmcli connection up vlan100
复制代码- # 安装网桥工具
- yum install bridge-utils
- # 创建网桥
- nmcli connection add type bridge ifname br0 con-name br0
- nmcli connection modify br0 ipv4.addresses 192.168.1.150/24
- nmcli connection modify br0 ipv4.gateway 192.168.1.1
- nmcli connection modify br0 ipv4.method manual
- # 添加接口到网桥
- nmcli connection add type ethernet ifname eth0 master br0
- # 启用网桥
- nmcli connection up br0
- # 查看网桥状态
- brctl show
复制代码
4.3 网络故障排除
- # 测试网络连通性
- ping 8.8.8.8
- ping -c 4 example.com
- # 跟踪网络路径
- traceroute 8.8.8.8
- tracepath example.com
- # 查看路由表
- ip route show
- netstat -rn
- # 查看网络连接
- netstat -tuln
- ss -tuln
- # 查看网络接口统计信息
- ip -s link show eth0
- cat /proc/net/dev
复制代码- # 安装网络诊断工具
- yum install tcpdump nmap-ncat mtr nmap
- # 使用tcpdump捕获网络数据包
- tcpdump -i eth0 -n -c 10
- tcpdump -i eth0 port 80 -w capture.pcap
- # 使用ncat进行网络连接测试
- nc -zv 192.168.1.1 80
- nc -l -p 8080
- nc 192.168.1.100 8080
- # 使用mtr进行网络诊断
- mtr 8.8.8.8
- # 使用nmap进行端口扫描
- nmap -sT -p- 192.168.1.100
- nmap -sU -p 53,67,68 192.168.1.100
复制代码- # 安装网络性能测试工具
- yum install iperf3 netperf
- # 使用iperf3测试网络带宽
- # 服务器端
- iperf3 -s
- # 客户端
- iperf3 -c 192.168.1.100 -t 60
- # 使用netperf测试网络性能
- # 服务器端
- netserver
- # 客户端
- netperf -H 192.168.1.100 -t TCP_STREAM -l 60
- netperf -H 192.168.1.100 -t UDP_STREAM -l 60
复制代码
5. 故障排除
故障排除是系统管理员的核心技能之一。本节将介绍如何诊断和解决RHEL系统中的常见问题。
5.1 系统启动问题
- # 查看GRUB配置
- cat /etc/default/grub
- cat /boot/grub2/grub.cfg
- # 重新生成GRUB配置
- grub2-mkconfig -o /boot/grub2/grub.cfg
- # 安装GRUB到MBR
- grub2-install /dev/sda
- # 进入救援模式
- # 1. 重启系统并在GRUB菜单按'e'编辑启动参数
- # 2. 在linux行末尾添加 'rd.break'
- # 3. 按'Ctrl+X'启动
- # 4. 在救援模式下执行以下命令
- # 挂载根文件系统
- mount -o remount,rw /sysroot
- chroot /sysroot
- # 修复SELinux上下文
- touch /.autorelabel
- exit
- reboot
复制代码- # 查看系统启动日志
- journalctl -b
- journalctl -b -p err
- journalctl -b -1 # 查看上次启动的日志
- # 查看服务启动失败原因
- systemctl --failed
- systemctl status <failed-service>
- journalctl -u <failed-service>
- # 重置失败的服务
- systemctl reset-failed <failed-service>
- # 分析启动时间
- systemd-analyze
- systemd-analyze blame
- systemd-analyze critical-chain
复制代码
5.2 文件系统问题
- # 查看磁盘使用情况
- df -h
- df -i # 查看inode使用情况
- # 查找大文件
- find / -type f -size +100M -exec ls -lh {} \;
- find / -type f -size +100M -exec du -h {} \;
- # 查找大目录
- du -sh /var/* | sort -rh
- du -sh /home/* | sort -rh
- # 清理旧日志
- journalctl --vacuum-size=100M
- logrotate -f /etc/logrotate.conf
复制代码- # 检查文件系统
- fsck -n /dev/sda1 # 只检查,不修复
- # 修复文件系统
- # 首先卸载文件系统
- umount /dev/sda1
- # 修复ext4文件系统
- fsck -y /dev/sda1
- # 修复XFS文件系统
- xfs_repair -n /dev/sda1 # 只检查,不修复
- xfs_repair /dev/sda1 # 修复文件系统
复制代码
5.3 性能问题
- # 查看CPU使用情况
- top
- htop
- mpstat 1 5
- # 查看CPU负载
- uptime
- cat /proc/loadavg
- # 查看CPU使用率高的进程
- ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head
- # 查看进程的线程
- ps -eLf
- ps -T -p <PID>
- top -H -p <PID>
复制代码- # 查看内存使用情况
- free -h
- cat /proc/meminfo
- # 查看内存使用率高的进程
- ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head
- # 查看进程的内存映射
- pmap <PID>
- # 查看系统内存统计信息
- vmstat 1 5
- sar -r 1 5
复制代码- # 查看磁盘I/O统计
- iostat -xz 1 5
- iotop
- # 查看进程的I/O统计
- pidstat -d 1 5
- # 查看块设备统计
- cat /proc/diskstats
- lsblk
复制代码
5.4 网络问题
- # 检查网络接口状态
- ip addr show
- ip link show
- # 检查网络连接
- ping 8.8.8.8
- ping -c 4 example.com
- # 检查DNS解析
- nslookup example.com
- dig example.com
- host example.com
- # 检查路由
- ip route show
- traceroute 8.8.8.8
- tracepath example.com
复制代码- # 检查网络服务状态
- systemctl status network
- systemctl status NetworkManager
- # 检查网络连接
- netstat -tuln
- ss -tuln
- # 检查防火墙状态
- firewall-cmd --state
- firewall-cmd --list-all
- # 检查SELinux状态
- sestatus
- getsebool -a | grep http
复制代码
6. 系统管理员实战案例
本节将通过一些实际案例,展示如何应用前面介绍的知识解决实际问题。
6.1 案例一:Web服务器性能优化
一个运行Apache的Web服务器在高峰期响应缓慢,需要优化性能。
- # 1. 检查系统资源使用情况
- top
- free -h
- iostat -xz 1 5
- # 2. 检查Apache配置
- cat /etc/httpd/conf/httpd.conf | grep -E "^KeepAlive|^MaxKeepAliveRequests|^KeepAliveTimeout|^ServerLimit|^MaxClients"
- # 3. 优化Apache配置
- cat >> /etc/httpd/conf/httpd.conf << EOF
- # 性能优化配置
- KeepAlive On
- MaxKeepAliveRequests 100
- KeepAliveTimeout 5
- <IfModule prefork.c>
- ServerLimit 256
- MaxClients 256
- StartServers 10
- MinSpareServers 10
- MaxSpareServers 30
- MaxRequestsPerChild 4000
- </IfModule>
- EOF
- # 4. 启用Apache缓存模块
- yum install mod_cache
- cat >> /etc/httpd/conf.d/cache.conf << EOF
- LoadModule cache_module modules/mod_cache.so
- LoadModule cache_disk_module modules/mod_cache_disk.so
- <IfModule mod_cache_disk.c>
- CacheEnable disk /
- CacheRoot /var/cache/httpd
- CacheDirLevels 2
- CacheDirLength 1
- </IfModule>
- EOF
- # 5. 优化系统内核参数
- cat >> /etc/sysctl.conf << EOF
- # 网络优化
- net.ipv4.tcp_fin_timeout = 30
- net.ipv4.tcp_keepalive_time = 1200
- net.ipv4.tcp_max_syn_backlog = 8192
- net.ipv4.tcp_max_tw_buckets = 5000
- net.ipv4.tcp_tw_reuse = 1
- net.ipv4.tcp_tw_recycle = 1
- net.core.netdev_max_backlog = 65535
- net.core.somaxconn = 65535
- # 文件系统优化
- vm.swappiness = 10
- vm.dirty_ratio = 60
- vm.dirty_background_ratio = 2
- EOF
- # 6. 应用配置并重启服务
- sysctl -p
- systemctl restart httpd
- # 7. 监控性能
- yum install htop iotop
- htop
- iotop
复制代码
6.2 案例二:数据库服务器安全加固
一个运行MySQL的数据库服务器需要加强安全性,防止未授权访问和数据泄露。
- # 1. 更新系统
- yum update -y
- # 2. 安装MySQL
- yum install mysql-server -y
- systemctl start mysqld
- systemctl enable mysqld
- # 3. 运行MySQL安全脚本
- mysql_secure_installation
- # 4. 配置MySQL
- cat > /etc/my.cnf << EOF
- [mysqld]
- # 安全配置
- skip-symbolic-links
- local-infile=0
- skip-show-database
- max_connect_errors=10
- max_user_connections=25
- # 网络配置
- bind-address=127.0.0.1
- port=3306
- # 日志配置
- log-error=/var/log/mysqld.log
- slow_query_log=1
- slow_query_log_file=/var/log/mysql-slow.log
- long_query_time=2
- # 性能配置
- innodb_buffer_pool_size=2G
- innodb_log_file_size=256M
- innodb_log_buffer_size=8M
- innodb_flush_log_at_trx_commit=2
- innodb_flush_method=O_DIRECT
- innodb_file_per_table=1
- EOF
- # 5. 重启MySQL服务
- systemctl restart mysqld
- # 6. 配置防火墙
- firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="mysql" accept'
- firewall-cmd --reload
- # 7. 配置SELinux
- setsebool -P mysql_connect_any on
- setsebool -P daemons_enable_cluster_mode on
- # 8. 创建数据库用户并授权
- mysql -u root -p
- CREATE DATABASE myapp;
- CREATE USER 'myappuser'@'localhost' IDENTIFIED BY 'strongpassword';
- GRANT ALL PRIVILEGES ON myapp.* TO 'myappuser'@'localhost';
- FLUSH PRIVILEGES;
- EXIT;
- # 9. 配置数据库备份
- yum install mariadb-backup
- cat > /etc/cron.daily/mysql-backup << EOF
- #!/bin/bash
- DATE=\$(date +%Y%m%d)
- mkdir -p /backup/mysql
- mysqldump --all-databases --single-transaction --routines --triggers | gzip > /backup/mysql/mysql-backup-\$DATE.sql.gz
- find /backup/mysql -name "*.sql.gz" -mtime +7 -delete
- EOF
- chmod +x /etc/cron.daily/mysql-backup
- # 10. 配置审计规则
- echo "-w /etc/my.cnf -p wa -k mysql_config" >> /etc/audit/rules.d/audit.rules
- echo "-w /var/lib/mysql -p wa -k mysql_data" >> /etc/audit/rules.d/audit.rules
- systemctl restart auditd
复制代码
6.3 案例三:高可用性集群配置
需要配置一个高可用性的Web服务集群,确保在节点故障时服务不中断。
- # 1. 在所有节点上安装必要的软件
- yum install -y pacemaker pcs fence-agents-all resource-agents
- # 2. 设置hacluster用户密码
- echo "hacluster:StrongPassword" | chpasswd
- # 3. 启动并启用pcsd服务
- systemctl start pcsd
- systemctl enable pcsd
- # 4. 认证集群节点
- pcs cluster auth node1 node2 node3 -u hacluster -p StrongPassword
- # 5. 创建集群
- pcs cluster setup --name mycluster node1 node2 node3
- # 6. 启动集群
- pcs cluster start --all
- pcs cluster enable --all
- # 7. 查看集群状态
- pcs status
- # 8. 禁用STONITH(用于测试环境,生产环境应配置STONITH)
- pcs property set stonith-enabled=false
- # 9. 配置浮动IP
- pcs resource create vip ocf:heartbeat:IPaddr2 ip=192.168.1.100 cidr_netmask=24 op monitor interval=20s
- # 10. 配置Web服务
- pcs resource create webserver systemd:httpd op monitor interval=20s
- # 11. 配置资源约束
- pcs constraint colocation add webserver with vip
- pcs constraint order vip then webserver
- # 12. 配置文件系统(如果需要共享存储)
- # 假设使用NFS共享存储
- yum install -y nfs-utils
- mkdir -p /var/www/html
- echo "192.168.1.200:/share /var/www/html nfs defaults 0 0" >> /etc/fstab
- mount -a
- # 13. 配置文件系统资源
- pcs resource create websitefs Filesystem device="192.168.1.200:/share" directory="/var/www/html" fstype="nfs" op monitor interval=20s
- # 14. 更新资源约束
- pcs constraint colocation add websitefs with vip
- pcs constraint order vip then websitefs
- pcs constraint colocation add webserver with websitefs
- pcs constraint order websitefs then webserver
- # 15. 查看集群状态
- pcs status
- # 16. 测试故障转移
- # 在node1上停止pacemaker服务
- systemctl stop pacemaker
- # 在其他节点上查看集群状态
- pcs status
复制代码
6.4 案例四:系统备份与恢复
需要为关键服务器配置可靠的备份策略,并测试恢复流程。
结论
Red Hat Enterprise Linux作为企业级服务器操作系统,提供了强大的功能和灵活性。通过本文详细介绍的高级配置、性能优化、安全加固、网络配置和故障排除技术,系统管理员可以更好地管理和维护RHEL服务器。
在实际工作中,系统管理员需要不断学习和实践,结合具体的应用场景和需求,灵活运用这些技术,以确保服务器的稳定、安全和高效运行。同时,定期进行系统备份和测试恢复流程也是保障业务连续性的重要措施。
希望本文能够为系统管理员提供实用的指导和参考,帮助他们更好地应对日常工作中的挑战。 |
|