前言 本文介绍的 Shell 脚本,适用于一键初始化 CentOS 7 系统。
脚本代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 #!/bin/bash if [[ "$(whoami) " != "root" ]]; then echo "please run this script as root !" >&2 exit 1 fi echo -e "\033[31m the script only Support CentOS_7 x86_64 \033[0m" echo -e "\033[31m system initialization script, Please Seriously. press ctrl+C to cancel \033[0m" platform=`uname -i` if [ $platform != "x86_64" ];then echo "this script is only for 64bit Operating System !" exit 1 fi if [ "$1 " == "" ];then echo "The host name is empty." exit 1 else hostnamectl --static set-hostname $1 hostnamectl set-hostname $1 fi cat << EOF +---------------------------------------+ | your system is CentOS 7 x86_64 | | start optimizing | +---------------------------------------+ EOF sleep 1 yum_update (){yum update -y yum install -y nmap unzip wget vim lsof xz net-tools iptables-services ntpdate ntp-doc psmisc } zone_time (){timedatectl set-timezone Asia/Shanghai /usr/sbin/ntpdate 0.cn.pool.ntp.org > /dev/null 2>&1 /usr/sbin/hwclock --systohc /usr/sbin/hwclock -w cat > /var/spool/cron/root << EOF 10 0 * * * /usr/sbin/ntpdate 0.cn.pool.ntp.org > /dev/null 2>&1 * * * * */1 /usr/sbin/hwclock -w > /dev/null 2>&1 EOF chmod 600 /var/spool/cron/root /sbin/service crond restart sleep 1 } limits_config (){cat > /etc/rc.d/rc.local << EOF #!/bin/bash touch /var/lock/subsys/local ulimit -SHn 1024000 EOF sed -i "/^ulimit -SHn.*/d" /etc/rc.d/rc.local echo "ulimit -SHn 1024000" >> /etc/rc.d/rc.localsed -i "/^ulimit -s.*/d" /etc/profile sed -i "/^ulimit -c.*/d" /etc/profile sed -i "/^ulimit -SHn.*/d" /etc/profile cat >> /etc/profile << EOF ulimit -c unlimited ulimit -s unlimited ulimit -SHn 1024000 EOF source /etc/profileulimit -acat /etc/profile | grep ulimit if [ ! -f "/etc/security/limits.conf.bak" ]; then cp /etc/security/limits.conf /etc/security/limits.conf.bak fi cat > /etc/security/limits.conf << EOF * soft nofile 1024000 * hard nofile 1024000 * soft nproc 1024000 * hard nproc 1024000 hive - nofile 1024000 hive - nproc 1024000 EOF if [ ! -f "/etc/security/limits.d/20-nproc.conf.bak" ]; then cp /etc/security/limits.d/20-nproc.conf /etc/security/limits.d/20-nproc.conf.bak fi cat > /etc/security/limits.d/20-nproc.conf << EOF * soft nproc 409600 root soft nproc unlimited EOF sleep 1 } sysctl_config (){if [ ! -f "/etc/sysctl.conf.bak" ]; then cp /etc/sysctl.conf /etc/sysctl.conf.bak fi cat > /etc/sysctl.conf << EOF net.ipv6.conf.all.disable_ipv6 = 1 net.ipv6.conf.default.disable_ipv6 = 1 net.ipv4.tcp_syn_retries = 1 net.ipv4.tcp_synack_retries = 1 net.ipv4.tcp_keepalive_time = 600 net.ipv4.tcp_keepalive_probes = 3 net.ipv4.tcp_keepalive_intvl =15 net.ipv4.tcp_retries1 = 3 net.ipv4.tcp_retries2 = 5 net.ipv4.tcp_fin_timeout = 10 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_window_scaling = 1 net.ipv4.tcp_max_tw_buckets = 60000 net.ipv4.tcp_max_orphans = 32768 net.ipv4.tcp_max_syn_backlog = 16384 net.ipv4.tcp_mem = 94500000 915000000 927000000 net.ipv4.tcp_wmem = 4096 16384 13107200 net.ipv4.tcp_rmem = 4096 87380 17476000 net.ipv4.ip_local_port_range = 1024 65000 net.ipv4.ip_forward = 1 net.ipv4.route.gc_timeout = 100 net.core.somaxconn = 32768 net.core.netdev_max_backlog = 32768 net.nf_conntrack_max = 6553500 net.netfilter.nf_conntrack_max = 6553500 net.netfilter.nf_conntrack_tcp_timeout_established = 180 vm.overcommit_memory = 1 vm.swappiness = 1 fs.file-max = 1024000 EOF /sbin/sysctl -p sleep 1 } LANG_config (){echo "LANG=\"en_US.UTF-8\"" >/etc/locale.confsource /etc/locale.conf} selinux_config (){sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config setenforce 0 sleep 1 } log_config (){setenforce 0 systemctl start systemd-journald systemctl status systemd-journald } firewalld_config (){/usr/bin/systemctl stop firewalld.service /usr/bin/systemctl disable firewalld.service } sshd_config (){if [ ! -f "/etc/ssh/sshd_config.bak" ]; then cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak fi cat >/etc/ssh/sshd_config<<EOF Port 22 ClientAliveInterval 60 ClientAliveCountMax 5 AddressFamily inet ListenAddress 0.0.0.0 Protocol 2 HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_ecdsa_key HostKey /etc/ssh/ssh_host_ed25519_key SyslogFacility AUTHPRIV PermitRootLogin yes MaxAuthTries 6 RSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys PasswordAuthentication yes ChallengeResponseAuthentication no UsePAM yes UseDNS no X11Forwarding yes UsePrivilegeSeparation sandbox AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE AcceptEnv XMODIFIERS Subsystem sftp /usr/libexec/openssh/sftp-server EOF /sbin/service sshd restart } ipv6_config (){echo "NETWORKING_IPV6=no" >/etc/sysconfig/networkecho 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6echo 1 > /proc/sys/net/ipv6/conf/default/disable_ipv6echo "127.0.0.1 localhost localhost.localdomain" >/etc/hostsfor line in $(ls -lh /etc/sysconfig/network-scripts/ifcfg-* | awk -F '[ ]+' '{print $9}' )do if [ -f $line ] then sed -i 's/IPV6INIT=yes/IPV6INIT=no/g' $line echo $i fi done } history_config (){export HISTFILESIZE=10000000export HISTSIZE=1000000export PROMPT_COMMAND="history -a" export HISTTIMEFORMAT="%Y-%m-%d_%H:%M:%S " cat >>/etc/bashrc<<EOF alias vi='vim' HISTDIR='/var/log/command.log' if [ ! -f \$HISTDIR ];then touch \$HISTDIR chmod 666 \$HISTDIR fi export HISTTIMEFORMAT="{\"TIME\":\"%F %T\",\"IP\":\"\$(ip a | grep -E '192.168|172' | head -1 | awk '{print \$2}' | cut -d/ -f1)\",\"LI\":\"\$(who -u am i 2>/dev/null| awk '{print \$NF}'|sed -e 's/[()]//g')\",\"LU\":\"\$(who am i|awk '{print \$1}')\",\"NU\":\"\${USER}\",\"CMD\":\"" export PROMPT_COMMAND='history 1|tail -1|sed "s/^[ ]\+[0-9]\+ //"|sed "s/$/\"}/">> /var/log/command.log' EOF source /etc/bashrc} service_config (){/usr/bin/systemctl enable NetworkManager-wait-online.service /usr/bin/systemctl start NetworkManager-wait-online.service /usr/bin/systemctl stop postfix.service /usr/bin/systemctl disable postfix.service chmod +x /etc/rc.local chmod +x /etc/rc.d/rc.local } vim_config (){cat > /root/.vimrc << EOF set history=1000 EOF } docker_install (){ if command -v docker >/dev/null 2>&1; then echo "Docker 已安装,跳过安装步骤。" return fi echo "Docker 未安装,开始安装。" sudo yum install -y yum-utils device-mapper-persistent-data lvm2 sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo sudo yum makecache fast sudo yum -y install docker-ce sudo service docker start } done_ok (){touch /var/log /init-ok cat << EOF +-------------------------------------------------+ | optimizer is done | | it's recommond to restart this server ! | | Please Reboot system | +-------------------------------------------------+ EOF } main (){ yum_update zone_time limits_config sysctl_config LANG_config selinux_config log_config firewalld_config sshd_config ipv6_config history_config service_config vim_config docker_install done_ok } main
脚本执行 将上述代码保存到 centos7-init.sh
文件中,然后执行以下命令即可运行脚本,其中的 hostname
是系统的主机名。
1 sudo bash centos7-init.sh hostname