SSH基础

1、 前言

本篇主要用来讲述SSH的连接相关信息,并解释配置文件的用途。

2、 SSH连接

SSH作为secure shell,用来取代telnet的远程连接,从而需要对数据进行加密传输,对主机进行身份认证,并且需要保证数据的完整性。

SSH分为两个版本,在有些设备上使用的还是v1版本,在现在大部分的设备上使用的是v2版本,主要是因为v1版本的安全性是比不上v2的。

SSH配置详解_java

SSH协议的基础上,在linux上的实现主要为openssh(为C/S架构,也就是客户端/服务器端),查看系统上安装的软件如下所示:

[root@rsyncserver~]# rpm -qa |grep ssh

openssh-5.3p1-70.el6.x86_64客户端和服务端的的核心类库文件

openssh-server-5.3p1-70.el6.x86_64ssh服务端,主要提供sshd进程

openssh-clients-5.3p1-70.el6.x86_64ssh客户端,主要实现加密连接

libssh2-1.2.2-7.el6_1.1.x86_64ssh2协议实现的类库

在查看各个类库信息描述的时候,可以使用如下命令:

[root@rsyncserver~]# for i in `rpm -qa |grep ssh`;do rpm -qi $i ;done查看ssh软件的描述信息

 

查看相关的配置文件:

[root@rsyncserver~]# cd /etc/sshssh配置文件路径

[root@rsyncserverssh]# ls -l密钥权限均为600

total 160

-rw-------.1 root root 125811 Sep  7  2011 moduli

-rw-r--r--. 1root root   2047 Sep  7  2011ssh_config客户端配置文件

-rw-------  1 root root  3869 Sep 29 01:47 sshd_config服务端配置文件

-rw-------.1 root root    668 Aug 11 08:22ssh_host_dsa_key主机dsa密钥V2版本

-rw-r--r--. 1root root    590 Aug 11 08:22ssh_host_dsa_key.pub主机dsa公钥V2版本

-rw-------. 1 root root    963 Aug 11 08:22 ssh_host_key主机密钥V1版本

-rw-r--r--. 1root root    627 Aug 11 08:22ssh_host_key.pub主机公钥V1版本

-rw-------.1 root root   1675 Aug 11 08:22ssh_host_rsa_key主机rsa密钥V2版本

-rw-r--r--. 1root root    382 Aug 11 08:22ssh_host_rsa_key.pub主机rsa密钥V2版本

2.1 服务端配置文件

服务端配置文件主要内容如下所示:

Port 49999 默认端口为22,如果要修改端口,那么取消注释,写上端口号即可,这里修改为49999端口,安全性,修改为其他端口

#AddressFamilyany表示使用的协议,any表示为IPV4IPV6,

#ListenAddress0.0.0.0监听地址,默认表示所有网卡均监听,也可以指定特定的网卡监听

#ListenAddress::

 

# Disable legacy(protocol version 1) support in the server for new

# installations.In future the default will change to require explicit

# activation ofprotocol 1

Protocol 2 指定使用使用ssh协议版本,默认版本号为SSHV2

 

# HostKey forprotocol version 1

#HostKey/etc/ssh/ssh_host_key主机密钥信息,SSH V1

# HostKeys forprotocol version 2

#HostKey/etc/ssh/ssh_host_rsa_key主机密钥信息,SSHV2 rsa算法密钥

#HostKey/etc/ssh/ssh_host_dsa_key主机密钥信息,SSHV2 dsa算法密钥

 

# Lifetime andsize of ephemeral version 1 server key

#KeyRegenerationInterval1h随机生成的公钥更换时间

#ServerKeyBits1024密钥长度

 

# Logging

# obsoletesQuietMode and FascistLogging

#SyslogFacilityAUTH

SyslogFacilityAUTHPRIV 记录的日志子系统未AUTHPRIV,主要使用服务rsyslog来记录

#LogLevel INFO日志信息级别为INFO

 

#Authentication:

 

#LoginGraceTime2m 输入账号密码的超时时间

PermitRootLoginyes是否允许root账户登录,安全性,最好禁止

#StrictModes yes

#MaxAuthTries 6最大尝试次数

#MaxSessions 10 最大连接数

 

#RSAAuthenticationyes

#PubkeyAuthenticationyes启用密钥认证连接

#AuthorizedKeysFile.ssh/authorized_keys密钥文件存放位置

#AuthorizedKeysCommandnone

#AuthorizedKeysCommandRunAsnobody

 

# For this towork you will also need host keys in /etc/ssh/ssh_known_hosts主机认证相关信息,也就是在known_hosts中的主机是否信任连接

#RhostsRSAAuthenticationno

# similar forprotocol version 2

#HostbasedAuthenticationno

# Change to yesif you don't trust ~/.ssh/known_hosts for

#RhostsRSAAuthentication and HostbasedAuthentication

#IgnoreUserKnownHostsno

# Don't read theuser's ~/.rhosts and ~/.shosts files

#IgnoreRhostsyes

 

# To disabletunneled clear text passwords, change to no here!

