**生产环境中的使用原则:系统安装的应用程序包越少,服务器就越稳定。**
*基础优化*
1. 配置网络自启动
cat > /etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF DEVICE=eth0 BOOTPROTO=static IPV6INIT=no IPV6_AUTOCONF=yes ONBOOT=yes IPADDR=172.16.10.10 NETMASK=255.255.255.0 GATEWAY=172.16.10.254 TYPE=Ethernet PEERDNS=yes # 允许DHCP获得的dns覆盖本地的DNS USERCTL=no # 不允许普通用户修改网卡 EOF cat >> /etc/resolv.conf << EOF nameserver 114.114.114.114 EOF
2. 更新yum源为国内镜像 (要求可连接公网,内网可配置内部源)
#Yum源更换为国内阿里源 yum install wget telnet -y mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo rpm -ivh http://dl.Fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch.rpm #yum重新建立缓存 yum clean all && yum makecache
3. 关闭不必要的服务
for serv in ` systemctl list-unit-files | grep enabled|awk '{print $1}'`;do systemctl disable $serv ;done for serv in autovt@.service crond.service getty@.service irqbalance.service kdump.service microcode.service rsyslog.service sshd.service sysstat.service systemd-readahead-collect.service systemd-readahead-drop.service systemd-readahead-replay.service tuned.service lvm2-lvmetad.socket lvm2-lvmpolld.socket default.target multi-user.target runlevel2.target runlevel3.target runlevel4.target ;do systemctl enable $serv;done
4. 关闭防火墙和selinux
systemctl disable firewalld sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
5. 修改history记录
sed -i 's@^HISTSIZE=1000@HISTSIZE=100@' /etc/profile sed -i '/^HISTSIZE/a\HISTTIMEFORMAT="%F %T `whoami` "' /etc/profile
6. 定时同步服务器时间(要求可联网,或配置内网web为时间服务器)
yum -y install ntp /usr/sbin/ntpdate cn.pool.ntp.org echo "* 4 * * * /usr/sbin/ntpdate cn.pool.ntp.org > /dev/null 2>&1" >> /var/spool/cron/root systemctl restart crond.service
7. 关闭IPv6网络(当前部分环境要求适配IPv6不选)
sed -i 's@GRUB_CMDLINE_LINUX="@GRUB_CMDLINE_LINUX="ipv6.disable=1 @' /etc/default/grub grub2-mkconfig -o /boot/grub2/grub.cfg
8. 调整最大文件打开数
echo "ulimit -SHn 102400" >> /etc/rc.local cat >> /etc/security/limits.conf << EOF * soft nofile 655350 * hard nofile 655350 EOF
9. 关闭磁盘I/O功能(分布式存储磁盘建议可选)
#原理:存放多个小文件的目录无需记录访问时间atime,关闭以减少写磁盘I/O echo "dev/sdb1 /bigData xfs noatime,nodiratime 0 0" >> /etc/fstab
10. 增加sudo权限用户(前提用户已存在)
#原理:给一个本地已有用户授权不输密码即可sudo权限操作 sed -i '/^root/a\dengzz ALL=(ALL) NOPASSWD:ALL' /etc/sudoers
11. 修改SSH登录配置
#原理:关闭GSSAPI反解析关闭SSH反向查询解决连接慢;禁止root远程登录禁止空密码登录提高安全性 sed -i 's/^GSSAPIAuthentication yes$/GSSAPIAuthentication no/' /etc/ssh/sshd_config sed -i 's@#PermitRootLogin yes@PermitRootLogin no@' /etc/ssh/sshd_config sed -i 's@#PermitEmptyPasswords no@PermitEmptyPasswords no@' /etc/ssh/sshd_config sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config systemctl restart sshd
12.修改字符集
# 设置为中文 localectl set-locale LANG=zh_CN.UTF-8 source /etc/locale.conf # 设置为英文 localectl set-locale LANG=en_US.utf8 source /etc/locale.conf # 查看当前字符集 echo $LANG
*系统性能*
13.内核参数(部署应用不同优化不同)