全球主机交流论坛

标题: 用GPT生成了一个一键修改秘钥登录的脚本 [打印本页]

作者: Sure    时间: 2024-1-11 19:28
标题: 用GPT生成了一个一键修改秘钥登录的脚本
  1. #!/bin/bash

  2. # 设置公钥下载链接
  3. public_key_url="公钥网址"

  4. # 下载公钥文件
  5. wget -O /root/.ssh/id_rsa.pub "$public_key_url"

  6. # 检查 .ssh 文件夹是否存在,如果不存在则创建
  7. if [ ! -d "/root/.ssh" ]; then
  8.     mkdir /root/.ssh
  9. fi


  10. # 检查下载是否成功
  11. if [ $? -ne 0 ]; then
  12.     echo "下载公钥文件失败,请检查公钥下载链接或网络连接。"
  13.     exit 1
  14. fi

  15. # 备份 SSH 服务器配置文件
  16. cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

  17. # 检测当前操作系统
  18. if [ -f /etc/redhat-release ]; then
  19.   # CentOS 或 Red Hat 系列操作系统
  20.   # 禁用密码登录
  21.   sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config

  22.   # 启用公钥登录
  23.   sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/' /etc/ssh/sshd_config

  24.   # 设置公钥文件路径
  25.   sed -i 's/#AuthorizedKeysFile/AuthorizedKeysFile/' /etc/ssh/sshd_config
  26.   echo "AuthorizedKeysFile     .ssh/authorized_keys /root/.ssh/authorized_keys" >> /etc/ssh/sshd_config

  27.   # 将公钥复制到 /root/.ssh/authorized_keys
  28.   cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys

  29.   # 设置正确的权限
  30.   chmod 700 /root/.ssh
  31.   chmod 600 /root/.ssh/authorized_keys

  32.   # 重启 SSH 服务
  33.   service sshd restart
  34. elif [ -f /etc/debian_version ]; then
  35.   # Debian 或 Ubuntu 系列操作系统
  36.   # 禁用密码登录
  37.   sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config

  38.   # 启用公钥登录
  39.   sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/' /etc/ssh/sshd_config

  40.   # 设置公钥文件路径
  41.   sed -i 's/#AuthorizedKeysFile/AuthorizedKeysFile/' /etc/ssh/sshd_config
  42.   echo "AuthorizedKeysFile     .ssh/authorized_keys /root/.ssh/authorized_keys" >> /etc/ssh/sshd_config

  43.   # 将公钥复制到 /root/.ssh/authorized_keys
  44.   cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys

  45.   # 设置正确的权限
  46.   chmod 700 /root/.ssh
  47.   chmod 600 /root/.ssh/authorized_keys

  48.   # 重启 SSH 服务
  49.   systemctl restart sshd
  50. else
  51.   echo "不支持的操作系统。"
  52.   exit 1
  53. fi
复制代码

作者: Lebron    时间: 2024-1-11 19:29
公钥网址填什么
作者: pag    时间: 2024-1-11 19:37
Lebron 发表于 2024-1-11 19:29
公钥网址填什么

最方便的当然是把公钥放到GitHub上了,到时候的地址形式就类似于:https://raw.githubusercontent.com/xxxx/xxxx/main/public_key/id_rsa.pub
作者: Sure    时间: 2024-1-11 19:39
pag 发表于 2024-1-11 19:37
最方便的当然是把公钥放到GitHub上了,到时候的地址形式就类似于:https://raw.githubusercontent.com/xx ...

对 自己生成秘钥后将公饵上传到GITHUB或者R2存储桶 然后已地址形式填写到脚本
作者: 有球必硬    时间: 2024-1-12 00:57
牛逼
作者: kailiboy    时间: 2024-1-12 05:58
就你上传公钥到第三方,然后下载或者拷贝你这个脚本,然后设置执行权限,然后执行脚本的时间,已经比手动改配置文件然后重启服务的时间还长了
作者: zhongziso    时间: 2024-1-12 06:03
mark
作者: 随便起个名字    时间: 2024-1-12 07:16
pag 发表于 2024-1-11 19:37
最方便的当然是把公钥放到GitHub上了,到时候的地址形式就类似于:https://raw.githubusercontent.com/xx ...

反正要脚本,公钥写到脚本里echo不好吗?
作者: yandan    时间: 2024-1-12 08:23
debian也能用吧,看这操作差不多
作者: 兜兜    时间: 2024-1-12 08:29
路过,看看。
作者: 下颚撕裂器    时间: 2024-1-12 08:36
我不干脆把公钥直接写脚本里不就行了
作者: Sam_Edward    时间: 2024-1-12 08:47
下颚撕裂器 发表于 2024-1-12 08:36
我不干脆把公钥直接写脚本里不就行了

不方便后续管理吧,做一个通用的脚本,也不用反复修改
作者: yfzz    时间: 2024-1-12 09:07
看看这个,更全面:

bash <(curl -fsSL git.io/key.sh) [选项...] <参数>

-o - 覆盖模式,必须写在最前面才会生效
-g - 从 GitHub 获取公钥,参数为 GitHub 用户名
-u - 从 URL 获取公钥,参数为 URL
-f - 从本地文件获取公钥,参数为本地文件路径
-p - 修改 SSH 端口,参数为端口号
-d - 禁用密码登录


如下面的命令将禁止密码登录,拉取 github 用户公钥,修改 ssh 端口:
bash <(curl -fsSL git.io/key.sh) -og zhaojun1998 -d -p 2233


作者博客(不是我): https://p3terx.com/archives/ssh-key-installer.html
作者: zxxx    时间: 2024-1-12 09:10
yfzz 发表于 2024-1-12 09:07
看看这个,更全面:

bash

这个灵活




欢迎光临 全球主机交流论坛 (https://loc.888543.xyz/) Powered by Discuz! X3.4