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日

相关推荐

  • 灵活解除网页操作限制

    前言 下午帮客户分析某文学登陆业务中,发现有页面禁用了网页右键,非常影响调试,平时遇到这种情况通常都是JS即可,但是网上查阅了资料后发现用控制台调节更灵活一些,毕竟禁用 JS 可能…

    代码程序 2021年3月21日
  • 自建Gravatar国内+全球镜像加速服务

    很多网站及项目都使用了Gravatar的头像,由于政策和国际网路带宽波动等原因,Gravatar在不同时段不同地区,加载十分缓慢,甚至无法加载,甚至影响访客粘度以及增加跳出率,所以…

    2021年6月8日
  • Proxify-统一管理、轻松调用各类大模型 API

    Proxify 是一个开源、轻量、可以自己部署的 AI 接口反向代理。 在日常开发 AI 应用过程中,一些项目需要同时接入 OpenAI、Claude,偶尔还要测试一下 DeepS…

    代码程序 2025年10月22日
  • 全自动优选香港阿里云的CloudFlare优选反代Cname

    开发环境 代码由Grok替我修改完成 语言为Pyhton 脚本由我自己提需求和反复测试完成我自己是不会写代码的 但我脑海里有程序最终效果的画面我会测试 会找bug 承认自己不会写代…

    2025年1月4日
  • Linux/Windows下,使用BestTrace查询IP回程路由;查询IP去程路由

    一、查询IP去程路由   使用 https://tools.ipip.net/newping.php ,ping完后,可查看到各地的IP去程路由,建议TCP(部分商家优化…

    代码程序 2021年4月10日
  • 富士 XF56mm F1.2 R WR

    富士 XF56mmF1.2 R WR 最近买了一只镜头,主要原因是X100V 夜拍能力太差,晚上拿起相机想快速拍一张的时候大概率是糊的,要么就拉高ISO 全是噪点。对富士镜头群不了…

    2023年9月7日
  • 在Alist挂载自己的OneDrive网盘

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

    2023年7月16日
  • cloudflare配置详解

    本篇文章会以我目前的能力,来尽量解释cloudflare每一个功能,肯定会有错误 欢迎指出 可以点击右侧目录调转到自己需要的章节 已学习: website Overview Ana…

    2023年3月3日
  • 一个更好用的 Maven 搜索引擎

    pache Maven 是一款非常优秀的软件项目管理工具,对于 Java 生态的开发者们再熟悉不过了。 在多年的开发过程中,对现有的 Maven 生态工具颇有微词,主要体现在: M…

    代码程序 2023年12月28日
  • 2023年B2 PRO主题最新版绕过授权方法

    B2 PRO主题最新版4.2.6绕过授权方法: 1、附件中的b2.zip是原版压缩包 2、附件中的b2-active.php是免授权激活文件,上传到WordPress根目录,浏览器…

    代码程序 2023年4月3日

发表回复

登录后才能评论