1、备份原sshd配置文件

cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup

2、修改sshd配置文件

可以使用winscp、nano、vim之类的软件编辑 /etc/ssh/sshd_config

查找Port 22,有可能 Port 22 是注释的(即前面有#号,有的话删掉 # 号)。

在 Port 22 下面添加一行 Port 3322 其中3322为你更改SSH后的端口。

这里建议先保留原来的22端口,待新的SSH端口测试正常工作后再删掉原Port 22 端口行。

修改完成后保存。

查看端口占用情况

1
lsof -i:端口号
1
2
3
4
5
6
7
8
9
lsof -i:8080:查看8080端口占用
lsof abc.txt:显示开启文件abc.txt的进程
lsof -c abc:显示abc进程现在打开的文件
lsof -c -p 1234:列出进程号为1234的进程所打开的文件
lsof -g gid:显示归属gid的进程情况
lsof +d /usr/local/:显示目录下被进程开启的文件
lsof +D /usr/local/:同上,但是会搜索目录下的目录,时间较长
lsof -d 4:显示使用fd为4的进程
lsof -i -U:显示所有打开的端口和UNIX domain文件
1
2
3
4
5
6
7
8
9
10
11
netstat
netstat -tunlp 用于显示 tcp,udp 的端口和进程等相关情况。

netstat 查看端口占用语法格式:

netstat -tunlp | grep 端口号
-t (tcp) 仅显示tcp相关选项
-u (udp)仅显示udp相关选项
-n 拒绝显示别名,能显示数字的全部转化为数字
-l 仅列出在Listen(监听)的服务状态
-p 显示建立相关链接的程序名

3、重启SSH服务器

重启SSH服务器命令:

1
systemctl restart sshd

如果没有systemctl的话可以执行:

1
/etc/init.d/sshd restart** 或 **/etc/init.d/ssh restart

如果没有报错的话就生效了,可以 ss -ntl 或 netstat -ntl 查看一下端口。

4、防火墙、安全组规则设置

iptables添加SSH新端口规则:

1
iptables -A INPUT -p tcp --dport 3322 -j ACCEPT       #3322替换为新的端口#

然后保存iptables规则

firewalld添加SSH新端口规则:

1
2
firewall-cmd --permanent --zone=public --add-port=3322/tcp      #3322替换为新的端口
firewall-cmd --reload #重载firewalld#

阿里云之类的安全组规则添加SSH新端口规则:

阿里云之类的有安全组之类设置的云服务器一定要在安全组规则里将新端口添加到“入方向”的允许规则。

5、putty、xshell之类的SSH软件连接测试

添加上新的SSH端口后一定要通过putty、xshell之类的SSH软件连接测试一下,测试可以正常连接然后再编辑 /etc/ssh/sshd_config 将Port 22 这一行直接删除或这一行前面加 # 注释掉,然后再按前面的命令重启SSHd服务。

SSH隧道代理

最近pypi镜像搭建好了,实际上pip本身有一个–proxy的指令,能够制定一个代理,就能完成内网机器无法通过pip访问外网资源的问题. 通过代理就能完成基本的下载包的需求了,而且其他的一些命令如gem,curl等等大多数常见的命令都支持走代理模式。

而且大家总在说 “用nginx做反向代理”,那么到底什么代理,什么是反向代理呢?

正向代理

正向代理是客户端和服务器中间的服务器,为了从原始服务器取得内容,客户端向代理服务器发送一个请求并指定目标(原始服务器), 然后代理服务器向原始服务器转发请求并将获得的内容返回给客户端。

举个例子,比如正常情况下我没法办上youtube,但是我有个aws的机器,它不受GFW的限制,能够访问youtube,我也能正常访问那个aws的机器, 那么我通过发送请求给aws的机器,让他转发我的请求给youtube,然后把youtube返回的数据给我,我就能通过aws的机器作为跳板访问GFW, 那个aws的机器也就是代理服务器的角色,并且这种方式就是正向代理。

总结一下,正向代理就是我想访问一台机器,但是被墙了访问不到,我需要一台机器作为跳板转发我的请求。

反向代理

于正向代理不同,反向代理更多的是为了保护原始服务器。 对于客户端而言,反向代理中的代理服务器就是原始服务器,客户端并不需要知道有这个代理的角色存在, 因此客户端也不需要一些额外的设置,比如正向代理中制定代理服务器是谁。

比如在原始服务器A上配置防火墙,使得只有服务器B能够访问A,并且通过B服务器转发A的数据实现于外界的通信。这样对于客户端,它只需要和B交互, 从而隐藏了服务器A,B服务器也就是反向代理服务器。由于有代理服务器的存在,对于后面的原始服务器来说,也就多了一层做负载均衡的服务器。

透明代理

还有一种代理方式叫做透明代理,比如我在公司的时候机器就不能上qq,这个就是透明代理,它在内网和外网之间捕捉用户的请求,过滤一部分请求。

SSH端口转发

有一种很简单的方式就能做到代理功能,那就是SSH本身提供的端口转发功能。 要想理解清楚SSH端口转发,首先必须记住这样几个原则:

  • SSH简单的理解就是2台机器之间安全的数据通道,它包括ssh的client和ssh的server2个角色,这样的一条通道也就是ssh隧道(ssh tunneling)
  • SSH 端口转发自然需要 SSH 连接,而 SSH 连接是有方向的,从 SSH Client 到 SSH Server
  • 我们的应用的请求也是有方向的,一般是客户端向服务器端发出请求
  • 一旦这2个方向相同,我们称为ssh的本地转发(-L),不同则为远端转发(-R)
  • 命令一般是跑在ssh client的机器上的

本地转发

ssh本地转发命令为:

1
ssh -L <local port>:<remote host>:<remote port> <SSH hostname>

这条命令可以翻译成:从本地的端口发出请求,通过这台机器作为跳板转发请求到的端口。 是相对而言的,比如是127.0.0.1的话,就是本身。 所以一般如果是127.0.0.1的话,跳板机或者代理服务器就是目标服务器。

举个例子:

  1. A是一台在我家的机器macbook air,它可以访问taobao,也就是服务器C
  2. B是一台在公司的机器imac,由于在公司的内网,所以在家的A访问不到B
  3. C是taobao的服务器,公司不让上taobao,所以服务器B访问不了C

现在要想在让B服务器能访问C的80端口,由于防火墙这条路本身是走不通的,但是由于B能访问A,A能访问C,所以能把A作为代理服务器实现这一要求。

在服务器B和服务器A之间建立ssh隧道,在SSH端口转发中,由于服务器B能连接到服务器A,并且请求是从服务器B发出,所以B既是ssh的client,也是请求的客户端 所以此时应该在B上去运行ssh的本地转发命令: ssh -L 8080:HOST_C:80 HOST_A

远端转发

ssh远端转发的命令为:

1
ssh -R <local port>:<remote host>:<remote port> <SSH hostname>

由于本身B是可以访问A的,但是A访问不到B,现在要想服务器A访问到B,也就是在家能连上公司的机器。

服务器B能连上A,所以B应该是ssh的client,此时请求是由A发起的,所以A是请求客户端,方向不同,所以是远端转发。 因此在B上运行命令: ssh -R 2222:127.0.0.1:22 HOST_A ,这里127.0.0.1是因为通过A服务器转发的目的服务器就是A本身。 这样在A上的2222端口就映射到了B的22端口。

动态转发

不管是本地转发还是远端转发,都需要一个具体的应用服务器的地址和端口号,要想访问其他机器的内容就得绑定很多条这样的转发命令, 通过动态转发就能省去这一个应用服务器的信息。

1
ssh -D <local port> <SSH Server>

当我们在一个不安全的 WiFi 环境下上网,用 SSH 动态转发来保护我们的网页浏览等信息无疑是十分必要的。 比如在本机运行: sh -D 7001 <SSH Server> 这样就相当于通过创建了一个SOCKS代理。

我们可以直接使用localhost:7001 来作为正常的 SOCKS 代理来使用,直接在浏览器上设置即可。 在 SSH Client 端无法访问的网站现在也都可以正常浏览。 而这里需要值得注意的是,此时 SSH 所包护的范围只包括从浏览器端(SSH Client 端)到 SSH Server 端的连接,并不包含从 SSH Server 端 到目标网站的连接。 如果后半截连接的安全不能得到充分的保证的话,这种方式仍不是合适的解决方案。

这个时候还可以在本机将SOCKS代理转成HTTP代理。 比如在本地安装polipo,修改polipo.conf文件,把SOCKS代理填上127.0.0.1:7001,然后 export http_proxy="127.0.0.1:8123" && export https_proxy="127.0.0.1:8123" 就在本地的8123端口起了一个http代理。

参考了IBM的实战 SSH 端口转发

服务端添加心跳时间

1
2
3
GatewayPorts yes          # 开启反向隧道
ClientAliveInterval 60 # 保证服务不断开,每60秒自动发送一次请求
ClientAliveCountMax 36000 # 十个小时没有发送请求即自动断开

客户端配置心跳包时间

如果没有服务器端的权限,也可在客户端进行配置,这样这个客户端所发起的所有会话都会产生效果。

修改/etc/ssh/ssh_config文件,与服务器端配置类似,添加以下两个参数即可:

1
2
ServerAliveInterval  
ServerAliveCountMax

会话连接添加心跳时间

如果不希望或不能修改配置文件,也可以在每次建立SSH连接时通过-o参数指定当前会话配置:

1
2
3

ssh -o ServerAliveInterval=30 root@192.168.1.1

Debian OpenSSH默认配置文件 (翻译)

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/usr/bin:/bin:/usr/games

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.

Include /etc/ssh/sshd_config.d/*.conf

# 连接端口 默认22:
#Port 22
# 指SSH用什么地址 参数:any、inet、inet6:
#AddressFamily any
# 主机名|域名|ipv4 :
#ListenAddress 0.0.0.0
#ListenAddress ::

# 私钥地址
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key

# Ciphers and keying
# 指定可以传输的最大数据量或在重新协商会话密钥之前收到,以字节为单位,可能有后缀"K"、"M"或"G"来指示分别为千字节、兆字节或千兆字节。默认介于"1G"和"4G"之间,具体取决于密码。该操作第二个值以秒为单位指定,可以使用以下任何一个"时间格式"部分中记录的单位:
#RekeyLimit default none

# Logging
# 记录消息值使用的设施代码,参数:DAEMON,USER,AUTH,LOCAL0,LOCAL1,LOCAL2,LOCAL3,LOCAL4,LOCAL5,LOCAL6,LOCAL7,默认AUTH:
#SyslogFacility AUTH
# 参数:QUIET,FATAL,ERROR,INFO,VERBOSE,DEBUG,DEBUG1,DEBUG2,DEBUG3.默认INFO,DEBUG与DEBUG1等效,DEBUG2,DEBUG3各自指定更高级的调试:
#LogLevel INFO

# Authentication:

# 用户未登录成功,指定服务端多久断开连接,默认120s即2m,0表示不断开:
#LoginGraceTime 2m

# 指定root用户是否可以登录,参数:yes,prohibit-password,forced-commands-only,no.注意的是prohibit-password,forced-commands-only都为yes可以root用户登录
# prohibit-password:关闭root用户密码和键盘交互验证
# forced-commands-only:root允许公钥认证登录,禁止其他认证方法:

#PermitRootLogin prohibit-password

# 用户登录之前检查文件模式以及用户文件和主目录的所有权。这通常是可取的,因为新手有时会意外地将其目录或文件设置为全局可写。默认值为 yes:
#StrictModes yes
# 允许的最大身份验证尝试次数。一旦失败次数达到该值的一半,就会记录更多失败。默认值为 6,合理设置辞职,可以防止攻击者穷举登录服务器:
#MaxAuthTries 6
# 每个网络连接允许的打开 shell、登录或子系统(例如 sftp)会话的最大数量。支持连接复用的客户端可以建立多个会话。将MaxSessions设置为 1 将有效禁用会话复用,而将其设置为 0 将阻止所有 shell、登录和子系统会话,同时仍允许转发。默认值为 10:
#MaxSessions 10

# 指定是否公钥认证:
#PubkeyAuthentication yes

# Expect .ssh/authorized_keys2 to be disregarded by default in future.
# 指定授权密钥文件路径,该指令中可以使用下列根据连接时的实际情况进行展开的符号: %% 表示'%'、%h 表示用户的主目录、%u 表示该用户的用户名:
#AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2

#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用来设置ssh在进行RhostsRSAAuthentication安全验证时是否忽略用户的"/$HOME/.ssh/known_hosts"文件:
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
# 用来设置是否开启密码验证机制,如果用密码登录系统,则设置yes:
#PasswordAuthentication yes
# 设置是否允许用口令为空的账号登录系统,设置no:
#PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
# 指定是否允许键盘交互式身份验证。支持login.conf(5)中所有身份验证样式。默认值为yes。此关键字的参数必须为yes或no。ChallengeResponseAuthentication是此功能的过时别名:
KbdInteractiveAuthentication no

# Kerberos options
# 指定了用户为PasswordAuthentication提供的密码是否通过Kerberos KDC进行验证。要使用此选项,服务器需要一个Kerberos servtab,以便验证KDC的身份。默认情况下为no:
#KerberosAuthentication no
# 如果通过Kerberos的身份验证失败,密码将通过任何其他本地机制(如/etc/passwd)进行验证。默认值为yes:
#KerberosOrLocalPasswd yes
# 指定是否在注销时自动销毁用户的票据缓存文件。默认值为 yes:
#KerberosTicketCleanup yes
# 如果AFS已启用并且用户具有Kerberos 5 TGT,则在访问用户主目录之前尝试获取AFS令牌。默认情况下为no:
#KerberosGetAFSToken no

# GSSAPI options
# 指定了用户为PasswordAuthentication提供的密码是否通过Kerberos KDC进行验证。要使用此选项,服务器需要一个Kerberos servtab,以便验证KDC的身份。默认情况下为no:
#GSSAPIAuthentication no
# 指定注销时是否自动销毁用户凭据缓存。默认为yes:
#GSSAPICleanupCredentials yes
# 确定客户端对GSS API接受者的身份验证是否严格。如果设置为yes,则客户端必须针对当前主机名上的主机服务进行身份验证。如果设置为no,则客户端可以针对存储在机器默认存储中的任何服务密钥进行身份验证。此功能旨在协助多 homed 机器上的操作。默认情况下是yes:
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange 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 KbdInteractiveAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via KbdInteractiveAuthentication may bypass
# the setting of "PermitRootLogin prohibit-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and KbdInteractiveAuthentication to 'no'.
UsePAM yes

# 指定了是否允许ssh-agent(1)转发。默认值为yes。注意,禁用代理转发并不能增强安全性,除非还拒绝了用户的shell访问权限,因为他们总是可以安装自己的转发器:
#AllowAgentForwarding yes
# 指定是否允许TCP转发。可用选项包括:yes(默认)或all以允许TCP转发,no以阻止所有TCP转发,local仅允许本地(从ssh(1)的角度)转发,或remote仅允许远程转发。请注意,禁用TCP转发并不一定能增强安全性,除非同时拒绝用户shell访问,因为用户总能自行安装转发工具:
#AllowTcpForwarding yes
# 指定是否允许远程主机连接到为客户端转发的端口。默认情况下,sshd(8)将远程端口转发绑定到回环地址,从而阻止其他远程主机连接到这些转发端口。使用GatewayPorts选项可以指示sshd允许远程端口转发绑定到非回环地址,从而允许其他主机进行连接。该论点可能主张远程端口转发仅对本地主机可用,即选择"no";也可能要求远程端口转发绑定到通配符地址,即选择"yes";或者允许客户端自选绑定地址,即选择"客户端指定"。默认设置为"no":
#GatewayPorts no
# 指定是否允许 X11 转发。参数 必须是 yes 或 no。默认值为 no。启用 X11 转发后,如果 sshd(8) 代理 显示配置为监听通配符地址(请参阅 X11UseLocalhost),则服务器和客户端显示可能会受到额外的暴露,尽管这不是默认值。此外,身份验证欺骗和身份验证数据验证和替换发生在客户端。使用 X11 转发的安全风险是,当 SSH 客户端请求转发时,客户端的 X11 显示服务器可能会受到攻击(请参阅 ssh_config(5) 中有关 ForwardX11 的警告)。系统管理员可能希望保护可能因不知情地请求 X11 转发而使自己暴露于攻击的客户端,这可能需要设置为 no。请注意,禁用 X11 转发并不会阻止用户转发 X11 流量,因为用户始终可以安装自己的转发器:
X11Forwarding yes
# 指定 sshd(8) 的 X11 转发可用的第一个显示编号。这可防止 sshd 干扰真实的 X11 服务器。默认值为 10:
#X11DisplayOffset 10
# 指定 sshd(8) 是否应将 X11 转发服务器绑定到环回地址或通配符地址。默认情况下,sshd 将转发服务器绑定到环回地址并将 DISPLAY 环境变量的主机名部分设置为 localhost。这可防止远程主机连接到代理显示。但是,某些较旧的 X11 客户端可能无法使用此配置。可以将 X11UseLocalhost 设置为 no,以指定转发服务器应绑定到通配符地址。参数必须是 yes 或 no。默认为 yes:
#X11UseLocalhost yes
# 指定是否允许 pty(4) 分配。默认为是:
#PermitTTY yes
# 指定当用户以交互方式登录时,sshd(8) 是否应打印 /etc/motd。(在某些系统上,它也由 shell、/etc/profile 或等效文档打印。)默认值为是:
PrintMotd no
# 指定 sshd(8) 是否应在用户以交互方式登录时打印上次用户登录的日期和时间。默认为是:
#PrintLastLog yes
# 指定系统是否应向另一端发送 TCP 保持活动消息。如果发送了这些消息,则连接中断或其中一台机器崩溃将会被正确注意到。但是,这意味着如果路由暂时中断,连接将会中断,有些人会觉得这很烦人。另一方面,如果不发送TCP 保持活动消息,会话可能会无限期地挂在服务器上,留下“幽灵”用户并消耗服务器资源。默认值为是(发送 TCP 保持活动消息),服务器将注意到网络中断或客户端主机崩溃。这避免了无限挂起的会话。要禁用 TCP 保持活动消息,应将值设置为否:
#TCPKeepAlive yes
# 指定 sshd(8) 是否处理 ~/.ssh/environment 和 ~/.ssh/authorized_keys 中的 environment= 选项。有效选项为 yes、no 或指定要接受哪些环境变量名称的模式列表(例如“LANG,LC_*”)。默认值为 no。启用环境处理可能使用户能够使用 LD_PRELOAD 等机制绕过某些配置中的访问限制:
#PermitUserEnvironment no
# 指定用户成功验证后是否启用压缩。参数必须是 yes、delayed(yes 的旧同义词)或 no。默认值为 yes:
#Compression delayed
# 设置超时间隔(以秒为单位),如果在此间隔后未从客户端收到任何数据,sshd(8) 将通过加密信道发送一条消息,以请求客户端做出响应。默认值为 0,表示这些消息将不会发送到客户端:
#ClientAliveInterval 0
# 设置在 sshd(8) 未从客户端收到任何消息的情况下可以发送的客户端活动消息数。如果在发送客户端活动消息时达到此阈值,sshd 将断开客户端连接,终止会话。需要注意的是,客户端活动消息的使用与 TCPKeepAlive 非常不同。客户端活动消息通过加密信道发送,因此不会被伪造。TCPKeepAlive 启用的 TCP keepalive 选项是可伪造的。当客户端或服务器依赖于了解连接何时无响应时,客户端活动机制非常有用。默认值为 3。如果 ClientAliveInterval 设置为 15,并且 ClientAliveCountMax 保留为默认值,则无响应的 SSH 客户端将在大约 45 秒后断开连接。将 ClientAliveCountMax 设置为零将禁用连接终止:
#ClientAliveCountMax 3
# 指定 sshd(8) 是否应查找远程主机名,并检查远程 IP 地址的解析主机名是否映射回相同的 IP 地址:
#UseDNS no
# 指定包含 SSH 守护进程的进程 ID 的文档,或 none 表示不写入。默认值为 /var/run/sshd.pid:
#PidFile /run/sshd.pid
# 指定与 SSH 守护进程的最大并发未认证连接数。其他连接将被丢弃,直到认证成功或连接的 LoginGraceTime 到期。默认值为 10:30:100。或者,可以通过指定三个冒号分隔的值 start:rate:full(例如“10:30:60”)来启用随机早期丢弃。如果当前有 start (10) 个未认证连接,sshd(8) 将拒绝概率为 rate/100 (30%) 的连接尝试。如果未认证连接数达到 full (60),概率将随时间增加,并且所有连接尝试都将被拒绝:
#MaxStartups 10:30:100
# 指定是否允许 tun(4) 设备转发。参数必须是 yes、点对点(第 3 层)、以太网(第 2 层)或 no。指定 yes 表示允许点对点和以太网。默认值为 no:
#PermitTunnel no
# 指定在身份验证后 chroot(2) 到的目录的路径名。在会话启动时,sshd(8) 检查路径名的所有组件是否都是 root 拥有的目录,这些目录不能由组或其他人写入。chroot 后,sshd(8) 将工作目录更改为用户的主目录。ChrootDirectory 的参数接受“TOKENS”部分中描述的令牌。ChrootDirectory 必须包含支持用户会话所需的文档和目录。对于交互式会话,这至少需要一个 shell(通常是 sh(1))和基本 /dev 节点,例如 null(4)、zero(4)、stdin(4)、stdout(4)、stderr(4) 和 tty(4) 设备。对于使用 SFTP 的文档传输会话,如果使用进程内sftp 服务器,则无需对环境进行额外的配置,但在某些操作系统上,使用日志记录的会话可能需要 chroot 目录中的 /dev/log(有关详细信息,请参阅 sftp-server(8))。为了安全起见,防止目录层次结构被系统上的其他进程(尤其是 jail 之外的进程)修改非常重要。配置错误可能导致 sshd(8) 无法检测到的不安全环境。默认值为 none,表示不 chroot(2):
#ChrootDirectory none
# 可选地指定附加文本以附加到服务器在连接时发送的 SSH 协议横幅。默认值为"FreeBSD-20240318"。值 none 可用于禁用此功能:
#VersionAddendum none

# no default banner path
# 设置登录前的横幅,指定文件路径:
#Banner none

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

# override default of no subsystems
# 配置外部子系统(例如文件传输守护进程)。参数应该是子系统名称和 根据子系统请求执行的命令(带有可选参数)。命令sftp-server实现了 SFTP 文件传输子系统。或者,名称 internal-sftp 实现进程内SFTP 服务器。这可以简化使用ChrootDirectory的配置 ,以在客户端上强制使用不同的文件系统根。默认情况下没有定义子系统:
Subsystem sftp /usr/lib/openssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
# 指定是否允许 X11 转发。参数 必须是 yes 或 no。默认值为 no,启用 X11 转发后,如果 sshd(8) 代理 显示配置为监听通配符地址(请参阅 X11UseLocalhost),则服务器和客户端显示可能会受到额外的暴露,尽管这不是默认值。此外,身份验证欺骗和身份验证数据验证和替换发生在客户端。使用 X11 转发的安全风险是,当 SSH 客户端请求转发时,客户端的 X11 显示服务器可能会受到攻击(请参阅 ssh_config(5) 中有关 ForwardX11 的警告)。系统管理员可能希望保护可能因不知情地请求 X11 转发而使自己暴露于攻击的客户端,这可能需要设置为 no。请注意,禁用 X11 转发并不会阻止用户转发 X11 流量,因为用户始终可以安装自己的转发器:
# X11Forwarding no
# 指定是否允许TCP转发。可用选项为yes(默认值)或all,允许TCP转发,no阻止所有TCP转发,local仅允许本地(从ssh(1)的视角)转发,或remote仅允许远程转发。请注意,禁用TCP转发并不能提高安全性,除非还拒绝用户访问shell,因为他们总是可以安装自己的转发器
# AllowTcpForwarding no
# 指定是否允许 pty(4) 分配。默认为是。
# PermitTTY no
# ForceCommand cvs server

FreeBSD解释

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322

NAME
sshd_config -- OpenSSH daemon configuration file

DESCRIPTION
sshd(8) reads configuration data from /etc/ssh/sshd_config (or the file
specified with -f on the command line). The file contains keyword-ar-
gument pairs, one per line. Unless noted otherwise, for each keyword,
the first obtained value will be used. Lines starting with `#' and
empty lines are interpreted as comments. Arguments may optionally be
enclosed in double quotes (") in order to represent arguments contain-
ing spaces.

The possible keywords and their meanings are as follows (note that key-
words are case-insensitive and arguments are case-sensitive):

AcceptEnv
Specifies what environment variables sent by the client will be
copied into the session's environ(7). See SendEnv and SetEnv
in ssh_config(5) for how to configure the client. The TERM en-
vironment variable is always accepted whenever the client re-
quests a pseudo-terminal as it is required by the protocol.
Variables are specified by name, which may contain the wildcard
characters `*' and `?'. Multiple environment variables may be
separated by whitespace or spread across multiple AcceptEnv di-
rectives. Be warned that some environment variables could be
used to bypass restricted user environments. For this reason,
care should be taken in the use of this directive. The default
is not to accept any environment variables.

AddressFamily
Specifies which address family should be used by sshd(8).
Valid arguments are any (the default), inet (use IPv4 only), or
inet6 (use IPv6 only).

AllowAgentForwarding
Specifies whether ssh-agent(1) forwarding is permitted. The
default is yes. Note that disabling agent forwarding does not
improve security unless users are also denied shell access, as
they can always install their own forwarders.

AllowGroups
This keyword can be followed by a list of group name patterns,
separated by spaces. If specified, login is allowed only for
users whose primary group or supplementary group list matches
one of the patterns. Only group names are valid; a numerical
group ID is not recognized. By default, login is allowed for
all groups. The allow/deny groups directives are processed in
the following order: DenyGroups, AllowGroups.

See PATTERNS in ssh_config(5) for more information on patterns.
This keyword may appear multiple times in sshd_config with each
instance appending to the list.

AllowStreamLocalForwarding
Specifies whether StreamLocal (Unix-domain socket) forwarding
is permitted. The available options are yes (the default) or
all to allow StreamLocal forwarding, no to prevent all Stream-
Local forwarding, local to allow local (from the perspective of
ssh(1)) forwarding only or remote to allow remote forwarding
only. Note that disabling StreamLocal forwarding does not im-
prove security unless users are also denied shell access, as
they can always install their own forwarders.

AllowTcpForwarding
Specifies whether TCP forwarding is permitted. The available
options are yes (the default) or all to allow TCP forwarding,
no to prevent all TCP forwarding, local to allow local (from
the perspective of ssh(1)) forwarding only or remote to allow
remote forwarding only. Note that disabling TCP forwarding
does not improve security unless users are also denied shell
access, as they can always install their own forwarders.

AllowUsers
This keyword can be followed by a list of user name patterns,
separated by spaces. If specified, login is allowed only for
user names that match one of the patterns. Only user names are
valid; a numerical user ID is not recognized. By default, lo-
gin is allowed for all users. If the pattern takes the form
USER@HOST then USER and HOST are separately checked, restrict-
ing logins to particular users from particular hosts. HOST
criteria may additionally contain addresses to match in CIDR
address/masklen format. The allow/deny users directives are
processed in the following order: DenyUsers, AllowUsers.

See PATTERNS in ssh_config(5) for more information on patterns.
This keyword may appear multiple times in sshd_config with each
instance appending to the list.

AuthenticationMethods
Specifies the authentication methods that must be successfully
completed for a user to be granted access. This option must be
followed by one or more lists of comma-separated authentication
method names, or by the single string any to indicate the de-
fault behaviour of accepting any single authentication method.
If the default is overridden, then successful authentication
requires completion of every method in at least one of these
lists.

For example, "publickey,password
publickey,keyboard-interactive" would require the user to com-
plete public key authentication, followed by either password or
keyboard interactive authentication. Only methods that are
next in one or more lists are offered at each stage, so for
this example it would not be possible to attempt password or
keyboard-interactive authentication before public key.

For keyboard interactive authentication it is also possible to
restrict authentication to a specific device by appending a
colon followed by the device identifier bsdauth or pam. de-
pending on the server configuration. For example,
"keyboard-interactive:bsdauth" would restrict keyboard interac-
tive authentication to the bsdauth device.

If the publickey method is listed more than once, sshd(8) veri-
fies that keys that have been used successfully are not reused
for subsequent authentications. For example,
"publickey,publickey" requires successful authentication using
two different public keys.

Note that each authentication method listed should also be ex-
plicitly enabled in the configuration.

The available authentication methods are: "gssapi-with-mic",
"hostbased", "keyboard-interactive", "none" (used for access to
password-less accounts when PermitEmptyPasswords is enabled),
"password" and "publickey".

AuthorizedKeysCommand
Specifies a program to be used to look up the user's public
keys. The program must be owned by root, not writable by group
or others and specified by an absolute path. Arguments to
AuthorizedKeysCommand accept the tokens described in the
"TOKENS" section. If no arguments are specified then the user-
name of the target user is used.

The program should produce on standard output zero or more
lines of authorized_keys output (see "AUTHORIZED_KEYS" in
sshd(8)). AuthorizedKeysCommand is tried after the usual
AuthorizedKeysFile files and will not be executed if a matching
key is found there. By default, no AuthorizedKeysCommand is
run.

AuthorizedKeysCommandUser
Specifies the user under whose account the
AuthorizedKeysCommand is run. It is recommended to use a dedi-
cated user that has no other role on the host than running au-
thorized keys commands. If AuthorizedKeysCommand is specified
but AuthorizedKeysCommandUser is not, then sshd(8) will refuse
to start.

AuthorizedKeysFile
Specifies the file that contains the public keys used for user
authentication. The format is described in the AUTHORIZED_KEYS
FILE FORMAT section of sshd(8). Arguments to
AuthorizedKeysFile accept the tokens described in the "TOKENS"
section. After expansion, AuthorizedKeysFile is taken to be an
absolute path or one relative to the user's home directory.
Multiple files may be listed, separated by whitespace. Alter-
nately this option may be set to none to skip checking for user
keys in files. The default is ".ssh/authorized_keys
.ssh/authorized_keys2".

AuthorizedPrincipalsCommand
Specifies a program to be used to generate the list of allowed
certificate principals as per AuthorizedPrincipalsFile. The
program must be owned by root, not writable by group or others
and specified by an absolute path. Arguments to
AuthorizedPrincipalsCommand accept the tokens described in the
"TOKENS" section. If no arguments are specified then the user-
name of the target user is used.

The program should produce on standard output zero or more
lines of AuthorizedPrincipalsFile output. If either
AuthorizedPrincipalsCommand or AuthorizedPrincipalsFile is
specified, then certificates offered by the client for authen-
tication must contain a principal that is listed. By default,
no AuthorizedPrincipalsCommand is run.

AuthorizedPrincipalsCommandUser
Specifies the user under whose account the
AuthorizedPrincipalsCommand is run. It is recommended to use a
dedicated user that has no other role on the host than running
authorized principals commands. If AuthorizedPrincipalsCommand
is specified but AuthorizedPrincipalsCommandUser is not, then
sshd(8) will refuse to start.

AuthorizedPrincipalsFile
Specifies a file that lists principal names that are accepted
for certificate authentication. When using certificates signed
by a key listed in TrustedUserCAKeys, this file lists names,
one of which must appear in the certificate for it to be ac-
cepted for authentication. Names are listed one per line pre-
ceded by key options (as described in "AUTHORIZED_KEYS FILE
FORMAT" in sshd(8)). Empty lines and comments starting with
`#' are ignored.

Arguments to AuthorizedPrincipalsFile accept the tokens de-
scribed in the "TOKENS" section. After expansion,
AuthorizedPrincipalsFile is taken to be an absolute path or one
relative to the user's home directory. The default is none,
i.e. not to use a principals file - in this case, the username
of the user must appear in a certificate's principals list for
it to be accepted.

Note that AuthorizedPrincipalsFile is only used when authenti-
cation proceeds using a CA listed in TrustedUserCAKeys and is
not consulted for certification authorities trusted via
~/.ssh/authorized_keys, though the principals= key option of-
fers a similar facility (see sshd(8) for details).

Banner The contents of the specified file are sent to the remote user
before authentication is allowed. If the argument is none then
no banner is displayed. By default, no banner is displayed.

CASignatureAlgorithms
Specifies which algorithms are allowed for signing of certifi-
cates by certificate authorities (CAs). The default is:

ssh-ed25519,ecdsa-sha2-nistp256,
ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,
sk-ssh-ed25519@openssh.com,
sk-ecdsa-sha2-nistp256@openssh.com,
rsa-sha2-512,rsa-sha2-256

If the specified list begins with a `+' character, then the
specified algorithms will be appended to the default set in-
stead of replacing them. If the specified list begins with a
`-' character, then the specified algorithms (including wild-
cards) will be removed from the default set instead of replac-
ing them.

Certificates signed using other algorithms will not be accepted
for public key or host-based authentication.

ChannelTimeout
Specifies whether and how quickly sshd(8) should close inactive
channels. Timeouts are specified as one or more
"type=interval" pairs separated by whitespace, where the "type"
must be a channel type name (as described in the table below),
optionally containing wildcard characters.

The timeout value "interval" is specified in seconds or may use
any of the units documented in the "TIME FORMATS" section. For
example, "session:*=5m" would cause all sessions to terminate
after five minutes of inactivity. Specifying a zero value dis-
ables the inactivity timeout.

The available channel types include:

agent-connection
Open connections to ssh-agent(1).

direct-tcpip, direct-streamlocal@openssh.com
Open TCP or Unix socket (respectively) connections that
have been established from a ssh(1) local forwarding,
i.e. LocalForward or DynamicForward.

forwarded-tcpip, forwarded-streamlocal@openssh.com
Open TCP or Unix socket (respectively) connections that
have been established to a sshd(8) listening on behalf
of a ssh(1) remote forwarding, i.e. RemoteForward.

session:command
Command execution sessions.

session:shell
Interactive shell sessions.

session:subsystem:...
Subsystem sessions, e.g. for sftp(1), which could be
identified as session:subsystem:sftp.

x11-connection
Open X11 forwarding sessions.

Note that in all the above cases, terminating an inactive ses-
sion does not guarantee to remove all resources associated with
the session, e.g. shell processes or X11 clients relating to
the session may continue to execute.

Moreover, terminating an inactive channel or session does not
necessarily close the SSH connection, nor does it prevent a
client from requesting another channel of the same type. In
particular, expiring an inactive forwarding session does not
prevent another identical forwarding from being subsequently
created. See also UnusedConnectionTimeout, which may be used
in conjunction with this option.

The default is not to expire channels of any type for inactiv-
ity.

ChrootDirectory
Specifies the pathname of a directory to chroot(2) to after au-
thentication. At session startup sshd(8) checks that all com-
ponents of the pathname are root-owned directories which are
not writable by any other user or group. After the chroot,
sshd(8) changes the working directory to the user's home direc-
tory. Arguments to ChrootDirectory accept the tokens described
in the "TOKENS" section.

The ChrootDirectory must contain the necessary files and direc-
tories to support the user's session. For an interactive ses-
sion this requires at least a shell, typically sh(1), and basic
/dev nodes such as null(4), zero(4), stdin(4), stdout(4),
stderr(4), and tty(4) devices. For file transfer sessions us-
ing SFTP no additional configuration of the environment is nec-
essary if the in-process sftp-server is used, though sessions
which use logging may require /dev/log inside the chroot direc-
tory on some operating systems (see sftp-server(8) for de-
tails).

For safety, it is very important that the directory hierarchy
be prevented from modification by other processes on the system
(especially those outside the jail). Misconfiguration can lead
to unsafe environments which sshd(8) cannot detect.

The default is none, indicating not to chroot(2).

Ciphers
Specifies the ciphers allowed. Multiple ciphers must be comma-
separated. If the specified list begins with a `+' character,
then the specified ciphers will be appended to the default set
instead of replacing them. If the specified list begins with a
`-' character, then the specified ciphers (including wildcards)
will be removed from the default set instead of replacing them.
If the specified list begins with a `^' character, then the
specified ciphers will be placed at the head of the default
set.

The supported ciphers are:

3des-cbc
aes128-cbc
aes192-cbc
aes256-cbc
aes128-ctr
aes192-ctr
aes256-ctr
aes128-gcm@openssh.com
aes256-gcm@openssh.com
chacha20-poly1305@openssh.com

The default is:

chacha20-poly1305@openssh.com,
aes128-ctr,aes192-ctr,aes256-ctr,
aes128-gcm@openssh.com,aes256-gcm@openssh.com

The list of available ciphers may also be obtained using "ssh
-Q cipher".

ClientAliveCountMax
Sets the number of client alive messages which may be sent
without sshd(8) receiving any messages back from the client.
If this threshold is reached while client alive messages are
being sent, sshd will disconnect the client, terminating the
session. It is important to note that the use of client alive
messages is very different from TCPKeepAlive. The client alive
messages are sent through the encrypted channel and therefore
will not be spoofable. The TCP keepalive option enabled by
TCPKeepAlive is spoofable. The client alive mechanism is valu-
able when the client or server depend on knowing when a connec-
tion has become unresponsive.

The default value is 3. If ClientAliveInterval is set to 15,
and ClientAliveCountMax is left at the default, unresponsive
SSH clients will be disconnected after approximately 45 sec-
onds. Setting a zero ClientAliveCountMax disables connection
termination.

ClientAliveInterval
Sets a timeout interval in seconds after which if no data has
been received from the client, sshd(8) will send a message
through the encrypted channel to request a response from the
client. The default is 0, indicating that these messages will
not be sent to the client.

Compression
Specifies whether compression is enabled after the user has au-
thenticated successfully. The argument must be yes, delayed (a
legacy synonym for yes) or no. The default is yes.

DenyGroups
This keyword can be followed by a list of group name patterns,
separated by spaces. Login is disallowed for users whose pri-
mary group or supplementary group list matches one of the pat-
terns. Only group names are valid; a numerical group ID is not
recognized. By default, login is allowed for all groups. The
allow/deny groups directives are processed in the following or-
der: DenyGroups, AllowGroups.

See PATTERNS in ssh_config(5) for more information on patterns.
This keyword may appear multiple times in sshd_config with each
instance appending to the list.

DenyUsers
This keyword can be followed by a list of user name patterns,
separated by spaces. Login is disallowed for user names that
match one of the patterns. Only user names are valid; a numer-
ical user ID is not recognized. By default, login is allowed
for all users. If the pattern takes the form USER@HOST then
USER and HOST are separately checked, restricting logins to
particular users from particular hosts. HOST criteria may ad-
ditionally contain addresses to match in CIDR address/masklen
format. The allow/deny users directives are processed in the
following order: DenyUsers, AllowUsers.

See PATTERNS in ssh_config(5) for more information on patterns.
This keyword may appear multiple times in sshd_config with each
instance appending to the list.

DisableForwarding
Disables all forwarding features, including X11, ssh-agent(1),
TCP and StreamLocal. This option overrides all other forward-
ing-related options and may simplify restricted configurations.

ExposeAuthInfo
Writes a temporary file containing a list of authentication
methods and public credentials (e.g. keys) used to authenticate
the user. The location of the file is exposed to the user ses-
sion through the SSH_USER_AUTH environment variable. The de-
fault is no.

FingerprintHash
Specifies the hash algorithm used when logging key finger-
prints. Valid options are: md5 and sha256. The default is
sha256.

ForceCommand
Forces the execution of the command specified by ForceCommand,
ignoring any command supplied by the client and ~/.ssh/rc if
present. The command is invoked by using the user's login
shell with the -c option. This applies to shell, command, or
subsystem execution. It is most useful inside a Match block.
The command originally supplied by the client is available in
the SSH_ORIGINAL_COMMAND environment variable. Specifying a
command of internal-sftp will force the use of an in-process
SFTP server that requires no support files when used with
ChrootDirectory. The default is none.

GatewayPorts
Specifies whether remote hosts are allowed to connect to ports
forwarded for the client. By default, sshd(8) binds remote
port forwardings to the loopback address. This prevents other
remote hosts from connecting to forwarded ports. GatewayPorts
can be used to specify that sshd should allow remote port for-
wardings to bind to non-loopback addresses, thus allowing other
hosts to connect. The argument may be no to force remote port
forwardings to be available to the local host only, yes to
force remote port forwardings to bind to the wildcard address,
or clientspecified to allow the client to select the address to
which the forwarding is bound. The default is no.

GSSAPIAuthentication
Specifies whether user authentication based on GSSAPI is al-
lowed. The default is no.

GSSAPICleanupCredentials
Specifies whether to automatically destroy the user's creden-
tials cache on logout. The default is yes.

GSSAPIStrictAcceptorCheck
Determines whether to be strict about the identity of the GSS-
API acceptor a client authenticates against. If set to yes
then the client must authenticate against the host service on
the current hostname. If set to no then the client may authen-
ticate against any service key stored in the machine's default
store. This facility is provided to assist with operation on
multi homed machines. The default is yes.

HostbasedAcceptedAlgorithms
Specifies the signature algorithms that will be accepted for
hostbased authentication as a list of comma-separated patterns.
Alternately if the specified list begins with a `+' character,
then the specified signature algorithms will be appended to the
default set instead of replacing them. If the specified list
begins with a `-' character, then the specified signature algo-
rithms (including wildcards) will be removed from the default
set instead of replacing them. If the specified list begins
with a `^' character, then the specified signature algorithms
will be placed at the head of the default set. The default for
this option is:

ssh-ed25519-cert-v01@openssh.com,
ecdsa-sha2-nistp256-cert-v01@openssh.com,
ecdsa-sha2-nistp384-cert-v01@openssh.com,
ecdsa-sha2-nistp521-cert-v01@openssh.com,
sk-ssh-ed25519-cert-v01@openssh.com,
sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,
rsa-sha2-512-cert-v01@openssh.com,
rsa-sha2-256-cert-v01@openssh.com,
ssh-ed25519,
ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,
sk-ssh-ed25519@openssh.com,
sk-ecdsa-sha2-nistp256@openssh.com,
rsa-sha2-512,rsa-sha2-256

The list of available signature algorithms may also be obtained
using "ssh -Q HostbasedAcceptedAlgorithms". This was formerly
named HostbasedAcceptedKeyTypes.

HostbasedAuthentication
Specifies whether rhosts or /etc/hosts.equiv authentication to-
gether with successful public key client host authentication is
allowed (host-based authentication). The default is no.

HostbasedUsesNameFromPacketOnly
Specifies whether or not the server will attempt to perform a
reverse name lookup when matching the name in the ~/.shosts,
~/.rhosts, and /etc/hosts.equiv files during
HostbasedAuthentication. A setting of yes means that sshd(8)
uses the name supplied by the client rather than attempting to
resolve the name from the TCP connection itself. The default
is no.

HostCertificate
Specifies a file containing a public host certificate. The
certificate's public key must match a private host key already
specified by HostKey. The default behaviour of sshd(8) is not
to load any certificates.

HostKey
Specifies a file containing a private host key used by SSH.
The defaults are /etc/ssh/ssh_host_ecdsa_key,
/etc/ssh/ssh_host_ed25519_key and /etc/ssh/ssh_host_rsa_key.

Note that sshd(8) will refuse to use a file if it is
group/world-accessible and that the HostKeyAlgorithms option
restricts which of the keys are actually used by sshd(8).

It is possible to have multiple host key files. It is also
possible to specify public host key files instead. In this
case operations on the private key will be delegated to an
ssh-agent(1).

HostKeyAgent
Identifies the UNIX-domain socket used to communicate with an
agent that has access to the private host keys. If the string
"SSH_AUTH_SOCK" is specified, the location of the socket will
be read from the SSH_AUTH_SOCK environment variable.

HostKeyAlgorithms
Specifies the host key signature algorithms that the server of-
fers. The default for this option is:

ssh-ed25519-cert-v01@openssh.com,
ecdsa-sha2-nistp256-cert-v01@openssh.com,
ecdsa-sha2-nistp384-cert-v01@openssh.com,
ecdsa-sha2-nistp521-cert-v01@openssh.com,
sk-ssh-ed25519-cert-v01@openssh.com,
sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,
rsa-sha2-512-cert-v01@openssh.com,
rsa-sha2-256-cert-v01@openssh.com,
ssh-ed25519,
ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,
sk-ssh-ed25519@openssh.com,
sk-ecdsa-sha2-nistp256@openssh.com,
rsa-sha2-512,rsa-sha2-256

The list of available signature algorithms may also be obtained
using "ssh -Q HostKeyAlgorithms".

IgnoreRhosts
Specifies whether to ignore per-user .rhosts and .shosts files
during HostbasedAuthentication. The system-wide
/etc/hosts.equiv and /etc/ssh/shosts.equiv are still used re-
gardless of this setting.

Accepted values are yes (the default) to ignore all per-user
files, shosts-only to allow the use of .shosts but to ignore
.rhosts or no to allow both .shosts and rhosts.

IgnoreUserKnownHosts
Specifies whether sshd(8) should ignore the user's
~/.ssh/known_hosts during HostbasedAuthentication and use only
the system-wide known hosts file /etc/ssh/ssh_known_hosts. The
default is "no".

Include
Include the specified configuration file(s). Multiple path-
names may be specified and each pathname may contain glob(7)
wildcards that will be expanded and processed in lexical order.
Files without absolute paths are assumed to be in /etc/ssh. An
Include directive may appear inside a Match block to perform
conditional inclusion.

IPQoS Specifies the IPv4 type-of-service or DSCP class for the con-
nection. Accepted values are af11, af12, af13, af21, af22,
af23, af31, af32, af33, af41, af42, af43, cs0, cs1, cs2, cs3,
cs4, cs5, cs6, cs7, ef, le, lowdelay, throughput, reliability,
a numeric value, or none to use the operating system default.
This option may take one or two arguments, separated by white-
space. If one argument is specified, it is used as the packet
class unconditionally. If two values are specified, the first
is automatically selected for interactive sessions and the sec-
ond for non-interactive sessions. The default is af21 (Low-La-
tency Data) for interactive sessions and cs1 (Lower Effort) for
non-interactive sessions.

KbdInteractiveAuthentication
Specifies whether to allow keyboard-interactive authentication.
All authentication styles from login.conf(5) are supported.
The default is yes. The argument to this keyword must be yes
or no. ChallengeResponseAuthentication is a deprecated alias
for this.

KerberosAuthentication
Specifies whether the password provided by the user for
PasswordAuthentication will be validated through the Kerberos
KDC. To use this option, the server needs a Kerberos servtab
which allows the verification of the KDC's identity. The de-
fault is no.

KerberosGetAFSToken
If AFS is active and the user has a Kerberos 5 TGT, attempt to
acquire an AFS token before accessing the user's home direc-
tory. The default is no.

KerberosOrLocalPasswd
If password authentication through Kerberos fails then the
password will be validated via any additional local mechanism
such as /etc/passwd. The default is yes.

KerberosTicketCleanup
Specifies whether to automatically destroy the user's ticket
cache file on logout. The default is yes.

KexAlgorithms
Specifies the available KEX (Key Exchange) algorithms. Multi-
ple algorithms must be comma-separated. Alternately if the
specified list begins with a `+' character, then the specified
algorithms will be appended to the default set instead of re-
placing them. If the specified list begins with a `-' charac-
ter, then the specified algorithms (including wildcards) will
be removed from the default set instead of replacing them. If
the specified list begins with a `^' character, then the speci-
fied algorithms will be placed at the head of the default set.
The supported algorithms are:

curve25519-sha256
curve25519-sha256@libssh.org
diffie-hellman-group1-sha1
diffie-hellman-group14-sha1
diffie-hellman-group14-sha256
diffie-hellman-group16-sha512
diffie-hellman-group18-sha512
diffie-hellman-group-exchange-sha1
diffie-hellman-group-exchange-sha256
ecdh-sha2-nistp256
ecdh-sha2-nistp384
ecdh-sha2-nistp521
sntrup761x25519-sha512@openssh.com

The default is:

sntrup761x25519-sha512@openssh.com,
curve25519-sha256,curve25519-sha256@libssh.org,
ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,
diffie-hellman-group-exchange-sha256,
diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,
diffie-hellman-group14-sha256

The list of available key exchange algorithms may also be ob-
tained using "ssh -Q KexAlgorithms".

ListenAddress
Specifies the local addresses sshd(8) should listen on. The
following forms may be used:

ListenAddress hostname|address [rdomain domain]
ListenAddress hostname:port [rdomain domain]
ListenAddress IPv4_address:port [rdomain domain]
ListenAddress [hostname|address]:port [rdomain domain]

The optional rdomain qualifier requests sshd(8) listen in an
explicit routing domain. If port is not specified, sshd will
listen on the address and all Port options specified. The de-
fault is to listen on all local addresses on the current de-
fault routing domain. Multiple ListenAddress options are per-
mitted. For more information on routing domains, see
rdomain(4).

LoginGraceTime
The server disconnects after this time if the user has not suc-
cessfully logged in. If the value is 0, there is no time
limit. The default is 120 seconds.

LogLevel
Gives the verbosity level that is used when logging messages
from sshd(8). The possible values are: QUIET, FATAL, ERROR,
INFO, VERBOSE, DEBUG, DEBUG1, DEBUG2, and DEBUG3. The default
is INFO. DEBUG and DEBUG1 are equivalent. DEBUG2 and DEBUG3
each specify higher levels of debugging output. Logging with a
DEBUG level violates the privacy of users and is not recom-
mended.

LogVerbose
Specify one or more overrides to LogLevel. An override con-
sists of a pattern lists that matches the source file, function
and line number to force detailed logging for. For example, an
override pattern of:

kex.c:*:1000,*:kex_exchange_identification():*,packet.c:*

would enable detailed logging for line 1000 of kex.c, every-
thing in the kex_exchange_identification() function, and all
code in the packet.c file. This option is intended for debug-
ging and no overrides are enabled by default.

MACs Specifies the available MAC (message authentication code) algo-
rithms. The MAC algorithm is used for data integrity protec-
tion. Multiple algorithms must be comma-separated. If the
specified list begins with a `+' character, then the specified
algorithms will be appended to the default set instead of re-
placing them. If the specified list begins with a `-' charac-
ter, then the specified algorithms (including wildcards) will
be removed from the default set instead of replacing them. If
the specified list begins with a `^' character, then the speci-
fied algorithms will be placed at the head of the default set.

The algorithms that contain "-etm" calculate the MAC after en-
cryption (encrypt-then-mac). These are considered safer and
their use recommended. The supported MACs are:

hmac-md5
hmac-md5-96
hmac-sha1
hmac-sha1-96
hmac-sha2-256
hmac-sha2-512
umac-64@openssh.com
umac-128@openssh.com
hmac-md5-etm@openssh.com
hmac-md5-96-etm@openssh.com
hmac-sha1-etm@openssh.com
hmac-sha1-96-etm@openssh.com
hmac-sha2-256-etm@openssh.com
hmac-sha2-512-etm@openssh.com
umac-64-etm@openssh.com
umac-128-etm@openssh.com

The default is:

umac-64-etm@openssh.com,umac-128-etm@openssh.com,
hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,
hmac-sha1-etm@openssh.com,
umac-64@openssh.com,umac-128@openssh.com,
hmac-sha2-256,hmac-sha2-512,hmac-sha1

The list of available MAC algorithms may also be obtained using
"ssh -Q mac".

Match Introduces a conditional block. If all of the criteria on the
Match line are satisfied, the keywords on the following lines
override those set in the global section of the config file,
until either another Match line or the end of the file. If a
keyword appears in multiple Match blocks that are satisfied,
only the first instance of the keyword is applied.

The arguments to Match are one or more criteria-pattern pairs
or the single token All which matches all criteria. The avail-
able criteria are User, Group, Host, LocalAddress, LocalPort,
RDomain, and Address (with RDomain representing the rdomain(4)
on which the connection was received).

The match patterns may consist of single entries or comma-sepa-
rated lists and may use the wildcard and negation operators de-
scribed in the "PATTERNS" section of ssh_config(5).

The patterns in an Address criteria may additionally contain
addresses to match in CIDR address/masklen format, such as
192.0.2.0/24 or 2001:db8::/32. Note that the mask length pro-
vided must be consistent with the address - it is an error to
specify a mask length that is too long for the address or one
with bits set in this host portion of the address. For exam-
ple, 192.0.2.0/33 and 192.0.2.0/8, respectively.

Only a subset of keywords may be used on the lines following a
Match keyword. Available keywords are AcceptEnv,
AllowAgentForwarding, AllowGroups, AllowStreamLocalForwarding,
AllowTcpForwarding, AllowUsers, AuthenticationMethods,
AuthorizedKeysCommand, AuthorizedKeysCommandUser,
AuthorizedKeysFile, AuthorizedPrincipalsCommand,
AuthorizedPrincipalsCommandUser, AuthorizedPrincipalsFile,
Banner, CASignatureAlgorithms, ChannelTimeout, ChrootDirectory,
ClientAliveCountMax, ClientAliveInterval, DenyGroups,
DenyUsers, DisableForwarding, ExposeAuthInfo, ForceCommand,
GatewayPorts, GSSAPIAuthentication,
HostbasedAcceptedAlgorithms, HostbasedAuthentication,
HostbasedUsesNameFromPacketOnly, IgnoreRhosts, Include, IPQoS,
KbdInteractiveAuthentication, KerberosAuthentication, LogLevel,
MaxAuthTries, MaxSessions, PasswordAuthentication,
PermitEmptyPasswords, PermitListen, PermitOpen,
PermitRootLogin, PermitTTY, PermitTunnel, PermitUserRC,
PubkeyAcceptedAlgorithms, PubkeyAuthentication,
PubkeyAuthOptions, RekeyLimit, RevokedKeys, RDomain, SetEnv,
StreamLocalBindMask, StreamLocalBindUnlink, TrustedUserCAKeys,
UnusedConnectionTimeout, X11DisplayOffset, X11Forwarding and
X11UseLocalhost.

MaxAuthTries
Specifies the maximum number of authentication attempts permit-
ted per connection. Once the number of failures reaches half
this value, additional failures are logged. The default is 6.

MaxSessions
Specifies the maximum number of open shell, login or subsystem
(e.g. sftp) sessions permitted per network connection. Multi-
ple sessions may be established by clients that support connec-
tion multiplexing. Setting MaxSessions to 1 will effectively
disable session multiplexing, whereas setting it to 0 will pre-
vent all shell, login and subsystem sessions while still per-
mitting forwarding. The default is 10.

MaxStartups
Specifies the maximum number of concurrent unauthenticated con-
nections to the SSH daemon. Additional connections will be
dropped until authentication succeeds or the LoginGraceTime ex-
pires for a connection. The default is 10:30:100.

Alternatively, random early drop can be enabled by specifying
the three colon separated values start:rate:full (e.g.
"10:30:60"). sshd(8) will refuse connection attempts with a
probability of rate/100 (30%) if there are currently start (10)
unauthenticated connections. The probability increases lin-
early and all connection attempts are refused if the number of
unauthenticated connections reaches full (60).

ModuliFile
Specifies the moduli(5) file that contains the Diffie-Hellman
groups used for the "diffie-hellman-group-exchange-sha1" and
"diffie-hellman-group-exchange-sha256" key exchange methods.
The default is /etc/moduli.

PasswordAuthentication
Specifies whether password authentication is allowed. Note
that passwords may also be accepted via
KbdInteractiveAuthentication. See also UsePAM. The default is
no.

PermitEmptyPasswords
When password authentication is allowed, it specifies whether
the server allows login to accounts with empty password
strings. The default is no.

PermitListen
Specifies the addresses/ports on which a remote TCP port for-
warding may listen. The listen specification must be one of
the following forms:

PermitListen port
PermitListen host:port

Multiple permissions may be specified by separating them with
whitespace. An argument of any can be used to remove all re-
strictions and permit any listen requests. An argument of none
can be used to prohibit all listen requests. The host name may
contain wildcards as described in the PATTERNS section in
ssh_config(5). The wildcard `*' can also be used in place of a
port number to allow all ports. By default all port forwarding
listen requests are permitted. Note that the GatewayPorts op-
tion may further restrict which addresses may be listened on.
Note also that ssh(1) will request a listen host of "localhost"
if no listen host was specifically requested, and this name is
treated differently to explicit localhost addresses of
"127.0.0.1" and "::1".

PermitOpen
Specifies the destinations to which TCP port forwarding is per-
mitted. The forwarding specification must be one of the fol-
lowing forms:

PermitOpen host:port
PermitOpen IPv4_addr:port
PermitOpen [IPv6_addr]:port

Multiple forwards may be specified by separating them with
whitespace. An argument of any can be used to remove all re-
strictions and permit any forwarding requests. An argument of
none can be used to prohibit all forwarding requests. The
wildcard `*' can be used for host or port to allow all hosts or
ports respectively. Otherwise, no pattern matching or address
lookups are performed on supplied names. By default all port
forwarding requests are permitted.

PermitRootLogin
Specifies whether root can log in using ssh(1). The argument
must be yes, prohibit-password, forced-commands-only, or no.
The default is no. Note that if
ChallengeResponseAuthentication and UsePAM are both yes, this
setting may be overridden by the PAM policy.

If this option is set to prohibit-password (or its deprecated
alias, without-password), password and keyboard-interactive au-
thentication are disabled for root.

If this option is set to forced-commands-only, root login with
public key authentication will be allowed, but only if the
command option has been specified (which may be useful for tak-
ing remote backups even if root login is normally not allowed).
All other authentication methods are disabled for root.

If this option is set to no, root is not allowed to log in.

PermitTTY
Specifies whether pty(4) allocation is permitted. The default
is yes.

PermitTunnel
Specifies whether tun(4) device forwarding is allowed. The ar-
gument must be yes, point-to-point (layer 3), ethernet (layer
2), or no. Specifying yes permits both point-to-point and
ethernet. The default is no.

Independent of this setting, the permissions of the selected
tun(4) device must allow access to the user.

PermitUserEnvironment
Specifies whether ~/.ssh/environment and environment= options
in ~/.ssh/authorized_keys are processed by sshd(8). Valid op-
tions are yes, no or a pattern-list specifying which environ-
ment variable names to accept (for example "LANG,LC_*"). The
default is no. Enabling environment processing may enable
users to bypass access restrictions in some configurations us-
ing mechanisms such as LD_PRELOAD.

PermitUserRC
Specifies whether any ~/.ssh/rc file is executed. The default
is yes.

PerSourceMaxStartups
Specifies the number of unauthenticated connections allowed
from a given source address, or "none" if there is no limit.
This limit is applied in addition to MaxStartups, whichever is
lower. The default is none.

PerSourceNetBlockSize
Specifies the number of bits of source address that are grouped
together for the purposes of applying PerSourceMaxStartups lim-
its. Values for IPv4 and optionally IPv6 may be specified,
separated by a colon. The default is 32:128, which means each
address is considered individually.

PidFile
Specifies the file that contains the process ID of the SSH dae-
mon, or none to not write one. The default is
/var/run/sshd.pid.

Port Specifies the port number that sshd(8) listens on. The default
is 22. Multiple options of this type are permitted. See also
ListenAddress.

PrintLastLog
Specifies whether sshd(8) should print the date and time of the
last user login when a user logs in interactively. The default
is yes.

PrintMotd
Specifies whether sshd(8) should print /etc/motd when a user
logs in interactively. (On some systems it is also printed by
the shell, /etc/profile, or equivalent.) The default is yes.

PubkeyAcceptedAlgorithms
Specifies the signature algorithms that will be accepted for
public key authentication as a list of comma-separated pat-
terns. Alternately if the specified list begins with a `+'
character, then the specified algorithms will be appended to
the default set instead of replacing them. If the specified
list begins with a `-' character, then the specified algorithms
(including wildcards) will be removed from the default set in-
stead of replacing them. If the specified list begins with a
`^' character, then the specified algorithms will be placed at
the head of the default set. The default for this option is:

ssh-ed25519-cert-v01@openssh.com,
ecdsa-sha2-nistp256-cert-v01@openssh.com,
ecdsa-sha2-nistp384-cert-v01@openssh.com,
ecdsa-sha2-nistp521-cert-v01@openssh.com,
sk-ssh-ed25519-cert-v01@openssh.com,
sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,
rsa-sha2-512-cert-v01@openssh.com,
rsa-sha2-256-cert-v01@openssh.com,
ssh-ed25519,
ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,
sk-ssh-ed25519@openssh.com,
sk-ecdsa-sha2-nistp256@openssh.com,
rsa-sha2-512,rsa-sha2-256

The list of available signature algorithms may also be obtained
using "ssh -Q PubkeyAcceptedAlgorithms".

PubkeyAuthOptions
Sets one or more public key authentication options. The sup-
ported keywords are: none (the default; indicating no addi-
tional options are enabled), touch-required and
verify-required.

The touch-required option causes public key authentication us-
ing a FIDO authenticator algorithm (i.e. ecdsa-sk or
ed25519-sk) to always require the signature to attest that a
physically present user explicitly confirmed the authentication
(usually by touching the authenticator). By default, sshd(8)
requires user presence unless overridden with an autho-
rized_keys option. The touch-required flag disables this over-
ride.

The verify-required option requires a FIDO key signature attest
that the user was verified, e.g. via a PIN.

Neither the touch-required or verify-required options have any
effect for other, non-FIDO, public key types.

PubkeyAuthentication
Specifies whether public key authentication is allowed. The
default is yes.

RekeyLimit
Specifies the maximum amount of data that may be transmitted or
received before the session key is renegotiated, optionally
followed by a maximum amount of time that may pass before the
session key is renegotiated. The first argument is specified
in bytes and may have a suffix of `K', `M', or `G' to indicate
Kilobytes, Megabytes, or Gigabytes, respectively. The default
is between `1G' and `4G', depending on the cipher. The op-
tional second value is specified in seconds and may use any of
the units documented in the "TIME FORMATS" section. The de-
fault value for RekeyLimit is default none, which means that
rekeying is performed after the cipher's default amount of data
has been sent or received and no time based rekeying is done.

RequiredRSASize
Specifies the minimum RSA key size (in bits) that sshd(8) will
accept. User and host-based authentication keys smaller than
this limit will be refused. The default is 1024 bits. Note
that this limit may only be raised from the default.

RevokedKeys
Specifies revoked public keys file, or none to not use one.
Keys listed in this file will be refused for public key authen-
tication. Note that if this file is not readable, then public
key authentication will be refused for all users. Keys may be
specified as a text file, listing one public key per line, or
as an OpenSSH Key Revocation List (KRL) as generated by
ssh-keygen(1). For more information on KRLs, see the KEY REVO-
CATION LISTS section in ssh-keygen(1).

RDomain
Specifies an explicit routing domain that is applied after au-
thentication has completed. The user session, as well as any
forwarded or listening IP sockets, will be bound to this
rdomain(4). If the routing domain is set to %D, then the do-
main in which the incoming connection was received will be ap-
plied.

SecurityKeyProvider
Specifies a path to a library that will be used when loading
FIDO authenticator-hosted keys, overriding the default of using
the built-in USB HID support.

SetEnv Specifies one or more environment variables to set in child
sessions started by sshd(8) as "NAME=VALUE". The environment
value may be quoted (e.g. if it contains whitespace charac-
ters). Environment variables set by SetEnv override the de-
fault environment and any variables specified by the user via
AcceptEnv or PermitUserEnvironment.

StreamLocalBindMask
Sets the octal file creation mode mask (umask) used when creat-
ing a Unix-domain socket file for local or remote port forward-
ing. This option is only used for port forwarding to a Unix-
domain socket file.

The default value is 0177, which creates a Unix-domain socket
file that is readable and writable only by the owner. Note
that not all operating systems honor the file mode on Unix-do-
main socket files.

StreamLocalBindUnlink
Specifies whether to remove an existing Unix-domain socket file
for local or remote port forwarding before creating a new one.
If the socket file already exists and StreamLocalBindUnlink is
not enabled, sshd will be unable to forward the port to the
Unix-domain socket file. This option is only used for port
forwarding to a Unix-domain socket file.

The argument must be yes or no. The default is no.

StrictModes
Specifies whether sshd(8) should check file modes and ownership
of the user's files and home directory before accepting login.
This is normally desirable because novices sometimes acciden-
tally leave their directory or files world-writable. The de-
fault is yes. Note that this does not apply to
ChrootDirectory, whose permissions and ownership are checked
unconditionally.

Subsystem
Configures an external subsystem (e.g. file transfer daemon).
Arguments should be a subsystem name and a command (with op-
tional arguments) to execute upon subsystem request.

The command sftp-server implements the SFTP file transfer sub-
system.

Alternately the name internal-sftp implements an in-process
SFTP server. This may simplify configurations using
ChrootDirectory to force a different filesystem root on
clients.

By default no subsystems are defined.

SyslogFacility
Gives the facility code that is used when logging messages from
sshd(8). The possible values are: DAEMON, USER, AUTH, LOCAL0,
LOCAL1, LOCAL2, LOCAL3, LOCAL4, LOCAL5, LOCAL6, LOCAL7. The
default is AUTH.

TCPKeepAlive
Specifies whether the system should send TCP keepalive messages
to the other side. If they are sent, death of the connection
or crash of one of the machines will be properly noticed. How-
ever, this means that connections will die if the route is down
temporarily, and some people find it annoying. On the other
hand, if TCP keepalives are not sent, sessions may hang indefi-
nitely on the server, leaving "ghost" users and consuming
server resources.

The default is yes (to send TCP keepalive messages), and the
server will notice if the network goes down or the client host
crashes. This avoids infinitely hanging sessions.

To disable TCP keepalive messages, the value should be set to
no.

TrustedUserCAKeys
Specifies a file containing public keys of certificate authori-
ties that are trusted to sign user certificates for authentica-
tion, or none to not use one. Keys are listed one per line;
empty lines and comments starting with `#' are allowed. If a
certificate is presented for authentication and has its signing
CA key listed in this file, then it may be used for authentica-
tion for any user listed in the certificate's principals list.
Note that certificates that lack a list of principals will not
be permitted for authentication using TrustedUserCAKeys. For
more details on certificates, see the CERTIFICATES section in
ssh-keygen(1).

UnusedConnectionTimeout
Specifies whether and how quickly sshd(8) should close client
connections with no open channels. Open channels include ac-
tive shell, command execution or subsystem sessions, connected
network, socket, agent or X11 forwardings. Forwarding listen-
ers, such as those from the ssh(1) -R flag, are not considered
as open channels and do not prevent the timeout. The timeout
value is specified in seconds or may use any of the units docu-
mented in the "TIME FORMATS" section.

Note that this timeout starts when the client connection com-
pletes user authentication but before the client has an oppor-
tunity to open any channels. Caution should be used when using
short timeout values, as they may not provide sufficient time
for the client to request and open its channels before termi-
nating the connection.

The default none is to never expire connections for having no
open channels. This option may be useful in conjunction with
ChannelTimeout.

UseBlacklist
Specifies whether sshd(8) attempts to send authentication suc-
cess and failure messages to the blacklistd(8) daemon. The de-
fault is no. For forward compatibility with an upcoming
blacklistd rename, the UseBlocklist alias can be used instead.

UseDNS Specifies whether sshd(8) should look up the remote host name,
and to check that the resolved host name for the remote IP ad-
dress maps back to the very same IP address.

If this option is set to no, then only addresses and not host
names may be used in ~/.ssh/authorized_keys from and
sshd_config Match Host directives. The default is "yes".

UsePAM Enables the Pluggable Authentication Module interface. If set
to yes this will enable PAM authentication using
KbdInteractiveAuthentication and PasswordAuthentication in ad-
dition to PAM account and session module processing for all au-
thentication types.

Because PAM keyboard-interactive authentication usually serves
an equivalent role to password authentication, you should dis-
able either PasswordAuthentication or
KbdInteractiveAuthentication.

If UsePAM is enabled, you will not be able to run sshd(8) as a
non-root user. The default is yes.

VersionAddendum
Optionally specifies additional text to append to the SSH pro-
tocol banner sent by the server upon connection. The default
is "FreeBSD-20240104". The value none may be used to disable
this.

X11DisplayOffset
Specifies the first display number available for sshd(8)'s X11
forwarding. This prevents sshd from interfering with real X11
servers. The default is 10.

X11Forwarding
Specifies whether X11 forwarding is permitted. The argument
must be yes or no. The default is no.

When X11 forwarding is enabled, there may be additional expo-
sure to the server and to client displays if the sshd(8) proxy
display is configured to listen on the wildcard address (see
X11UseLocalhost), though this is not the default. Addition-
ally, the authentication spoofing and authentication data veri-
fication and substitution occur on the client side. The secu-
rity risk of using X11 forwarding is that the client's X11 dis-
play server may be exposed to attack when the SSH client re-
quests forwarding (see the warnings for ForwardX11 in
ssh_config(5)). A system administrator may have a stance in
which they want to protect clients that may expose themselves
to attack by unwittingly requesting X11 forwarding, which can
warrant a no setting.

Note that disabling X11 forwarding does not prevent users from
forwarding X11 traffic, as users can always install their own
forwarders.

X11UseLocalhost
Specifies whether sshd(8) should bind the X11 forwarding server
to the loopback address or to the wildcard address. By de-
fault, sshd binds the forwarding server to the loopback address
and sets the hostname part of the DISPLAY environment variable
to localhost. This prevents remote hosts from connecting to
the proxy display. However, some older X11 clients may not
function with this configuration. X11UseLocalhost may be set
to no to specify that the forwarding server should be bound to
the wildcard address. The argument must be yes or no. The de-
fault is yes.

XAuthLocation
Specifies the full pathname of the xauth(1) program, or none to
not use one. The default is /usr/local/bin/xauth.

TIME FORMATS
sshd(8) command-line arguments and configuration file options that
specify time may be expressed using a sequence of the form:
time[qualifier], where time is a positive integer value and qualifier
is one of the following:

<none> seconds
s | S seconds
m | M minutes
h | H hours
d | D days
w | W weeks

Each member of the sequence is added together to calculate the total
time value.

Time format examples:

600 600 seconds (10 minutes)
10m 10 minutes
1h30m 1 hour 30 minutes (90 minutes)

TOKENS
Arguments to some keywords can make use of tokens, which are expanded
at runtime:

%% A literal `%'.
%C Identifies the connection endpoints, containing four space-
separated values: client address, client port number,
server address, and server port number.
%D The routing domain in which the incoming connection was re-
ceived.
%F The fingerprint of the CA key.
%f The fingerprint of the key or certificate.
%h The home directory of the user.
%i The key ID in the certificate.
%K The base64-encoded CA key.
%k The base64-encoded key or certificate for authentication.
%s The serial number of the certificate.
%T The type of the CA key.
%t The key or certificate type.
%U The numeric user ID of the target user.
%u The username.

AuthorizedKeysCommand accepts the tokens %%, %C, %D, %f, %h, %k, %t,
%U, and %u.

AuthorizedKeysFile accepts the tokens %%, %h, %U, and %u.

AuthorizedPrincipalsCommand accepts the tokens %%, %C, %D, %F, %f, %h,
%i, %K, %k, %s, %T, %t, %U, and %u.

AuthorizedPrincipalsFile accepts the tokens %%, %h, %U, and %u.

ChrootDirectory accepts the tokens %%, %h, %U, and %u.

RoutingDomain accepts the token %D.

FILES
/etc/ssh/sshd_config
Contains configuration data for sshd(8). This file should be
writable by root only, but it is recommended (though not neces-
sary) that it be world-readable.

SEE ALSO
sftp-server(8), sshd(8)

AUTHORS
OpenSSH is a derivative of the original and free ssh 1.2.12 release by
Tatu Ylonen. Aaron Campbell, Bob Beck, Markus Friedl, Niels Provos,
Theo de Raadt and Dug Song removed many bugs, re-added newer features
and created OpenSSH. Markus Friedl contributed the support for SSH
protocol versions 1.5 and 2.0. Niels Provos and Markus Friedl con-
tributed support for privilege separation.

FreeBSD 13.2 July 28, 2023 SSHD_CONFIG(5)

实验介绍

1.1 实验内容

在本节内容中,我们将会给大家简单描述 SSH 的原理,并且尝试 SSH 的几种验证方式。

1.2 实验知识点

  • 基于主机的身份验证
  • 密码验证
  • 公钥验证

1.3 推荐阅读 由于在 SSH 的连接过程中会涉及到有关密钥,签名等一些安全方面的知识,但是在实验内容中并不会涉及太多这方面的有关内容,而要对 SSH 的原理有一定的了解,必然绕不开这些知识,所以推荐大家阅读下列文章:

2. SSH 概述

2.1 SSH 协议

SSH 的全称是 Secure Shell。可以通过其对远程主机进行安全的访问。在最开始设计的时候,SSH 被设计为替代 telnet 以及一些其它不安全远程 shell 协议。

SSH 基于 C/S 架构,即 客户端/服务器。通过客户端与服务器相连,进行安全的远程访问。

2.2 OpenSSH

对于 SSH 协议有很多的实现。而最常用的开源实现为 OpenSSH。在很多操作系统中默认使用,例如在 Ubuntu 和 CentOS 中默认使用 OpenSSH。在下面的安装和配置的操作过程也将基于 OpenSSH 进行操作。

2.3 SSH 的体系结构

根据 RFC4251 关于 SSH 体系结构的定义。SSH 协议主要由三个组件组成:

  • 传输层协议(SSH-TRANS):该协议提供服务器身份验证,隐私和具有完美转发隐私的完整性。传输层通常是通过 TCP/IP 连接运行。
  • 用户认证协议(SSH-USERAUTH):验证连接到服务器的客户端用户。运行在传输层的协议之上。
  • 连接协议(SSH-CONNECT):将加密隧道复用到多个逻辑通道。运行在用户认证协议上。

在 SSH 的三个组件中的 SSH-USERAUTH,用户认证协议。对于用户的身份认证,在 RFC4252 中,有相关的身份认证方式的描述。在本节内容中,我们将带领大家配置以下几种身份认证方式:

  • 基于主机的身份验证(hostbased)
  • 使用密码 (password)
  • 公钥的认证方式(public key)

3. 服务的搭建与配置

3.1 安装与启动

在实验环境中已经有安装相应的组件,若是需要在本机安装相应的内容,可以参考我下面的命令。

在 Centos 上,使用 yum 进行安装:

1
2
3
4
5
# 服务端
$ sudo yum install openssh-server

# 客户端
$ sudo yum install openssh-clients

在 Ubuntu 上,使用 apt 进行安装:

1
2
3
4
5
# 服务端
$ sudo apt-get install openssh-server

# 客户端
$ sudo apt-get install openssh-client

为避免服务未启动,可以手动尝试开启服务,使用如下命令:

1
$ sudo service sshd start

最后,我们可以将要修改的配置文件进行一个备份,以便还原初始值,使用如下命令:

1
2
3
4
5
# 备份服务器端的配置文件
$ sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.back

# 备份客户端的配置文件
$ sudo cp /etc/ssh/ssh_config /etc/ssh/ssh_config.back

3.2 配置文件

在安装好相应的软件包之后,会在 /etc/ssh 文件夹下生成一些相应的配置文件,下面我们简单说明:

img

  • ssh_config 为客户端的配置文件,
  • sshd_config 为服务端的配置文件,
  • 其它的文件属于服务端安装时自动生成的密钥文件,以 .pub 结尾的为公钥,对应的不以 .pub 结尾的为私钥文件。

在启动服务的时候,会加载相应的相应的配置文件。对应的还可以在用户目录下的 ~/.ssh/ 目录中编辑配置文件,例如编辑 ~/.ssh/config 文件,则用户在启动客户端时,就会读取 ~/.ssh/config 文件的配置项而不是 /ect/ssh/ssh_config 针对全局的配置。

在这里我们并不会介绍有关于各种加密算法的内容,需要大家自行去了解相关的知识。

3.3 信任模式

如上所示,在安装完服务器端之后,会有相应的密钥对文件(一个公钥一个私钥)生成,由于 SSH 支持多种不同的加密算法,所以可能会根据实际情况会有多个文件被生成。

每个服务器都有一个密钥,由于使用不同的算法可以拥有多个密钥。在密钥交换过程中使用服务器主机密钥来验证客户端是否与正确的服务器交谈,为了做到这一点,客户必须先知道服务器的公共主机密钥(公钥),在 RFC4251 中给出了两种解决的思路或者说信任模式:

  1. 客户端在本地保存主机名与对应的服务器的公钥信息。
  2. 主机关联的公共密钥由受信任的证书机构进行认证(如 CA)

但是对于上述的两种方式来说,第 1 种需要一定的人力维护成本,而第 2 种,对于现在互联网上无数的服务器来说,明显是不可行的。

因此对于 SSH 协议而言,在首次连接到服务器时,允许事先通信而不需要密钥或证书,然而这一过程容易受到中间人攻击。

3.4 hostbased

hostbased 方式允许受信任主机上的指定用户无需输入密码即可直接登录进服务器。服务器会读取配置的可信任主机的信息,一般为 /etc/hosts.equiv。

在默认设置中,SSH 并未启用该验证方式,我们需要修改 /etc/ssh/sshd_config 的配置文件:

1
HostbasedAuthentication yes

上述编辑的 HostbasedAuthentication 可以选择设置是否启用基于主机的身份验证。将会从全局的 /etc/hosts.equiv 文件中匹配指定的主机和用户,以便允许这些主机上的这些用户无密码登录。

除了编辑服务器端的配置文件之外,我们还需编辑客户端的配置文件,这里我们编辑 ~/.ssh/config,则仅针对当前用户的客户端配置文件 :

1
2
3
4
5
# localhost 属于别名,未指定时只能使用 IP 地址
Host localhost
HostName 127.0.0.1
EnableSSHKeysign yes
HostbasedAuthentication yes

注意,如果 ~/.ssh/config 文件之前不存在,编辑完成之后需要修改其读写权限为 600 ,命令为 chmod 600 ~/.ssh/config,否则会报权限错误。

img

另外全局的客户端配置文件 /etc/ssh/ssh_config 也需要按如下修改:

1
2
3
4
Host *
GSSAPIAuthentication yes
EnableSSHKeysign yes
HostbasedAuthentication yes

img

上述图片中的 Host * 代表的是所有的服务器,就是该配置项下的子项针对所有的服务器。

最后还需要编辑 /etc/hosts.equiv 文件(不存在需要新创建)。该文件的格式为:

1
<IP ADDRESS> <USERNAME>

我们在该文件中添加一行内容,允许本机的 shiyanlou 用户无密码登录本机:

1
127.0.0.1 shiyanlou

重新启动服务器:

1
$ sudo service sshd restart

最后,我们可以使用 ssh 连接到本地的 SSH 服务器上,使用如下命令:

1
$ ssh shiyanlou@localhost

这里需要说明的是,使用 ssh 命令连接远程主机的格式为 ssh [user@]hostname,而 SSH 服务默认运行在 22 端口上,若是配置了使用其它端口,可以通过 -p 参数指定端口号。

我们可以看到显示结果如下图,对于初次连接服务器时,会检测是否已经有相应的记录(检测 known_hosts 文件),若是并未有相应的记录,则会提示是否继续(即在上述信任模式种提到的初次连接时 SSH 允许不需要预先知晓服务器的公共主机密钥)。如果选择继续则代表信任该服务器提供的公钥,并会将其保存到 ~/.ssh/known_hosts 文件中。可以通过在 /etc/ssh/sshd_config 文件中设置 IgnoreUserKnownHosts yes 不进行检测。

img

这时我们可以输入 yes,则会提示保存该公钥并且登陆成功,如下图所示:

img

此时,我们可以查看 ~/.ssh/known_hosts 文件的内容,与刚刚的提示信息进行比对,如下图所示:

img

这里可以看到主机名与对应的公钥的信息,由于提示信息显示的公钥是经过 HASH 算法计算过的值,所以会与文件中的内容不一致,在下面的内容中,我们将会验证两者为同一内容。

这里需要说明的是,由于我们是通过客户端直接连接当前的服务器端,即 localhost,在进行登陆时,登陆成功后不会有太明显的区别。有条件的同学可以在自己电脑上尝试通过客户端连接到服务器端,或者在进行登陆之前切换目录,不要停留在 shiyanlou 用户的家目录,因为登陆成功之后,会默认切换到被登陆用户的家目录。

如下所示,我们首先使用 exit 命令退出登陆,然后切换到 Code 目录,再次尝试登陆,可以看出明显的区别,即登陆成功之后会切换到到 /home/shiyanlou 目录,退出登陆会回到 ~/Code 目录下,如下所示:

img

3.5 password

SSH 默认使用的即是密码验证的方式,即在 /etc/sshd_config 配置文件中的 PasswordAuthentication 配置项默认为 yes ,如下所示,我们查看相关的配置文件:

img

由于刚刚我们配置了基于主机的身份验证,所以需要将之前的设置重新还原,可以手动修改配置,也可以使用我们刚刚备份的配置文件,或者直接删除掉配置的 /etc/hosts.equiv 文件即可。

这里我们删除刚刚配置的 /etc/hosts.equiv 文件,并且停止 sshd 服务,新打开一个终端,使用调试模式启动 sshd,如下所示:

img

此时,我们使用 ssh shiyanlou@localhost 去连接服务器端,由于已经删除掉 /etc/hosts.equiv 配置文件,会提示基于主机的身份验证失败,需要输入密码,如下所示:

img

此时,输入相应的密码即可成功连接,如下所示:

img

注意,实验环境暂不支持密码登录,这里的截图仅做参考。

3.6 public key

最后我们要介绍的验证方式为 public key 即公钥认证,该方式属于上述讨论的信任模式中的第一种实现方式,即通过手动在客户端和服务端配置相应的密钥对,客户端保存私钥,服务器端保存公钥信息。通过公私钥的认证,能够保证客户端是与真正的服务器通信,并且客户端和服务器端互相信任,不需要进行密码认证。

这里所使用的公钥认证的过程与传统意义上公私钥认证方式没有什么不同。具体的原理大家可以参考我在之前给出的链接内容。

对于 SSH 支持的加密算法,我们可以通过 ssh -Q key 来查询,如下所示:

img

下面我们创建一个密钥对,用于配置 public key 认证方式,使用如下命令:

1
2
3
4
5
# 直接使用 ssh-keygen 会默认使用 rsa 算法,并且将生成的密钥文件放到 ~/.ssh/ 目录下
$ ssh-keygen

# 还可以使用 -t 参数指定使用的算法类型, rsa | ecdsa| ed25519 | dsa 等,上述命令等同于下列命令
$ ssh-keygen -t rsa

img

所有提示要输入的地方直接回车即可。生成的公私钥文件,分别保存在 /.ssh/id_rsa,/.ssh/id_rsa.pub 中。

SSH 默认启用了 public key 的认证方式,不需要进行配置,我们直接通过下图查看需要设置的相关配置项,如下所示:

img

我们需要将 ~/.ssh/id_rsa.pub 的公钥信息追加到 ~/.ssh/authorized_keys 文件中,可以在其中添加多个公钥。

使用如下命令:

1
$ cat ~/.ssh/id_rsa.pub | tee -a ~/.ssh/authorized_keys

如果 ~/.ssh/authorized_keys 是首次创建,需要修改其文件权限为 600,命令为 chmod 600 ~/.ssh/authorized_keys。

此时,可以直接使用 ssh shiyanlou@localhost 进行验证,就能够直接进行登陆而不需要输入密码:

img

在客户端,也可以使用 -i 参数,指定要使用的私钥文件,使用如下命令:

1
$ ssh -i ~/.ssh/id_rsa shiyanlou@localhost

img

SSH 使用公私钥进行登录操作视频:

4. 其它配置项

对于 SSH 而言,还有很多其它的可用配置项,针对于 sshd_config 的配置项如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
# 可以自定义端口,默认为 22,在客户端使用 ssh 连接服务端时,可以通过 -p 参数指定端口
Port 22

# 允许 root 用户使用 ssh 登陆,可以设置为 yes ,no ,prohibit-password,without-password 后面两个都代表允许登陆而不使用密码
PermitRootLogin yes

# 指定允许组内的用户登陆,可以指定多个组,以空格分隔。
AllowGroups shiyanlou mysql

# 类似还有 DenyUsers AllowUsers DenyGroups 等,它们的优先级顺序从高到低为 DenyUsers,AllowUsers,DenyGroups,AllowGroups。

# 指定应该监听的本地地址,可以指定多个,默认为监听正在使用的路由域上的所有本地地址
ListenAddress hostname:port

5. scp 和 sftp

这里我们将会简单介绍的 scp 和 sftp 两个传输文件的工具。

5.1 scp

scp 可以在多个主机之间复制文件,使用 SSH 传输数据,并且使用它的认证方式进行身份认证。

scp 的使用方式为:

1
scp source target

可以将源(source) 指定为本地路径名,将目标(target)指定为远程主机,代表将本地的文件复制到远程主机。

也可以将源指定为远程主机,将目标指定为本地路径,代表的是将远程主机上的文件复制到本地。

对于远程主机的格式为 [user@]host:[path],或者 scp://[user@]host[:port][/path]。

本地路径则使用绝对路径或相对路径即可。

如下所示,我们分别在 /home/shiyanlou 目录下创建一个 test1 文件,在 /home/shiyanlou/Code 目录下创建一个 test2 文件,并使用 scp 复制文件:

1
2
3
4
5
6
7
8
9
10
11
# 创建文件
$ touch /home/shiyanlou/test1 /home/shiyanlou/Code/test2

# 切换到 /home/shiyanlou 目录
$ cd ~

# 将当前目录下的 test1 上传到远程服务器上的 /home/shiyanlou/Code 目录下
$ sudo scp test1 shiyanlou@localhost:/home/shiyanlou/Code

# 将远程服务器上的 /home/shiyanlou/Code 目录下的 test2 复制到本地
$ sudo scp shiyanlou@localhost:/home/shiyanlou/Code/test2 /home/shiyanlou

img

如上所示,我们成功进行了文件的复制。scp 使用的是 SSH 进行数据的传输和身份的验证,在之前的内容中,我们配置了 public key 的认证方式,所以能够直接复制成功,而不需要输入密码等验证操作。

除此之外,scp 还有一些常用的参数,例如 -r 递归复制目录,-p 指定端口等。

5.2 sftp

跟 scp 类似的还有 sftp,它 通过SSH 来安全的上传和下载文件,是常用的文件传输工具。关于sftp 详细的使用方法与 ftp 类似,可以参考前面关于 ftp 的章节。

简单示例如下:

img

同样,上述操作也是由于在刚刚的 SSH 身份验证我们启用了公钥验证并设置相关的内容,所以可以直接连接而不需要输入密码。