掌握CentOS Stream云主机,轻松配置高效运维全攻略

引言
CentOS Stream是一款基于Red Hat Enterprise Linux(RHEL)的免费操作系统,旨在为开发者提供最新的上游技术,同时保证稳定性和安全性。掌握CentOS Stream云主机的配置和高效运维,对于云计算和DevOps领域的专业人士来说至关重要。本文将详细介绍如何配置和运维CentOS Stream云主机,以帮助您提高工作效率。
第一节:CentOS Stream云主机的基础配置
1.1 选择合适的服务器硬件
在选择服务器硬件时,应考虑以下因素:
处理器:选择支持虚拟化技术的处理器,如Intel的VT-x或AMD的Virt-Extension。
内存:至少8GB内存,根据实际需求可适当增加。
存储:SSD存储能提供更快的读写速度,建议使用SSD硬盘。
网络:至少一个千兆以太网接口。
1.2 安装CentOS Stream
下载CentOS Stream镜像文件。
使用虚拟机软件(如VirtualBox、VMware等)创建虚拟机,并选择下载的镜像文件进行安装。
按照安装向导进行操作,完成安装。
1.3 首次登录后的基本设置
更新系统包列表:
sudo dnf update
安装网络配置工具:
sudo dnf install net-tools
配置网络连接:
编辑
/etc/sysconfig/network-scripts/ifcfg-ens33
(ens33为网络接口名,根据实际情况修改):
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=”System eth0″
UUID=3f8a6d4e-9c2b-4c8b-8f7a-2f7c6b1c6b2e
DEVICE=”ens33″
ONBOOT=yes
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4
重启网络服务:
sudo systemctl restart network
设置系统时区和语言:
编辑
/etc/sysconfig/clock
:
ZONE=”Asia/Shanghai”
编辑
/etc/locale.conf
:
LANG=”zh_CN.UTF-8″
重启系统:
sudo systemctl reboot
第二节:CentOS Stream云主机的安全加固
2.1 禁用不必要的服务
查看所有服务:
sudo systemctl list-unit-files –type=service
禁用不必要的服务:
sudo systemctl disable <服务名称>
例如,禁用Apache服务:
sudo systemctl disable httpd
2.2 设置防火墙
安装防火墙软件:
sudo dnf install firewalld
启动和使能防火墙:
sudo systemctl start firewalld
sudo systemctl enable firewalld
创建防火墙规则:
允许SSH访问:
sudo firewall-cmd –permanent –add-port=22/tcp
sudo firewall-cmd –reload
允许HTTP访问(如果需要):
sudo firewall-cmd –permanent –add-port=80/tcp
sudo firewall-cmd –reload
2.3 设置SSH密钥认证
生成SSH密钥对:
ssh-keygen -t rsa -b 2048
将公钥复制到目标主机的
~/.ssh/authorized_keys
文件中:
ssh-copy-id -i ~/.ssh/id_rsa.pub 用户名@目标主机IP
第三节:CentOS Stream云主机的自动化部署
3.1 使用Ansible进行自动化部署
安装Ansible:
sudo dnf install ansible
编写Ansible playbook:
创建一个名为
deploy.yml
的文件,并添加以下内容:
“`yaml
—
name: 安装Apache服务
hosts: all
become: yes
tasks:
name: 安装Apache服务
apt:
name: httpd
state: present
name: 启动Apache服务
service:
name: httpd
state: started
enabled: yes
”`
执行playbook:
ansible-playbook deploy.yml
3.2 使用Docker进行容器化部署
安装Docker:
sudo dnf -y install dnf-plugins-core
sudo dnf config-manager –add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo dnf install docker-ce docker-ce-cli containerd.io
启动和使能Docker服务:
sudo systemctl start docker
sudo systemctl enable docker
部署容器:
编写Dockerfile:
FROM nginx:latest
ADD index.html /usr/share/nginx/html/index.html
构建镜像:
docker build -t mynginx .
运行容器:
docker run -d -p 8080:80 mynginx
第四节:CentOS Stream云主机的监控与日志管理
4.1 使用Nagios进行监控
安装Nagios:
sudo dnf install nagios nagios-plugins nagios-plugins-all
配置Nagios:
编辑
/etc/nagios/nagios.cfg
,配置Nagios的运行用户和组:
user = nagios
group = nagios
配置Nagios的命令执行用户:
sudo usermod -a -G nagios <执行命令的用户>
重新加载Nagios配置:
sudo systemctl restart nagios
sudo systemctl enable nagios
添加监控对象:
编辑
/etc/nagios/cgi cfg
,添加以下内容:

