系统初始优化脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了系统初始优化脚本相关的知识,希望对你有一定的参考价值。
#!/bin/bash #1、精简开机自启动服务,只保留crond,network,syslog,sshd这四个服务。 for k in `chkconfig --list|grep 3:on|awk ‘{print $1}‘`; do chkconfig --level 3 $k off; done for j in crond rsyslog sshd network; do chkconfig --level 3 $j on; done #2、修改ip地址、网关、主机名、DNS #修改ip,网关 BOOTPROTO=`sed -n ‘/^BOOTPROTO/p‘ /etc/sysconfig/network-scripts/ifcfg-eth0` ONBOOT=`sed -n ‘/^ONBOOT/p‘ /etc/sysconfig/network-scripts/ifcfg-eth0` IPADDR=`sed -n ‘/^IPADDR/p‘ /etc/sysconfig/network-scripts/ifcfg-eth0` NETMASK=`sed -n ‘/^NETMASK/p‘ /etc/sysconfig/network-scripts/ifcfg-eth0` GATEWAY=`sed -n ‘/^GATEWAY/p‘ /etc/sysconfig/network-scripts/ifcfg-eth0` grep $IPADDR if [ $? == 0 ] then sed -i ‘s/‘"$IPADDR"‘/IPADDR=192.168.1.102/‘ /etc/sysconfig/network-scripts/ifcfg-eth0 else echo "IPADDR=192.168.1.102" >> /etc/sysconfig/network-scripts/ifcfg-eth0 fi grep $NETMASK if [ $? == 0 ] then sed -i ‘s/‘"$NETMASK"‘/NETMASK=255.255.255.0/‘ /etc/sysconfig/network-scripts/ifcfg-eth0 else echo "NETMASK=255.255.255.0" >> /etc/sysconfig/network-scripts/ifcfg-eth0 fi grep $GATEWAY if [ $? == 0 ] then sed -i ‘s/‘"$GATEWAY"‘/GATEWAY=192.168.1.1/‘ /etc/sysconfig/network-scripts/ifcfg-eth0 else echo "GATEWAY=192.168.1.1" >> /etc/sysconfig/network-scripts/ifcfg-eth0 fi sed -i ‘s/‘"$BOOTPROTO"‘/BOOTPROTO=static/‘ /etc/sysconfig/network-scripts/ifcfg-eth0 sed -i ‘s/‘"$ONBOOT"‘/ONBOOT=yes/‘ /etc/sysconfig/network-scripts/ifcfg-eth0 #修改主机名 sed -i ‘s/^HOSTNAME/HOSTNAME=ns1/‘ /etc/sysconfig/network #修改DNS echo "nameserver 114.114.114.114" >> /etc/resolv.conf echo "nameserver 8.8.8.8" >> /etc/resolv.conf #重启网络 /etc/init.d/network restart #3、关闭selinux,清空iptables sed -i ‘s/SEINUX=enforcing/SELINUX=disabled/‘ /etc/selinux/config iptables -F /etc/init.d/iptables save #4、添加普通用户并进行sudo授权管理 useradd qu echo "123456"|passwd --stdin qu history -c #清空历史命令 echo -e "qu\tALL=(ALL)\tALL" >> /etc/sudoers chmod u-w /etc/sudoers #禁止写入 #5、定时自动更新服务器时间 echo ‘*/5 * * * * /usr/sbin/ntpdate time.windows.com >/dev/null 2 >&1‘ >>/var/spool/cron/root #6、变更默认的ssh服务端口,禁止root用户远程连接 cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak Port=`sed -n ‘/^#Port/p‘ /etc/ssh/sshd_config` PermitRootLogin=`sed -n ‘/^#PermitRootLogin/p‘ /etc/ssh/sshd_config` PermitEmptyPasswords=`sed -n ‘/^#PermitEmptyPasswords/p‘ /etc/ssh/sshd_config` UseDNS=`sed -n ‘/^#UseDNS/p‘ /etc/ssh/sshd_config` sed -i ‘s/‘"$Port"‘/Port 51111/‘ /etc/ssh/sshd_config #更改ssh默认端口 sed -i ‘s/‘"$PermitRootLogin"‘/PermitRootLogin no/‘ /etc/ssh/sshd_config #禁止root用户远>程登录 sed -i ‘s/‘"$PermitEmptyPasswords"‘/PermitEmptyPasswords no/‘ /etc/ssh/sshd_config #禁止空密码登录 sed -i ‘s/‘"$UseDNS"‘/UseDNS no/‘ /etc/ssh/sshd_config #不使用DNS /etc/init.d/sshd reload #7、锁定关键文件系统 chattr +i /etc/passwd chattr +i /etc/inittab chattr +i /etc/group chattr +i /etc/shadow chattr +i /etc/gshadow #为了更安全把chattr命令也改了 /bin/mv /usr/bin/chattr /usr/bin/suoding #8、调整文件描述符大小 echo ‘* - nofile 65535‘ >> /etc/security/limits.conf #9、调整字符集,使其支持中文 sed-i ‘s/LANG="en_US.UTF-8"/LANG="zh_CN.GB18030"/‘/etc/sysconfig/i18n source/etc/sysconfig/i18n #10、登录系统前不显示版本信息 >/etc/redhat-release >/etc/issue #11、内核参数优化 cp /etc/sysctl.conf /etc/sysctl.conf.bak cat>/etc/sysctl.conf<<EOT net.ipv4.tcp_fin_timeout = 2 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_keepalive_time =600 net.ipv4.ip_local_port_range = 4000 65000 net.ipv4.tcp_max_syn_backlog = 16384 net.ipv4.tcp_max_tw_buckets = 36000 net.ipv4.route.gc_timeout = 100 net.ipv4.tcp_syn_retries = 1 net.ipv4.tcp_synack_retries = 1 net.core.somaxconn = 16384 net.core.netdev_max_backlog = 16384 net.ipv4.tcp_max_orphans = 16384 #以下参数是对iptables防火墙的优化,防火墙不开会有提示,可以忽略不理。 net.ipv4.ip_conntrack_max = 25000000 net.ipv4.netfilter.ip_conntrack_max = 25000000 net.ipv4.netfilter.ip_conntrack_tcp_timeout_established = 180 net.ipv4.netfilter.ip_conntrack_tcp_timeout_time_wait = 120 net.ipv4.netfilter.ip_conntrack_tcp_timeout_close_wait = 60 net.ipv4.netfilter.ip_conntrack_tcp_timeout_fin_wait = 120 EOT sysctl -p #使配置文件生效 if [ $? == 0 ] then echo "系统将在10秒后重启" sleep 10; reboot else echo "脚本运行有问题,请检查" fi
本文出自 “竹子” 博客,请务必保留此出处http://huanjingyouxian.blog.51cto.com/9849266/1906957
以上是关于系统初始优化脚本的主要内容,如果未能解决你的问题,请参考以下文章