|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +################################################################################### |
| 4 | +# Linux Centos7 设置环境配置脚本(根据需要选择) |
| 5 | +# 注:不了解脚本中配置意图的情况下,不要贸然执行此脚本 |
| 6 | +# Author: Zhang Peng |
| 7 | +################################################################################### |
| 8 | + |
| 9 | +# 获取当前机器 IP |
| 10 | +ip="" |
| 11 | +function getDeviceIp() { |
| 12 | + ip=`ifconfig eth0 | grep "inet addr" | awk '{ print $2}' | awk -F: '{print $2}'` |
| 13 | + if [ "$ip" == "" ] |
| 14 | + then |
| 15 | + ip=`ifconfig ens32 | grep "inet"|grep "broadcast" | awk '{ print $2}' | awk -F: '{print $1}'` |
| 16 | + fi |
| 17 | + |
| 18 | + if [ "$ip" == "" ] |
| 19 | + then |
| 20 | + ip=`echo $1` |
| 21 | + fi |
| 22 | + |
| 23 | + if [ "${ip}" == "" ] |
| 24 | + then |
| 25 | + echo "无法获取IP地址" |
| 26 | + exit 0 |
| 27 | + fi |
| 28 | +} |
| 29 | + |
| 30 | +function setDNS() { |
| 31 | +getDeviceIp |
| 32 | +host=`hostname` |
| 33 | +cat >> /etc/hosts << EOF |
| 34 | +${ip} ${host} |
| 35 | +EOF |
| 36 | +} |
| 37 | + |
| 38 | +function setNameServer() { |
| 39 | + echo "添加域名服务器" |
| 40 | + echo "nameserver 218.2.135.1" >> /etc/resolv.conf |
| 41 | +} |
| 42 | + |
| 43 | +function setNtp() { |
| 44 | +# 时钟同步工具 |
| 45 | +yum -y install ntp |
| 46 | +# 同步上海交通大学网络中心NTP服务器 |
| 47 | +echo "* 4 * * * /usr/sbin/ntpdate ntp.sjtu.edu.cn > /dev/null 2>&1" >> /var/spool/cron/root |
| 48 | +# 以一个服务器时间为标准定时更新时间(有时需要以公司中的服务器作为标准) |
| 49 | +#echo "*/30 * * * * /usr/local/bin/ntpdate 192.168.16.182" >> /var/spool/cron/root |
| 50 | +} |
| 51 | + |
| 52 | +function setLimit() { |
| 53 | +cat >> /etc/security/limits.conf << EOF |
| 54 | + * - nofile 65535 |
| 55 | + * - nproc 65535 |
| 56 | +EOF |
| 57 | +} |
| 58 | + |
| 59 | +function setLang() { |
| 60 | +cat > /etc/sysconfig/i18n << EOF |
| 61 | +LANG="zh_CN.UTF-8" |
| 62 | +EOF |
| 63 | +} |
| 64 | + |
| 65 | +function closeShutdownShortkey() { |
| 66 | + echo "关闭 Ctrl+Alt+Del 快捷键防止重新启动" |
| 67 | + sed -i 's#exec /sbin/shutdown -r now#\#exec /sbin/shutdown -r now#' /etc/init/control-alt-delete.conf |
| 68 | +} |
| 69 | + |
| 70 | +function closeSelinux() { |
| 71 | + echo "关闭 selinux" |
| 72 | + |
| 73 | + # see http://blog.51cto.com/13570193/2093299 |
| 74 | + sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config |
| 75 | +} |
| 76 | + |
| 77 | +function closeFirewall() { |
| 78 | + echo "关闭防火墙" |
| 79 | + |
| 80 | + # see https://www.cnblogs.com/moxiaoan/p/5683743.html |
| 81 | + systemctl stop firewalld |
| 82 | + systemctl disable firewalld |
| 83 | +} |
| 84 | + |
| 85 | +function setBootMode() { |
| 86 | + # 1. 停机(记得不要把 initdefault 配置为 0,因为这样会使 Linux 不能启动) |
| 87 | + # 2. 单用户模式,就像 Win9X 下的安全模式 |
| 88 | + # 3. 多用户,但是没有 NFS |
| 89 | + # 4. 完全多用户模式,准则的运行级 |
| 90 | + # 5. 通常不用,在一些特殊情况下可以用它来做一些事情 |
| 91 | + # 6. X11,即进到 X-Window 系统 |
| 92 | + # 7. 重新启动 (记得不要把 initdefault 配置为 6,因为这样会使 Linux 不断地重新启动) |
| 93 | + echo "设置 Linux 启动模式" |
| 94 | + sed -i 's/id:5:initdefault:/id:3:initdefault:/' /etc/inittab |
| 95 | +} |
| 96 | + |
| 97 | +function configIpv4(){ |
| 98 | +echo "配置 ipv4" |
| 99 | + |
| 100 | +cat >> /etc/sysctl.conf << EOF |
| 101 | +net.ipv4.tcp_tw_reuse = 1 |
| 102 | +net.ipv4.tcp_tw_recycle = 1 |
| 103 | +net.ipv4.tcp_fin_timeout = 2 |
| 104 | +net.ipv4.tcp_syncookies = 1 |
| 105 | +net.ipv4.tcp_keepalive_time = 1200 |
| 106 | +net.ipv4.tcp_max_syn_backlog = 16384 |
| 107 | +net.core.netdev_max_backlog = 16384 |
| 108 | +net.core.somaxconn = 32768 |
| 109 | +net.core.wmem_default = 8388608 |
| 110 | +net.core.rmem_default = 8388608 |
| 111 | +net.core.rmem_max = 16777216 |
| 112 | +net.core.wmem_max = 16777216 |
| 113 | +net.ipv4.tcp_timestamps = 0 |
| 114 | +net.ipv4.route.gc_timeout = 100 |
| 115 | +net.ipv4.tcp_synack_retries = 1 |
| 116 | +net.ipv4.tcp_syn_retries = 1 |
| 117 | +net.ipv4.tcp_mem = 94500000 915000000 927000000 |
| 118 | +net.ipv4.tcp_max_orphans = 3276800 |
| 119 | +net.ipv4.ip_local_port_range = 2000 65535 |
| 120 | +net.ipv4.tcp_max_tw_buckets = 5000 |
| 121 | +vm.swappiness=10 |
| 122 | +EOF |
| 123 | +} |
| 124 | + |
| 125 | +function closeIpv6() { |
| 126 | +echo "关闭 ipv6" |
| 127 | + |
| 128 | +cat > /etc/modprobe.d/ipv6.conf << EOF |
| 129 | +alias net-pf-10 off |
| 130 | +options ipv6 disable=1 |
| 131 | +EOF |
| 132 | + |
| 133 | +echo "NETWORKING_IPV6=off" >> /etc/sysconfig/network |
| 134 | +} |
| 135 | + |
| 136 | +######################################## MAIN ######################################## |
| 137 | +echo -e "\n>>>>>>>>> 配置系统环境" |
| 138 | + |
| 139 | +# 关闭 selinux |
| 140 | +closeSelinux |
| 141 | + |
| 142 | +# 关闭防火墙 |
| 143 | +closeFirewall |
| 144 | + |
| 145 | +# 设置 DNS 服务器和本机 Host |
| 146 | +setNameServer |
| 147 | +setDNS |
| 148 | + |
| 149 | +# 设置时间同步 |
| 150 | +setNtp |
| 151 | + |
| 152 | +echo -e "\n>>>>>>>>> 配置系统环境结束" |
0 commit comments