限制用户通过ssh密钥进行认证登陆

发布时间:2020-06-05 16:41:46 作者:xlogin
来源:网络 阅读:1625

 为了服务器和用户的安全,禁止用户密码的认证方式,而基于“钥匙”的方式。

  1. Last login: Fri Oct 12 14:14:01 2012 from 192.168.7.251 
  2. root@Cacti.Nagios:[/root]vi /etc/ssh/sshd_config  
  3. #       $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm Exp $ 
  4.  
  5. # This is the sshd server system-wide configuration file.  See 
  6. # sshd_config(5) for more information. 
  7.  
  8. # This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin 
  9.  
  10. # The strategy used for options in the default sshd_config shipped with 
  11. # OpenSSH is to specify options with their default value where 
  12. # possible, but leave them commented.  Uncommented options change a 
  13. # default value. 
  14.  
  15. #Port 22 
  16. #AddressFamily any 
  17. #ListenAddress 0.0.0.0 
  18. #ListenAddress :: 
  19.  
  20. # Disable legacy (protocol version 1) support in the server for new 
  21. # installations. In future the default will change to require explicit 
  22. # activation of protocol 1 
  23. Protocol 2 ← 修改后变为此状态,仅使用SSH2
  24.  
  25. # HostKey for protocol version 1 
  26. #HostKey /etc/ssh/ssh_host_key 
  27. # HostKeys for protocol version 2 
  28. #HostKey /etc/ssh/ssh_host_rsa_key 
  29. #HostKey /etc/ssh/ssh_host_dsa_key 
  30.  
  31. # Lifetime and size of ephemeral version 1 server key 
  32. #KeyRegenerationInterval 1h 
  33. #ServerKeyBits 1024 
  34.  
  35. # Logging 
  36. # obsoletes QuietMode and FascistLogging 
  37. #SyslogFacility AUTH 
  38. SyslogFacility AUTHPRIV 
  39. #LogLevel INFO 
  40.  
  41. # Authentication: 
  42.  
  43. #LoginGraceTime 2m 
  44. #PermitRootLogin yes 
  45. PermitRootLogin no ← 修改后变为此状态,不允许用root进行登录
  46. #StrictModes yes 
  47. #MaxAuthTries 6 
  48. #MaxSessions 10 
  49.  
  50. #RSAAuthentication yes 
  51. #PubkeyAuthentication yes 
  52. #AuthorizedKeysFile     .ssh/authorized_keys 
  53. #AuthorizedKeysCommand none 
  54. #AuthorizedKeysCommandRunAs nobody 
  55.  
  56. # For this to work you will also need host keys in /etc/ssh/ssh_known_hosts 
  57. #RhostsRSAAuthentication no 
  58. # similar for protocol version 2 
  59. #HostbasedAuthentication no 
  60. # Change to yes if you don't trust ~/.ssh/known_hosts for 
  61. # RhostsRSAAuthentication and HostbasedAuthentication 
  62. #IgnoreUserKnownHosts no 
  63. # Don't read the user's ~/.rhosts and ~/.shosts files 
  64. #IgnoreRhosts yes 
  65.  
  66. # To disable tunneled clear text passwords, change to no here! 
  67. #PasswordAuthentication yes 
  68. PasswordAuthentication no ← 修改后变为此状态,不允许密码方式的登录
  69. #PermitEmptyPasswords no 
  70. PermitEmptyPasswords no ← 修改后变为此状态,禁止空密码进行登录
  71. "/etc/ssh/sshd_config" 141L, 3941C written                             
  72. root@Cacti.Nagios:[/root]vi /etc/hosts.deny    ← 修改屏蔽规则,在文尾添加相应行
  73. # hosts.deny    This file contains access rules which are used to 
  74. #               deny connections to network services that either use 
  75. #               the tcp_wrappers library or that have been 
  76. #               started through a tcp_wrappers-enabled xinetd. 
  77. #               The rules in this file can also be set up in 
  78. #               /etc/hosts.allow with a 'deny' option instead. 
  79. #               See 'man 5 hosts_options' and 'man 5 hosts_access' 
  80. #               for information on rule syntax. 
  81. #               See 'man tcpd' for information on tcp_wrappers 
  82. sshd:ALL  添加这一行,屏蔽来自所有的SSH连接请求
  83. "/etc/hosts.deny" 14L, 469C written 
  84. You have new mail in /var/spool/mail/root 
  85. root@Cacti.Nagios:[/root]vi /etc/hosts.allow   修改允许规则,在文尾添加相应行
  86. # hosts.allow   This file contains access rules which are used to 
  87. #               allow or deny connections to network services that 
  88. #               either use the tcp_wrappers library or that have been 
  89. #               started through a tcp_wrappers-enabled xinetd. 
  90. #               See 'man 5 hosts_options' and 'man 5 hosts_access' 
  91. #               for information on rule syntax. 
  92. #               See 'man tcpd' for information on tcp_wrappers 
  93. sshd:192.168.7. 只允许192.168.7。网段的机器ssh登陆
  94. ~                                                                                        
  95. ~                                                                                        
  96. ~                                                                                        
  97. "/etc/hosts.allow" 11L, 386C written                                   
  98.  
  99. root@Cacti.Nagios:[/root]su - admin 
  100. admin@Cacti.Nagios:[/data]ssh-keygen -t rsa 
  101. Generating public/private rsa key pair. 
  102. Enter file in which to save the key (/data/.ssh/id_rsa):  
  103. Created directory '/data/.ssh'. 
  104. Enter passphrase (empty for no passphrase):  
  105. Enter same passphrase again:  
  106. Your identification has been saved in /data/.ssh/id_rsa. 
  107. Your public key has been saved in /data/.ssh/id_rsa.pub. 
  108. The key fingerprint is: 
  109. e5:15:ba:be:59:ef:2e:74:df:b6:ee:e1:6a:24:be:da admin@Cacti.Nagios 
  110. The key's randomart p_w_picpath is: 
  111. +--[ RSA 2048]----+ 
  112. |            .    | 
  113. |           . .   | 
  114. |          o .    | 
  115. |         o o     | 
  116. |        S o      | 
  117. |         . ....  | 
  118. |          o.+. o.| 
  119. |          .=.o. =| 
  120. |         .+Eo=B*.| 
  121. +-----------------+ 
  122. admin@Cacti.Nagios:[/data]ls -a 
  123. .  ..  .bash_history  .bash_logout  .bash_profile  .bashrc  lost+found  .ssh  .viminfo 
  124. admin@Cacti.Nagios:[/data]cd .ssh/ 
  125. admin@Cacti.Nagios:[/data/.ssh]ll 
  126. total 8 
  127. -rw------- 1 admin admin 1751 Oct 12 17:19 id_rsa 
  128. -rw-r--r-- 1 admin admin  401 Oct 12 17:19 id_rsa.pub 
  129. admin@Cacti.Nagios:[/data/.ssh]cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys 
  130. admin@Cacti.Nagios:[/data/.ssh]ls -a 
  131. .  ..  authorized_keys  id_rsa  id_rsa.pub 
  132. admin@Cacti.Nagios:[/data/.ssh]chmod 400 authorized_keys  
  133. admin@Cacti.Nagios:[/data/.ssh]ll -a 
  134. total 20 
  135. drwx------ 2 admin admin 4096 Oct 12 17:20 . 
  136. drwxr-xr-x 4 admin admin 4096 Oct 12 17:19 .. 
  137. -r-------- 1 admin admin  401 Oct 12 17:20 authorized_keys 
  138. -rw------- 1 admin admin 1751 Oct 12 17:19 id_rsa 
  139. -rw-r--r-- 1 admin admin  401 Oct 12 17:19 id_rsa.pub
  140.  
  141. 至此,将私钥id_rsa导出到windows客户端上。然后删除生成的公钥id_rsa.pub。
  142. 重启sshd服务,使得刚才所做的配置修改生效。
  143. root@Cacti.Nagios:[/root]/etc/rc.d/init.d/sshd restart Stopping sshd:                                             [  OK  ] Starting sshd:                                             [  OK  ]

 

限制用户通过ssh密钥进行认证登陆 限制用户通过ssh密钥进行认证登陆 限制用户通过ssh密钥进行认证登陆 限制用户通过ssh密钥进行认证登陆

 

推荐阅读:
  1. MQTT 限制匿名用户访问,开启用户密码认证
  2. Linux的SSH服务之密钥验证登陆

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ssh密钥进行认证登陆 限制用户

上一篇:Centos 7.2 安装配置 Nginx、PHP7、Mysql、JDK、Tomcat、Redis的详细教程

下一篇:mysql仅查询一条数据时遇到的坑

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》