Centos7系统配置优化

#!/bin/bash
#centos7系统优化

# 关闭selinux
if [ `getenforce` = "Enforcing" ]
then
    # 临时关闭selinux
    setenforce 0
	if [ $? = 0 -a `getenforce` = "Permissive" ]
	then		
		echo -e "\033[32m selinux状态是:`getenforce` 临时关闭selinux成功 \033[0m"
	fi	
	# 永久关闭selinux
	sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
	if [ $? = 0 -a `egrep -v "#|^$|SELINUXTYPE" /etc/selinux/config |awk -F"=" '{print $2}'` = "disabled" ]
	then
		echo -e "\033[32m `egrep -v "#|^$|SELINUXTYPE" /etc/selinux/config` 永久关闭selinux成功 \033[0m"
	fi
elif [ `getenforce` = "Permissive" ]
then
    echo -e "\033[33m selinux已临时关闭 \033[0m"
elif [ `getenforce` = "Disabled" ]
then
    echo -e "\033[33m selinux已永久关闭 \033[0m"
else
    echo -e "\033[41;37m selinux状态异常 \033[0m"
fi

# 关闭防火墙
if [ `systemctl status firewalld |grep Active |awk '{print $3}'|cut -d '(' -f2|cut -d ')' -f1` = "running" ]
then
    # 临时关闭防火墙
    systemctl stop firewalld	
	if [ $? = 0 ]
    then
    	echo -e "\033[32m 临时关闭防火墙成功 \033[0m"
    else
    	echo -e "\033[41;37m 临时关闭防火墙失败 \033[0m"
    fi
	# 永久关闭防火墙
    systemctl disable firewalld	
	if [ $? = 0 ]
    then
    	echo -e "\033[32m 永久关闭防火墙成功 \033[0m"
    else
    	echo -e "\033[41;37m 永久关闭防火墙失败 \033[0m"
    fi
elif [ `systemctl status firewalld |grep Active |awk '{print $3}'|cut -d '(' -f2|cut -d ')' -f1` = "dead" ]
then
    echo -e "\033[33m 防火墙已关闭 \033[0m"
else
    echo -e "\033[41;37m 防火墙状态异常 \033[0m"
fi
systemctl stop firewalld

#清理防火墙规则
iptables -F
if [ $? = 0 ]
then
	echo -e "\033[32m 清理防火墙规则成功 \033[0m"
else
	echo -e "\033[41;37m 清理防火墙规则失败 \033[0m"
fi

# 隐藏系统版本
if [ `cat /etc/issue |wc -l` -ne 0 ]
then
    cat /dev/null > /etc/issue
    if [ $? = 0 ]
    then
    	echo -e "\033[32m 隐藏系统版本成功 \033[0m"
    else
    	echo -e "\033[41;37m 隐藏系统版本失败 \033[0m"
    fi
else
    echo -e "\033[33m 系统版本已隐藏 \033[0m"
fi

#安装所需软件
for i in ntpdate net-tools lsof
do
	rpm -q $i
	if [ $? -ne 0 ]
	then		
		yum -y install ntpdate net-tools lsof
	else
		echo -e "\033[33m $i软件已安装 \033[0m"
	fi
done

# 加大打开文件数的限制
if [ `cat /etc/security/limits.conf |grep "soft nofile" |wc -l` = 0 ]
then
    echo "* soft nofile 65535" >> /etc/security/limits.conf
    if [ $? = 0 ]
    then
    	echo -e "\033[32m soft nofile设置成功 \033[0m"
    else
    	echo -e "\033[41;37m soft nofile设置失败 \033[0m"
    fi
else
    echo -e "\033[41;37m soft nofile已设置 \033[0m"
fi

if [ `cat /etc/security/limits.conf |grep "hard nofile" |wc -l` = 0 ]
then
    echo "* hard nofile 65535" >> /etc/security/limits.conf
    if [ $? = 0 ]
    then
    	echo -e "\033[32m hard nofile设置成功 \033[0m"
    else
    	echo -e "\033[41;37m hard nofile设置失败 \033[0m"
    fi
else
    echo -e "\033[41;37m hard nofile已设置 \033[0m"
fi

#更改时区
if [ `timedatectl status |grep Time |awk '{print $3}'` = "America/New_York" ]
then
	echo -e "\033[33m 你当前的时区是:`timedatectl status |grep Time |awk '{print $3}'` \033[0m"
	timedatectl set-timezone Asia/Shanghai
	echo -e "\033[32m 时区已设置为Asia/Shanghai \033[0m"
else
	echo -e "\033[41;37m 你当前的时区是:`timedatectl status |grep Time |awk '{print $3}'` \033[0m"
fi


已整理成脚本,需要的可以直接下载后保存成.sh文件就可以用了。欢迎交流指正!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
对于CentOS 7系统的优化,可以考虑以下几个方面: 1. 更新系统:保持系统处于最新状态,及时安装系统更新和补丁,以确保系统的安全性和稳定性。 2. 关闭不必要的服务:查看并关闭不需要的服务,以减少系统资源的使用和提高系统性能。可以使用命令`systemctl list-unit-files --type=service`查看所有服务,并使用`systemctl disable <service>`禁用不需要的服务。 3. 禁用 SELinux:如果你不需要 SELinux 的安全特性,可以禁用它以提高系统性能。可以通过编辑`/etc/selinux/config`文件,将`SELINUX=enforcing`修改为`SELINUX=disabled`,然后重启系统生效。 4. 调整文件描述符限制:增加系统的最大文件描述符数,可以提高系统的性能和并发连接数。可以编辑`/etc/security/limits.conf`文件,在文件末尾添加以下两行: ``` * soft nofile 65536 * hard nofile 65536 ``` 5. 调整内核参数:根据系统的需求和硬件配置,调整内核参数以优化系统性能。可以编辑`/etc/sysctl.conf`文件,在文件末尾添加或修改需要调整的参数,并执行命令`sysctl -p`使配置生效。 6. 安装性能监控工具:安装一些性能监控工具,如sar、top等,可以帮助你监控系统的性能和资源使用情况,及时发现并解决性能瓶颈。 以上是一些常见的CentOS 7系统优化方法,根据具体情况和需求,可能还会有其他的优化措施。在进行任何优化操作之前,请确保对系统有足够的了解,并备份重要数据以防止意外发生。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Echo^?

你的关注是我最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值