|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +help_info(){ |
| 4 | +cat <<EOF |
| 5 | +用法: bash $0 选项 [选项...] |
| 6 | +示例: |
| 7 | + bash $0 all |
| 8 | + bash $0 help |
| 9 | + bash $0 sudo |
| 10 | +选项: |
| 11 | + help 打印这个帮助信息 |
| 12 | + all 配置所有 |
| 13 | + repo 换源(阿里源) |
| 14 | + ssh 配置ssh服务 |
| 15 | + sudo 配置sudo权限 |
| 16 | + selinux 关闭selinux |
| 17 | + firewalld 关闭防火墙 |
| 18 | + swap 关闭swap |
| 19 | + pip 配置pip源 |
| 20 | + shutdown 配置关机时间 |
| 21 | + cron 打开cron日志 |
| 22 | + clock 配置时区 |
| 23 | + editor 配置默认文本编辑器 |
| 24 | + git 配置git |
| 25 | + email 配置默认邮件系统 |
| 26 | +EOF |
| 27 | + exit 1 |
| 28 | +} |
| 29 | + |
| 30 | +judge_user(){ # {{{ |
| 31 | + if [[ ! $(whoami) == "root" ]];then |
| 32 | + echo "请使用root用户执行此脚本!" |
| 33 | + exit 1 |
| 34 | + fi |
| 35 | +} # }}} |
| 36 | + |
| 37 | +config_repository(){ # {{{ |
| 38 | + if [[ -f /etc/redhat-release ]];then |
| 39 | + centos_major_version=$(awk '{print $4}' /etc/redhat-release | awk -F. '{print $1}') |
| 40 | + wget -c http://mirrors.aliyun.com/repo/Centos-"${centos_major_version}".repo -O /etc/yum.repos.d/CentOS-Base.repo |
| 41 | + yum makecache |
| 42 | + yum -y update |
| 43 | + yum clean all |
| 44 | + else |
| 45 | + echo "非centos或redhat系统" |
| 46 | + fi |
| 47 | +} # }}} |
| 48 | + |
| 49 | +config_ssh(){ # {{{ |
| 50 | + judge_user |
| 51 | + sed -i '/^\(#\|\)UseDNS/cUseDNS no' /etc/ssh/sshd_config |
| 52 | + sed -i '/^\(#\|\)GSSAPIAuthentication/cGSSAPIAuthentication no' /etc/ssh/sshd_config |
| 53 | + sed -i '/^\(#\|\)ClientAliveInterval/cClientAliveInterval 60' /etc/ssh/sshd_config |
| 54 | + sed -i '/^\(#\|\)ClientAliveCountMax/cClientAliveCountMax 60' /etc/ssh/sshd_config |
| 55 | + sed -i '/^\(#\|\)PermitRootLogin/cPermitRootLogin without-password' /etc/ssh/sshd_config |
| 56 | + sed -i '/^\(#\|\)PasswordAuthentication/cPasswordAuthentication yes' /etc/ssh/sshd_config |
| 57 | + sed -i '/^\(#\|\)PermitEmptyPasswords/cPermitEmptyPasswords no' /etc/ssh/sshd_config |
| 58 | + sed -i '/^\(#\|\) StrictHostKeyChecking/c StrictHostKeyChecking no' /etc/ssh/ssh_config |
| 59 | + sed -i 's/\tGSSAPIAuthentication no/\ GSSAPIAuthentication yes/g' /etc/ssh/ssh_config |
| 60 | + [[ $(grep "ServerAliveInterval 20" /etc/ssh/ssh_config | wc -l) == 0 ]] && sed -i '$a\ ServerAliveInterval 20' /etc/ssh/ssh_config |
| 61 | + [[ $(grep "ServerAliveCountMax 999" /etc/ssh/ssh_config | wc -l) == 0 ]] && sed -i '$a\ ServerAliveCountMax 999' /etc/ssh/ssh_config |
| 62 | + systemctl reload sshd |
| 63 | + echo "configure ssh done." |
| 64 | +} # }}} |
| 65 | + |
| 66 | +config_sudo_privileges(){ # {{{ |
| 67 | + judge_user |
| 68 | + sed -i '/^%wheel/c%wheel ALL=(ALL) NOPASSWD: ALL' /etc/sudoers |
| 69 | + if [[ ! -f /var/log/sudo.log ]];then |
| 70 | + touch /var/log/sudo.log |
| 71 | + fi |
| 72 | + [[ $(grep "local2.debug" /etc/rsyslog.conf | wc -l) == 0 ]] && sed -i '$alocal2.debug /var/log/sudo.log' /etc/rsyslog.conf |
| 73 | + [[ $(grep "Defaults logfile" /etc/sudoers | wc -l) == 0 ]] && sed -i '$aDefaults logfile=/var/log/sudo.log' /etc/sudoers |
| 74 | + [[ $(grep "Defaults loglinelen" /etc/sudoers | wc -l) == 0 ]] && sed -i '$aDefaults loglinelen=0' /etc/sudoers |
| 75 | + [[ $(grep "Defaults \!syslog" /etc/sudoers | wc -l ) == 0 ]] && sed -i '$aDefaults !syslog' /etc/sudoers |
| 76 | + systemctl restart rsyslog |
| 77 | + echo "config sudo done." |
| 78 | +} # }}} |
| 79 | + |
| 80 | +disable_selinux(){ # {{{ |
| 81 | + judge_user |
| 82 | + sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config |
| 83 | + setenforce 0 |
| 84 | + echo "disable selinux done." |
| 85 | +} # }}} |
| 86 | + |
| 87 | +disable_firewalld(){ # {{{ |
| 88 | + judge_user |
| 89 | + systemctl stop firewalld |
| 90 | + systemctl disable firewalld |
| 91 | + echo "disable firewalld done." |
| 92 | +} # }}} |
| 93 | + |
| 94 | +disable_swap(){ # {{{ |
| 95 | + judge_user |
| 96 | + swapoff -a |
| 97 | + sed -i '/swap/d' /etc/fstab |
| 98 | + echo "disable swap done." |
| 99 | +} # }}} |
| 100 | + |
| 101 | +config_pip(){ # {{{ |
| 102 | + judge_user |
| 103 | + if [[ ! -d /root/.pip ]];then |
| 104 | + mkdir /root/.pip |
| 105 | + fi |
| 106 | + |
| 107 | +cat <<EOF > /root/.pip/pip.conf |
| 108 | +[global] |
| 109 | +index-url = http://mirrors.aliyun.com/pypi/simple/ |
| 110 | +[install] |
| 111 | +trusted-host=mirrors.aliyun.com |
| 112 | +EOF |
| 113 | + echo "config pip done." |
| 114 | +} # }}} |
| 115 | + |
| 116 | +config_shutdown_wait_time(){ # {{{ |
| 117 | + judge_user |
| 118 | + sed -i '/^#DefaultTimeoutStartSec/cDefaultTimeoutStartSec=10s' /etc/systemd/system.conf |
| 119 | + sed -i '/^#DefaultTimeoutStopSec/cDefaultTimeoutStopSec=10s' /etc/systemd/system.conf |
| 120 | + echo "config shutdown wait time done." |
| 121 | +} # }}} |
| 122 | + |
| 123 | +open_cron_log(){ # {{{ |
| 124 | + judge_user |
| 125 | + sed -i 's/#cron/cron/g' /etc/rsyslog.conf |
| 126 | + systemctl restart rsyslog |
| 127 | + echo "open cron log done." |
| 128 | +} # }}} |
| 129 | + |
| 130 | +config_clock(){ # {{{ |
| 131 | + judge_user |
| 132 | + timedatectl set-local-rtc 0 |
| 133 | + timedatectl set-timezone Asia/Shanghai |
| 134 | + echo "config clock done." |
| 135 | +} # }}} |
| 136 | + |
| 137 | +config_default_editor(){ # {{{ |
| 138 | + judge_user |
| 139 | + echo 'SELECTED_EDITOR="/usr/bin/vim.basic"' > /root/.selected_editor |
| 140 | + echo "config default editor done." |
| 141 | +} # }}} |
| 142 | + |
| 143 | +config_git(){ # {{{ |
| 144 | + judge_user |
| 145 | + if [[ -f /usr/bin/git ]];then |
| 146 | + git config --global core.editor vim |
| 147 | + git config --global core.quotepath false |
| 148 | + fi |
| 149 | + echo "config git done." |
| 150 | +} # }}} |
| 151 | + |
| 152 | +config_email(){ # {{{ |
| 153 | + judge_user |
| 154 | + find /var/mail/* -path "*/var/mail/*" -type f -delete |
| 155 | + echo "config email done." |
| 156 | +} # }}} |
| 157 | + |
| 158 | +reboot_server(){ # {{{ |
| 159 | + read -rp "Whether to restart ?(y or n):" choice |
| 160 | + case $choice in |
| 161 | + "y") echo -e '\033[1;32m You choose to reboot \033[0m' && reboot ;; |
| 162 | + "n") echo "You chose not to reboot" ;; |
| 163 | + *) echo "Input error please try again." && reboot_server ;; |
| 164 | + esac |
| 165 | +} # }}} |
| 166 | + |
| 167 | +all(){ # {{{ |
| 168 | + config_repository |
| 169 | + config_ssh |
| 170 | + config_sudo_privileges |
| 171 | + disable_selinux |
| 172 | + disable_firewalld |
| 173 | + disable_swap |
| 174 | + config_pip |
| 175 | + config_shutdown_wait_time |
| 176 | + open_cron_log |
| 177 | + config_clock |
| 178 | + config_default_editor |
| 179 | + config_git |
| 180 | + config_email |
| 181 | + reboot_server |
| 182 | +} # }}} |
| 183 | + |
| 184 | +if (($#==0)) |
| 185 | +then |
| 186 | + help_info |
| 187 | +else |
| 188 | + case $1 in |
| 189 | + help) help_info;; |
| 190 | + all) all;; |
| 191 | + repo) config_repository;; |
| 192 | + ssh) config_ssh;; |
| 193 | + sudo) config_sudo_privileges;; |
| 194 | + selinux) disable_selinux;; |
| 195 | + firewalld) disable_firewalld;; |
| 196 | + swap) disable_swap;; |
| 197 | + pip) config_pip;; |
| 198 | + shutdown) config_shutdown_wait_time;; |
| 199 | + cron) config_shutdown_wait_time;; |
| 200 | + clock) config_clock;; |
| 201 | + editor) config_default_editor;; |
| 202 | + git) config_git;; |
| 203 | + email) config_email;; |
| 204 | + *) echo "未识别的参数: $1";; |
| 205 | + esac |
| 206 | +fi |
0 commit comments