-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathubuntu-init.sh
More file actions
executable file
·637 lines (550 loc) · 17.8 KB
/
Copy pathubuntu-init.sh
File metadata and controls
executable file
·637 lines (550 loc) · 17.8 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
#!/bin/bash
set -x -e -u
set -o pipefail
# 支持 Ubuntu 18.04 / 20.04 / 22.04 / 24.04+
config_url="https://raw.githubusercontent.com/Pterosaur/linux-config/master/conf/"
local_conf_dir=""
init_conf_flag="Zegan conf init"
# 获取配置文件内容(优先本地,否则远程)
get_config() {
# 参数: 1 = 相对路径 (如 vimrc, fish/config.fish)
if [[ -n "${local_conf_dir}" && -f "${local_conf_dir}/${1}" ]]; then
cat "${local_conf_dir}/${1}"
else
curl -fsSL "${config_url}${1}"
fi
}
# 显示帮助信息
show_help() {
cat <<'EOF'
用法: ubuntu-init.sh [选项] [模块...]
一键初始化 Linux 开发环境。
可用模块:
git - Git 配置
ssh - SSH 客户端配置 (KeepAlive, 免确认等)
zsh - Zsh + Oh My Zsh
tmux - Tmux 终端复用器
vim - Vim 编辑器配置
tools - 常用工具 (htop, tree, fzf, ripgrep 等)
fish - Fish shell + 常用缩写
atuin - Atuin 历史记录管理
nodejs - Node.js (通过 nvm 安装)
dev - 开发工具链 (build-essential, cmake 等)
samba - Samba 文件共享
docker - Docker 引擎
kvm - KVM 虚拟化
kde - KDE 桌面环境
xrdp - XRDP 远程桌面
openclaw - OpenClaw AI 助手 (不在默认模块中)
默认模块: git ssh zsh tmux vim tools
选项:
--help, -h 显示此帮助信息
--list 列出所有可用模块
--local <目录> 使用本地配置文件目录(Docker 构建时使用)
示例:
bash ubuntu-init.sh # 安装默认模块
bash ubuntu-init.sh git zsh # 只安装指定模块
bash ubuntu-init.sh --help # 显示帮助
EOF
exit 0
}
# 列出所有可用模块
list_modules() {
echo "可用模块:"
echo " 默认: git ssh zsh tmux vim tools"
echo " 可选: fish atuin nodejs dev samba docker kvm kde xrdp openclaw"
exit 0
}
execute() {
# args
# 1: command
# 2: run as root > 0, else as current user
local prefix=""
if [[ $# -gt 1 && $2 -gt 0 ]]
then
# If the user is root
if [[ $EUID -ne 0 ]]
then
prefix="sudo"
fi
fi
if [[ $- == *i* ]]
then
tput sgr0
fi
${prefix} bash -c " $1"
}
is_command() {
# args
# 1: command name
if command -v $1 > /dev/null 2>&1
then
echo 1
else
echo 0
fi
}
# 已安装包缓存(延迟加载,避免 apt 数据库未初始化时出错)
installed_pkgs=""
installed_pkgs_loaded=0
# 刷新已安装包缓存
_refresh_installed_pkgs() {
installed_pkgs=$(dpkg-query -W -f='${Package} ' 2>/dev/null || true)
installed_pkgs_loaded=1
}
is_installed() {
# 参数: 1 = 包名
if [[ ${installed_pkgs_loaded} -eq 0 ]]; then
_refresh_installed_pkgs
fi
if [[ " ${installed_pkgs} " == *" ${1} "* ]]
then
echo 1
else
# 刷新一次缓存再试
_refresh_installed_pkgs
if [[ " ${installed_pkgs} " == *" ${1} "* ]]
then
echo 1
else
echo 0
fi
fi
}
install_modules() {
# args
# 1: package name
if [[ $(is_command "apt-fast") == 0 ]]
then
execute "apt-get update" 1
execute "printf \"6\n70\n\" | apt-get install -y expect" 1
execute "apt-get install -y software-properties-common" 1
execute "add-apt-repository main" 1
execute "add-apt-repository universe" 1
execute "add-apt-repository restricted" 1
execute "add-apt-repository multiverse" 1
execute "add-apt-repository -y ppa:apt-fast/stable" 1
execute "apt-get update" 1
execute "printf \"1\n$(grep -c ^processor /proc/cpuinfo)\nyes\n\" | apt-get -y install apt-fast " 1
fi
if [[ $(is_installed ${1}) == 0 ]]
then
execute "apt-fast install -y ${@}" 1
fi
}
write_config() {
# args
# 1: config name
# 2: content
# 3: action == 1 overwirte, else append
# 4: need_sudo == 1 sudo, else no
local file=$1
local content=$( base64 <<< "${2}" )
local action=">>"
if [ ! -e $file ]
then
touch $file
fi
if [[ $# -gt 2 && $3 -eq 1 ]]
then
action=">"
fi
local need_sudo=0
if [[ $# -gt 3 && $4 -eq 1 ]]
then
need_sudo=1
fi
execute "printf \"${content}\n\" | base64 -d ${action} ${file}" "${need_sudo}"
execute "printf \"\n\" >> ${file}" "${need_sudo}"
}
init_vim() {
# 安装 vim
local vimdir="${HOME}/.vim"
install_modules "vim"
# 配置 vimrc
local vimrc="${HOME}/.vimrc"
local content="$(get_config "vimrc")"
if [[ -e ${vimrc} && $(cat ${vimrc}) == *"${init_conf_flag}"* ]]
then
return
fi
write_config "${vimrc}" "\" ${init_conf_flag}"
write_config "${vimrc}" "${content}"
if [[ ! -d ${vimdir} ]] || [[ $( find ${vimdir} -name 'pathogen*' | wc -l ) -eq 0 ]]
then
# pathogen
execute "mkdir -p ~/.vim/autoload ~/.vim/bundle && curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim"
write_config "${vimrc}" "execute pathogen#infect()"
fi
# 修改主题
execute "git clone https://github.com/morhetz/gruvbox.git ~/.vim/bundle/gruvbox"
write_config "${vimrc}" "set t_Co=256"
write_config "${vimrc}" "colorscheme gruvbox"
write_config "${vimrc}" "set background=dark"
write_config "${vimrc}" "\" ${init_conf_flag}"
}
init_git() {
# 安装 git
install_modules "git"
execute "git config --global core.editor \"vim\""
# 部署额外 git 配置 (alias 等)
local git_extra="${HOME}/.gitconfig.d"
execute "mkdir -p ${git_extra}"
local git_conf_file="${git_extra}/linux-config.inc"
if [[ ! -f "${git_conf_file}" ]]; then
local content="$(get_config "git.conf")"
write_config "${git_conf_file}" "${content}" 1
# 让 ~/.gitconfig 引用这个文件
execute "git config --global include.path ${git_conf_file}"
fi
}
init_ssh() {
# 部署 SSH 客户端配置
install_modules "openssh-client"
local ssh_dir="${HOME}/.ssh"
local ssh_conf="${ssh_dir}/config"
execute "mkdir -p ${ssh_dir}"
execute "chmod 700 ${ssh_dir}"
if [[ -f "${ssh_conf}" ]] && [[ $(cat "${ssh_conf}") == *"${init_conf_flag}"* ]]
then
return
fi
local content="$(get_config "ssh.config")"
write_config "${ssh_conf}" "# ${init_conf_flag}"
write_config "${ssh_conf}" "${content}"
write_config "${ssh_conf}" "# ${init_conf_flag}"
execute "chmod 600 ${ssh_conf}"
}
init_zsh() {
# 安装 zsh
install_modules "zsh"
if [[ ! -e "${HOME}/.oh-my-zsh" ]]
then
execute 'print "exit\n" | sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"'
fi
execute "chsh -s `which zsh` `whoami`" 1
local zshrc="${HOME}/.zshrc"
if [[ -e ${zshrc} && $(cat ${zshrc}) == *"${init_conf_flag}"* ]]
then
return
fi
write_config "${zshrc}" "# ${init_conf_flag}"
execute "sed -i -E \"s/^ZSH_THEME=.*$/ZSH_THEME=\\\"ys\\\"/\" ${zshrc}"
execute "sed -i -E \"s/^plugins=\(/plugins=\( extract z sudo fzf-tab zsh-completions /\" ${zshrc}"
local content=$(get_config "zshrc")
write_config "${zshrc}" "${content}"
execute "git clone https://github.com/Aloxaf/fzf-tab ${HOME}/.oh-my-zsh/custom/plugins/fzf-tab || true"
execute "git clone https://github.com/zsh-users/zsh-completions.git ${HOME}/.oh-my-zsh/custom/plugins/zsh-completions || true"
}
init_samba() {
# 安装 samba
install_modules "samba"
install_modules "smbclient"
# 配置 samba
local smbconf="/etc/samba/smb.conf"
local user=$(whoami)
local passwd="000000"
local content="$(get_config "smb.conf")"
if [[ $(cat ${smbconf}) == *"${init_conf_flag}"* ]]
then
return
fi
# 替换占位符为实际用户名和家目录
content="${content//__USER__/${user}}"
content="${content//__HOME__/${HOME}}"
write_config "${smbconf}" "# ${init_conf_flag}" 0 1
write_config "${smbconf}" "${content}" 0 1
execute "printf \"${passwd}\n${passwd}\" | sudo smbpasswd -a ${user} -s"
execute "service smbd restart" 1
}
init_tmux() {
# 安装 tmux
install_modules "tmux"
# 配置 tmux
local tmuxconf="$HOME/.tmux.conf"
local content="$(get_config "tmux.conf")"
if [[ -f "${tmuxconf}" ]] && [[ $(cat ${tmuxconf}) == *"${init_conf_flag}"* ]]
then
:
else
write_config "${tmuxconf}" "# ${init_conf_flag}"
write_config "${tmuxconf}" "${content}"
write_config "${tmuxconf}" "# ${init_conf_flag}"
fi
# 安装 TPM (Tmux Plugin Manager)
if [[ ! -d "${HOME}/.tmux/plugins/tpm" ]]; then
execute "git clone https://github.com/tmux-plugins/tpm ${HOME}/.tmux/plugins/tpm || true"
fi
local bash_aliases="$HOME/.bash_aliases"
if [[ ! -f "${bash_aliases}" ]] || [[ $(cat ${bash_aliases}) != *tmux* ]]
then
write_config "${bash_aliases}" "alias tmux='tmux -2'"
write_config "${bash_aliases}" "alias tn='tmux -2 new-session -s'"
write_config "${bash_aliases}" "alias tnw='tmux -2 new-window'"
write_config "${bash_aliases}" "alias tl='tmux -2 list-session'"
write_config "${bash_aliases}" "alias ta='tmux -2 attach -t'"
fi
}
init_tools() {
# 安装常用工具
tools_packages=(
"htop"
"tree"
"telnet"
"net-tools"
"iputils-ping"
"iproute2"
"iproute2-doc"
"fzf"
"ripgrep"
)
execute "apt-fast install -y ${tools_packages[*]}" 1
init_man
}
init_docker() {
#install docker
if [[ $(is_command "docker") == 0 ]]
then
execute 'sh -c "$(curl -fsSL get.docker.com)"' 1
fi
if [[ $(id -u) != 0 ]]
then
execute "usermod -aG docker $(whoami)" 1
fi
}
init_dev() {
# 安装开发工具
dev_packages=(
"build-essential"
"python3-dev"
"python3-pip"
"cmake"
"libtool"
"m4"
"automake"
"gdb"
"vim-scripts"
"vim-doc"
)
execute "apt-fast install -y ${dev_packages[*]}" 1
#install vim plugin
local vimdir="${HOME}/.vim"
local vimrc="${HOME}/.vimrc"
install_modules "vim-addon-manager"
if [[ $( find ${vimdir} -name 'pathogen.vim' | wc -l ) -eq 0 ]]
then
# pathogen
execute "mkdir -p ${vimdir}autoload ${vimdir}/bundle && curl -LSso ${vimdir}/autoload/pathogen.vim https://tpo.pe/pathogen.vim"
write_config "${vimrc}" "execute pathogen#infect()"
fi
#install ConqueGDB
if [[ $(find ${vimdir} -name 'conque_gdb.vim' | wc -l) -eq 0 ]]
then
execute "git clone https://github.com/vim-scripts/Conque-GDB.git ${vimdir}/bundle/Conque-GDB"
local conquegdb="$(get_config "vimrc.conquegdb")"
write_config "${vimrc}" "${conquegdb}"
fi
if [[ $( find ${vimdir} -name 'youcompleteme*' | wc -l ) -eq 0 ]]
then
# YouCompleteMe
execute "apt-fast install -y vim-youcompleteme" 1
execute "vim-addon-manager install youcompleteme"
local ycm="$(get_config "vimrc.youcompleteme")"
write_config "${vimrc}" "${ycm}"
fi
if [[ $( find ${vimdir} -name 'nerdtree*' | wc -l ) -eq 0 ]]
then
# NerdTree
execute "git clone https://github.com/scrooloose/nerdtree.git ${vimdir}/bundle/nerdtree"
local nt="$(get_config "vimrc.nerdtree")"
write_config "${vimrc}" "${nt}"
fi
if [[ $( find ${vimdir} -name 'taglist*' | wc -l ) -eq 0 ]]
then
# tag list
execute "apt-fast install -y universal-ctags" 1
execute "vim-addon-manager install taglist"
fi
if [[ $( find ${vimdir} -name 'winmanager.vim' | wc -l ) -eq 0 ]]
then
# WinManager
execute "vim-addon-manager install winmanager"
local wm_vim="${vimdir}/plugin/winmanager.vim"
local wm="$(get_config "winmanager.vim")"
write_config "${wm_vim}" "${wm}" 0 1
local wm="$(get_config "vimrc.winmanager")"
write_config "${vimrc}" "${wm}"
fi
if [[ $( find ${vimdir} -name 'minibufexpl.vim' | wc -l ) -eq 0 ]]
then
execute "wget https://raw.githubusercontent.com/fholgado/minibufexpl.vim/master/plugin/minibufexpl.vim -O ${vimdir}/plugin/minibufexpl.vim "
local minibufexpl="$(get_config "vimrc.minibufexpl")"
write_config "${vimrc}" "${minibufexpl}"
fi
if [[ $( find ${vimdir} -name 'ycm-generator.vim' | wc -l ) -eq 0 ]]
then
execute "apt-fast install -y clang" 1
execute "git clone https://github.com/rdnetto/YCM-Generator.git ${vimdir}/bundle/YCM-Generator"
fi
if [[ $( find ${vimdir} -name 'color_coded.vim' | wc -l ) -eq 0 ]]
then
if ! [[ $(vim --version) =~ (lua5.([0-9]+)) ]]
then
echo "Miss vim that support Lua"
exit 1
fi
execute "apt-fast install -y build-essential libtinfo5 libclang-dev libncurses-dev libz-dev cmake xz-utils libpthread-workqueue-dev lib${BASH_REMATCH[1]}-dev ${BASH_REMATCH[1]} " 1
execute "git clone https://github.com/jeaye/color_coded.git ${vimdir}/bundle/color_coded"
execute "cd ${vimdir}/bundle/color_coded && mkdir -p build && cd build && cmake .. && make -j && sudo make install && make clean && make clean_clang"
fi
}
init_man() {
execute "yes | unminimize || true" 1
man_packets=(
"man"
"man-db"
"manpages"
"manpages-dev"
"freebsd-manpages"
"man2html"
"manpages-posix"
"manpages-posix-dev"
)
execute " apt-fast install -y ${man_packets[*]} " 1
}
init_kvm() {
# 安装 KVM 虚拟化 (兼容新旧版本 Ubuntu)
local kvm_packages="qemu-kvm virtinst bridge-utils cpu-checker"
# Ubuntu 22.04+ 使用 libvirt-daemon-system,旧版使用 libvirt-bin
if apt-cache show libvirt-daemon-system >/dev/null 2>&1; then
kvm_packages="${kvm_packages} libvirt-daemon-system libvirt-clients"
else
kvm_packages="${kvm_packages} libvirt-bin"
fi
execute "apt-get install -y ${kvm_packages}" 1
}
init_kde() {
install_modules "expect"
execute "cat <<EOF | expect
set timeout -1
spawn apt-get install -y kubuntu-desktop
expect \"Default display manager: \"
send \"sddm\n\"
expect eof
EOF" 1
}
init_xrdp() {
execute "apt-get install -y xrdp" 1
execute "sed -e 's/^new_cursors=true/new_cursors=false/g' \
-i /etc/xrdp/xrdp.ini" 1
execute "systemctl enable xrdp" 1
execute "systemctl restart xrdp" 1
execute "echo \"startkde\" > ~/.xsession"
execute "cat <<EOF > ~/.xsessionrc
export XDG_SESSION_DESKTOP=KDE
export XDG_DATA_DIRS=/usr/share/plasma:/usr/local/share:/usr/share:/var/lib/snapd/desktop
export XDG_CONFIG_DIRS=/etc/xdg/xdg-plasma:/etc/xdg:/usr/share/kubuntu-default-settings/kf5-settings
EOF"
execute "cat <<EOF | \
sudo tee /etc/polkit-1/localauthority/50-local.d/xrdp-NetworkManager.pkla
[Netowrkmanager]
Identity=unix-group:sudo
Action=org.freedesktop.NetworkManager.network-control
ResultAny=yes
ResultInactive=yes
ResultActive=yes
EOF"
execute "cat <<EOF | \
sudo tee /etc/polkit-1/localauthority/50-local.d/xrdp-packagekit.pkla
[Netowrkmanager]
Identity=unix-group:sudo
Action=org.freedesktop.packagekit.system-sources-refresh
ResultAny=yes
ResultInactive=auth_admin
ResultActive=yes
EOF"
execute "systemctl restart polkit" 1
}
init_fish() {
# 安装 fish shell
install_modules "fish"
# 创建 fish 配置目录
local fish_conf_dir="${HOME}/.config/fish"
local fish_conf="${fish_conf_dir}/config.fish"
execute "mkdir -p ${fish_conf_dir}"
if [[ -f "${fish_conf}" ]] && [[ $(cat ${fish_conf}) == *"${init_conf_flag}"* ]]
then
return
fi
local content="$(get_config "fish/config.fish")"
write_config "${fish_conf}" "# ${init_conf_flag}"
write_config "${fish_conf}" "${content}"
write_config "${fish_conf}" "# ${init_conf_flag}"
}
init_atuin() {
# 安装 atuin 历史记录管理
if [[ $(is_command "atuin") == 0 ]]
then
execute 'bash -c "$(curl --proto =https --tlsv1.2 -sSf https://setup.atuin.sh)"'
fi
}
init_nodejs() {
# 通过 nvm 安装 Node.js
if [[ ! -d "${HOME}/.nvm" ]]
then
execute 'bash -c "$(curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh)"'
fi
# 安装最新 LTS 版本
export NVM_DIR="${HOME}/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
execute "bash -c 'source ${HOME}/.nvm/nvm.sh && nvm install --lts'"
}
init_openclaw() {
# 安装 OpenClaw AI 助手 (可选模块,不在默认列表中)
# 需要先安装 Node.js
if [[ ! -d "${HOME}/.nvm" ]]
then
init_nodejs
fi
execute "bash -c 'source ${HOME}/.nvm/nvm.sh && npm install -g openclaw'"
}
main() {
# 解析参数
local args=()
while [[ $# -gt 0 ]]; do
case "$1" in
--help|-h) show_help ;;
--list) list_modules ;;
--local)
# 使用本地配置文件目录(Docker 构建时使用)
shift
local_conf_dir="$1"
;;
*) args+=("$1") ;;
esac
shift
done
set -- "${args[@]+"${args[@]}"}"
# 默认模块: git ssh zsh tmux vim tools
# 可选模块: fish atuin nodejs dev samba docker kvm kde xrdp openclaw
local init_modules=("git" "ssh" "zsh" "tmux" "vim" "tools")
if [ $# -gt 0 ]
then
init_modules=($@)
fi
echo 'Install "'${init_modules[*]}'"'
install_modules "sudo"
install_modules "curl"
install_modules "wget"
for module in ${init_modules[@]};
do
# 检查模块函数是否存在
if declare -f "init_${module}" > /dev/null 2>&1; then
"init_${module}"
else
echo "错误: 未知模块 '${module}'"
exit 1
fi
done
}
main "$@"