#PasswordAuthenticationyes

PermitEmptyPasswordsno不允许空密码登录

PasswordAuthenticationyes(启用密码认证)

 

# Change to noto disable s/key passwords

#ChallengeResponseAuthenticationyes

ChallengeResponseAuthenticationno

 

# KerberosoptionsKerberos认证相关信息,默认未启用

#KerberosAuthenticationno

#KerberosOrLocalPasswdyes

#KerberosTicketCleanupyes

#KerberosGetAFSTokenno

#KerberosUseKuserokyes

 

# GSSAPI optionsGSSAPI认证信息,默认是开启的,会导致ssh连接缓慢

#GSSAPIAuthenticationno

GSSAPIAuthenticationno

#GSSAPICleanupCredentialsyes

GSSAPICleanupCredentialsyes

#GSSAPIStrictAcceptorCheckyes

#GSSAPIKeyExchangeno

 

# Set this to'yes' to enable PAM authentication, account processing,

# and sessionprocessing. If this is enabled, PAM authentication will

# be allowedthrough the ChallengeResponseAuthentication and

#PasswordAuthentication.  Depending onyour PAM configuration,

# PAMauthentication via ChallengeResponseAuthentication may bypass

# the setting of"PermitRootLogin without-password".

# If you justwant the PAM account and session checks to run without

# PAMauthentication, then enable this but set PasswordAuthentication

# andChallengeResponseAuthentication to 'no'.

#UsePAM no

UsePAM yes 使用PAM模块进行认证

 

# Acceptlocale-related environment variables

AcceptEnv LANGLC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES

AcceptEnvLC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT

AcceptEnvLC_IDENTIFICATION LC_ALL LANGUAGE

AcceptEnvXMODIFIERS

 

#AllowAgentForwardingyes

#AllowTcpForwardingyes

#GatewayPorts no

#X11Forwardingno

X11Forwardingyes是否允许转发

#X11DisplayOffset10

#X11UseLocalhostyes

#PrintMotd yes打印/etc/motd中内容,安全性,最好不打印

#PrintLastLogyes(打印上次登录信息,安全性,最好不打印

#TCPKeepAliveyes

#UseLogin no

#UsePrivilegeSeparationyes

#PermitUserEnvironmentno

#Compression delayed

#ClientAliveInterval0客户端超时时间,0表示无限

#ClientAliveCountMax3

#ShowPatchLevelno

UseDNS no是否进行DNS解析可能会导致ssh连接时间过长

#PidFile/var/run/sshd.pid

#MaxStartups 10

#PermitTunnel no

#ChrootDirectorynone

 

# no defaultbanner path

#Banner none 打印自定义的欢迎信息

 

# overridedefault of no subsystems

Subsystem   sftp   /usr/libexec/openssh/sftp-server子系统,可以使用sftp

 

# Example ofoverriding settings on a per-user basis

#Match Useranoncvs

#   X11Forwarding no

#   AllowTcpForwarding no

#   ForceCommand cvs server

更多信息可以使用man sshd_config来进行查看

2.2 查看ssh连接过程

查看ssh连接过程的时候,主要使用选项-v,如下所示:

[root@rsyncserver~]# ssh -v 192.168.1.63 使用调试选项来连接远程主机

OpenSSH_5.3p1,OpenSSL 1.0.0-fips 29 Mar 2010

debug1: Readingconfiguration data /etc/ssh/ssh_config加载客户端配置文件

debug1: Applyingoptions for *

debug1:Connecting to 192.168.1.63 [192.168.1.63] port 22.读取参数,例如主机地址和端口

debug1:Connection established.

debug1:permanently_set_uid: 0/0

debug1: identityfile /root/.ssh/identity type -1

debug1: identityfile /root/.ssh/id_rsa type 1

debug1: identityfile /root/.ssh/id_dsa type -1

debug1: Remoteprotocol version 2.0, remote software version OpenSSH_5.3确定要使用的SSH版本及协议版本号

debug1: match:OpenSSH_5.3 pat OpenSSH*

debug1: Enablingcompatibility mode for protocol 2.0

debug1: Localversion string SSH-2.0-OpenSSH_5.3

debug1:SSH2_MSG_KEXINIT sent发送客户端支持的密钥算法

debug1:SSH2_MSG_KEXINIT received接受服务端支持的密钥算法

debug1: kex:server->client aes128-ctr hmac-md5 none

debug1: kex:client->server aes128-ctr hmac-md5 none

debug1:SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent

debug1:expecting SSH2_MSG_KEX_DH_GEX_GROUP

debug1:SSH2_MSG_KEX_DH_GEX_INIT sent

debug1:expecting SSH2_MSG_KEX_DH_GEX_REPLY

The authenticityof host '192.168.1.63 (192.168.1.63)' can't be established.

RSA key fingerprint is e2:b9:ba:6b:ca:f5:a8:29:b8:1a:60:85:8b:a4:e3:a6.

Are you sure youwant to continue connecting (yes/no)? Yes确认主机密钥,主机身份认证

