#!/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/configure_user.sh # ============================================================ configure_tuna() { [ $offline -eq 1 ] && return [ $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() { [ $offline -eq 1 ] && return 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" && $(uname -m) != "aarch64" ]]; then echo "eza is not supported on $(uname -m)." elif ! command_exists eza; then if [[ "$(lsb_release -is)" == "Ubuntu" && ("$(lsb_release -rs)" == "24.04" || "$(lsb_release -rs)" > "24.04") ]]; then sudo apt install eza -y else unzip $scriptdir/files/eza_$(uname -m)-unknown-linux-musl.zip -d $tempdir/eza sudo cp $tempdir/eza/eza /usr/local/bin/ rm $tempdir/eza -r fi 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 } configure_zsh() { [ -f ~/.zshrc ] && return [ $offline -eq 1 ] && 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 local zshrc="$HOME/.zshrc" local search_line="source \$ZSH/oh-my-zsh.sh" local add_line="source ~/.oh-my-zsh/custom/00-init.zsh" if [ "$(uname)" = "Darwin" ]; then # macOS awk -v search="$search_line" -v add="$add_line" '{ if (index($0, search) > 0) print add print $0 }' "$zshrc" > ~/.zshrc.temp && mv ~/.zshrc.temp "$zshrc" else # Linux sed -i "/${search_line}/i ${add_line}" "$zshrc" fi 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-env.zsh ~/.oh-my-zsh/custom/ cp $scriptdir/files/zsh/51-alias.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/80-proxy.zsh ~/.oh-my-zsh/custom/ fi } configure_tmux() { [ -f ~/.tmux.conf ] && return [ $offline -eq 1 ] && 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 [ $offline -eq 1 ] && 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 # 检查系统架构是否为 x86_64 if [[ $(uname -m) != "x86_64" ]]; then echo "Docker is only supported on amd64 architecture." return fi # 添加软件源的 GPG 密钥,并向 sources.list 中添加 Docker 软件源 if [ $abroad -eq 1 ]; then curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/docker-archive-keyring.gpg > /dev/null 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 | gpg --dearmor | sudo tee /usr/share/keyrings/docker-archive-keyring.gpg > /dev/null 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 echo "Latest version of docker-compose is $latest_version" 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 | gpg --dearmor | sudo tee /usr/share/keyrings/yarn.gpg > /dev/null 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 | gpg --dearmor | sudo tee /usr/share/keyrings/mongodb-server-$version.gpg > /dev/null 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() { [ $offline -eq 1 ] && return if [ ! -f /etc/apt/sources.list.d/nginx.list ]; then curl -fsSL https://nginx.org/keys/nginx_signing.key | gpg --dearmor | sudo tee /usr/share/keyrings/nginx.gpg > /dev/null 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 fi if command_exists nginx; then if confirm_action "要更新 nginx 吗?" "N"; then sudo apt update sudo apt upgrade nginx -y fi else if confirm_action "要安装 nginx 吗?" "Y"; then sudo apt update sudo apt install nginx -y fi fi } configure_fail2ban() { [ -f /etc/fail2ban/jail.local ] && return [ $offline -eq 1 ] && return [ "$(uname)" = "Darwin" ] && 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 [ $offline -eq 1 ] && 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() { [ -d /etc/update-motd.d/disable ] && return [ "$(uname)" = "Darwin" ] && 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() { [ -f /etc/default/earlyoom ] && return [ $offline -eq 1 ] && return [ "$(uname)" = "Darwin" ] && 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() { [ -f /etc/proxychains4.conf ] && return [ $offline -eq 1 ] && return [ "$(uname)" = "Darwin" ] && 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 [ $offline -eq 1 ] && 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 # configure_swap