# server-setup **Repository Path**: dai4371/server-setup ## Basic Information - **Project Name**: server-setup - **Description**: 仅供自己使用,实现了批量修改主机名密码和关闭防火墙 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2024-12-23 - **Last Updated**: 2025-01-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README Hostname and SSH Management Tool 这是一个用于管理多台主机的工具,支持通过 SSH 设置主机名,生成 SSH 密钥对,并将公钥复制到目标主机以及批量关闭防火墙。该工具基于 Go 语言开发,使用 YAML 配置文件来指定需要操作的主机。 ## 特性 目前已经支持 `centos、Ubuntu、Redhat、kylin` - 生成 SSH 密钥对 - 将公钥复制到多台远程主机 - 设置远程主机的主机名 - 批量关闭防火墙(如 Ubuntu 上的 UFW) - 支持自定义 YAML 配置文件 - 支持命令行参数 -f 来指定配置文件 ## 配置文件 配置文件使用 YAML 格式,用来定义操作的主机信息。以下是一个示例配置文件 `config-sample.yaml`: ```yaml hosts: - name: master1 address: 172.16.0.43 internalAddress: 172.16.0.43 user: root password: "yourpassword" - name: master2 address: 172.16.0.54 internalAddress: 172.16.0.54 user: root password: "yourpassword" - name: master3 address: 172.16.0.49 internalAddress: 172.16.0.49 user: root password: "yourpassword" - name: node1 address: 172.16.0.45 internalAddress: 172.16.0.45 user: root password: "yourpassword" - name: node2 address: 172.16.0.48 internalAddress: 172.16.0.48 user: root password: "yourpassword" ``` ### 配置说明: name: 主机名,后续将会设置为主机的 hostname。 address: 主机的 IP 地址,用于 SSH 连接。 internalAddress: 内部网络地址,若需要可以使用。 user: 用于 SSH 连接的用户名。 password: 用于 SSH 连接的密码。 批量关闭防火墙功能 目前已经支持 `centos、Ubuntu、Redhat、kylin` 工具支持批量关闭主机上的防火墙,尤其是在 Ubuntu 上关闭 UFW 防火墙。通过在配置文件中指定多个主机,工具将自动连接每个主机并执行相关操作。 ### 使用说明 基本命令 ```bash ./hostname-and-ssh -f ``` ## 命令行参数 `-f : 指定 YAML 配置文件的路径(必填)。` `-h: 显示帮助信息并退出。` 示例 执行操作: 如果你已经准备好配置文件 config-sample.yaml,你可以通过以下命令执行操作: ```shell ./hostname-and-ssh -f config-sample.yaml ``` **该命令将会:** - 生成 SSH 密钥对 - 将公钥复制到所有主机 - 设置每台主机的主机名 - 查看帮助信息: - 批量关闭防火墙(如 Ubuntu 上的 UFW) - 如果你不确定如何使用该工具,可以运行以下命令来查看帮助: 查看帮助信息: 如果你不确定如何使用该工具,可以运行以下命令来查看帮助: ```shell ./hostname-and-ssh -h ``` 输出示例: ```shell Usage: ./hostname-and-ssh -f -f string Path to the YAML config file (required) -h Display help information ``` ```shell root@master1:/data# ./hostname-and-ssh -f config-sample.yaml SSH key pair generated successfully 2024/12/24 10:07:04 SSH connection established to 172.16.0.43 2024/12/24 10:07:04 SSH connection established to 172.16.0.54 2024/12/24 10:07:04 SSH connection established to 172.16.0.49 2024/12/24 10:07:04 SSH connection established to 172.16.0.45 2024/12/24 10:07:04 SSH connection established to 172.16.0.48 SSH key copied to 172.16.0.43 successfully SSH key copied to 172.16.0.54 successfully SSH key copied to 172.16.0.49 successfully SSH key copied to 172.16.0.45 successfully SSH key copied to 172.16.0.48 successfully Successfully set hostname for 172.16.0.43 to master1 Successfully set hostname for 172.16.0.54 to master2 Successfully set hostname for 172.16.0.49 to master3 Successfully set hostname for 172.16.0.45 to node1 Successfully set hostname for 172.16.0.48 to node2 PRETTY_NAME="Ubuntu 22.04.1 LTS" NAME="Ubuntu" VERSION_ID="22.04" VERSION="22.04.1 LTS (Jammy Jellyfish)" VERSION_CODENAME=jammy ID=ubuntu ID_LIKE=debian HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" UBUNTU_CODENAME=jammy 2024/12/24 10:07:05 Configuring Ubuntu for host 172.16.0.43 Synchronizing state of ufw.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install disable ufw Unit /etc/systemd/system/ufw.service is masked, ignoring. PRETTY_NAME="Ubuntu 22.04.1 LTS" NAME="Ubuntu" VERSION_ID="22.04" VERSION="22.04.1 LTS (Jammy Jellyfish)" VERSION_CODENAME=jammy ID=ubuntu ID_LIKE=debian HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" UBUNTU_CODENAME=jammy 2024/12/24 10:07:06 Configuring Ubuntu for host 172.16.0.54 Synchronizing state of ufw.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install disable ufw Unit /etc/systemd/system/ufw.service is masked, ignoring. PRETTY_NAME="Ubuntu 22.04.1 LTS" NAME="Ubuntu" VERSION_ID="22.04" VERSION="22.04.1 LTS (Jammy Jellyfish)" VERSION_CODENAME=jammy ID=ubuntu ID_LIKE=debian HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" UBUNTU_CODENAME=jammy 2024/12/24 10:07:07 Configuring Ubuntu for host 172.16.0.49 Synchronizing state of ufw.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install disable ufw Unit /etc/systemd/system/ufw.service is masked, ignoring. PRETTY_NAME="Ubuntu 22.04.1 LTS" NAME="Ubuntu" VERSION_ID="22.04" VERSION="22.04.1 LTS (Jammy Jellyfish)" VERSION_CODENAME=jammy ID=ubuntu ID_LIKE=debian HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" UBUNTU_CODENAME=jammy 2024/12/24 10:07:08 Configuring Ubuntu for host 172.16.0.45 Synchronizing state of ufw.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install disable ufw Unit /etc/systemd/system/ufw.service is masked, ignoring. PRETTY_NAME="Ubuntu 22.04.1 LTS" NAME="Ubuntu" VERSION_ID="22.04" VERSION="22.04.1 LTS (Jammy Jellyfish)" VERSION_CODENAME=jammy ID=ubuntu ID_LIKE=debian HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" UBUNTU_CODENAME=jammy 2024/12/24 10:07:09 Configuring Ubuntu for host 172.16.0.48 Synchronizing state of ufw.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install disable ufw Unit /etc/systemd/system/ufw.service is masked, ignoring. 2024/12/24 10:07:10 SSH connection to 172.16.0.43 closed 2024/12/24 10:07:10 SSH connection to 172.16.0.54 closed 2024/12/24 10:07:10 SSH connection to 172.16.0.49 closed 2024/12/24 10:07:10 SSH connection to 172.16.0.45 closed 2024/12/24 10:07:10 SSH connection to 172.16.0.48 closed ``` **重要提示** 在执行程序之前,请确保你已经能够通过 SSH 无密码登录到目标主机。否则,程序将无法复制 SSH 密钥。 该工具要求在运行时必须提供 -f 参数,未提供该参数时将无法执行。 如果你需要自定义主机名或 SSH 配置,请修改 YAML 配置文件中的相关字段。 错误和日志 程序会在执行过程中记录错误信息,并将其打印到控制台。如果发生错误,检查输出的日志信息以确定失败的原因。