全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

IP归属甄别会员请立即修改密码
查看: 1790|回复: 13
打印 上一主题 下一主题

[特价VPS] 用GPT生成了一个一键修改秘钥登录的脚本

[复制链接]
跳转到指定楼层
1#
发表于 2024-1-11 19:28:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  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
复制代码
推荐
发表于 2024-1-12 05:58:59 来自手机 | 只看该作者
就你上传公钥到第三方,然后下载或者拷贝你这个脚本,然后设置执行权限,然后执行脚本的时间,已经比手动改配置文件然后重启服务的时间还长了
2#
发表于 2024-1-11 19:29:17 | 只看该作者
公钥网址填什么
3#
发表于 2024-1-11 19:37:33 | 只看该作者
Lebron 发表于 2024-1-11 19:29
公钥网址填什么

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

对 自己生成秘钥后将公饵上传到GITHUB或者R2存储桶 然后已地址形式填写到脚本
8#
发表于 2024-1-12 07:16:43 | 只看该作者
pag 发表于 2024-1-11 19:37
最方便的当然是把公钥放到GitHub上了,到时候的地址形式就类似于:https://raw.githubusercontent.com/xx ...

反正要脚本,公钥写到脚本里echo不好吗?
9#
发表于 2024-1-12 08:23:50 | 只看该作者
debian也能用吧,看这操作差不多
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2025-10-9 12:25 , Processed in 0.148674 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表