SSH通过密钥认证方式登录服务器

Kevin 教程 4,270 次浏览 69条评论

如果平时有查看服务器日志习惯,时常会看到一些企图破解SSH登录密码的情况,这些大多由自动扫描器弄的,密码弱的很容易中招。要解决这个问题,除了增加密码强度外,更好方法是采用密钥对认证方式登录,能有效提升安全性。

要启用密钥登录方式,首先需创建密钥对,然后在服务器端写入公钥,再由SSH客户端使用私钥认证登录。下面介绍具体配置流程。

创建SSH密钥对文件

在Linux系统上创建

大多Linux发行版系统自带有OpenSSH软件,可以很方便的在服务器上生成。

1. SSH登录服务器,运行ssh-keygen -t rsa -b 4096命令生成密钥。选择密钥存储目录,可以按Enter键使用默认目录。

# ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):

2. 是否设置私钥保护密码(设置后SSH登录除了验证私钥,还需输入密码,相等于二重认证),如果不设置则直接Enter两次。

Enter passphrase (empty for no passphrase): 
Enter same passphrase again:

3. 显示密钥生成结果,文件存储在/root/.ssh/目录下,id_rsa是私钥文件,id_rsa.pub为公钥文件。使用SFTP工具下载到本地保存。或直接使用cat命令查看文件内容,复制到记事本文件保存(避免使用.doc文档存储,字符问题可能导致内容变动)。之后删除在服务器上的密钥文件。

Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:FNljtsS/cQueU3plPIPgQIwDMupDSMqHEHyuJzxFDW4 root@vpsuse
The key's randomart image is:
+---[RSA 4096]----+
|+o +o.. ==       |
|*.+oo. o.+B.     |
|o=+E    o+o+. .. |
|o oo   .  ..+.oo+|
|.oo     S  . O +o|
| =..        * o  |
|  +          o   |
|                 |
|                 |
+----[SHA256]-----+

在Windows系统上创建

如果希望在Windows电脑上生成,需要额外下载软件,例如PuTTYgen(点击前往下载)。如果使用Xshell,自身也提供有类似密钥生成工具。

1. 运行puttygen.exe,选择SSH-2(RSA)类型密钥,密钥长度调至4096,点击Generate生成。

设置密钥参数

2. 密钥生成过程中,需要将鼠标移至软件空白区域反复移动,以产生随机数据。

移动鼠标获取随机数据

3. 生成密钥后,可以为私钥设置保护密码,然后导出密钥文件备用。

导出密钥文件

Linux服务器端配置公钥

SSH登录服务器,使用下面命令创建公钥文件。将之前生成的公钥文件内容复制进去保存。

mkdir ~/.ssh; vi ~/.ssh/authorized_keys

为公钥目录和密钥文件设置合适的权限。

chmod 700 -R ~/.ssh && chmod 600 ~/.ssh/authorized_keys

到处,应该就能使用密钥方式登录服务器了。之后可考虑关闭密码登录方式(关闭前建议先验证SSH密钥登录是否正常,以免意外情况无法登录服务器)

关闭SSH密码登录方法:编辑SSH配置文件vi /etc/ssh/sshd_config,将PasswordAuthentication参数改为no,重启sshd服务即可生效。

SSH客户端配置密钥登录

下面以Xshell SSH客户端为例,修改SSH连接会话属性,将用户身份验证方式改为验证密钥登录。

SSH客户端设置密钥认证

如果你习惯使用PuTTY,可以参考这篇文章设置。

