-
Notifications
You must be signed in to change notification settings - Fork 66
/
setup_ubuntu_host_env.sh
304 lines (254 loc) · 10.6 KB
/
setup_ubuntu_host_env.sh
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
#!/bin/bash
# -------------------------------------------------------------------------------
# Filename: setup_ubuntu_host_env.sh
# Revision: 1.0
# Date: 2017/08/05
# Author: hceng
# Email: huangcheng.job@foxmail.com
# Website: www.hceng.cn
# Function: setup ubuntu host env.
# Notes: learn
# -------------------------------------------------------------------------------
#
# Description:
#1.check env.
#1.1 check network
#1.2 check use root
#1.3 check set name
#1.4 configure samba
#2.update software sourcev.
#3.install vim tmux htop ftp ssh nfs samba.
#3.1 configure vim
#3.2 configure ftp
#3.3 configure nfs
#3.4 configure samba
#4.install system tool eg:g++ ...
#
# -------------------------------------------------------------------------------
#define echo print color.
RED_COLOR='\E[1;31m'
PINK_COLOR='\E[1;35m'
YELOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
GREEN_COLOR='\E[1;32m'
END_COLOR='\E[0m'
#Set linux host user name.
user_name=hceng
# Check network.
check_network() {
ping -c 1 www.baidu.com > /dev/null 2>&1
if [ $? -eq 0 ];then
echo -e "${GREEN_COLOR}Network OK.${END_COLOR}"
else
echo -e "${RED_COLOR}Network failure!${END_COLOR}"
exit 1
fi
}
# Check user must root.
check_root() {
if [ $(id -u) != "0" ]; then
echo -e "${RED_COLOR}Error: You must be root to run this script, please use root.${END_COLOR}"
exit 1
fi
}
# Check set linux host user name.
check_user_name() {
cat /etc/passwd|grep $user_name
if [ $? -eq 0 ];then
echo -e "${GREEN_COLOR}Check the set user name OK.${END_COLOR}"
else
echo -e "${RED_COLOR}Check the set user name failure!${END_COLOR}"
exit 1
fi
}
# Check the results of the operation.
check_status() {
ret=$?
if [ "$ret" -ne "0" ]; then
echo -e "${RED_COLOR}Failed setup, aborting..${END_COLOR}"
exit 1
fi
}
# Get the code name of the Linux host release to the caller.
get_host_type() {
local __host_type=$1
local the_host=`lsb_release -a 2>/dev/null | grep Codename: | awk {'print $2'}`
eval $__host_type="'$the_host'"
}
#This function will update the source of the software.
update_software_source() {
local back_file=/etc/apt/sources.list.backup
if [ ! -e "$back_file" ];then
cp /etc/apt/sources.list $back_file
fi
check_status
get_host_type host_release
check_status
echo \
"#Ali source.
deb-src http://archive.ubuntu.com/ubuntu $host_release main restricted
deb http://mirrors.aliyun.com/ubuntu/ $host_release main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ $host_release main restricted multiverse universe
deb http://mirrors.aliyun.com/ubuntu/ $host_release-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ $host_release-updates main restricted multiverse universe
deb http://mirrors.aliyun.com/ubuntu/ $host_release universe
deb http://mirrors.aliyun.com/ubuntu/ $host_release-updates universe
deb http://mirrors.aliyun.com/ubuntu/ $host_release multiverse
deb http://mirrors.aliyun.com/ubuntu/ $host_release-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ $host_release-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $host_release-backports main restricted universe multiverse
deb http://archive.canonical.com/ubuntu $host_release partner
deb-src http://archive.canonical.com/ubuntu $host_release partner
deb http://mirrors.aliyun.com/ubuntu/ $host_release-security main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ $host_release-security main restricted multiverse universe
deb http://mirrors.aliyun.com/ubuntu/ $host_release-security universe
deb http://mirrors.aliyun.com/ubuntu/ $host_release-security multiverse
#Netease source.
deb http://mirrors.163.com/ubuntu/ $host_release main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ $host_release-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ $host_release-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ $host_release-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ $host_release-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ $host_release main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ $host_release-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ $host_release-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ $host_release-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ $host_release-backports main restricted universe multiverse
#Official source
deb http://archive.ubuntu.com/ubuntu/ $host_release main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ $host_release-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ $host_release-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ $host_release-proposed main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ $host_release-backports main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ $host_release main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ $host_release-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ $host_release-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ $host_release-proposed main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ $host_release-backports main restricted universe multiverse" \
> /etc/apt/sources.list
check_status
#apt-get update 1>/dev/null
apt-get update
check_status
echo -e "${GREEN_COLOR}Update source completed.${END_COLOR}"
}
# Configure vim form github.
vim_configure() {
git clone --depth=1 https://github.com/amix/vimrc.git /home/$user_name/.vim_runtime
touch /home/$user_name/.vim_runtime/my_configs.vim
echo ":set number" > /home/$user_name/.vim_runtime/my_configs.vim
chown -R $user_name /home/$user_name/.vim_runtime
chmod u+x /home/$user_name/.vim_runtime/install_awesome_vimrc.sh
su - $user_name -s /home/$user_name/.vim_runtime/install_awesome_vimrc.sh
}
# Configure ftp.
ftp_configure() {
sed -i 's/#loacl_enable=YES/loacl_enable=YES/g' /etc/vsftpd.conf
sed -i 's/#write_enable=YES/write_enable=YES/g' /etc/vsftpd.conf
}
# Configure nfs.
nfs_configure() {
local work_file=/work
if [ ! -d "$work_file" ];then
mkdir /work
fi
grep "/work" /etc/exports 1>/dev/null
if [ $? -ne 0 ];then
sed -i '$a\/work *(rw,sync,no_root_squash,no_subtree_check)' /etc/exports
fi
}
# Configure samba.
samba_configure() {
local back_file=/etc/samba/smb.conf.bakup
if [ ! -e "$back_file" ];then
cp /etc/samba/smb.conf $back_file
fi
check_status
grep "/work" /etc/samba/smb.conf 1>/dev/null
if [ $? -ne 0 ];then
sed -i \
'$a[share_work]\n\
path = \/work\n\
available = yes\n\
public = yes\n\
guest ok = yes\n\
read only = no\n\
writeable = yes\n' /etc/samba/smb.conf
fi
/etc/init.d/samba restart
chmod -R 777 /work
}
# Execute an action.
FA_DoExec() {
echo -e "${BLUE_COLOR}==> Executing: '${@}'.${END_COLOR}"
eval $@ || exit $?
}
# Install list software.
install_software() {
local install_software_list=("git" "vim" "tmux" "htop" "vsftpd" "openssh-server" "nfs-kernel-server" "portmap" "samba")
echo -e "${PINK_COLOR}install_software_list:${install_software_list[*]}.${END_COLOR}"
#install git
if (echo "${install_software_list[@]}" | grep -wq "git");then
apt-get -y install git && echo -e "${BLUE_COLOR}git install completed.${END_COLOR}"
fi
#install and configure vim
if (echo "${install_software_list[@]}" | grep -wq "vim");then
apt-get -y install vim && vim_configure && echo -e "${BLUE_COLOR}vim install completed.${END_COLOR}"
fi
#install tmux
if (echo "${install_software_list[@]}" | grep -wq "tmux");then
apt-get -y install tmux && echo -e "${BLUE_COLOR}tmux install completed.${END_COLOR}"
fi
#install htop
if (echo "${install_software_list[@]}" | grep -wq "htop");then
apt-get -y install htop && echo -e "${BLUE_COLOR}htop install completed.${END_COLOR}"
fi
#install and configure vsftpd
if (echo "${install_software_list[@]}" | grep -wq "vsftpd");then
apt-get -y install vsftpd && ftp_configure && echo -e "${BLUE_COLOR}vsftpd install completed.${END_COLOR}"
fi
#install openssh-server
if (echo "${install_software_list[@]}" | grep -wq "openssh-server");then
apt-get -y install openssh-server && echo -e "${BLUE_COLOR}openssh-server install completed.${END_COLOR}"
fi
#install and configure nfs-kernel-server
if (echo "${install_software_list[@]}" | grep -wq "nfs-kernel-server");then
apt-get -y install nfs-kernel-server && nfs_configure && \
/etc/init.d/nfs-kernel-server restart && echo -e "${BLUE_COLOR}nfs-kernel-server install completed.${END_COLOR}"
fi
#install portmap
if (echo "${install_software_list[@]}" | grep -wq "portmap");then
apt-get -y install portmap && echo -e "${BLUE_COLOR}portmap install completed.${END_COLOR}"
fi
#install and configure samba
if (echo "${install_software_list[@]}" | grep -wq "samba");then
apt-get -y install samba && samba_configure && echo -e "${BLUE_COLOR}samba install completed.${END_COLOR}"
fi
#others
get_host_type host_release
FA_DoExec apt-get -y install \
gnupg flex bison gperf build-essential \
zip curl libc6-dev libncurses5-dev libncurses5-dev:i386 x11proto-core-dev \
libx11-dev:i386 libreadline6-dev:i386 \
libgl1-mesa-glx-lts-$host_release:i386 libgl1-mesa-dev-lts-$host_release \
g++-multilib mingw32 tofrodos libncurses5-dev:i386 \
python-markdown libxml2-utils xsltproc zlib1g-dev:i386
if [ ! -h /usr/lib/i386-linux-gnu/libGL.so ]; then
FA_DoExec ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 \
/usr/lib/i386-linux-gnu/libGL.so
fi
# Development support
FA_DoExec apt-get -y install \
dos2unix minicom gawk
echo -e "${GREEN_COLOR}software install completed.${END_COLOR}"
}
check_network
check_root
check_user_name
update_software_source
install_software
echo -e "${GREEN_COLOR}===================================================${END_COLOR}"
echo -e "${GREEN_COLOR}============setup ubuntu host env ok!==============${END_COLOR}"
echo -e "${GREEN_COLOR}===================================================${END_COLOR}"
su $user_name
exit 0