debug1:ssh_rsa_verify: signature correct

debug1:SSH2_MSG_NEWKEYS sent认证请求

debug1:expecting SSH2_MSG_NEWKEYS

debug1:SSH2_MSG_NEWKEYS received

debug1:SSH2_MSG_SERVICE_REQUEST sent

debug1:SSH2_MSG_SERVICE_ACCEPT received

debug1:Authentications that can continue: publickey,password密钥认证

debug1: Nextauthentication method: publickey

debug1: Tryingprivate key: /root/.ssh/identity

debug1: Offeringpublic key: /root/.ssh/id_rsa

debug1:Authentications that can continue: publickey,password

debug1: Tryingprivate key: /root/.ssh/id_dsa

debug1: Nextauthentication method: password密码认证

root@192.168.1.63'spassword:

debug1:Authentication succeeded (password).

debug1: channel0: new [client-session]

debug1:Requesting no-more-sessions@openssh.com

debug1: Enteringinteractive session.

debug1: Sendingenvironment.

debug1: Sendingenv.UTF-8

Last login: SatNov  5 09:52:59 2016 from 192.168.1.70打印相关信息,此处为上次登录信息

[root@django ~]#会话建立

在上面发送的主机密钥可以用如下方式得到:

[root@djangossh]# ssh-keygen -lf /etc/ssh/ssh_host_rsa_key主机公钥的指纹信息,-l表示获取fingerprint-f表示公钥文件的名称

2048e2:b9:ba:6b:ca:f5:a8:29:b8:1a:60:85:8b:a4:e3:a6/etc/ssh/ssh_host_rsa_key.pub (RSA)

 

总体大概过程如下所示:

SSH配置详解_java_02

2.3 ssh其他

在进行主机认证的时候,其实发送的文件如下所示:

[root@rsyncserver.ssh]# ls -l /etc/ssh/ssh_host_*.pub(主机公钥信息)

-rw-r--r--. 1root root 590 Aug 11 08:22 /etc/ssh/ssh_host_dsa_key.pub

-rw-r--r--. 1root root 627 Aug 11 08:22 /etc/ssh/ssh_host_key.pub

-rw-r--r--. 1root root 382 Aug 11 08:22 /etc/ssh/ssh_host_rsa_key.pub

默认情况下是rsa,从而发送的是ssh_host_rsa_key.pub文件,在进行测试的时候,可以查看当前用户家目录下文件,对内容进行对比即可得到:

[root@rsyncserver~]# ls -l .ssh/known_hosts 主机认证信息存放文件

-rw-r--r-- 1root root 394 Nov  5 03:05 .ssh/known_hosts

[root@rsyncserver~]# cat .ssh/known_hosts 主机认证内容

192.168.1.63ssh-rsaAAAAB3NzaC1yc2EAAAABIwAAAQEAyCT4nKUkzUp6WXTlIAphvuildWWOw3wUZRbVHG1JVztVbMAXqLIqh0EhD3P3H4+vXrgxszUUsfkkuP9ONJSBwLxOhi3u9aZA/MGCYNjTmGJEIPkSQaurikdfRSMKmeFN5HubXK5d0nKwCQ7QI/bL0htESCVK1L1aUpP2Y6Anj2UwuDfHiR/eT6Ifk6GYYk+cx5TzTTEYZGHypP5NwQ4jhQJjHiZJ0MC79BzV3hdfqVgd/920NX7uCEkXjOKaE5vimxQ+5nWceu7fz82wrghpliIfisIKRA5ILCM5wH25e00/LIRjsIQZ5kmn7UTa6PQdaFlcQOTknEFtsAC8ejDlTQ==

[root@djangossh]# pwd

/etc/ssh

[root@djangossh]# cat ssh_host_rsa_key.pub 远程主机公钥文件内容

ssh-rsaAAAAB3NzaC1yc2EAAAABIwAAAQEAyCT4nKUkzUp6WXTlIAphvuildWWOw3wUZRbVHG1JVztVbMAXqLIqh0EhD3P3H4+vXrgxszUUsfkkuP9ONJSBwLxOhi3u9aZA/MGCYNjTmGJEIPkSQaurikdfRSMKmeFN5HubXK5d0nKwCQ7QI/bL0htESCVK1L1aUpP2Y6Anj2UwuDfHiR/eT6Ifk6GYYk+cx5TzTTEYZGHypP5NwQ4jhQJjHiZJ0MC79BzV3hdfqVgd/920NX7uCEkXjOKaE5vimxQ+5nWceu7fz82wrghpliIfisIKRA5ILCM5wH25e00/LIRjsIQZ5kmn7UTa6PQdaFlcQOTknEFtsAC8ejDlTQ==

 

3、 关于SSH的安全性

在使用SSH的时候,建议如下:

Ø  禁止root登录,如果在互联网上传输root密码,那么可能会被暴力破解,可以使用普通用户登录,然后使用suroot用户或者sudo

Ø  修改ssh的端口号,可以使用windows的远程连接端口号