lazy/init_priv.sh

395 lines
14 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
scriptdir=$(dirname $(realpath $0))
tempdir=$(mktemp -d)
if [[ $(sudo id -u) -ne 0 ]]; then
echo "Please get sudo access first."
exit 1
fi
# ============================================================
. $scriptdir/init_user.sh
# ============================================================
configure_tuna() {
[ $abroad -eq 1 ] && return
if confirm_action "要切换 Tuna 源吗?" "N"; then
python3 $scriptdir/files/oh-my-tuna.py
# 如果 uid 不为 0 则 sudo 再跑一次
if [[ $EUID -ne 0 ]]; then
sudo python3 $scriptdir/files/oh-my-tuna.py
fi
fi
}
configure_upgrade() {
if [ "$(uname)" = "Darwin" ]; then
# macOS 系统使用 brew
if confirm_action "要运行 brew upgrade 吗?" "N"; then
brew update
brew upgrade
brew cleanup
fi
else
# 其他系统使用 apt
if confirm_action "要运行 apt upgrade 吗?" "N"; then
sudo apt update
sudo apt upgrade -y
sudo apt autoremove -y
fi
fi
}
configure_install_basic() {
if [ "$(uname)" = "Darwin" ]; then
[ "$1" != "noupdate" ] && brew update
brew install tmux htop
else
[ "$1" != "noupdate" ] && sudo apt update
sudo apt install tmux git curl htop net-tools tar unzip -y
fi
pip3 install trash-cli
}
configure_install_useful() {
if [ "$(uname)" = "Darwin" ]; then
[ "$1" != "noupdate" ] && brew update
brew install hexyl bat neofetch tree cloc
else
[ "$1" != "noupdate" ] && sudo apt update
sudo apt install hexyl bat neofetch fd-find fzf tcptraceroute tree cloc -y
fi
}
configure_install_new() {
if [ "$(uname)" = "Darwin" ]; then
[ "$1" != "noupdate" ] && brew update
brew install pigz
else
[ "$1" != "noupdate" ] && sudo apt update
sudo apt install pigz -y
fi
pip3 install speedtest-cli
}
configure_install_eza() {
if [ "$(uname)" = "Darwin" ]; then
brew install eza
elif [[ $(uname -m) != "x86_64" ]]; then
echo "eza is not supported on $(uname -m)."
elif ! command_exists eza; then
unzip $scriptdir/files/eza_x86_64-unknown-linux-musl.zip -d $tempdir/eza
sudo cp $tempdir/eza/eza /usr/local/bin/
rm $tempdir/eza -r
fi
}
configure_install() {
if [ "$(uname)" = "Darwin" ]; then
# macOS 系统使用 brew
if confirm_action "要运行 brew/pip3 install 吗?" "N"; then
brew update
else
return
fi
else
if confirm_action "要运行 apt/pip3 install 吗?" "N"; then
sudo apt update
else
return
fi
fi
configure_install_basic noupdate
configure_install_useful noupdate
configure_install_new noupdate
configure_install_eza noupdate
}
configure_nano() {
[ -f ~/.nanorc ] && return
if confirm_action "要配置 nano 吗?" "Y"; then
pkg_install nano
cp $scriptdir/files/.nanorc ~/.nanorc
fi
}
configure_vim() {
[ -f ~/.vimrc ] && return
if confirm_action "要配置 vim 吗?" "Y"; then
pkg_install vim
cp $scriptdir/files/.vimrc ~/.vimrc
fi
}
replace_zsh_source() {
local zshrc="$HOME/.zshrc"
local old_source="\$ZSH/oh-my-zsh.sh"
local new_source="~/.oh-my-zsh/custom/00-init.zsh"
# 使用 sed 进行替换,注意 macOS 和 Linux 的 sed 语法稍有不同
if [ "$(uname)" = "Darwin" ]; then
# macOS 使用的是 BSD sed需要给 -i 选项传递一个空字符串作为扩展名
sed -i '' "s|${old_source}|${new_source}|g" "$zshrc"
else
# Linux 使用的是 GNU sed
sed -i "s|${old_source}|${new_source}|g" "$zshrc"
fi
}
configure_zsh() {
[ -f ~/.zshrc ] && return
if confirm_action "要配置 zsh 吗?" "Y"; then
pkg_install zsh
rm ~/.oh-my-zsh -r
git_clone ohmyzsh/ohmyzsh ~/.oh-my-zsh
RUNZSH=no ZSH=~/.oh-my-zsh $scriptdir/files/install-ohmyzsh.sh
git_clone zsh-users/zsh-autosuggestions ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions
git_clone zsh-users/zsh-syntax-highlighting ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting
git_clone paulirish/git-open ~/.oh-my-zsh/custom/plugins/git-open --norecursive
pkg_install autojump
if ! grep -Fxq "source ~/.oh-my-zsh/custom/00-init.zsh" ~/.zshrc; then
replace_zsh_source
fi
cp $scriptdir/files/zsh/.zsh_user ~/.zsh_user
cp $scriptdir/files/zsh/00-init.zsh ~/.oh-my-zsh/custom/
cp $scriptdir/files/zsh/10-theme.zsh ~/.oh-my-zsh/custom/
cp $scriptdir/files/zsh/20-function.zsh ~/.oh-my-zsh/custom/
cp $scriptdir/files/zsh/50-alias.zsh ~/.oh-my-zsh/custom/
cp $scriptdir/files/zsh/80-env.zsh ~/.oh-my-zsh/custom/
cp $scriptdir/files/zsh/90-other.zsh ~/.oh-my-zsh/custom/
cp $scriptdir/files/zsh/ys-simple.zsh-theme ~/.oh-my-zsh/custom/themes/
[ $abroad -eq 1 ] && return
cp $scriptdir/files/zsh/81-proxy.zsh ~/.oh-my-zsh/custom/
fi
}
configure_tmux() {
[ -f ~/.tmux.conf ] && return
if confirm_action "要配置 tmux 吗?" "Y"; then
sudo apt install tmux -y
rm ~/.tmux -r
git_clone tmux-plugins/tpm ~/.tmux/plugins/tpm --norecursive
git_clone tmux-plugins/tmux-sensible ~/.tmux/plugins/tmux-sensible
git_clone tmux-plugins/tmux-yank ~/.tmux/plugins/tmux-yank
git_clone tmux-plugins/tmux-prefix-highlight ~/.tmux/plugins/tmux-prefix-highlight
git_clone seebi/tmux-colors-solarized ~/.tmux/plugins/tmux-colors-solarized
cp $scriptdir/files/.tmux.conf ~/.tmux.conf
[ "$(uname)" = "Darwin" ] && return
sudo apt install xsel -y # 能够让 tmux 剪切板与 terminal 同步
fi
}
configure_docker() {
command_exists docker && return
if confirm_action "要安装 docker 吗?" "N"; then
# 卸载旧版本
sudo apt remove docker docker-engine docker.io
sudo apt update
sudo apt install apt-transport-https ca-certificates curl gnupg lsb-release -y
# 添加软件源的 GPG 密钥,并向 sources.list 中添加 Docker 软件源
if [ $abroad -eq 1 ]; then
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
else
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
fi
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io -y
sudo systemctl enable docker
sudo systemctl start docker
# 安装 docker-compose-plugin
sudo apt-get install docker-compose-plugin -y
# 安装 docker-compose
latest_version=$(curl -s "https://api.github.com/repos/docker/compose/releases/latest" | grep -o '"tag_name": ".*"' | cut -d'"' -f4)
if [ -z "$latest_version" ]; then
latest_version="v2.27.0"
fi
sudo curl -L "$github/docker/compose/releases/download/$latest_version/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
fi
}
configure_yarn() {
echo "Incomplete"; return
curl -fsSL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo gpg --dearmor -o /usr/share/keyrings/yarn.gpg
echo "deb [signed-by=/usr/share/keyrings/yarn.gpg] https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list > /dev/null
}
configure_mongodb() {
echo "Incomplete"; return
version=7.0
curl -fsSL https://pgp.mongodb.com/server-$version.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-$version.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/mongodb-server-$version.gpg] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/$version multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-$version.list > /dev/null
}
configure_nginx() {
echo "Incomplete"; return
curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo gpg --dearmor -o /usr/share/keyrings/nginx.gpg
echo "deb [signed-by=/usr/share/keyrings/nginx.gpg] https://nginx.org/packages/ubuntu/ $(lsb_release -cs) nginx" | sudo tee /etc/apt/sources.list.d/nginx.list > /dev/null
echo "deb-src [signed-by=/usr/share/keyrings/nginx.gpg] https://nginx.org/packages/ubuntu/ $(lsb_release -cs) nginx" | sudo tee -a /etc/apt/sources.list.d/nginx.list > /dev/null
}
configure_fail2ban() {
[ "$(uname)" = "Darwin" ] && return
[ -f /etc/fail2ban/jail.local ] && return
if confirm_action "要配置 fail2ban 吗?" "N"; then
sudo apt install fail2ban -y
sudo mkdir -p /etc/fail2ban
sudo cp $scriptdir/files/jail.local /etc/fail2ban/jail.local
sudo systemctl start fail2ban
sudo systemctl enable fail2ban
sudo fail2ban-client status sshd
fi
}
configure_iptables() {
[ -f /etc/network/if-pre-up.d/iptables-load ] && return
if confirm_action "要配置 iptables 吗?" "N"; then
sudo cp $scriptdir/files/iptables-load /etc/network/if-pre-up.d/iptables-load
sudo chmod +x /etc/network/if-pre-up.d/iptables-load
sudo sh /etc/network/if-pre-up.d/iptables-load
fi
}
configure_hostname() {
if confirm_action "要修改 hostname 吗?" "N"; then
read -p "hostname: " new_hostname
echo $new_hostname | sudo tee /etc/hostname >/dev/null
fi
}
configure_timedate() {
[ "$(uname)" = "Darwin" ] && return
[[ $(timedatectl | grep "Time zone" | awk '{print $3}') == "Asia/Shanghai" ]] && return
if confirm_action "要修改 时区 吗?" "N"; then
sudo timedatectl set-timezone Asia/Shanghai
fi
}
configure_bootinfo() {
[ "$(uname)" = "Darwin" ] && return
[ -d /etc/update-motd.d/disable ] && return
if confirm_action "要修改 启动信息 吗?" "Y"; then
sudo mkdir /etc/update-motd.d/disable
sudo mv /etc/update-motd.d/10-help-text /etc/update-motd.d/disable/
sudo mv /etc/update-motd.d/50-motd-news /etc/update-motd.d/disable/
# sudo mv /etc/update-motd.d/88-esm-announce /etc/update-motd.d/disable/
if [ -d /usr/lib/python3/dist-packages/landscape/sysinfo ]; then
sudo sed -i 's/"Network"/"Network_Simple"/g' /usr/lib/python3/dist-packages/landscape/sysinfo/deployment.py
sudo cp $scriptdir/files/sysinfo_network_simple.py /usr/lib/python3/dist-packages/landscape/sysinfo/network_simple.py
fi
fi
}
configure_oomkiller() {
[ "$(uname)" = "Darwin" ] && return
[ -f /etc/default/earlyoom ] && return
if confirm_action "要启用 earlyoom 吗?" "Y"; then
sudo apt install earlyoom -y
sudo cp $scriptdir/files/earlyoom.conf /etc/default/earlyoom
sudo systemctl restart earlyoom
fi
}
configure_proxychains() {
[ "$(uname)" = "Darwin" ] && return
[ -f /etc/proxychains4.conf ] && return
if confirm_action "要配置 proxychains 吗?" "N"; then
sudo apt install proxychains4 -y
sudo cp $scriptdir/files/proxychains4.conf /etc/proxychains4.conf
fi
}
configure_nodejs() {
[ -f /usr/local/bin/node ] && return
if confirm_action "要安装 nodejs 吗?" "Y"; then
local version="v20.10.0"
local tarname="node-$version-linux-x64"
wget https://nodejs.org/dist/$version/$tarname.tar.xz -O $tempdir/$tarname.tar.xz
tar -xf $tempdir/$tarname.tar.xz -C $tempdir
sudo cp $tempdir/$tarname/bin/node /usr/local/bin/node
sudo cp $tempdir/$tarname/bin/npm /usr/local/bin/npm
rm $tempdir/$tarname.tar.xz
rm $tempdir/$tarname -r
fi
}
# ============================================================
configure_swap() {
# Input the swap size
read -p "Swap size (MB): " swap_size
# Create a virtual memory file
sudo dd if=/dev/zero of=/mnt/swap bs=1M count=$swap_size
sudo chmod 0600 /mnt/swap
sudo mkswap /mnt/swap
sudo swapon /mnt/swap
# Write auto mount parameters
if ! grep -q swap /etc/fstab; then
echo "/mnt/swap swap swap defaults 0 0" | sudo tee -a /etc/fstab >/dev/null
fi
# Set virtual memory usage
if ! grep -q swappiness /etc/sysctl.conf; then
echo "vm.swappiness = 10" | sudo tee -a /etc/sysctl.conf >/dev/null
else
# sed -i 's/vm.swappiness = 0/vm.swappiness = 10/' /etc/sysctl.conf
sudo sed -i 's/^vm.swappiness.*/vm.swappiness = 10/' /etc/sysctl.conf
fi
# Make the configuration take effect
sudo sysctl -p
}
# ============================================================
if [ $# -ne 0 ]; then
for func in $@; do
declare -F configure_$func >/dev/null || continue
echo "Configuring $func..."
eval "configure_$func"
done
return
fi
configure_tuna
configure_upgrade
configure_install
configure_nano
configure_vim
configure_zsh
configure_tmux
configure_nodejs
configure_docker
configure_fail2ban
configure_iptables
configure_hostname
configure_timedate
configure_bootinfo
configure_oomkiller