centos selinux初始化脚本

这是一个用于设置和查看SELinux状态的bash脚本。用户可以选择启用、禁用、宽松模式,并相应地更新配置文件。脚本通过`setenforce`命令即时更改状态,并使用`sed`永久修改`/etc/selinux/config`文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


```bash
#!/bin/bash
#
echo "########################################"
echo -e "Current status:\n`sestatus`"
echo "########################################"
echo -e "\n"
read -p "Please input enable|disable|permissive|quit to set selinux:" Arg
if [ $Arg == "enable" ] ; then
    setenforce 1
    sed -ri.bak  "s#^SELINUX=.*#SELINUX=enforcing#" /etc/selinux/config &>/dev/null
    echo "Selinux current status is `getenforce`"
    echo "Selinux'config file is set `sed -n "/^SELINUX=/p" /etc/selinux/config`"
    exit
elif [ $Arg == "disable" ] ; then
    setenforce 0
    sed -ri.bak  "s#^SELINUX=.*#SELINUX=disabled#" /etc/selinux/config &>/dev/null
    echo "Selinux current status is `getenforce`,But Disabled's status need to reboot"
    echo "Selinux'config file is set `sed -n "/^SELINUX=/p" /etc/selinux/config`"
    exit
elif [ $Arg == "permissive" ] ; then
    setenforce Permissive
    sed -ri.bak  "s#^SELINUX=.*#SELINUX=Permissive#" /etc/selinux/config &>/dev/null
    echo "Selinux current status is `getenforce`"
    echo "Selinux'config file is set `sed -n "/^SELINUX=/p" /etc/selinux/config`"
    exit
else [ $Arg == "quit" ] 
    echo "The shell exit"
    exit
fi

CentOS是一种流行的Linux发行版,用于服务器和个人计算机。初始化通用脚本旨在自动化CentOS服务器的初始配置和设置。以下是一个示例: ```bash #!/bin/bash # 更新系统 yum update -y # 安装常用软件 yum install -y git wget unzip # 配置防火墙 systemctl enable firewalld systemctl start firewalld firewall-cmd --zone=public --add-port=80/tcp --permanent # 添加HTTP端口 firewall-cmd --reload # 设置时区 timedatectl set-timezone Asia/Shanghai # 安装和配置NTP服务 yum install -y ntp systemctl enable ntpd systemctl start ntpd # 配置SSH服务 sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config systemctl restart sshd # 配置SELinux sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config # 配置Swap空间 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 # 安装常用工具 yum install -y vim htop # 完成初始化 echo "CentOS初始化完成!" ``` 这个脚本首先通过`yum update -y`命令更新系统。然后使用`yum install -y`命令安装常用软件,如git、wget和unzip。接下来,配置防火墙以允许HTTP流量通过端口80。然后设置时区并安装、启用NTP服务以同步系统时间。之后,通过修改SSH配置文件启用密码身份验证,并重启SSH服务。接着,禁用SELinux以简化安全设置。最后,分配并启用Swap空间以增加系统的内存容量。最后一步安装vim和htop等常用工具。脚本结束时会显示一条提示信息,确认CentOS初始化已完成。 使用这个通用脚本可以快速为CentOS服务器进行初始配置,节省时间和工作量,并确保系统按照一致的标准进行设置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值