-
Notifications
You must be signed in to change notification settings - Fork 1
/
wsl2_init.sh
325 lines (261 loc) · 12.8 KB
/
wsl2_init.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/env bash
## https://docs.microsoft.com/zh-cn/windows/wsl/
## https://docs.microsoft.com/zh-cn/windows/wsl/wsl-config
## Please make sure that virtualization is enabled inside BIOS
## https://learn.microsoft.com/en-us/windows/wsl/install
## 1. run PowerShell as Admin
# wsl --list --online
## wsl --install -d <DistroName>
# wsl --install -d Debian
# wsl --status
# Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux -NoRestart
# Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform
## or:
# dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
# dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
## 2. restart computer
## 3. make WSL 2 as default architecture
# wsl --set-default-version 2
## 4. install distro from Microsoft Store (https://aka.ms/wslstore)
## 5. list installed distro
# wsl -l
## 6. set a distro to be backed by WSL 2
# wsl --set-version Debian 2
## 5. verify what versions of WSL each distro is using
# wsl -l -v
## set default distro
# wsl --set-default Debian
## restart wsl in PowerShell
# Restart-Service -Name LxssManager
# net stop "LxssManager"; net start "LxssManager"
## Uninstall distro
# wsl --set-default Ubuntu
# wsl --unregister Debian
## Then uninstall `Debian` from `Start Menu`
## Restart Computer
## Sharing your WSL2 environment with Linux
## https://www.nmelnick.com/2020/07/sharing-your-wsl2-environment-with-linux/
# sudo apt install -y libguestfs-tools
# sudo yum install -y libguestfs libguestfs-tools
## `WSL2` also need a kernel
# sudo apt install -y linux-image-generic
## Virtual machine disk image file:
## Ubuntu distro: /mnt/c/Users/<username>/AppData/Local/Packages/CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc/LocalState/ext4.vhdx
## Debian distro: /mnt/c/Users/<username>/AppData/Local/Packages/TheDebianProject.DebianGNULinux_76v4gfsz19hv4/LocalState/ext4.vhdx
## list distro filesystems
# sudo virt-filesystems -a <image file>
## mount Debian distro
# sudo mkdir -p /mnt/wslDebian
# sudo guestmount -o allow_other \
# -a /mnt/c/Users/<username>/AppData/Local/Packages/TheDebianProject.DebianGNULinux_76v4gfsz19hv4/LocalState/ext4.vhdx \
# -i /mnt/wslDebian
## unmount Debian distro
# sudo guestunmount /mnt/wslDebian
[[ -z "${MY_SHELL_SCRIPTS}" ]] && MY_SHELL_SCRIPTS="$HOME/.dotfiles"
# Load custom functions
if type 'colorEcho' 2>/dev/null | grep -q 'function'; then
:
else
if [[ -s "${MY_SHELL_SCRIPTS}/custom_functions.sh" ]]; then
source "${MY_SHELL_SCRIPTS}/custom_functions.sh"
else
echo "${MY_SHELL_SCRIPTS}/custom_functions.sh does not exist!"
exit 0
fi
fi
if ! check_os_wsl; then
colorEcho "${RED}Please run this script in ${FUCHSIA}WSL(Windows Subsystem for Linux)${RED}!"
exit 0
fi
[[ -z "${CURL_CHECK_OPTS[*]}" ]] && Get_Installer_CURL_Options
[[ -z "${AXEL_DOWNLOAD_OPTS[*]}" ]] && Get_Installer_AXEL_Options
# Use proxy or mirror when some sites were blocked or low speed
[[ -z "${THE_WORLD_BLOCKED}" ]] && set_proxy_mirrors_env
# Check & set global proxy
if [[ "${THE_WORLD_BLOCKED}" == "true" ]]; then
check_set_global_proxy "${GLOBAL_PROXY_SOCKS_PORT:-7891}" "${GLOBAL_PROXY_MIXED_PORT:-7890}"
fi
# Custom WSL settings
colorEcho "${BLUE}Custom WSL settings..."
# make drives mounted at /c or /e instead of /mnt/c and /mnt/e.
if ! grep -q "automount" /etc/wsl.conf 2>/dev/null; then
sudo tee /etc/wsl.conf >/dev/null <<-'EOF'
[automount]
enabled = true
root = /
options = "metadata,umask=22,fmask=11"
mountFsTab = false
[boot]
systemd = true
EOF
fi
# colorEcho "${BLUE}map localhost to \`vEthernet (WSL)\` ip${BLUE}..."
# source "${MY_SHELL_SCRIPTS:-$HOME/.dotfiles}/wsl/wsl2-map-win-localhost.sh"
# Install packages
# Use apt mirror & Install pre-requisite packages
[[ -z "${MIRROR_PACKAGE_MANAGER_APT}" ]] && MIRROR_PACKAGE_MANAGER_APT="mirror.sjtu.edu.cn"
if [[ "${THE_WORLD_BLOCKED}" == "true" ]]; then
colorEcho "${BLUE}Setting apt mirror..."
sudo sed -i \
-e "s|ftp.debian.org|${MIRROR_PACKAGE_MANAGER_APT}|g" \
-e "s|deb.debian.org|${MIRROR_PACKAGE_MANAGER_APT}|g" \
-e "s|security.debian.org/debian-security|${MIRROR_PACKAGE_MANAGER_APT}/debian-security|g" \
-e "s|security.debian.org |${MIRROR_PACKAGE_MANAGER_APT}/debian-security |g" "/etc/apt/sources.list"
fi
colorEcho "${BLUE}Installing ${FUCHSIA}pre-requisite packages${BLUE}..."
sudo apt update && \
sudo apt install -y apt-transport-https apt-utils ca-certificates \
lsb-release software-properties-common curl wget gnupg2 sudo
# Add custom repositories
colorEcho "${BLUE}Add ${FUCHSIA}custom repositories${BLUE}..."
if [[ "${THE_WORLD_BLOCKED}" == "true" ]]; then
# Use https mirror
sudo sed -i "s|http://${MIRROR_PACKAGE_MANAGER_APT}|https://${MIRROR_PACKAGE_MANAGER_APT}|g" "/etc/apt/sources.list"
sudo apt update
fi
# [wslu - A collection of utilities for WSL](https://github.com/wslutilities/wslu)
if [[ ! -x "$(command -v wslfetch)" ]]; then
colorEcho "${BLUE}Installing ${FUCHSIA}wslu${BLUE}..."
# wget -O - https://pkg.wslutiliti.es/public.key | sudo tee -a "/etc/apt/trusted.gpg.d/wslu.asc" >/dev/null
# if ! grep -q "^deb https://pkg.wslutiliti.es/debian" "/etc/apt/sources.list" 2>/dev/null; then
# RELEASE_CODENAME=$(lsb_release -c | awk '{print $2}')
# # RELEASE_CODENAME=$(dpkg --status tzdata | grep Provides | cut -f2 -d'-')
# echo "deb https://pkg.wslutiliti.es/debian ${RELEASE_CODENAME} main" | sudo tee -a "/etc/apt/sources.list" >/dev/null
# fi
curl -sL https://raw.githubusercontent.com/wslutilities/wslu/master/extras/scripts/wslu-install | bash
fi
## git lfs
## https://github.com/git-lfs/git-lfs/wiki/Tutorial
# if [[ ! -e "/etc/apt/sources.list.d/github_git-lfs.list" ]]; then
# curl -fsSL "https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh" | sudo bash
# # echo 'Acquire::http::Proxy::packagecloud-repositories.s3.dualstack.us-west-1.amazonaws.com "http://127.0.0.1:7890/";' \
# # | sudo tee -a /etc/apt/apt.conf.d/99proxy >/dev/null
# fi
## .NET Core SDK
## https://docs.microsoft.com/zh-cn/dotnet/core/install/linux-debian
# if [[ ! -e "/etc/apt/sources.list.d/microsoft-prod.list" ]]; then
# curl "${CURL_DOWNLOAD_OPTS[@]}" -o packages-microsoft-prod.deb "https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb" && \
# sudo dpkg -i packages-microsoft-prod.deb && \
# rm packages-microsoft-prod.deb
# # echo 'Acquire::http::Proxy::packages.microsoft.com "http://127.0.0.1:7890/";' \
# # | sudo tee -a /etc/apt/apt.conf.d/99proxy >/dev/null
# fi
## yarn
## https://yarnpkg.com/zh-Hans/docs/install
# if [[ ! -e "/etc/apt/sources.list.d/yarn.list" ]]; then
# curl -fsSL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
# echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
# fi
# Update all repositories & Upgrade
colorEcho "${BLUE}Updating ${FUCHSIA}all repositories & Upgrade${BLUE}..."
sudo apt update && sudo apt upgrade -y
# Install useful packages
colorEcho "${BLUE}Installing ${FUCHSIA}Pre-requisite packages${BLUE}..."
sudo apt install -y binutils build-essential di dnsutils g++ gcc keychain \
git htop iproute2 make net-tools netcat-openbsd p7zip-full psmisc tree unzip zip
## Login with SSH Key
# pssh -i -H "host01 host02" -l root \
# -x "-o StrictHostKeyChecking=no -i $HOME/.ssh/id_ecdsa" \
# "hostname -i && uname -a"
## Login with Passphrase Protected SSH Key ( all hosts in ~/.ssh/config )
# pssh -i -H "host01 host02" -A -l root "hostname -i && uname -a"
## Enable broadcast WINS
# colorEcho "${BLUE}Enable broadcast ${FUCHSIA}WINS${BLUE}..."
# sudo apt install -y libnss-winbind
# if ! grep -q "wins" "/etc/nsswitch.conf" 2>/dev/null; then
# sudo sed -i 's/ dns$/ wins dns/' "/etc/nsswitch.conf"
# fi
# sudo service winbind start # /etc/init.d/winbind start
# fix ping: socket: Operation not permitted
sudo chmod u+s /bin/ping
## wslu
## https://github.com/wslutilities/wslu
# sudo apt install -y gnupg2 apt-transport-https && \
# wget -O - https://access.patrickwu.space/wslu/public.asc | sudo apt-key add - && \
# echo "deb https://access.patrickwu.space/wslu/debian buster main" | sudo tee -a /etc/apt/sources.list && \
# sudo apt update && \
# sudo apt install -y wslu
## translate from a Windows path to a WSL path
# wslpath 'c:\users'
# colorEcho "${BLUE}Install git lfs${BLUE}..."
# sudo apt install -y git-lfs && git lfs install
[[ -s "${MY_SHELL_SCRIPTS}/installer/git-lfs_installer.sh" ]] && source "${MY_SHELL_SCRIPTS}/installer/git-lfs_installer.sh"
[[ -s "${MY_SHELL_SCRIPTS}/installer/gitflow_installer.sh" ]] && source "${MY_SHELL_SCRIPTS}/installer/gitflow_installer.sh"
[[ -s "${MY_SHELL_SCRIPTS}/installer/cargo_rust_installer.sh" ]] && source "${MY_SHELL_SCRIPTS}/installer/cargo_rust_installer.sh"
[[ -s "${MY_SHELL_SCRIPTS}/installer/goup_go_installer.sh" ]] && source "${MY_SHELL_SCRIPTS}/installer/goup_go_installer.sh"
[[ -s "${MY_SHELL_SCRIPTS}/nodejs/nvm_node_installer.sh" ]] && source "${MY_SHELL_SCRIPTS}/nodejs/nvm_node_installer.sh"
[[ -s "${MY_SHELL_SCRIPTS}/installer/python_pip_config.sh" ]] && source "${MY_SHELL_SCRIPTS}/installer/python_pip_config.sh"
# Parallel SSH Tools
# https://github.com/lilydjwg/pssh
# https://www.escapelife.site/posts/8c0f83d.html
pip_Package_Install git+https://github.com/lilydjwg/pssh
# colorEcho "${BLUE}Installing ${FUCHSIA}.NET Core SDK${BLUE}..."
## sudo apt install -y dotnet-sdk-5.0
## https://docs.microsoft.com/zh-cn/dotnet/core/tools/dotnet-install-script
# curl -fsSL -o "$HOME/dotnet-install.sh" "https://dot.net/v1/dotnet-install.sh" && \
# chmod +x "$HOME/dotnet-install.sh" && \
# sudo "$HOME/dotnet-install.sh"
# [[ -f "$HOME/dotnet-install.sh" ]] && rm -f "$HOME/dotnet-install.sh"
# colorEcho "${BLUE}Installing ${FUCHSIA}yarn${BLUE}..."
# sudo apt install -y yarn --no-install-recommends
# SSH
if [[ ! -d "$HOME/.ssh" ]]; then
mkdir -p "$HOME/.ssh" && chmod -R 700 "$HOME/.ssh" # && chmod 600 "$HOME/.ssh/"*
fi
# Change default editor to nano
if [[ -x "$(command -v nano)" ]]; then
sudo update-alternatives --install /usr/bin/editor editor "$(which nano)" 100
# sudo update-alternatives --config editor
fi
# make sudo more user friendly
# remove sudo timeout, make cache global, extend timeout
colorEcho "${BLUE}Making sudo more user friendly..."
sudo tee "/etc/sudoers.d/20-password-timeout-0-ppid-60min" >/dev/null <<-'EOF'
Defaults passwd_timeout=0
Defaults timestamp_type="global"
# sudo only once for 60 minute
Defaults timestamp_timeout=60
EOF
## Allow members of the group sudo to execute any command without password prompt
## `sudo visudo` OR `sudo EDITOR=nano visudo`
# sudo sed -i 's/%sudo.*/%sudo ALL=(ALL:ALL) NOPASSWD:ALL/' /etc/sudoers
CommandList=(
service
apt
apt-get
pacman
pacapt
pacaptr
)
for TargetCommand in "${CommandList[@]}"; do
[[ -x "$(command -v "${TargetCommand}")" ]] && \
echo "%sudo ALL=NOPASSWD:$(which "${TargetCommand}")" \
| sudo tee "/etc/sudoers.d/nopasswd_sudo_command_${TargetCommand}" >/dev/null && \
sudo chmod 440 "/etc/sudoers.d/nopasswd_sudo_command_${TargetCommand}"
done
## Allow user sudo to execute any command without password prompt
# sudo sed "/^# User privilege specification/a $(whoami) ALL=(ALL:ALL) NOPASSWD:ALL,\!/bin/su" /etc/sudoers
# echo "$(whoami) ALL=(ALL:ALL) NOPASSWD:ALL,\!/bin/su" | sudo tee "/etc/sudoers.d/nopasswd_sudo_$(whoami)" >/dev/null && \
# sudo chmod 440 "/etc/sudoers.d/nopasswd_sudo_$(whoami)"
## Command alias
# sudo tee "/etc/sudoers.d/cmdalias_sudo" >/dev/null <<-'EOF'
# Cmnd_Alias SHUTDOWN = /sbin/shutdown,/usr/bin/poweroff,/sbin/poweroff
# Cmnd_Alias HALT = /usr/sbin/halt
# Cmnd_Alias REBOOT = /usr/bin/reboot,/sbin/reboot,/sbin/init
# Cmnd_Alias SHELLS = /bin/sh,/bin/bash,/usr/bin/csh, /usr/bin/ksh, \
# /usr/local/bin/tcsh, /usr/bin/rsh, \
# /usr/local/bin/zsh
# Cmnd_Alias NETWORK = /etc/init.d/network,/sbin/ifconfig,/sbin/ip,/bin/hostname,/sbin/ifup,/sbin/ifdown
# Cmnd_Alias PASSWORD = /usr/bin/passwd
# Cmnd_Alias USERS = /usr/sbin/usermod,/usr/sbin/adduser,/usr/sbin/userdel,/usr/sbin/useradd,/usr/sbin/vipw,/usr/sbin/visudo,/usr/bin/gpasswd
# Cmnd_Alias DISKS = /sbin/fdisk,/sbin/parted
# EOF
# sudo chmod 440 "/etc/sudoers.d/cmdalias_sudo"
# echo "%minsu ALL=(ALL) NOPASSWD:ALL,\!SHELLS,\!HALT,\!SHUTDOWN,\!REBOOT,\!NETWORK,\!PASSWORD,\!DISKS,\!USERS" \
# | sudo tee "/etc/sudoers.d/nopasswd_sudo_minsu" >/dev/null && \
# sudo chmod 440 "/etc/sudoers.d/nopasswd_sudo_minsu"
# check sudo file for config and other errors
sudo visudo -c
# Clear bash history
cat /dev/null > "$HOME/.bash_history"
colorEcho "${GREEN}WSL init done, please restart WSL!"