评论列表

  1. #1

    Hi, very nice website, cheers!
    ——————————————————
    Need cheap and reliable hosting? Our shared plans start at $10 for an year and VPS plans for $6/Mo.
    ——————————————————
    Check here: https://www.reliable-webhosting.com/

  2. #2

    dapoxetine coupons card https://dapoxetine.confrancisyalgomas.com/

  3. #3

    bimatoprost medication class https://carepro1st.com/

  4. #4

    contrave naltrexone bupropion https://naltrexoneonline.confrancisyalgomas.com/

  5. #5

    generic dapoxetine overnight delivery usa https://salemeds24.wixsite.com/dapoxetine

  6. #6

    ivermectin alcohol https://ivermectin.mlsmalta.com/

  7. #7

    hydroxychloroquine coronavirus works https://hydroxychloroquine.webbfenix.com/

  8. #8

    sildenafil and dapoxetine tablets https://ddapoxetine.com/

  9. #9

    daily cialis vs regular cialis https://wisig.org/

  10. #10

    vidalista research chem https://vidalista40mg.mlsmalta.com/

  11. #11

    ventolin price in uk https://amstyles.com/

  12. #12

    hydroxychloroquine sulfate uses https://hydroxychloroquinee.com/

  13. #13

    amax auto insurance company

  14. #14

    car insurance comparison

  15. #15

    free sex chat rooms

  16. #16

    aaa life insurance

  17. #17

    cialis for sale uk

  18. #18

    trustage whole life insurance rates

  19. #19

    online medication cialis

  20. #20

    where can i buy provigil

  21. #21

    california cash advance

  22. #22

    loans tulsa ok

  23. #23

    adalat medicine

  24. #24

    live girl

  25. #25

    return of premium life insurance

  26. #26

    auto insurance brokers

  27. #27

    sildenafil 50 mg no prescription

  28. #28

    buy lyrica online

  29. #29

    singulair price uk

  30. #30

    homeowner

  31. #31

    foremost manufactured home insurance

  32. #32

    bad credit payday loans online

  33. #33

    house insurance prices

  34. #34

    cheap viagra online in india

  35. #35

    hydroxychloroquine south dakota study https://sale.azhydroxychloroquine.com/

  36. #36

    levitra prescription prices

  37. #37

    order cialis no prescription https://tadalafili.com/

  38. #38

    prednisone dose pack 6 day directions https://prednisone.bvsinfotech.com/

  39. #39

    hydroxychloroquine side effects heart https://hydroxychloroquine.wisig.org/

  40. #40

    cenforce time frame http://cavalrymenforromney.com/

  41. #41

    us cialis online pharmacy https://tadfil.online/

  42. #42

    us cialis online pharmacy https://tadalafil365.online/

  43. #43

    stromectol dosage for scabies http://ivermmectin.co/

  44. #44

    prednisone generic cost https://bvsinfotech.com/

  45. #45

    Wohh just what I was searching for, appreciate it for posting. https://local-auto-locksmith.co.uk/kia/

  46. #46

    I really like looking at and I believe this website got some really useful stuff on it! https://local-auto-locksmith.co.uk/services/

  47. #47

    I am also writing to make you know what a incredible encounter my friend’s girl gained reading through your web page. She learned plenty of pieces, which include what it’s like to possess an amazing helping mood to let other folks smoothly know just exactly several multifaceted subject matter. You truly exceeded our own expectations. Thank you for offering these necessary, safe, explanatory and even fun tips about this topic to Julie. https://johnpisanohomeimprovements.com/

  48. #48

    Thank you for your entire labor on this web site. My mom take interest in participating in investigations and it is obvious why. My partner and i learn all regarding the lively tactic you produce simple things through your web site and in addition invigorate response from the others on the situation then our favorite princess is truly studying a whole lot. Take advantage of the rest of the year. You have been doing a superb job. https://harrington-service.com/

  49. #49

    I am glad for writing to make you know what a notable encounter my wife’s girl had studying your web site. She mastered several pieces, which include what it’s like to possess an awesome helping nature to let other individuals with ease know precisely some problematic subject matter. You truly exceeded people’s expectations. Thank you for producing these practical, trusted, informative and in addition fun tips about this topic to Kate. https://lexaproescitalopram.com/

  50. #50

    I am commenting to let you know of the fabulous encounter my cousin’s daughter experienced reading your site. She even learned many issues, not to mention what it’s like to have a wonderful giving mindset to have many people just grasp chosen grueling subject areas. You really exceeded our expectations. Many thanks for distributing the informative, healthy, educational and as well as easy thoughts on the topic to Jane. https://seroquelquetiapine.com/#

  51. #51

    I am just writing to make you understand what a notable experience my wife’s girl had using your web site. She mastered so many pieces, which included what it is like to possess an excellent helping nature to let other people with ease learn some problematic things. You truly surpassed people’s expected results. Thank you for producing these precious, trusted, informative and in addition fun tips about this topic to Lizeth. https://wellbutrinbupropions.com/#

  52. #52

    Thanks for the useful information on credit repair on your web-site. The thing I would offer as advice to people will be to give up a mentality they will buy at this moment and pay back later. As being a society most people tend to repeat this for many factors. This includes family vacations, furniture, plus items we really want to have. However, you must separate a person’s wants from the needs. When you’re working to fix your credit score you really have to make some trade-offs. For example you possibly can shop online to economize or you can click on second hand suppliers instead of highly-priced department stores to get clothing. https://zyprexaolanzapine.com/#

  53. #53

    doctor appointment online https://medpills.bee-rich.com/

  54. #54

    doctor appointment online https://edmeds.buszcentrum.com/

  55. #55

    cialis without seeing doctor http://ciaalis2u.com/

  56. #56

    how to enhance aurogra https://aurogra.buszcentrum.com/

  57. #57

    generic for tadalafil tadalafil https://cialis.webbfenix.com/

  58. #58

    generic for tadalafil tadalafil https://cialis.bee-rich.com/

  59. #59

    buy cialis 20mg without prescription https://cialzi.com/

  60. #60

    cialis online prescription uk https://www.lm360.us/

  61. #61

    tadalafil generic tadalafil for sale https://cialis.mlsmalta.com/

  62. #62

    tadalafil generic tadalafil for sale https://cialis.grassfed.us/

  63. #63

    viagra prices http://viaagra1.com/

  64. #64

    viagra 100 mg lowest price http://viiagra.co/

  65. #65

    generic tadalafil https://atadalafil.online/

  66. #66

    Keep on working, great job! http://cleckleyfloors.com/viagra

  67. #67

    Hello there, I discovered your web site by the use of Google whilst searching
    for a similar subject, your web site got here up,
    it appears great. I’ve bookmarked it in my google bookmarks.

    Hello there, just turned into aware of your blog through Google, and
    located that it’s truly informative. I’m gonna be careful for
    brussels. I will appreciate if you proceed this in future.
    Many people will probably be benefited from your
    writing. Cheers! https://cialis.lm360.us/tadalafil

  68. #68

    This excellent website definitely has all the information I wanted about this subject and didn’t know who to
    ask. https://hydroxychloroquinem.mrdgeography.com/

  69. #69

    What i do not understood is actually how you are not actually much more well-liked than you may be now. You’re very intelligent. You realize thus considerably relating to this subject, made me personally consider it from numerous varied angles. Its like women and men aren’t fascinated unless it’s one thing to do with Lady gaga! Your own stuffs excellent. Always maintain it up!

    http://www.vreyrolinomit.com/

发表评论

电邮地址用于 Gravatar 头像显示,不会被公开可见。