Linux下SSH配置密钥key登录禁用密码登录

首先需要在ROOT用户下

操作步骤如下

1 创建用户haoduck

  1. useradd haoduck

2 创建haoduck用户ssh密钥文件夹

  1. mkdir p /home/haoduck/.ssh/
  2. chmod 700 /home/haoduck/.ssh/

3 创建公钥文件

公钥文件一般从自己的电脑上生成,诸如XShell、MobaXterm等工具都是可以生成的。这里就不赘述了

  1. vim /home/haoduck/.ssh/authorized_keys
  2. chmod 600 /home/haoduck/.ssh/authorized_keys
  3. chown R haoduck /home/haoduck/.ssh/ #设置文件所有者为新用户haoduck

4 配置sudo权限(可选)

在最后添加一行haoduck ALL=(ALL) ALL或者haoduck ALL=(ALL) NOPASSWD: ALL,后者可以免密码使用sudo

  1. chmod u+w /etc/sudoers
  2. vim /etc/sudoers
  3. chmod uw /etc/sudoers

5 修改sshd配置

  1. vim /etc/ssh/sshd_config

密钥登录:找到以下内容去掉签名的#号
#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile .ssh/authorized_keys

禁用密码和ROOT登录:
找到以下内容
PasswordAuthentication yes
PermitRootLogin yes

改为
PasswordAuthentication no
PermitRootLogin no

重启sshdsystemctl restart sshdsystemctl restart sshservice sshd restartservice ssh restart

一键脚本

  1. #!/bin/bash
  2. #username=${1:=”haoduck”}
  3. #pubkey=${2:=”ssh-xxxxx”}
  4. username=“haoduck”
  5. #pubkey=”$(wget -qO- https://直链)”
  6. pubkey=“ssh-xxxxx”
  7. #yum install -y sudo
  8. #apt-get install -y sudo
  9. useradd ${username}
  10. mkdir p /home/${username}/.ssh/
  11. chmod 700 /home/${username}/.ssh/
  12. echo $pubkey > /home/${username}/.ssh/authorized_keys
  13. chmod 600 /home/${username}/.ssh/authorized_keys
  14. chown R ${username} /home/${username}/.ssh/
  15. #sudo配置
  16. chmod u+w /etc/sudoers
  17. echo “${username} ALL=(ALL) ALL” > /etc/sudoers.d/${username}
  18. #echo “${username} ALL=(ALL) NOPASSWD: ALL” > /etc/sudoers.d/${username}
  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. #sed -i “s|#\?PasswordAuthentication.*|PasswordAuthentication no|” $sshd_file
  26. #sed -i “s|#\?PermitRootLogin.*|PermitRootLogin no|” $sshd_file
  27. systemctl restart sshd;systemctl restart ssh;service sshd restart;service ssh restart

如果只需要用ROOT用户,可以省略添加用户的步骤,一键脚本如下:

  1. #pubkey=”$(wget -qO- https://直链)”
  2. pubkey=“ssh-xxxxx” #这里改成你的公钥
  3. mkdir p /root/.ssh/
  4. chmod 700 /root/.ssh/
  5. echo $pubkey > /root/.ssh/authorized_keys
  6. chmod 600 /root/.ssh/authorized_keys
  7. sshd_file=“/etc/ssh/sshd_config”
  8. cp n $sshd_file /etc/ssh/sshd_config.bak
  9. sed i “s|#\?RSAAuthentication.*|RSAAuthentication yes|” $sshd_file
  10. sed i “s|#\?PubkeyAuthentication.*|PubkeyAuthentication yes|” $sshd_file
  11. sed i “s|#AuthorizedKeysFile .ssh/authorized_keys|AuthorizedKeysFile .ssh/authorized_keys|” $sshd_file
  12. sed i “s|#\?PasswordAuthentication.*|PasswordAuthentication no|” $sshd_file
  13. sed i “s|#\?PermitRootLogin.*|PermitRootLogin yes|” $sshd_file
  14. systemctl restart sshd;systemctl restart ssh;service sshd restart;service ssh restart

原创文章,作者:mantou,如若转载,请注明出处:https://v2ez.com/169.html

(0)
mantoumantou
上一篇 2021年4月28日
下一篇 2021年4月30日

相关推荐

  • Docker 搭建开源文件同步工具 Syncthing

    简介 Syncthing 是一个开源的文件同步工具,可以在不同设备之间同步文件和文件夹。它使用了点对点的方式进行同步,如果点对点连接成功则不需要通过云服务器中转,因此更加安全和私密…

    2023年12月7日
  • SSH链式端口转发:本地端口转发+远程端口转发

    SSH本地端口转发与SSH远程端口转发结合起来使用,可以进行链式转发。   假设A主机在公司,B主机在家,C主机为远程云主机。 A主机上运行了Node.js服务,需要在B…

    2021年4月4日
  • Linux服务器SSH端口一键修改脚本

    说明:我们的很多VPS服务器,默认的端口都是22,所以一直会被人扫描爆破,很容易会出现问题,所以我们需要通过修改端口来尽可能减少这种事情发生,但对于很多小白或者很懒的人来说,更喜欢…

    代码程序 2021年5月25日
  • Cloudflare R2+Workers!马上搭建自己的云上图床!

    结果图# 原理# 图源由 Cloudflare R2 托管,通过两个 Workers 连接 R2 以展示随机横屏/竖屏图片,静态页面引用 Workers 的 URL 以实现以上界面…

    2025年5月17日
  • WordPress迁移更换域名 固定链接404 修改SSH端口

    前言 WordPress迁移可以分为以下几种情况: 仅更改域名 仅更改服务器 变更域名+服务器 下面就以这三种情况,可以实现的方式有很多,选取了一种适合自己的,把需要进行的操作记录…

    代码程序 2021年4月15日
  • Google cloud抢占式自动开机方法

    抢占式实例会自动关机是硬伤,大概3~5天会被关机,刚研究了一下,发现有一个东西叫实例时间表,这个东西可以自定义实例什么时间开机和关机,创建个时间表让他每小时开机一次抢占式实例就行了…

    2024年9月2日
  • HEXO博客网站主题:Solitude

    一款优雅的 Hexo 主题,支持懒加载、PWA、Latex以及多种评论系统。 增加评论弹幕页 更高效的自定义侧边栏功能模块,新版本支持排序 公开主题适配资源供大家使用 增加 Pos…

    2024年5月5日
  • 一键查看浏览器中保存的明文密码

    有时候为了方便,总是把密码让浏览器把密码记住,这样时间久了可能就会忘记密码。今天就分享一个利用收藏JS代码的方法让密码明文化。 使用方法 在浏览器新建一个新的标签(收藏夹), 把下…

    2021年4月3日
  • 修改dns ipv6小鸡youtube轻松速度突破13万

    今天看到一个帖子说 测速能到200m 看视频速度很慢,它那个是dns解析错了ip,导致延迟很高 然后我就看了以下我自己小鸡,果然也是有问题就是用的谷歌的dns出现的这种问题 改成了…

    2025年10月31日
  • 在Alist挂载自己的OneDrive网盘

    在之前的文章中,我讲解过如何使用容器云 / VPS搭建Alist。对于Office 365 E5账号,有些小伙伴可以利用这个项目,挂载Alist来实现自动续期的过程。在这篇文章中,…

    2023年7月16日

发表回复

登录后才能评论