初始化Debian服务器的小脚本

根据你的选择执行安装 Docker、部署 Portainer、安装 OpenSSH Server、设置ustc更新源和安装 net-tools 等操作。

请在运行脚本之前确保你了解脚本的操作。最好在测试环境中运行脚本,以确保它适用于你的特定情况。如果有任何问题,请随时告诉我。

由于portainer 2.17之后有烦人的广告提示升级商业版。

所以这里使用的是2.16版本。

权限:

将脚本代码保存为一个文件(例如 install_with_choices.sh),然后运行以下命令以授予执行权限并运行脚本。

chmod +x install_with_choices.sh
./install_with_choices.sh

脚本内容:

#!/bin/bash

# Check if running as root
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root."
   exit 1
fi

# Prompt for installation options
echo "Select an option:"
echo "1. Install Docker and Docker Compose"
echo "2. Install Portainer"
echo "3. Install OpenSSH Server and configure"
echo "4. Update Debian 12 sources to USTC Mirror"
echo "5. Install net-tools"
echo "6. Install Everything"
read -p "Enter your choice: " choice

# Install Docker and Docker Compose
install_docker() {
    # Set up the repository
    echo "Setting up the repository..."
    apt-get update
    apt-get install -y ca-certificates curl gnupg

    # Add Docker's official GPG key
    echo "Adding Docker's GPG key..."
    install -m 0755 -d /etc/apt/keyrings
    curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
    chmod a+r /etc/apt/keyrings/docker.gpg

    echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian $(. /etc/os-release && echo $VERSION_CODENAME) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null

    apt-get update

    # Install Docker Engine and Docker Compose
    echo "Installing Docker Engine and Docker Compose..."
    apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose

    echo "Docker and Docker Compose have been installed."
}

# Install Portainer and deploy using docker-compose
install_portainer() {
    # Install Docker if not installed
    which docker > /dev/null
    if [[ $? -ne 0 ]]; then
        install_docker
    fi
    
    # Install Portainer
    echo "Installing Portainer..."
    docker volume create portainer_data
    docker run -d -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:2.16.1
    echo "Portainer has been installed and deployed."
}

# Install OpenSSH Server and configure
install_ssh() {
    echo "Installing and configuring OpenSSH Server..."
    apt-get install -y openssh-server
    sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
    service ssh restart
    echo "OpenSSH Server has been installed and configured."
}

# Update Debian 12 sources to USTC Mirror
update_sources() {
    echo "Updating Debian 12 sources to USTC Mirror..."
    cp /etc/apt/sources.list /etc/apt/sources.list.backup

    cat > /etc/apt/sources.list <<EOL
# Debian 12 USTC Mirror
deb http://mirrors.ustc.edu.cn/debian stable main contrib non-free non-free-firmware
# deb-src http://mirrors.ustc.edu.cn/debian stable main contrib non-free non-free-firmware
deb http://mirrors.ustc.edu.cn/debian stable-updates main contrib non-free non-free-firmware
# deb-src http://mirrors.ustc.edu.cn/debian stable-updates main contrib non-free non-free-firmware
# deb http://mirrors.ustc.edu.cn/debian stable-proposed-updates main contrib non-free non-free-firmware
# deb-src http://mirrors.ustc.edu.cn/debian stable-proposed-updates main contrib non-free non-free-firmware
deb http://mirrors.ustc.edu.cn/debian-security/ stable-security main non-free contrib
# deb-src http://mirrors.ustc.edu.cn/debian-security/ stable-security main non-free contrib
EOL

    apt update
    echo "Debian 12 sources have been updated to USTC Mirror."
}

# Install net-tools
install_net_tools() {
    echo "Installing net-tools..."
    apt-get install -y net-tools
    echo "net-tools have been installed."
}

# Main installation process
case $choice in
    1) install_docker ;;
    2) install_portainer ;;
    3) install_ssh ;;
    4) update_sources ;;
    5) install_net_tools ;;
    6) install_docker && install_portainer && install_ssh && update_sources && install_net_tools ;;
    *) echo "Invalid choice" ;;
esac
文章作者: scotee
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 思考题Scotee
Linux与网络 Docker
喜欢就支持一下吧