ssh_config 与 sshd_config

ssh_config: configuration file for the ssh client on the host machine you are running. For example, if you want to ssh to another remote host machine, you use a SSH client. Every settings for this SSH client will be using ssh_config, such as port number, protocol version and encryption/MAC algorithms.

sshd_config: configuration file for the sshd daemon (the program that listens to any incoming connection request to the ssh port) on the host machine. That is to say, if someone wants to connect to your host machine via SSH, their SSH client settings must match your sshd_config settings in order to communicate with you, such as port number, version and so on.

--https://prasadlinuxblog.wordpress.com/2012/09/13/what-is-the-difference-between-ssh_config-and-sshd_config/

Host 在 SSH 中的作用

第一次使用 SSH 登陆服务器时,会有一个警告,要你进行选择。 Yes 之后会加入 ~/.ssh/known_hosts。

known_hosts 的作用:

The client may check that the server is a known one, and not some rogue server trying to pass off as the right one. SSH provides only a simple mechanism to verify the server's legitimacy: it remembers servers you've already connected to, in the ~/.ssh/known_hosts file on the client machine (there's also a system-wide file /etc/ssh/known_hosts). The first time you connect to a server, you need to check by some other means that the public key presented by the server is really the public key of the server you wanted to connect to. If you have the public key of the server you're about to connect to, you can add it to ~/.ssh/known_hosts on the client manually.

第一次连接未知的服务器,你需要通过公钥来确定服务器的正统性。当你确定后,已连接的服务器地址会加入到 known_hosts,之后就把这个地址视为可信任地址。

比如

The authenticity of host 'mint.phcomp.co.uk (78.32.209.33)' can't be established.
RSA key fingerprint is SHA256:jP0pfKJ9OAXt2F+LM7j3+BMalQ/2Koihl5eH/kli6A4.
Are you sure you want to continue connecting (yes/no)?

那么,如何检查服务器指纹呢,看了 ssh-check-server-fingerprint 还是不明所以。所以暂时先不管了。

用户验证

The server only lets a remote user log in if that user can prove that they have the right to access that account. Depending on the server's configuration and the user's choice, the user may present one of several forms of credentials (the list below is not exhaustive).

  • The user may present the password for the account that he is trying to log into; the server then verifies that the password is correct.

  • The user may present a public key and prove that he possesses the private key associated with that public key. This is exactly the same method that is used to authenticate the server, but now the user is trying to prove their identity and the server is verifying them. The login attempt is accepted if the user proves that he knows the private key and the public key is in the account's authorization list (~/.ssh/authorized_keys on the server).

  • Another type of method involves delegating part of the work of authenticating the user to the client machine. This happens in controlled environments such as enterprises, when many machines share the same accounts. The server authenticates the client machine by the same mechanism that is used the other way round, then relies on the client to authenticate the user.

--来源:https://unix.stackexchange.com/questions/42643/ssh-key-based-authentication-known-hosts-vs-authorized-keys

常用的用户验证方式有:

  • 使用账号、密码登陆;
  • 使用公钥私钥验证;  

通过 sshd_config 进行配置。

配置密码登陆方式

设置 sshd_config 的
PasswordAuthentication no

登陆命令

ssh user@host
输入密码

配置公钥验证方式

PubkeyAuthentication
Specifies whether public key authentication is allowed. The default is ''yes''. Note that this option applies to protocol version 2 only.

根据 https://kb.iu.edu/d/aews

首先在你的机器与服务器上要安装好 SSH,这个不做赘述。然后,你要把你的公钥添加到服务器的对应用户的 ~/.ssh/authorized_keys 里面。

如果你的公钥名不是默认的,可以通过 ssh_config 修改,

For example, for connections to host2.somewhere.edu, to make SSH automatically invoke the private key host2_key, stored in the ~/.ssh/old_keys directory, create a ~/.ssh/config file with these lines included:

vim ~/.ssh/config
Host host2.somewhere.edu
IdentityFile ~/.ssh/old_keys/host2_key

设置好之后,直接 ssh user@host 就可以了。

调试 ssh

  1. 客户端调试

ssh -vvv git@github.com

是不是有一堆调试信息输出?!快去看看连接时候用的私钥对不对!

  1. 服务端调试

/usr/sbin/sshd -d -p 2222

在客户端去连接这个新端口

ssh -vvv host -p 2222

此时两边都有日志输出

sshd -d 进入 debug 模式; -p 指定监听的端口号。

ssh -v

-v      Verbose mode.  Causes ssh to print debugging messages about its

tion, and configuration problems.  Multiple -v options increase

the verbosity.  The maximum is 3.

来源:http://rockybean.info/2015/04/13/ssh-login-debug-method-and-problems

有时候日志报的错范围太大了,那就要根据经验来定位或者一个一个尝试了。

比如,我将 PubkeyAuthentication 设置为 no,然后连接报错

ssh_exchange_identification: Connection closed by remote host

这个错范围就大了。

MacOS 的 sshd_config 的问题

默认的 sshd_config 原文见:https://apple.stackexchange.com/questions/271948/setup-config-of-ssh-macos

我将 #PubkeyAuthentication yes 修改成 PubkeyAuthentication yes,然后使用 ssh 连接,就报错:

ssh_exchange_identification: Connection closed by remote host

  

学习 SSH的更多相关文章

  1. 烂泥:学习ssh之ssh密钥随身携带

    本文由秀依林枫提供友情赞助,首发于烂泥行天下 在上一篇文章<烂泥:学习ssh之ssh无密码登陆>中,我们讲解了如何使用ssh密钥,免密码登陆服务器. 这篇文章我们再来讲解,如何把已经生成的 ...

  2. 烂泥:学习ssh之ssh无密码登陆

    本文由秀依林枫提供友情赞助,首发于烂泥行天下 最近一个月没有写过文章,主要是刚刚换的新工作.新公司服务器OS使用的是ubuntu server版,和以前熟悉的centos还是有很多不同的. 刚好这几天 ...

  3. 从0开始学习ssh之搭建环境

    ssh即struts+spring+Hibernate,从头开始学习这个框架. struts环境配置,首先在apps目录下找到struts2-blank-xxx.war这个文件,这是已经发布好的war ...

  4. 学习SSH框架

    1.SSH框架的认知 在做相关的java的网页的开发制作时,良好的层次分解是十分有比要的,所以我们在云涌第三方的框架之下来简化还有名了我们相关的网站的开发. SSH框架实则为Struct + spri ...

  5. 菜鸟学习SSH——目录

    菜鸟学习Struts--配置Struts环境 菜鸟学习Struts--简易计算器 菜鸟学习Struts--bean标签库 菜鸟学习Struts--Scope属性 菜鸟学习Struts--国际化 菜鸟学 ...

  6. 菜鸟学习SSH(一)——Struts实现简单登录(附源码)

    从今天开始,一起跟各位聊聊java的三大框架——SSH.先从Struts开始说起,Struts对MVC进行了很好的封装,使用Struts的目的是为了帮助我们减少在运用MVC设计模型来开发Web应用的时 ...

  7. Struts2学习-ssh框架

    SSH是 struts+spring+hibernate的一个集成框架,是目前比较流行的一种Web应用程序开源框架. http://www.cnblogs.com/laibin/p/5847111.h ...

  8. 从零开始学习SSH框架笔记之四 几个关键配置文件备忘(模板)

    不多说,直接上代码.关于注释我尽量写详细点. 1.web.xml <?xml version="1.0" encoding="UTF-8"?> &l ...

  9. 从0开始学习ssh之资源分类

    更目录下面,新建config用于放配置文件,新建test用于放置测试文件.src目录用于放置源代码.由于ssh是三层,因此新建三层包(dao,service,view).其中dao和service还有 ...

随机推荐

  1. POJ1037 A decorative fence 【动态规划】

    A decorative fence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6489   Accepted: 236 ...

  2. 后缀数组suffix array

    倍增算法,时间复杂度O(nlogn) sa从小到大保存相对大小的下标 理解LSD,x数组,sa数组 char s[maxn]; int sa[maxn],t[maxn],t2[maxn],c[maxn ...

  3. 对非正确使用浮点型数据而导致项目BUG的问题探讨

    乘法分配律 在上小学的时候就已经学习过乘法分配律,乘法分配律的详细内容是:两个数的和与一个数相乘.能够先把他们分别与这个数相乘,再相加.得数不变.乘法分配律的定义还能够用表达式"(a+b)× ...

  4. (一)Linux——Linux基本概念

    Linux是一种自由和开放源码的类UNIX操作系统,使用Linux内核.目前存在着许多不同的Linux发行版,可安装在各种各样的电脑硬件设备,从手机.平板电脑.路由器和影音游戏控制台,到桌上型电脑,大 ...

  5. WP8 NavigationInTransition实现页面切换效果

    NavigationInTransition这个是实现页面切换效果,而且没控件来拖,要自己手动写, 将App.xaml.cs中InitializePhoneApplication()函数里的RootF ...

  6. TCP/IP ---分层

    TCP/IP的分层 ICMP是IP协议的附属协议.IP层用它来与其他主机或路由器交换错误报文和其他重要信息.尽管ICMP主要被IP使用,但应用程序也有可能访问它.我们将分析两个流行的诊断工具,Ping ...

  7. 牛散NO.1:MACD计啜诱多,勾魂枪连环夺命时

    上证日线“连环夺命勾魂枪” 话说MACD中圈C的回勾,好事者皆认为新的冲击波即将曙光再现.伴随着K线出现红柱中阳,更多的投资者将会被这一勾诱惑得群情亢奋,盲断行情又要 起来了.但往往事与愿违,“潘金莲 ...

  8. ucosii任务切换OS_TASK_SW()

    stm32F103中任务切换定义 //任务切换宏,由汇编实现. #define OS_TASK_SW() OSCtxSw() os_cpu_a.asm中任务切换函数的定义 NVIC_INT_CTRL ...

  9. Tomcat闲聊

    上面是Tomcat的执行过程. 下面说几个概念: 1.JSP:这个就是在html文件中嵌入Java代码之后重新生成的一个文件(.jsp的文件) 2.servlet:就是一个处理动态资源的小程序,一般位 ...

  10. centos 源码安装apache 之apr、apr-util

    apr 和 apr-util官网下载地址: http://apr.apache.org/download.cgi 安装顺序是先安装apr然后再安装 apr-util,因为安装apr-util需要apr ...