全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 1868|回复: 21

[疑问] 一键开启SSH密钥登陆

[复制链接]
发表于 2021-1-30 13:39:15 | 显示全部楼层 |阅读模式
本帖最后由 嗷嗷 于 2021-2-4 13:39 编辑
  1. #!/bin/bash
  2. #username=${1:="haoduck"}
  3. #pubkey=${2:="ssh-xxxxx"}
  4. username="haoduck"
  5. pubkey="ssh-xxxxx"
  6. #yum install -y sudo
  7. #apt-get install -y sudo
  8. useradd ${username}
  9. mkdir -p /home/${username}/.ssh/
  10. chmod 700 /home/${username}/.ssh/
  11. echo $pubkey > /home/${username}/.ssh/authorized_keys
  12. chmod 600 /home/${username}/.ssh/authorized_keys
  13. chown -R ${username} /home/${username}/.ssh/
  14. #sudo配置
  15. chmod u+w /etc/sudoers
  16. echo "${username} ALL=(ALL) ALL" >> /etc/sudoers
  17. #echo "${username} ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers #如果用这个新用户用sudo执行命令无需确认ROOT密码
  18. chmod u-w /etc/sudoers
  19. #sshd配置
  20. sshd_file="/etc/ssh/sshd_config"
  21. cp -n $sshd_file /etc/ssh/sshd_config.bak
  22. sed -i "s|#?RSAAuthentication.*|RSAAuthentication yes|" $sshd_file
  23. sed -i "s|#?PubkeyAuthentication.*|PubkeyAuthentication yes|" $sshd_file
  24. sed -i "s|#AuthorizedKeysFile .ssh/authorized_keys|AuthorizedKeysFile .ssh/authorized_keys|" $sshd_file
  25. #去掉下面两行的#号会禁用密码和ROOT用户登录
  26. #sed -i "s|#?PasswordAuthentication.*|PasswordAuthentication no|" $sshd_file
  27. #sed -i "s|#?PermitRootLogin.*|PermitRootLogin no|" $sshd_file
  28. systemctl restart sshd;systemctl restart ssh;service sshd restart;service ssh restart
复制代码


有些商家后台不能上传密钥或者不方便上传密钥,就可以用这个脚本自己配置。

使用的方法是这样,从useradd ${username}这一行(第8行)开始复制到最后一行,用ROOT用户在终端执行username="你的非root用户名"pubkey="你的公钥",然后粘贴刚刚复制的内容,回车。

需要有安装sudo

手动的步骤可见:https://haoduck.com/745.html

发表于 2021-1-30 15:36:23 | 显示全部楼层
本帖最后由 Trojan. 于 2021-1-30 16:18 编辑

简单问题复杂化,放个屁都要又**服又脱裤子的,这是怎么想的?

1、把你公钥直接放到github上,公钥而已,公开没所谓的

2、登陆小鸡后,把github公钥下载到小鸡,然后修改sshd_config 并重启ssh就OK了,多简单的事

https://www.hostloc.com/thread-802475-1-1.html

发表于 2021-1-30 14:05:47 | 显示全部楼层

代码隐藏了 麻烦传个附件
发表于 2021-1-30 13:42:12 | 显示全部楼层
多谢分享,学习了!
发表于 2021-1-30 13:43:09 | 显示全部楼层
干活,收藏
发表于 2021-1-30 13:51:11 | 显示全部楼层
路过,收藏了
发表于 2021-1-30 13:55:56 | 显示全部楼层
大佬能写个Ubuntu开启root登陆的脚本吗
 楼主| 发表于 2021-1-30 13:59:29 | 显示全部楼层
本帖最后由 嗷嗷 于 2021-1-30 14:02 编辑
小旭 发表于 2021-1-30 13:55
大佬能写个Ubuntu开启root登陆的脚本吗


以前写过手动的方法:https://haoduck.com/385.html

一键的话可以看这个:https://github.com/peng4740/gcpopenssh
发表于 2021-1-30 14:03:47 | 显示全部楼层
小旭 发表于 2021-1-30 13:55
大佬能写个Ubuntu开启root登陆的脚本吗

Debian的

ssh-rsa Gw4W7u5SuPOPJn2gz..........JCFMHAEBIMw== root@server

使用下面命令生成
  1. ssh-keygen -t rsa -b 4096 -f ./id_rsa
复制代码

的 id_rsa.pub 的内容

  1. mkdir -p /root/.ssh && \
  2. cat << "EOF" > /root/.ssh/authorized_keys
  3. ssh-rsa Gw4W7u5SuPOPJn2gz..........JCFMHAEBIMw== root@server
  4. EOF
  5. cat << EOF > /etc/ssh/sshd_config
  6. AcceptEnv LANG LC_*
  7. AddressFamily any
  8. AllowTcpForwarding yes
  9. ChallengeResponseAuthentication no
  10. ClientAliveInterval 60
  11. GatewayPorts yes
  12. PasswordAuthentication yes
  13. PermitRootLogin yes
  14. Port 22
  15. PrintMotd no
  16. PubkeyAuthentication yes
  17. Subsystem sftp /usr/lib/openssh/sftp-server
  18. TCPKeepAlive yes
  19. UsePAM yes
  20. X11Forwarding yes
  21. EOF
  22. systemctl restart sshd
复制代码
发表于 2021-1-30 14:03:59 | 显示全部楼层
嗷嗷 发表于 2021-1-30 13:59
以前写过手动的方法:https://haoduck.com/385.html

一键的话可以看这个:https://github.com/peng4740/ ...

乌班图不行 设置好了密码错误
然后机器内切换用户输入密码正确
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-26 10:50 , Processed in 0.066865 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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