一键配置脚本(用户添加配置、源修改、ss&BBR安装)

最近在各个服务器提供商中尝试适合的服务器,然而给多个服务器初始配置确实很是繁琐无聊。
索性自己写个脚本,这样一接手新的服务器就能自动配置,省下时间来好好折腾别的。


功能

目前适用环境:CentOS 5+、Ubuntu、Debian

  1. 显示系统信息
  2. 修改源为国内阿里云源镜像(也可修改参数配置其他源)
  3. 创建指定用户、创建Home目录、指定Shell、添加到 sudoers (可修改参数)
  4. 下载.bashrc、.vimrc 配置工作环境(来自博主自己)
  5. 安装Shadowsocks 、BBR(可选、来源 https://teddysun.com/ 秋水逸冰)
  6. 配置Java、Python等(待续完善)

脚本代码

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
#!/bin/bash
#########################################################################
# File Name: quickInit.sh
# Author: single-wolf
# mail: root@mail.zhongm.in
# Created Time: 2017年10月12日 10:43:49
#########################################################################
#
#Auto init ECS or VPS include useradd ,install pakeage ,add sys-config
#
#新建用户名
defaultUser='MyVPS'
#新用户默认Shell
defaultShell='bash'
#常用工具安装
initPakeage="sudo wget vim git dstat $defaultShell"
#自定义源URL,适用于Ubuntu、Debian
srcUrl='http://mirrors.aliyun.com/'

red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'

#待续
version_jdk=9

[[ $EUID -ne 0 ]] && echo -e "${red}ERROR:${plain} This script must be run as root!" && exit 1

if [ -f /etc/redhat-release ]; then
release="centos"
elif cat /etc/issue | grep -Eqi "debian"; then
release="debian"
elif cat /etc/issue | grep -Eqi "ubuntu"; then
release="ubuntu"
elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
release="centos"
elif cat /proc/version | grep -Eqi "debian"; then
release="debian"
elif cat /proc/version | grep -Eqi "ubuntu"; then
release="ubuntu"
elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
release="centos"
fi

get_opsy() {##获取系统信息
[ -f /etc/redhat-release ] && awk '{print ($1,$3~/^[0-9]/?$3:$4)}' /etc/redhat-release && return
[ -f /etc/os-release ] && awk -F'[= "]' '/PRETTY_NAME/{print $3,$4,$5}' /etc/os-release && return
[ -f /etc/lsb-release ] && awk -F'[="]+' '/DESCRIPTION/{print $2}' /etc/lsb-release && return
}

get_version(){##获取版本
if [[ "$release" == "centos" ]];then
[ -f /etc/os-release ] && awk -F'[="]+' '/VERSION_ID/{print $2}' /etc/os-release && return
elif [[ "$release" == "debian" ]];then
[ -f /etc/os-release ] && awk -F'[()]' '/VERSION=/{print $2}' /etc/os-release && return
elif [[ "$release" == "ubuntu" ]];then
[ -f /etc/lsb-release ] && awk -F'[="]+' '/DISTRIB_CODENAME/{print $2}' /etc/lsb-release && return
else
echo -e "${red}ERROR:${plain} OS is not be supported, please change to CentOS/Debian/Ubuntu and try again."
exit 1
fi
}

opsy=$( get_opsy )
arch=$( uname -m )
lbit=$( getconf LONG_BIT )
kern=$( uname -r )
version=$( get_version )

get_char() {
SAVEDSTTY=`stty -g`
stty -echo
stty cbreak
dd if=/dev/tty bs=1 count=1 2> /dev/null
stty -raw
stty echo
stty $SAVEDSTTY
}

init_install(){##修改源、安装常用软件
if [[ "$release" == "debian" ]];then
file="/etc/apt/sources.list"
[[ ! -e $file ]] && echo -e "${red}ERROR:${plain}src file $file not exist,please check it!" && exit 1
cp $file "$file.`date +%F`"
echo "#Created By quickInit.sh `date +%F`">$file
for kind in "deb" "deb-src";do
for sort in "" "-updates" ;do
echo "$kind $srcUrl$release $version$sort main contrib non-free" >> $file
done
echo "$kind $srcUrl$release-security $version/updates main contrib non-free" >> $file
done
apt-get update
apt-get -y install $initPakeage
if [[ $? -ne 0 ]];then
echo -e "${red}ERROR:${plain} Install InitPakeages failed, please check it."
exit 1
fi
elif [[ "$release" == "ubuntu" ]];then
file="/etc/apt/sources.list"
[[ ! -e $file ]] && echo -e "${red}ERROR:${plain}src file $file not exist,please check it!" && exit 1
cp $file "$file.`date +%F`"
echo "#Created By quickInit.sh `date +%F`">$file
for kind in "deb" "deb-src";do
for sort in "" "-updates" "-security";do
for free in "main" "universe";do
echo "$kind $srcUrl$release $version$sort $free" >> $file
done
done
done
apt-get update
apt-get -y install $initPakeage
if [[ $? -ne 0 ]];then
echo -e "${red}ERROR:${plain} Install InitPakeages failed, please check it."
exit 1
fi
elif [[ "$release" == "centos" ]];then
file="/etc/yum.repos.d/CentOS-Base.repo"
[[ ! -e $file ]] && echo -e "${red}ERROR:${plain}src file $file not exist,please check it!" && exit 1
cp $file "$file.`date +%F`"
if [[ ! -e "/usr/bin/wget" ]];then
wget -O $file "http://mirrors.aliyun.com/repo/Centos-$version.repo"
elif [[ -e "/usr/bin/curl" ]];then
curl -o $file "http://mirrors.aliyun.com/repo/Centos-$version.repo"
else
yum -y install wget && wget -O $file "http://mirrors.aliyun.com/repo/Centos-$version.repo"
fi
yum clean all && yum makecache
yum -y install $initPakeage
if [[ $? -ne 0 ]];then
echo -e "${red}ERROR:${plain} Install InitPakeages failed, please check it."
exit 1
fi
else
echo -e "${red}ERROR:${plain} OS is not be supported, please change to CentOS/Debian/Ubuntu and try again."
exit 1
fi
echo -e "${green}INFO:Init and install $initPakeage successfully."
}

adduser(){##添加用户、修改sudoers
shell="/bin/$defaultShell"
useradd $defaultUser -m -s $shell
[ $? -ne 0 ] && echo -e "${red}ERROR:${plain} Add user $defaultUser failed,please check it" && exit 1
echo -e "${green}INFO:${plain}Input password for your newUser"
passwd $defaultUser
while [ $? -ne 0 ]
do
passwd $defaultUser
done
echo -e "${green}INFO:${plain}Add user $defaultUser successfully"
chmod +w /etc/sudoers
echo "$defaultUser ALL=(ALL) ALL" >> /etc/sudoers
[ $? -ne 0 ] && echo -e "${red}ERROR:${plain} Give user $defaultUser failed" && chmod -w /etc/sudoers && exit 1
chmod -w /etc/sudoers
echo -e "${green}INFO:${plain}Give user $defaultUser Rootright successfully"
}
userconfig(){##配置.bash .vimrc环境
home="/home/$defaultUser/"
cd $home
wget -O .profile "http://cloud.zhongm.in/f/c92022b083/?raw=1" && wget -O .bashrc "http://cloud.zhongm.in/f/e933c3dcf0/?raw=1" && wget -O .vimrc "http://cloud.zhongm.in/f/1cecaee058/?raw=1" && echo -e "${green}INFO:${plain}Download userconfig successfully,relogin in to use it."
chown $defaultUser:$defaultUser .*
[ $? -ne 0 ] && echo -e "${red}ERROR:${plain}User config failed!"
}

disableRootLogin(){##禁用Root登录
config="/etc/ssh/sshd_config"
if [[ -e $config ]];then
if [[ -w $config ]];then
chmod +w $config
fi
sed -i '/^PermitRootLogin/'d $config && echo "PermitRootLogin no">>$config
[ $? -ne 0 ] && echo -e "${red}ERROR:${plain} Disable RootLogin failed" && exit 1
else
echo -e "${red}ERROR:${plain}config file $config not exist,please check it!" && exit 1
fi
echo -e "${green}INFO:${plain}Disable RootLogin successfully"
}

#install_java(){##安装Java,待续
# if [ -n `which java` ];then
# version_local=`java -version>version.tmp 2>&1 && awk -F'["]' '/java version/{print $2}' version.tmp && rm version.tmp`
# if [ $version_local -eq $version_jdk ];then
# echo -e "${green}INFO:${plain}JDK$version_jdk alreadly exists!"
# return
# else
# echo -e "${yellow}INFO:${plain}Prepare to remove local JDK$version_local,and install JDK$version_jdk"
# fi
# else
# fi
#}

install_ss(){##安装Shadowsocks
wget --no-check-certificate -O shadowsocks-all.sh https://raw.githubusercontent.com/teddysun/shadowsocks_install/master/shadowsocks-all.sh
chmod +x shadowsocks-all.sh
./shadowsocks-all.sh 2>&1 | tee shadowsocks-all.log
}

install_bbr(){##安装BBR
wget --no-check-certificate https://github.com/teddysun/across/raw/master/bbr.sh
chmod +x bbr.sh
./bbr.sh
}

clear
echo "---------- System Information ----------"
echo " OS : $opsy"
echo " Arch : $arch ($lbit Bit)"
echo " Kernel : $kern"
echo " Version : $version"
echo "----------------------------------------"
echo
echo "Press any key to start...or Press Ctrl+C to cancel"
char=`get_char`
init_install
adduser
userconfig
disableRootLogin
install_ss
exit 1

GitHub位置 一键快速配置

功能有待完善。欢迎拍砖

觉得不错不妨打赏一笔