RHCE实验(对于个人开发者的虚拟机需要先配置镜像源):
1、FTP (端口默认21/tcp/udp)多用于文件传输
两种模式
主动模式:服务器使用 20 端口主动连接客户端进行数据传输。
被动模式:客户端主动连接服务器的随机端口进行数据传输。
先使用yum安装vsftpd包
centos stream 9必须要换源BaseOS和AppStream
dnf clean all
修改配置文件进行配置 /etc/vsftpd/vsftp.conf
anonymous_enable 允许匿名用户登录
local_enable 允许本地登录
write_enable 允许上传
chroot_local_enable将用户锁定在家目录
anon_upload_enable 允许匿名用户上传
centos stream9可能需要打开httpd的两个选项
如何禁止root用户登录
在vsftpd.conf文件中添加或者修改
userlist_deny=NO
userlist_enable=YES
userlist_file=/etc/vsftpd/user_list
vsftpd.user_list文件中写入要禁止登陆的用户
比如root
vsftpd家目录为/var/ftp
如果无法登录或者无法上传文件,就是没关闭selinux
setsebool -P tftp_home_dir 1 允许用户访问家目录
setsebool -P ftpd_full_access 1 允许用户上传文件
firewall-cmd –permanent –add-service=ftp打开防火墙
firewall-cmd –reload 重新加载防火墙
添加ftp用户
useradd
passwd
usermod -s /sbin/nologin ftpuser 防止ftp用户进行ssh登录
启动ftp
systemctl start vsftpd
systemctl enable vsftpd
使用ftp链接管理文件
2、NFS(network file system) 网络共享文件系统(默认端口2049/tcp/udp,111/tcp/udp,20048)
安全性差,没有用户认证;对windows支持很差,高并发性能一般
- NFS 主端口:2049
- rpcbind 端口:111
- mount端口:20048
一、NFS服务器配置
1. 安装NFS服务组件
1
| dnf install nfs-utils rpcbind nfs4-acl-tools -y
|
- nfs-utils:包含NFS服务器守护进程、RPC支持程序和管理工具
- rpcbind:管理RPC连接,NFS需要RPC服务进行端口映射
- nfs4-acl-tools:提供NFSv4访问控制列表管理工具
2. 创建共享目录并设置权限
1 2 3
| mkdir -p /data/nfs_share chown -R nobody:nobody /data/nfs_share chmod -R 755 /data/nfs_share
|
- nobody:nobody:NFS默认匿名用户,确保客户端能正常访问
- 若需允许客户端写入,确保目录权限正确设置
3. 配置NFS导出规则(/etc/exports)
编辑主配置文件:
添加共享规则(示例):
1 2 3
| /data/nfs_share 192.168.0.0/24(rw,sync,no_subtree_check) # 允许192.168.0.0/24网段读写 /data/nfs_share 192.168.0.100(ro,sync,no_subtree_check) # 允许特定IP只读访问 /data/nfs_share *(rw,sync,no_root_squash) # 允许所有主机读写(生产环境不建议)
|
常用参数说明:
| 参数 |
功能 |
| rw |
允许读写访问 |
| ro |
只读访问 |
| sync |
数据同步写入磁盘(默认,安全性高) |
| async |
异步写入,性能高但断电可能丢失数据 |
| no_subtree_check |
禁用子树检查,提高性能 |
| no_root_squash |
保留客户端root权限(谨慎使用) |
| root_squash |
将客户端root映射为匿名用户(默认) |
4. 生效配置并启动服务
1 2 3
| exportfs -r systemctl enable --now nfs-server rpcbind systemctl status nfs-server
|
5. 防火墙配置(关键步骤)
1 2 3 4 5
| firewall-cmd --permanent --add-service=nfs firewall-cmd --permanent --add-service=rpc-bind firewall-cmd --permanent --add-service=mountd firewall-cmd --reload firewall-cmd --list-all
|
firewall-cmd –permanent –add-port={2049,111}/tcp –add-port={2049,111}/udp
firewall-cmd –permanent –add-port=20048/tcp –add-port=20048/udp # mountd端口
1 2 3 4
| #### 6. 验证NFS服务器配置 ```bash exportfs -v # 查看已导出的NFS共享 showmount -e localhost # 查看本地NFS共享列表
|
二、NFS客户端配置
1. 安装NFS客户端工具
1
| dnf install nfs-utils -y
|
2. 手动挂载NFS共享
1 2 3
| mkdir -p /mnt/nfs_mount mount -t nfs 192.168.0.5:/data/nfs_share /mnt/nfs_mount df -h
|
- 192.168.0.5:替换为你的NFS服务器IP地址
- /data/nfs_share:服务器共享目录路径
- /mnt/nfs_mount:客户端本地挂载点(可自定义)
3. 设置开机自动挂载(/etc/fstab)
编辑fstab文件:
添加以下内容:
1
| 192.168.0.5:/data/nfs_share /mnt/nfs_mount nfs defaults 0 0
|
- defaults:使用默认挂载选项(rw,suid,dev,exec,auto,nouser,async)
- 测试自动挂载:
4. 自动挂载(AutoFS)配置(可选)
自动挂载在访问时才挂载,闲置时自动卸载,适合非频繁访问的共享:
1 2
| dnf install autofs -y vi /etc/auto.master
|
添加:
1
| /- /etc/auto.nfs # 直接映射方式
|
创建自动挂载配置文件:
添加:
1
| /mnt/nfs_mount -fstype=nfs 192.168.0.5:/data/nfs_share
|
启动服务:
1
| systemctl enable --now autofs
|
3、DNS(默认端口53/udp) 将域名解析为IP地址
先安装dns服务 yum install -y bind bind-utils
修改主配置文件/etc/named.conf
listen-on port 53{any;};
allow-query {any;}
在末尾添加区域配置
zone “example.com” IN{
type master
file “/var/named/example.com.zone”
}
创建区域数据文件
cat > /var/named/example.com.zone << EOF
$TTL 86400
@ IN SOA ns.example.com. admin.example.com. (
2026030701 序列号
3600 刷新时间
1800 重试时间
604800 过期时间
86400其他服务器缓存时间
)
@ IN NS ns.example.com.
ns IN A 192.168.142.10
www IN A 192.168.142.10
EOF
改权限
chown root:named /var/named/example.com.zone
chmod 644 /var/named/example.com.zone
检察配置是否正确
named-checkconf 不报错就没问题
named-checkzone example.com /var/named/example.com.zone 出现ok就没问题
启动并设置开机自启
systemctl start named
systemctl enable named
关闭防火墙或者放行防火墙dns权限
测试dns是否生效
nslookup www.example.com 192.168.142.10
4、Samba 让windows和linux文件和打印机共享
- 139/tcp
- 445/tcp(最核心、最常用)
- 137/udp
- 138/udp
一、安装 Samba
1
| dnf install -y samba samba-common samba-client
|
二、创建共享目录 & 权限
1 2 3
| mkdir -p /srv/samba/share chmod -R 777 /srv/samba/share chown -R nobody:nobody /srv/samba/share
|
三、修改主配置 /etc/samba/smb.conf
先备份:
1
| cp /etc/samba/smb.conf /etc/samba/smb.conf.bak
|
编辑配置:
直接用下面最简稳定配置(替换原文件内容)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| [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
|
四、检查配置语法
没报错就继续。
五、启动 & 开机自启
1 2
| systemctl enable --now smb nmb systemctl status smb
|
六、防火墙放行(必须做)
1 2
| firewall-cmd --permanent --add-service=samba firewall-cmd --reload
|
七、SELinux 放行(不做一定访问失败)
1 2
| setsebool -P samba_enable_home_dirs on chcon -t samba_share_t /srv/samba/share -R
|
八、创建 Samba 用户(可选,用于密码访问)
Samba 用户必须先有系统用户:
1 2
| useradd smbuser smbpasswd -a smbuser
|
九、Windows 访问
打开文件管理器,输入:
例:
十、Linux 客户端访问
1
| mount -t cifs //192.168.100.10/share /mnt -o username=smbuser,password=你的密码
|
最常用 3 条命令
1. 查看远程主机的共享列表
1 2 3 4 5
| smbclient -L //192.168.x.x -U 用户名 ``
```bash smbclient //192.168.x.x/share -U 用户名
|
5、NTP(network time protocol)网络时间协议/Chrony(默认端口123/udp) 同步时间
1. 精度与速度
ntp:同步慢,资源占用较高,需要较好的网络环境,配置文件在/etc/ntp.conf,查看命令ntpq -p
chrony:同步极快,精度很高,资源占用低,对网络环境的要求不是很高,配置文件在/etc/chrony.conf,查看命令chronyc sources
2. 安装 NTP
3. 编辑配置文件
把里面的 server 换成国内 NTP 服务器(推荐)
1 2 3
| server ntp.aliyun.com iburst server ntp1.aliyun.com iburst server ntp2.aliyun.com iburst
|
允许内网同步(可选,根据需要加)
1
| restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
|
4. 启动并开机自启
1 2
| systemctl start ntpd systemctl enable ntpd
|
5. 查看同步状态
看到有 * 号开头的服务器 = 同步成功。
6. 立即强制同步(可选)
先停服务 → 同步 → 再启动
1 2 3
| systemctl stop ntpd ntpdate ntp.aliyun.com systemctl start ntpd
|
7. 防火墙放行 NTP(UDP 123)
1
| firewall-cmd --permanent --add-service=ntp
|
一、安装 Chrony
二、Chrony 服务端配置(给别人同步时间)
1. 修改配置
主要改 2 处:
1 2 3 4 5
| allow 192.168.0.0/24
local stratum 10
|
2. 启动并开机自启
1
| systemctl enable --now chronyd
|
3. 防火墙放行 NTP(UDP 123)
1 2
| firewall-cmd --permanent --add-service=ntp firewall-cmd --reload
|
三、Chrony 客户端配置(同步服务端时间)
1. 修改配置
注释掉原有 pool,写上你的 NTP 服务器 IP
1 2
| server 192.168.0.100 iburst
|
2. 重启服务
1
| systemctl restart chronyd
|
四、验证是否同步成功
1 2 3 4 5 6
| chronyc makestep
chronyc tracking
chronyc sources -v
|
看到 ^* 或 ^? 变成 ^* 且有 IP,就是同步成功。
看系统时间:
6、http/https(默认端口80/443) 搭建网站
一、安装 httpd + SSL 模块
1
| dnf install -y httpd mod_ssl
|
二、先配置 HTTP(80端口)
1. 启动并开机自启
1 2
| systemctl enable --now httpd systemctl status httpd
|
2. 防火墙放行 80/443
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
|
4. 访问测试
浏览器打开:
三、配置 HTTPS(443端口,SSL)
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. 检查 SSL 配置
CentOS 自带 /etc/httpd/conf.d/ssl.conf,一般不用改,默认就能跑。
3. 重启 httpd
4. HTTPS 访问
浏览器提示不安全 → 继续访问即可(自签名证书正常现象)。
四、SELinux 放行(不做必报错)
1 2 3
| chown -R apache:apache /var/www/html chmod -R 755 /var/www/html
|
五、常用验证命令
1 2 3 4 5 6 7 8
| httpd -t
ss -tulnp | grep httpd
systemctl status httpd
|
7、DHCP(默认使用67/68/udp端口) ip地址自动分配
一、安装 DHCP 服务
1
| dnf install -y dhcp-server
|
二、最关键一步:复制模板配置
默认配置文件是空的,必须先复制示例文件,否则启动必失败:
1
| cp /usr/share/doc/dhcp-server/dhcpd.conf.example /etc/dhcp/dhcpd.conf
|
三、编辑 DHCP 配置
把下面内容替换掉原有配置(只改 IP 网段即可)
示例网段:192.168.100.0(你按自己 VMware 网段改)
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 }
|
修改 /etc/sysconfig/dhcpd
添加DHCPDARGS=”网卡名”
四、启动并开机自启
1
| systemctl enable --now dhcpd
|
五、防火墙放行 DHCP(UDP 67 端口)
1 2
| firewall-cmd --permanent --add-service=dhcp firewall-cmd --reload
|
六、检查是否正常运行
出现 active (running) 就成功了。
七、Windows 客户端测试
- 把 Windows 网络设为 自动获取 IP
- 执行:
1 2
| ipconfig /release ipconfig /renew
|
CentOS Stream 10 安装 DHCP 服务
1.1 安装 kea
- 核心包说明:
kea.x86_64:DHCPv4/v6 服务主程序
kea-libs.x86_64:依赖库(自动安装)
1.2 基础配置(DHCPv4 示例)
1 2 3 4 5
| cp /etc/kea/kea-dhcp4.conf /etc/kea/kea-dhcp4.conf.bak
vi /etc/kea/kea-dhcp4.conf
|
写入最简配置(替换为你的网段):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| { "Dhcp4": { "interfaces-config": { "interfaces": ["ens33"] # 替换为你的网卡名(如 eth0、ens160) }, "lease-database": { "type": "memfile", "persist": true, "name": "/var/lib/kea/kea-leases4.csv" }, "subnet4": [ { "subnet": "192.168.10.0/24", # 你的网段 "pools": [{"pool": "192.168.10.100 - 192.168.10.200"}], # 地址池 "option-data": [ { "name": "routers", "data": "192.168.10.1" # 网关 }, { "name": "domain-name-servers", "data": "223.5.5.5,8.8.8.8" # DNS 服务器 }, { "name": "subnet-mask", "data": "255.255.255.0" # 子网掩码 } ], "valid-lifetime": 3600, # 地址租期(秒) "renew-timer": 1800, "rebind-timer": 2700 } ], "loggers": [ { "name": "kea-dhcp4", "severity": "INFO", "output_options": [ { "output": "stdout" } ] } ] } }
|
1.3 启动并开机自启
1 2 3 4
| systemctl enable --now kea-dhcp4
systemctl status kea-dhcp4
|
1.4 防火墙放行
1 2
| firewall-cmd --permanent --add-port=67/udp firewall-cmd --reload
|
1.5 验证服务
1 2 3 4
| ss -ulnp | grep kea
dhclient -v 网卡名
|
9、sendmail(常用25/tcp,465/tcp,587/tcp端口) 发送和转发电子邮件
一、安装 Sendmail + 邮件工具
1
| dnf install -y sendmail sendmail-cf mailx
|
mailx在9以上的系统中被替代为s-nail
二、修改监听地址(允许局域网连接)
1
| vi /etc/mail/sendmail.mc
|
找到这一行:
1
| DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl
|
改成 监听所有IP:
1
| DAEMON_OPTIONS(`Port=smtp,Addr=0.0.0.0, Name=MTA')dnl
|
(centos9及以上)配置s-nail
1 2
| set smtp=smtp.example.com set smtp-auth-user=your_email@example.com set smtp-auth-password=your_password set from=your_email@example.com
|
三、重新生成主配置文件(必须做)
1
| m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
|
四、配置本机域名(直接复制)
1 2 3 4
| cat >> /etc/mail/local-host-names << EOF localhost localhost.localdomain EOF
|
五、启动服务
1
| systemctl enable --now sendmail sm-client
|
六、防火墙放行 SMTP(25端口)
1 2
| firewall-cmd --permanent --add-service=smtp firewall-cmd --reload
|
七、测试发送邮件(最简单验证)
1
| echo "这是一封测试邮件" | mail -s "Sendmail 测试" root
|
查看邮件:
按 Enter 查看,q 退出。
9.1 postfix邮件服务
一、安装 postfix
二、停止并禁用 sendmail(如果有)
CentOS 里可能自带 sendmail,会冲突:
1 2
| systemctl stop sendmail systemctl disable sendmail
|
三、编辑主配置文件
1
| vim /etc/postfix/main.cf
|
把下面几项改成对应内容(找到并修改)
1 2 3 4 5 6 7 8 9 10 11 12
| myhostname = centos.localdomain
mydomain = localdomain
myorigin = $mydomain
inet_interfaces = all
mynetworks = 127.0.0.0/8, 192.168.0.0/16, 10.0.0.0/8
home_mailbox = Maildir/
|
如果你不知道主机名,直接用:
四、检查配置是否有错
没输出就是正常。
五、启动并开机自启
1 2
| systemctl start postfix systemctl enable postfix
|
查看状态:
1
| systemctl status postfix
|
六、关闭防火墙或开放 smtp 端口(25)
1 2
| firewall-cmd --permanent --add-service=smtp firewall-cmd --reload
|
七、安装 mailx 工具(用来发邮件测试)
发送测试邮件
1
| echo "测试邮件内容" | mail -s "测试标题" root
|
查看邮件
t 1 查看、d 1 删除、h 列表、q 退出
或
1
| cat /root/Maildir/new/文件名
|
八、常见问题排查
1. 看邮件日志
1
| tail -f /var/log/maillog
|
2. 重新加载配置
1
| systemctl reload postfix
|
3. 清空邮件队列
8、iscsi (3260/TCP)把远程储存设备当作本地硬盘使用
一、服务端(Target)配置(提供存储)
1. 安装 targetcli 工具
1 2 3
| yum install -y targetcli systemctl enable target systemctl start target
|
2. 准备后端存储(可选,推荐 LVM)
1 2 3 4
| pvcreate /dev/sdb vgcreate iscsi_vg /dev/sdb lvcreate -n iscsi_lv -L 100G iscsi_vg
|
3. 进入 targetcli 交互配置
3.1 创建后端存储对象(创建一个块设备文件)
1 2 3
| cd /backstores/block create iscsi_disk /dev/iscsi_vg/iscsi_lv
|
3.2 创建 iSCSI Target(IQN)(将linux变成远程硬盘服务器)
1 2 3
| cd /iscsi create iqn.2026-03.com.example:target01
|
3.3 绑定 LUN(将存储映射给 Target)(把硬盘挂在共享出口上)
1 2
| cd /iscsi/iqn.2026-03.com.example:target01/tpg1/luns create /backstores/block/iscsi_disk
|
3.4 设置 ACL(acccess control list)(允许客户端访问)
1 2 3
| cd ../acls
create iqn.2026-03.com.example:initiator01
|
3.5 保存并退出
4. 防火墙与 SELinux(可选)
1 2 3 4 5 6
| firewall-cmd --add-port=3260/tcp --permanent firewall-cmd --reload
setenforce 0
|
二、客户端(Initiator)配置(使用存储)
1. 安装 initiator 工具
1 2 3
| yum install -y iscsi-initiator-utils systemctl enable iscsid iscsi systemctl start iscsid iscsi
|
2. 配置 Initiator IQN(必须与服务端 ACL 匹配)
1 2 3 4 5
| echo "InitiatorName=iqn.2026-03.com.example:initiator01" > /etc/iscsi/initiatorname.iscsi
systemctl restart iscsid iscsi
|
3. 发现 Target
4. 登录 Target
1 2 3 4 5
| iscsiadm -m node -L all
|
5. 验证与使用
1 2 3 4 5 6 7 8 9
| lsblk
parted /dev/sdb mklabel gpt parted /dev/sdb mkpart primary 0% 100% mkfs.xfs /dev/sdb1 mkdir -p /mnt/iscsi mount /dev/sdb1 /mnt/iscsi
|
6. 开机自动挂载(关键:加 _netdev)
1 2 3 4 5 6 7 8
| blkid /dev/sdb1
UUID=xxxx-xxxx /mnt/iscsi xfs defaults,_netdev 0 0
mount -a
|
三、常用管理命令
服务端
1 2
| targetcli ls systemctl status target
|
客户端
1 2 3
| iscsiadm -m session -o show iscsiadm -m node -U all iscsiadm -m node -o delete
|
10、ansible 批量管理服务器,自动执行配置任务
一、控制节点(管理端)配置
1. 安装 Ansible(先装 EPEL 源)
1 2 3 4 5 6 7 8
| dnf install -y epel-release
dnf install -y ansible-core 7使用 dnf install -y ansible
ansible --version
|
在 CentOS 8 / RHEL 8 上通过 pip3 安装 Ansible
1. 安装 Python 3.8 pip(系统自带,无需额外源)
dnf install -y python3 python3-pip
2. 升级 pip 并安装 Ansible 2.15.0
pip3 install –upgrade pip
pip3 install ansible==2.5.11
3. 创建软链接,让系统能找到 ansible 命令
ln -s /usr/local/bin/ansible /usr/bin/ansible
ln -s /usr/local/bin/ansible-playbook /usr/bin/ansible-playbook
4. 验证
ansible –version
2. 生成 SSH 密钥(免密登录基础)
1 2
| ssh-keygen -t rsa -b 2048
|
3. 把密钥拷贝到被管理节点(替换为目标IP/用户名)
1 2 3
| ssh-copy-id root@192.168.142.20
|
多台被管理节点就执行多次,替换IP即可。
4. 配置 Ansible 主机清单(定义被管理节点)
添加被管理节点(支持分组,示例分 web 组):
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=root ansible_ssh_port=22 ansible_become=yes ansible_become_method=sudo ansible_become_user=root
|
被管理节点
1 2 3 4 5 6 7 8
| vi /etc/ssh/sshd_config
# 确保以下配置: PasswordAuthentication yes # 分发公钥时需要密码验证,分发后可改为no PermitRootLogin yes # 允许root用户登录(根据环境安全策略调整)
# 重启sshd服务 systemctl restart sshd
|
5. 测试连通性(核心验证)
1 2 3 4 5
| ansible all -m ping
ansible all -m command -a "cat /etc/redhat-release"
|
输出 SUCCESS + pong 就是连通成功!
二、被管理节点(客户端)仅需2步(无需装Ansible)
1 2 3 4 5 6 7 8
| dnf install -y openssh-server systemctl enable --now sshd
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
|
三、Ansible 常用命令(立刻能用)
1 2 3 4 5 6 7 8 9 10 11
| 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"
|
验证 Ansible 是否配置成功
1 2 3 4 5
| ansible all --list-hosts
ansible all -m shell -a "hostname"
|
能输出所有被管理节点的主机名,就是配置成功!
编写playbook文件
install_httpd.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| - name: Install and start httpd service on webservers hosts: webservers # 目标主机组 gather_facts: yes # 收集主机信息(可选,推荐开启) become: yes # 提权(使用sudo,这里用root可省略) tasks: - name: Install httpd package yum: name: httpd state: present # 安装httpd
- name: Start and enable httpd service service: name: httpd state: started # 启动服务 enabled: yes # 设置开机自启
|
执行
1
| ansible-playbook install_httpd.yml
|
验证
1
| ansible webservers -m command -a "systemctl status httpd"
|
NFS(Network File System)
- 共享内容:文件夹
- 适合:Linux ↔ Linux
- 特点:速度快、配置简单、无认证
- 场景:服务器之间共享文件
Samba
- 共享内容:文件夹
- 适合:Linux ↔ Windows、Linux ↔ Linux
- 特点:跨平台、有用户密码、兼容 Windows
- 场景:办公文件共享
iSCSI
- 共享内容:整块硬盘 / 分区(块设备)
- 适合:把远程硬盘当本地硬盘用
- 特点:速度最快、像本地磁盘、可格式化、可建分区
- 场景:虚拟机存储、数据库存储、备份盘
在linux中,dmesg记录内核启动到现在 ,所有硬件,驱动,和系统底层发生的事
如何升级ssh
完整升级流程
- 备份
1 2 3
| cp -r /etc/ssh /etc/ssh.bak.$(date +%Y%m%d) cp /usr/sbin/sshd /usr/sbin/sshd.bak cp /usr/bin/ssh /usr/bin/ssh.bak
|
- 安装依赖
1
| yum install -y gcc gcc-c++ make pam-devel zlib-devel wget tar perl
|
- 升级 OpenSSL 到 1.1.1w(必须)
1 2 3 4 5 6 7 8 9 10 11
| cd /usr/local/src wget https://www.openssl.org/source/openssl-1.1.1w.tar.gz tar zxf openssl-1.1.1w.tar.gz cd openssl-1.1.1w
./config --prefix=/usr --openssldir=/etc/ssl shared zlib make -j4 make install
ldconfig openssl version
|
出现 OpenSSL 1.1.1w 即为成功。
- 编译安装 OpenSSH 9.8p1
1 2 3 4 5 6 7 8
| cd /usr/local/src wget https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-9.8p1.tar.gz tar zxf openssh-9.8p1.tar.gz cd openssh-9.8p1
./configure --prefix=/usr \ --sysconfdir=/etc/ssh \ --with-pam
|
出现 WARNING: unrecognized options: --with-md5-passwords 是正常警告,无视即可
5.1 修复私钥权限错误
1 2 3
| chmod 600 /etc/ssh/ssh_host*_key chmod 644 /etc/ssh/ssh_host*_key.pub chown root:root /etc/ssh/ssh_host*
|
5.2 修复不支持 GSSAPI 配置
1 2
| sed -i 's/^GSSAPIAuthentication/#&/' /etc/ssh/sshd_config sed -i 's/^GSSAPICleanupCredentials/#&/' /etc/ssh/sshd_config
|
5.3 重新生成主机密钥
- 检查配置并启动 SSH
1 2 3 4
| vi /etc/ssh/sshd_config
sshd -t
|
无输出 = 配置正常
1 2
| systemctl daemon-reload systemctl restart sshd
|
- 验证最终版本
将openssh,openssl源码编译为rpm包通过ansible进行批量升级
一、环境说明
- 系统:CentOS 7 x86_64
- 编译机:任意一台 CentOS 7(用来打 RPM)
- 目标:批量升级所有服务器
- 顺序:
- 编译 OpenSSL 1.1.1w → 打包 RPM
- 编译 OpenSSH 9.8p1(依赖新版 OpenSSL)→ 打包 RPM
- Ansible 批量推送并升级
- 恢复 sshd_config、修复权限、重启 sshd
二、编译机:安装依赖
1 2
| yum install -y rpm-build make gcc automake autoconf \ zlib-devel pam-devel wget perl
|
三、编译 OpenSSL 1.1.1w 并制作 RPM
1. 下载源码
1 2 3 4
| cd /root wget https://www.openssl.org/source/openssl-1.1.1w.tar.gz mkdir -p ~/rpmbuild/{SOURCES,SPECS,BUILD,RPMS,SRPMS} cp openssl-1.1.1w.tar.gz ~/rpmbuild/SOURCES/
|
2. 编写 openssl.spec
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| cat > ~/rpmbuild/SPECS/openssl11.spec <<EOF Summary: OpenSSL 1.1.1w Name: openssl11 Version: 1.1.1w Release: 1.el7 License: Apache URL: https://www.openssl.org Source0: openssl-%{version}.tar.gz
BuildRoot: %{_tmppath}/%{name}-%{version}-root BuildRequires: gcc make zlib-devel
%description OpenSSL 1.1.1 compiled for CentOS 7
%prep %setup -n openssl-%{version}
%build ./config --prefix=/usr/local/openssl11 --openssldir=/usr/local/openssl11 shared zlib make -j4
%install rm -rf %{buildroot} make install DESTDIR=%{buildroot}
%files /usr/local/openssl11/*
%post echo "/usr/local/openssl11/lib" > /etc/ld.so.conf.d/openssl11.conf ldconfig EOF
|
3. 编译 RPM
1
| rpmbuild -ba ~/rpmbuild/SPECS/openssl11.spec
|
4. 得到 RPM
1
| ~/rpmbuild/RPMS/x86_64/openssl11-1.1.1w-1.el7.x86_64.rpm
|
四、编译 OpenSSH 9.8p1 并制作 RPM
1. 先安装 OpenSSL 1.1.1w
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| cd /root wget https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-9.8p1.tar.gz cp openssh-9.8p1.tar.gz ~/rpmbuild/SOURCES/
rm -rf /usr/local/openssl11 rm -f /etc/ld.so.conf.d/openssl11.conf ldconfig
cd /root wget https://www.openssl.org/source/openssl-1.1.1w.tar.gz tar zxf openssl-1.1.1w.tar.gz cd openssl-1.1.1w
./config --prefix=/usr --openssldir=/etc/ssl --shared zlib make -j4 make install
ldconfig openssl version
|
2. 编写 openssh98.spec
注意:新版 OpenSSH 安装路径是 /usr/libexec,不是 /usr/libexec/openssh,RPM 打包时需匹配。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| cat > ~/rpmbuild/SPECS/openssh98.spec <<'EOF' Summary: OpenSSH 9.8p1 Name: openssh Version: 9.8p1 Release: 1.el7 License: BSD URL: https://www.openssh.com Source0: openssh-%{version}.tar.gz
BuildRoot: %{_tmppath}/%{name}-%{version}-root BuildRequires: gcc make zlib-devel pam-devel
%description OpenSSH 9.8p1 for CentOS 7
%prep %setup -n openssh-%{version}
%build ./configure \ --prefix=/usr \ --sysconfdir=/etc/ssh \ --with-pam \ --with-md5-passwords \ --with-tcp-wrappers \ --with-ssl=/usr
make -j4
%install rm -rf %{buildroot} make install DESTDIR=%{buildroot}
%files /etc/ssh /usr/bin /usr/sbin /usr/libexec /usr/share/man
%post systemctl daemon-reload systemctl enable sshd 2>/dev/null EOF
|
3. 编译 RPM
1
| rpmbuild -ba ~/rpmbuild/SPECS/openssh98.spec
|
4. 成功输出
1
| Wrote: /root/rpmbuild/RPMS/x86_64/openssh-9.8p1-1.el7.x86_64.rpm
|
五、本地测试升级
1 2 3 4 5 6
| rpm -Uvh --force --nodeps openssl11-1.1.1w-1.el7.x86_64.rpm openssh-9.8p1-1.el7.x86_64.rpm
chmod 600 /etc/ssh/ssh_host_*_key chmod 700 /etc/ssh
|
添加 sshd.service 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| [Unit] Description=OpenSSH server daemon After=network.target syslog.target Wants=network.target
[Service] Type=simple ExecStart=/usr/sbin/sshd -D ExecReload=/bin/kill -HUP $MAINPID ExecStop=/bin/kill $MAINPID
Restart=on-failure RestartSec=2
PrivateTmp=true
[Install] WantedBy=multi-user.target
|
1 2
| systemctl enable --now sshd systemctl status sshd
|
六、准备 Ansible 部署结构
在 Ansible 控制机 新建目录:
1 2 3 4 5 6
| /root/ssh-upgrade/ ├── hosts ├── upgrade_ssh.yml └── files/ ├── openssl11-1.1.1w-1.el7.x86_64.rpm └── openssh-9.8p1-1.el7.x86_64.rpm
|
把上面两个 RPM 放进 files/。
1. hosts 文件
1 2 3 4
| [ssh_nodes] 192.168.142.55 192.168.142.56 192.168.142.57
|
2. Ansible 剧本:upgrade_ssh.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| - name: 批量升级 OpenSSH 9.8p1 hosts: ssh_nodes gather_facts: false tasks: - name: 上传 RPM copy: src: files/openssh-9.8p1-1.el7.x86_64.rpm dest: /tmp/
- name: 备份旧配置 copy: remote_src: true src: /etc/ssh/sshd_config dest: /etc/ssh/sshd_config.bak.{{ ansible_date_time.epoch }}
- name: 强制升级 OpenSSH shell: rpm -Uvh --force /tmp/openssh-9.8p1-1.el7.x86_64.rpm
- name: 修复密钥权限 shell: | chmod 600 /etc/ssh/ssh_host*_key chmod 644 /etc/ssh/ssh_host*.pub chown -R root.root /etc/ssh
- name: 写入兼容 sshd_config copy: dest: /etc/ssh/sshd_config content: | Port 22 ListenAddress 0.0.0.0 ListenAddress :: Protocol 2 HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_ecdsa_key HostKey /etc/ssh/ssh_host_ed25519_key SyslogFacility AUTHPRIV LogLevel INFO PermitRootLogin yes PermitEmptyPasswords no PasswordAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys ChallengeResponseAuthentication no GSSAPIAuthentication no X11Forwarding no TCPKeepAlive yes ClientAliveInterval 60 ClientAliveCountMax 3 UseDNS no UsePAM yes Subsystem sftp /usr/libexec/sftp-server
- name: 重启 sshd systemd: name: sshd state: restarted daemon_reload: yes
- name: 检查版本 shell: ssh -V register: result - debug: var=result.stdout
|
七、执行批量升级
1 2
| cd /root/ssh-upgrade ansible-playbook -i hosts upgrade_ssh.yml
|
八、升级后验证
1
| ssh root@192.168.142.55 ssh -V
|
应输出:
1
| OpenSSH_9.8p1, OpenSSL 1.1.1w
|