SSH远程登录配置文件详解

配置文件在/etc/ssh/目录

ssh_configsshd_config 都是 ssh 服务器的配置文件,二者区别在于,前者是针对客户端的配置文件,后者则是针对服务端的配置文件。两个配置文件都允许你通过设置不同的选项来改变客户端程序的运行方式。

1.ssh_config 文件

内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Site-wide defaults for various options
Host *
ForwardAgent no
ForwardX11 no
RhostsAuthentication no
RhostsRSAAuthentication no
RSAAuthentication yes
PasswordAuthentication yes
FallBackToRsh no
UseRsh no
BatchMode no
CheckHostIP yes
StrictHostKeyChecking no
IdentityFile ~/.ssh/identity
Port 22
Cipher blowfish
EscapeChar

详解:
# Site-wide defaults for various options

带“#”表示该句为注释不起作用,该句不属于配置文件原文,意在说明下面选项均为系统初始默认的选项。说明一下,实际配置文件中也有很多选项前面加有“#”注释,虽然表示不起作用,其实是说明此为系统默认的初始化设置。

Host *
“Host”只对匹配后面字串的计算机有效,“*”表示所有的计算机。从该项格式前置一些可以看出,这是一个类似于全局的选项,表示下面缩进的选项都适用于该设置,可以指定某计算机替换“*”号使下面选项只针对该算机器生效。

ForwardAgent no
“ForwardAgent”设置连接是否经过验证代理(如果存在)转发给远程计算机。

ForwardX11 no
“ForwardX11”设置 X11 连接是否被自动重定向到安全的通道和显示集(DISPLAY set)。

RhostsAuthentication no
“RhostsAuthentication”设置是否使用基于 rhosts 的安全验证。

RhostsRSAAuthentication no
“RhostsRSAAuthentication”设置是否使用用 RSA 算法的基于 rhosts 的安全验证。

RSAAuthentication yes
“RSAAuthentication”设置是否使用 RSA 算法进行安全验证。

PasswordAuthentication yes
“PasswordAuthentication”设置是否使用口令验证。

FallBackToRsh no
“FallBackToRsh”设置如果用 ssh 连接出现错误是否自动使用 rsh,由于 rsh 并不安全,所以此选项应当设置为”no”。

UseRsh no
“UseRsh”设置是否在这台计算机上使用”rlogin/rsh”,原因同上,设为”no”。

BatchMode no
“BatchMode”:批处理模式,一般设为”no”;如果设为”yes”,交互式输入口令的提示将被禁止,这个选项对脚本文件和批处理任务十分有用。

CheckHostIP yes
“CheckHostIP”设置 ssh 是否查看连接到服务器的主机的 IP 地址以防止 DNS 欺骗。建议设置为”yes”。

StrictHostKeyChecking no
“StrictHostKeyChecking”如果设为”yes”,ssh 将不会自动把计算机的密匙加入 $HOME/.ssh/known_hosts 文件,且一旦计算机的密匙发生了变化,就拒绝连接。

IdentityFile ~/.ssh/identity
“IdentityFile”设置读取用户的 RSA 安全验证标识。

Port 22
“Port”设置连接到远程主机的端口,ssh 默认端口为22。

Cipher blowfish
“Cipher”设置加密用的密钥,blowfish 可以自己随意设置。

EscapeChar
“EscapeChar”设置 escape 字符。

2.sshd_config文件

1
2
3
4
#Port 22 //这个是ssh服务的监听端口,在实际生产环境中一般都不用默认的22端口 
#AddressFamily any //any默认ipv4 ipv6
#ListenAddress 0.0.0.0
#ListenAddress ::

ListenAddress 用来设置 sshd 服务器绑定的 IP 地址;监听的主机适配卡,举个例子来说,如果您有两个 IP, 分别是 192.168.0.11192.168.2.20 那么只想要开放 192.168.0.11 时,就可以设置为:ListenAddress 192.168.0.11 表示只监听来自 192.168.0.11 这个 IPSSH 联机。如果不使用设定的话,则预设所有接口均接受 SSH

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
HostKey /etc/ssh/ssh_host_rsa_key 使用的 RSA 私钥
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
# Logging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV 日志
#LogLevel INFO
# Authentication:
#LoginGraceTime 2m 宽限登录时间不输入密码两分钟自动退出
#PermitRootLogin yes 改为no 是不让root连接(登录普通用户在切换到root可以)
#StrictModes yes 检查.ssh/文件的所有者,权限等
#MaxAuthTries 6 最大认证次数的一般6/2=3
#MaxSessions 10 克隆会话最大连接
#PubkeyAuthentication yes 支持公钥验证(一般开启公钥验证关闭用户登录)
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys
#AuthorizedPrincipalsFile none
#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes
# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no (允许空口令吗) 拒绝用户登录(一般开启公钥验证关闭用户登录)
PasswordAuthentication yes
# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no
# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes
# GSSAPI options
GSSAPIAuthentication yes//这里改为no 是让连接速度 加快
GSSAPICleanupCredentials no
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
#GSSAPIEnablek5users no
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
# WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several
# problems.
UsePAM no
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no (网关)
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation sandbox
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 30 (没操作的话30秒一到就断开连接)
#ClientAliveCountMax 0(没操作的话30秒一到就断开连接)
#ShowPatchLevel no
#UseDNS yes //这里改为no 是让连接速度 加快
#PidFile /var/run/sshd.pid //运行的服务编号
#MaxStartups 10:30:100 //当连接数超过10会以30%的失败率拒绝用户登录(达到100,100%拒绝)
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none
# no default banner path
#Banner none //ssh登录提示信息
Banner /etc/ssh/banner.txt //ssh登录读取内容
# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
AllowUsers wang //默认没这一行添加 只允许网用户登录(包括root)白名单
DenyUsers 黑名单(优先级高)
AllowGroups
DenyGroups
# override default of no subsystems
Subsystem sftp /usr/libexec/openssh/sftp-server
# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server

3.ssh登录日志

/var/log/secure