CentOS 7系统初始化和内核优化

1. 设置系统主机名
# 设置系统主机名
hostnamectl set-hostname mini2
2. 安装常见依赖包
yum install -y socat conntrack ntpdate ntp ipvsadm ipset jq iptables curl sysstat libseccomp wget vim net-tools git
3. 设置防火墙为 Iptables 并设置空规则
# 关闭firewalld防火墙和开启自启
systemctl stop firewalld && systemctl disable firewalld
# 安装iptables防火墙并清空规则
yum -y install iptables-services && systemctl start iptables && systemctl enable iptables && iptables -F && service iptables save
4. 关闭SELINUX和swap交换分区
# 关闭SELINUX
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
# 关闭swap交换分区
swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
5. 调整系统时区
# 设置系统时区为 中国/上海
timedatectl set-timezone Asia/Shanghai 
# 将当前的 UTC 时间写入硬件时钟
timedatectl set-local-rtc 0
# 重启依赖于系统时间的服务
systemctl restart rsyslog 
systemctl restart crond
6. 关闭系统不需要服务
systemctl stop postfix && systemctl disable postfix
7. 设置 rsyslogd 和 systemd journald
# 持久化保存日志的目录
mkdir /var/log/journal
mkdir /etc/systemd/journald.conf.d
cat > /etc/systemd/journald.conf.d/99-prophet.conf <<EOF 
[Journal]
# 持久化保存到磁盘
Storage=persistent

# 压缩历史日志
Compress=yes

SyncIntervalSec=5m 
RateLimitInterval=30s 
RateLimitBurst=1000

# 最大占用空间 5G 
SystemMaxUse=5G

# 单日志文件最大 100M 
SystemMaxFileSize=100M

# 日志保存时间 2 周
MaxRetentionSec=2week

# 不将日志转发到 syslog 
ForwardToSyslog=no 
EOF

# 重启systemd-journald服务
systemctl restart systemd-journald
8. 用户资源限制配置
vi /etc/security/limits.conf

# 加入如下内容
* - nofile 655360
* - as unlimited
* - nproc 2056474
* - fsize unlimited
* - memlock unlimited
9. 更新系统
yum -y update
10. 升级系统内核
# 安装yum的elrepo源
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm
# 安装最新内核
yum --enablerepo=elrepo-kernel install -y kernel-lt
# 查看系统中的全部内核
rpm -qa | grep kernel
# 删除无用内核,根据上一步的列表进行删除
yum remove -y kernel-3.10.0-1160.49.1.el7.x86_64
# 查看系统可用内核
cat /boot/grub2/grub.cfg |grep menuentry
# 设置开机从新内核启动,请根据上一步menuentry进行选择对应的内核
grub2-set-default 'CentOS Linux (5.4.168-1.el7.elrepo.x86_64) 7 (Core)'
# 重启系统使修改和配置生效
reboot
11. 调整内核参数
# 新建内核参数设置文件
cat > sysctl.conf <<EOF
net.ipv4.ip_forward=1
vm.swappiness=0 # 禁止使用 swap 空间,只有当系统 OOM 时才允许使用它
vm.overcommit_memory=1 # 不检查物理内存是否够用
vm.panic_on_oom=0 # 开启 OOM
fs.inotify.max_user_instances=8192 
fs.inotify.max_user_watches=1048576 
fs.file-max=52706963 
fs.nr_open=52706963 
net.ipv6.conf.all.disable_ipv6=1
EOF

#设置开机生效
cp sysctl.conf /etc/sysctl.d/sysctl.conf 
sysctl -p /etc/sysctl.d/sysctl.conf
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值