# Add any extra cgi directives here…

重新加载Nagios配置:
sudo systemctl restart nagios
4.2 使用Logwatch进行日志管理
安装Logwatch:
sudo dnf install logwatch
配置Logwatch:
编辑
/etc/logwatch.conf
,修改以下配置:
“`
Logwatch log file to output to
LOGFILE=”/var/log/logwatch.log”
# Logwatch log file to email
LOGFILE_EMAIL=“/var/log/logwatch-email.log”
# Logwatch output file
LOGFILE_OUTPUT=“/var/log/logwatch.html”
# Logwatch log file to save output to
LOGFILE_SAVEDIR=“/var/log/logwatch-savedir”
# Logwatch email subject
EMAIL_SUBJECT=“Logwatch report for %s”
# Logwatch email address
EMAIL_ADDRESS=“root@yourdomain.com”
# Logwatch report title
TITLE=“Logwatch Report for %s”
# Logwatch report footer
FOOTER=“This is the footer of the report”
# Logwatch report date
DATE=“Date: %s”
# Logwatch report time
TIME=“Time: %s”
# Logwatch report hostname
HOSTNAME=“Hostname: %s”
# Logwatch report username
USERNAME=“Username: %s”
# Logwatch report groupname
GROUPNAME=“Groupname: %s”
# Logwatch report logins
LOGINS=“Logins: %s”
# Logwatch report logouts
LOGOUTS=“Logouts: %s”
# Logwatch report sessions
SESSIONS=“Sessions: %s”
# Logwatch report auth success
AUTH_SUCCESS=“Auth Success: %s”
# Logwatch report auth failure
AUTH_FAILURE=“Auth Failure: %s”
# Logwatch report auth failure message
AUTH_FAILURE_MESSAGE=“Auth Failure Message: %s”
# Logwatch report auth failure reason
AUTH_FAILURE_REASON=“Auth Failure Reason: %s”
# Logwatch report auth failure program
AUTH_FAILURE_PROGRAM=“Auth Failure Program: %s”
# Logwatch report auth failure terminal
AUTH_FAILURE_TERMINAL=“Auth Failure Terminal: %s”
# Logwatch report auth failure user
AUTH_FAILURE_USER=“Auth Failure User: %s”
# Logwatch report auth failure uid
AUTH_FAILURE_UID=“Auth Failure UID: %s”
# Logwatch report auth failure gid
AUTH_FAILURE_GID=“Auth Failure GID: %s”
# Logwatch report auth failure host
AUTH_FAILURE_HOST=“Auth Failure Host: %s”
# Logwatch report auth failure logname
AUTH_FAILURE_LOGNAME=“Auth Failure Logname: %s”
# Logwatch report auth failure addr
AUTH_FAILURE_ADDR=“Auth Failure Addr: %s”
# Logwatch report auth failure ruser
AUTH_FAILURE_RUSER=“Auth Failure Ruser: %s”
# Logwatch report auth failure rhost
AUTH_FAILURE_RHOST=“Auth Failure Rhost: %s”
# Logwatch report auth failure euid
AUTH_FAILURE_EUID=“Auth Failure Euid: %s”
# Logwatch report auth failure egid
AUTH_FAILURE_EGID=“Auth Failure Egid: %s”
# Logwatch report auth failure suid
AUTH_FAILURE_SUID=“Auth Failure Suid: %s”
# Logwatch report auth failure egid
AUTH_FAILURE_EGID=“Auth Failure Egid: %s”
# Logwatch report auth failure sugid
AUTH_FAILURE_SUGID=“Auth Failure Suid: %s”
# Logwatch report auth failure su
AUTH_FAILURE_SU=“Auth Failure Su: %s”
# Logwatch report auth failure suid
AUTH_FAILURE_SUID=“Auth Failure Suid: %s”
# Logwatch report auth failure egid
AUTH_FAILURE_EGID=“Auth Failure Egid: %s”
# Logwatch report auth failure sugid
AUTH_FAILURE_SUGID=“Auth Failure Suid: %s”
# Logwatch report auth failure su
AUTH_FAILURE_SU=“Auth Failure Su: %s”
# Logwatch report auth failure suid
AUTH_FAILURE_SUID=“Auth Failure Suid: %s”
# Logwatch report auth failure egid
AUTH_FAILURE_EGID=“Auth Failure Egid: %s”
# Logwatch report auth failure sugid
AUTH_FAILURE_SUGID=“Auth Failure Suid: %s”
# Logwatch report auth failure su
AUTH_FAILURE_SU=“Auth Failure Su: %s”
# Logwatch report auth failure suid
AUTH_FAILURE_SUID=“Auth Failure Suid: %s”
# Logwatch report auth failure egid
AUTH_FAILURE_EGID=“Auth Failure Egid: %s”
# Logwatch report auth failure sugid
AUTH_FAILURE_SUGID=“Auth Failure Suid: %s”
# Logwatch report auth failure su
AUTH_FAILURE_SU=“Auth Failure Su: %s”
# Logwatch report auth failure suid
AUTH_FAILURE_SUID=“Auth Failure Suid: %s”
# Logwatch report auth failure egid
AUTH_FAILURE_EGID=“Auth Failure Egid: %s”
# Logwatch report auth failure sugid
AUTH_FAILURE_SUGID=“Auth Failure Suid: %s”
# Logwatch report auth failure su
AUTH_FAILURE_SU=“Auth Failure Su: %s”
# Logwatch report auth failure suid
AUTH_FAILURE_SUID=“Auth Failure Suid: %s”
# Logwatch report auth failure egid
AUTH_FAILURE_EGID=“Auth Failure Egid: %s”
# Logwatch report auth failure sugid
AUTH_FAILURE_SUGID=“Auth Failure Suid: %s”
# Logwatch report auth failure su
AUTH_FAILURE_SU=“Auth Failure Su: %s”
# Logwatch report auth failure suid
AUTH_FAILURE_SUID=“Auth Failure Suid: %s”
# Logwatch report auth failure egid
AUTH_FAILURE_EGID=“Auth Failure Egid: %s”
# Logwatch report auth failure sugid
AUTH_FAILURE_SUGID=“Auth Failure Suid: %s”
# Logwatch report auth failure su
AUTH_FAILURE_SU=“Auth Failure Su: %s”
# Logwatch report auth failure suid
AUTH_FAILURE_SUID=“Auth Failure Suid: %s”
# Logwatch report auth failure egid
AUTH_FAILURE_EGID=“Auth Failure Egid: %s”
# Logwatch report auth failure sugid
AUTH_FAILURE_SUGID=“Auth Failure Suid: %s”
# Logwatch report auth failure su
AUTH_FAILURE_SU=“Auth Failure Su: %s”
# Logwatch report auth failure suid
AUTH_FAILURE_SUID=“Auth Failure Suid: %s”
# Logwatch report auth failure egid
AUTH_FAILURE_EGID=“Auth Failure Egid: %s”
# Logwatch report auth failure sugid
AUTH_FAILURE_SUGID=“Auth Failure Suid: %s”
# Logwatch report auth failure su
AUTH_FAILURE_SU=“Auth Failure Su: %s”
# Logwatch report auth failure suid
AUTH_FAILURE_SUID=“Auth Failure Suid: %s”
# Logwatch report auth failure egid
AUTH_FAILURE_EGID=“Auth Failure Egid: %s”
# Logwatch report auth failure sugid
AUTH_FAILURE_SUGID=“Auth Failure Suid: %s”
# Logwatch report auth failure su
AUTH_FAILURE_SU=“Auth Failure Su: %s”
# Logwatch report auth failure suid
AUTH_FAILURE_SUID=“Auth Failure Suid: %s”
# Logwatch report auth failure egid
AUTH_FAILURE_EGID=“Auth Failure Egid: %s”
# Logwatch report auth failure sugid
AUTH_FAILURE_SUGID=“Auth Failure Suid: %s”
# Logwatch report auth failure su
AUTH_FAILURE_SU=“Auth Failure Su: %s”
# Logwatch report auth failure suid
AUTH_FAILURE_SUID=“Auth Failure Suid: %s”
# Logwatch report auth failure egid
AUTH_FAILURE_EGID=“Auth Failure Egid: %s”
# Logwatch report auth failure sugid
AUTH_FAILURE_SUGID=“Auth Failure Suid: %s”
# Logwatch report auth failure su
AUTH_FAILURE_SU=“Auth Failure Su: %s”
# Logwatch report auth failure suid
AUTH_FAILURE_SUID=“Auth Failure Suid: %s”
# Logwatch report auth failure egid
AUTH_FAILURE_EGID=“Auth Failure Egid: %s”
# Logwatch report auth failure sugid
AUTH_FAILURE_SUGID=“Auth Failure Suid: %s”
# Logwatch report auth failure su
AUTH_FAILURE_SU=“Auth Failure Su: %s”
# Logwatch report auth failure suid
AUTH_FAILURE_SUID=“Auth Failure Suid: %s”
# Logwatch report auth failure egid
AUTH_FAILURE_EGID=“Auth Failure Egid: %s”
# Logwatch report auth failure sugid
AUTH_FAILURE_SUGID=“Auth Failure Suid: %s”
# Logwatch report auth failure su
AUTH_FAILURE_SU=“Auth Failure Su: %s”
# Logwatch report auth failure suid
AUTH_FAILURE_SUID=“Auth Failure Suid: %s”
# Logwatch report auth failure egid
AUTH_FAILURE_EGID=“Auth Failure Egid: %s”
# Logwatch report auth failure sugid
AUTH_FAILURE_SUGID=“Auth Failure Suid: %s”
# Logwatch report auth failure su
AUTH_FAILURE_SU=“Auth Failure Su: %s”
# Logwatch report auth failure suid
AUTH_FAILURE_SUID=“Auth Failure Suid: %s”
# Logwatch report auth failure egid
AUTH_FAILURE_EGID=“Auth Failure Egid: %s”
# Logwatch report auth failure sugid
AUTH_FAILURE_SUGID=“Auth Failure Suid: %s”
# Logwatch report auth failure su
AUTH_FAILURE_SU=“Auth Failure Su: %s”
# Logwatch report auth failure suid
AUTH_FAILURE_SUID=“Auth Failure Suid: %s”
# Logwatch report auth failure egid
AUTH_FAILURE_EGID=“Auth Failure Egid: %s”
# Logwatch report auth failure sugid
AUTH_FAILURE_SUGID=“Auth Failure Suid: %s”
# Logwatch report auth failure su
AUTH_FAILURE_SU=“Auth Failure Su: %s”
# Logwatch report auth failure suid
AUTH_FAILURE_SUID=“Auth Failure Suid: %s”
# Logwatch report auth failure egid
AUTH_FAILURE_EGID=“Auth Failure Egid: %s”
# Logwatch report auth failure sugid
AUTH_FAILURE_SUGID=“Auth Failure Suid: %s”
# Logwatch report auth failure su
AUTH_FAILURE_SU=“Auth Failure Su: %s”
# Logwatch report auth failure suid
AUTH_FAILURE_SUID=“Auth Failure Suid: %s”
# Logwatch report auth failure egid
AUTH_FAILURE_EGID=“Auth Failure Egid: %s”
# Logwatch report auth failure sugid
AUTH_FAILURE_SUGID=“Auth Failure Suid: %s”
# Logwatch report auth failure su
AUTH_FAILURE_SU=“Auth Failure Su: %s”
# Logwatch report auth failure suid
AUTH_FAILURE_SUID=“Auth Failure