CentOS,全称Community Enterprise Operating System,是一个基于Red Hat Enterprise Linux(RHEL)的免费开源操作系统。它以其稳定性和安全性而闻名,是许多服务器和开发者的首选。对于新手来说,CentOS的安装和配置可能会显得有些繁琐。但别担心,本文将为你提供一份自动化安装与配置全攻略,让你轻松入门!
一、自动化安装
1. 准备工作
在开始自动化安装之前,你需要准备以下材料:
CentOS镜像文件
U盘或硬盘
服务器或虚拟机
2. 使用kickstart文件
Kickstart文件是一种自动化安装CentOS的方法。以下是一个简单的Kickstart文件示例:
# 安装类型
install
# 语言
lang en_US.UTF-8
# 时区
timezone Asia/Shanghai
# 主机名
network –hostname centos7
# IP地址、子网掩码、网关
network –device eth0 –onboot yes –ip 192.168.1.100 –netmask 255.255.255.0 –gateway 192.168.1.1
# root密码
rootpw –iscrypted $6$rounds=4096$YourPasswordHere
# 磁盘分区
part / –fstype ext4 –size 10G
# 安装源
repo –name=”Base” –baseurl=http:// mirrors.aliyun.com/centos/7/os/x86_64/ –enabled=1
# 安装软件包
%packages
@base
@development
@isdn
@kernel
@network-server
@virtualization
@xwindows
3. 创建ISO镜像
将Kickstart文件和CentOS镜像文件放在同一目录下,使用以下命令创建ISO镜像:
mkisofs -o centos7.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table kickstart/
4. 安装CentOS
将ISO镜像烧录到U盘或硬盘,并将服务器或虚拟机设置为从U盘或硬盘启动。按照提示完成安装。
二、自动化配置
1. 安装自动化配置工具
安装Ansible,一个开源的自动化运维工具:
sudo yum install ansible
2. 编写Ansible playbook
以下是一个简单的Ansible playbook示例,用于配置CentOS:
—
– name: Configure CentOS
hosts: all
become: yes
tasks:
– name: Update package repository
yum:
name: epel-release
state: present
– name: Install necessary packages
yum:
name:
– net-tools
– openssh-server
– firewalld
state: present
– name: Configure SSH
copy:
src: /path/to/ssh_config
dest: /etc/ssh/ssh_config
– name: Configure firewall
firewalld:
state: enabled
permanent: yes
services:
– ssh
3. 运行Ansible playbook
在Ansible目录下,使用以下命令运行playbook:
ansible-playbook playbook.yml
恭喜你!现在你已经完成了CentOS的自动化安装和配置。希望这份攻略能帮助你轻松入门CentOS。