SSH 远程访问
Cumulus Linux 使用 OpenSSH 软件包,通过安全外壳 (SSH) 协议提供对系统的访问。
配置 SSH
您可以配置 SSH,以便为 root 用户和特定用户帐户提供登录访问权限,将 SSH 限制为侦听特定的 VRF,以及配置超时和会话选项。
Root 用户设置
默认情况下,root 帐户不能使用 SSH 登录。
您可以将 root 帐户配置为使用 SSH 登录交换机,方式为
- 密码
- 公钥或任何允许的机制,不是密码,也不是键盘交互式。这是默认设置。
- 在
authorized_keys
文件中定义的一组命令。
要允许 root 帐户使用密码 SSH 登录交换机
cumulus@switch:~$ nv set system ssh-server permit-root-login enabled
cumulus@switch:~$ nv config apply
运行 nv set system ssh-server permit-root-login disabled
命令禁用 root 帐户使用密码进行 SSH 登录。
要允许 root 帐户 SSH 登录交换机,并使用公钥或任何允许的机制进行身份验证,不是密码,也不是键盘交互式
cumulus@switch:~$ nv set system ssh-server permit-root-login prohibit-password
cumulus@switch:~$ nv config apply
要允许 root 帐户 SSH 登录交换机,并且仅运行在 authorized_keys
文件中定义的一组命令
cumulus@switch:~$ nv set system ssh-server permit-root-login forced-commands-only
cumulus@switch:~$ nv config apply
要允许 root 帐户使用密码 SSH 登录交换机,请编辑 /etc/ssh/sshd_config
文件,并将 PermitRootLogin
选项设置为 yes
cumulus@switch:~$ sudo cat /etc/ssh/sshd_config
...
# Authentication:
LoginGraceTime 2m
PermitRootLogin yes
...
将 PermitRootLogin
命令设置为 no
以禁用使用密码进行 SSH 登录。
要允许 root 帐户 SSH 登录交换机,并使用公钥或任何允许的机制进行身份验证,不是密码,也不是键盘交互式
为 root 用户创建
.ssh
目录。cumulus@switch:~$ sudo mkdir -p /root/.ssh cumulus@switch:~$ sudo chmod 0700 /root/.ssh
作为特权用户(例如
cumulus
用户),可以echo
公钥内容并将内容重定向到授权密钥文件,或者将公钥文件复制到交换机,然后将其复制到 root 帐户(使用权限提升)。要 echo 公钥内容并将内容重定向到授权密钥文件
cumulus@switch:~$ echo "<SSH public key contents>" | sudo tee -a /root/.ssh/authorized_keys cumulus@switch:~$ sudo chmod 0644 /root/.ssh/authorized_keys
要将公钥文件复制到交换机,然后将其复制到 root 帐户
cumulus@switch:~$ sudo cp <SSH public key file> /root/.ssh/authorized_keys cumulus@switch:~$ sudo chmod 0644 /root/.ssh/authorized_keys
允许和拒绝用户
要允许某些用户建立 SSH 会话
cumulus@switch:~$ nv set system ssh-server allow-users user1
cumulus@switch:~$ nv config apply
要拒绝某些用户建立 SSH 会话
cumulus@switch:~$ nv set system ssh-server deny-users user4
cumulus@switch:~$ nv config apply
要允许某些用户建立 SSH 会话,请编辑 /etc/ssh/sshd_config
文件并添加 AllowUsers
参数
cumulus@switch:~$ sudo cat /etc/ssh/sshd_config
...
...
# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
AllowUsers = user1
要拒绝某些用户建立 SSH 会话,请编辑 /etc/ssh/sshd_config
文件并添加 DenyUsers
参数
cumulus@switch:~$ sudo cat /etc/ssh/sshd_config
...
# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
AllowUsers = user1
DenyUsers = user4
SSH 和 VRF
SSH 服务在交换机上的默认 VRF 中运行,但侦听所有 VRF 中的所有接口。您可以限制 SSH 仅侦听特定的 VRF。
您不能同时在默认 VRF 和其他 VRF 中运行 SSH。
以下示例配置 SSH 仅侦听管理 VRF
cumulus@switch:~$ nv set system ssh-server vrf mgmt
cumulus@switch:~$ nv config apply
以下示例配置 SSH 侦听管理 VRF 和 VRF RED
cumulus@switch:~$ nv set system ssh-server vrf mgmt
cumulus@switch:~$ nv set system ssh-server vrf RED
cumulus@switch:~$ nv config apply
将 SSH 服务绑定到 VRF。以下示例配置 SSH 仅侦听管理 VRF
cumulus@switch:~$ sudo systemctl stop ssh.service
cumulus@switch:~$ sudo systemctl disable ssh.service
cumulus@switch:~$ sudo systemctl start ssh@mgmt.service
cumulus@switch:~$ sudo systemctl enable ssh@mgmt.service
以下示例配置 SSH 侦听管理 VRF 和 VRF RED
cumulus@switch:~$ sudo systemctl stop ssh.service
cumulus@switch:~$ sudo systemctl disable ssh.service
cumulus@switch:~$ sudo systemctl start ssh@mgmt.service
cumulus@switch:~$ sudo systemctl enable ssh@mgmt.service
cumulus@switch:~$ sudo systemctl start ssh@RED.service
cumulus@switch:~$ sudo systemctl enable ssh@RED.service
要配置 SSH 仅侦听 VRF 中的一个 IP 地址或子网,您需要将服务绑定到该 VRF(如上所述),然后在 /etc/ssh/sshd_config
文件中将 ListenAddress
参数设置为该 VRF 中的 IP 地址或子网。
cumulus@switch:~$ sudo cat /etc/ssh/sshd_config
...
#Port 22
#AddressFamily any
ListenAddress 10.10.10.6
#ListenAddress ::
启用和禁用 SSH 服务器
Cumulus Linux 默认启用 SSH 服务器。要禁用 SSH 服务器
cumulus@switch:~$ nv set system ssh-server state disabled
cumulus@switch:~$ nv config apply
运行 nv set system ssh-server state enabled
命令以重新启用 SSH 服务器。
cumulus@switch:~$ sudo systemctl stop ssh.service
cumulus@switch:~$ sudo systemctl disable ssh.service
要重新启用 SSH 服务器
cumulus@switch:~$ sudo systemctl start ssh.service
cumulus@switch:~$ sudo systemctl enable ssh.service
SSH 严格模式
默认情况下,SSH 严格模式为 on
;Cumulus Linux 禁用 X11、TCP 转发和压缩,并强制执行安全密码。
要禁用 SSH 严格模式,请运行 nv set system ssh-server strict disabled
命令
cumulus@switch:~$ nv set system ssh-server strict disabled
cumulus@switch:~$ nv config apply
要重新启用严格模式,请运行 nv set system ssh-server strict enabled
命令。
要显示严格模式是 on
还是 off
,请运行 nv show system ssh-server
命令
cumulus@switch:~$ nv show system ssh-server
applied
--------------------------- --------
authentication-retries 6
login-timeout 120
inactive-timeout 15
permit-root-login enabled
max-sessions-per-connection 30
state enabled
strict disabled
...
编辑 /etc/ssh/sshd_config
文件,并将 AllowTcpForwarding
、X11Forwarding
和 Compression
参数更改为 yes
。此外,删除文件中 Ciphers and keying
部分中 #RekeyLimit default none
下的密码和密钥。
cumulus@switch:~$ sudo nano /etc/ssh/sshd_config
...
# Ciphers and keying
#RekeyLimit default none
...
#AllowAgentForwarding yes
AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
PrintMotd no
#PrintLastLog yes
#TCPKeepAlive yes
#PermitUserEnvironment no
Compression yes
ClientAliveInterval 0
ClientAliveCountMax 0
#UseDNS no
#PidFile /var/run/sshd.pid
MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none
配置超时和会话
您可以配置以下 SSH 超时和会话选项
- 拒绝 SSH 会话之前允许的登录尝试次数。您可以指定介于 3 和 100 之间的值。默认值为 3 次登录尝试。
- 登录超时前允许的秒数。您可以指定介于 1 和 600 之间的值。默认值为 120 秒。
- 侦听传入 SSH 会话的 TCP 端口号。您可以指定介于 1 和 65535 之间的值。
- 会话在 SSH 服务器终止连接之前可以保持不活动状态的分钟数。默认值为 0 分钟。
- 每个 TCP 连接允许的最大 SSH 会话数。您可以指定介于 1 和 100 之间的值。默认值为 10。
- 未经身份验证的 SSH 会话
- 允许的最大未经身份验证的 SSH 会话数。您可以指定介于 1 和 10000 之间的值。默认值为 100。
- 在开始限制之前允许的未经身份验证的 SSH 会话数。您可以指定介于 1 和 10000 之间的值。默认值为 10。
- 在达到会话计数限制之前,高于限制开始计数的要拒绝的连接的起始百分比。您可以指定介于 1 和 100 之间的值。默认值为 30。
以下示例将拒绝 SSH 会话之前允许的登录尝试次数配置为 10,并将登录超时前允许的秒数配置为 200
cumulus@switch:~$ nv set system ssh-server authentication-retries 10
cumulus@switch:~$ nv set system ssh-server login-timeout 200
cumulus@switch:~$ nv config apply
编辑 /etc/ssh/sshd_config
文件,并将 Authentication
部分中的 MaxAuthTries
参数更改为 10,并将 LoginGraceTime
参数更改为 200
cumulus@switch:~$ sudo nano /etc/ssh/sshd_config
...
# Authentication:
LoginGraceTime 200s
PermitRootLogin prohibit-password
#StrictModes yes
MaxAuthTries 10
MaxSessions 10
以下示例将侦听传入 SSH 会话的 TCP 端口配置为 443
cumulus@switch:~$ nv set system ssh-server port 443
cumulus@switch:~$ nv config apply
编辑 /etc/ssh/sshd_config
文件并添加 Port
参数
cumulus@switch:~$ sudo nano /etc/ssh/sshd_config
...
Port 443
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
...
以下示例将会话在 SSH 服务器终止连接之前可以保持不活动状态的时间量配置为 5 分钟(300 秒),并将每个 TCP 连接允许的最大 SSH 会话数配置为 5。默认的 inactive-timeout
为 15 分钟,默认的 max-sessions-per-connection
为 10
cumulus@switch:~$ nv set system ssh-server inactive-timeout 5
cumulus@switch:~$ nv set system ssh-server max-sessions-per-connection 5
cumulus@switch:~$ nv config apply
编辑 /etc/ssh/sshd_config
文件的 Authentication
部分。
- 要配置会话在 SSH 服务器终止连接之前可以保持不活动状态的时间量(以秒为单位),请更改
ClientAliveInterval
参数。 - 要配置每个 TCP 连接允许的最大 SSH 会话数,请更改
MaxSessions
参数。
cumulus@switch:~$ sudo nano /etc/ssh/sshd_config
...
# Authentication:
LoginGraceTime 120s
PermitRootLogin prohibit-password
#StrictModes yes
MaxAuthTries 10
MaxSessions 5
...
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
PrintMotd no
#PrintLastLog yes
#TCPKeepAlive yes
#PermitUserEnvironment no
#Compression delayed
ClientAliveInterval 300
...
以下示例配置
- 在开始限制之前允许的未经身份验证的 SSH 会话数为 5。
- 在达到会话计数限制之前,高于限制开始计数的要拒绝的连接的起始百分比为 22。
- 允许的最大未经身份验证的 SSH 会话数为 20。
cumulus@switch:~$ nv set system ssh-server max-unauthenticated throttle-start 5
cumulus@switch:~$ nv set system ssh-server max-unauthenticated throttle-percent 22
cumulus@switch:~$ nv set system ssh-server max-unauthenticated session-count 20
cumulus@switch:~$ nv config apply
编辑 /etc/ssh/sshd_config
文件并更改 MaxStartups
参数。
以下示例配置
- 在开始限制之前允许的未经身份验证的 SSH 会话数为 5。
- 在达到会话计数限制之前,高于限制开始计数的要拒绝的连接的起始百分比为 22。
- 允许的最大未经身份验证的 SSH 会话数为 20。
cumulus@switch:~$ sudo nano /etc/ssh/sshd_config
...
MaxStartups 5:22:20
...
SSH 登录通知
Cumulus Linux 在身份验证后在控制台上显示以下 SSH 登录信息
- 上次成功登录的日期和时间。
- 上次成功登录后不成功的登录次数。
- 上次不成功登录的日期和时间。
- 上次登录后对用户帐户的更改(密码、角色、组等)。
- 上次成功或不成功登录的位置(终端或 IP)。
- 特定日期和时间之后成功登录的总次数。
Cumulus Linux 显示 SSH 和串行连接的登录通知。此信息可以帮助检测不需要的或恶意活动,例如可疑登录或密码和角色更改。
要配置显示登录通知的时间段(以天为单位),请运行 nv set system ssh-server login-record-period <days>
命令。您可以指定介于 1 和 30 之间的值。默认值为 1。
以下示例将 SSH 登录通知周期设置为 20 天
cumulus@switch:~$ nv set system ssh-server login-record-period 20
cumulus@switch:~$ nv config apply
要将 SSH 登录通知周期设置回默认值(1 天),请运行 nv unset system ssh-server login-record-period
命令。
要显示配置的 SSH 登录通知周期,请运行 nv show system ssh-server
命令。请参阅下面的 故障排除。
生成和安装 SSH 密钥对
本节介绍如何在系统上生成 SSH 密钥对,并将该密钥作为授权密钥安装在另一个系统上。
生成 SSH 密钥对
要生成 SSH 密钥对,请运行 ssh-keygen
命令并按照提示操作。
Cumulus Linux 不支持 sha1 ssh 密钥交换方法。
要配置无密码的系统,请在下一步提示时不要输入密码短语。
cumulus@host01:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/cumulus/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/cumulus/.ssh/id_rsa.
Your public key has been saved in /home/cumulus/.ssh/id_rsa.pub.
The key fingerprint is:
5a:b4:16:a0:f9:14:6b:51:f6:f6:c0:76:1a:35:2b:bb cumulus@leaf04
The key's randomart image is:
+---[RSA 2048]----+
| +.o o |
| o * o . o |
| o + o O o |
| + . = O |
| . S o . |
| + . |
| . E |
| |
| |
+-----------------+
安装授权 SSH 密钥
要安装授权 SSH 密钥,您需要获取 SSH 公钥的内容,并将其添加到用户的 SSH 授权密钥文件 (~/.ssh/authorized_keys
) 中。
公钥是一个文本文件,包含三个空格分隔的字段
<type> <key string> <comment>
字段 | 描述 |
---|---|
<类型> | 您要用于哈希密钥的算法。该算法可以是 ecdsa-sha2-nistp256 、ecdsa-sha2-nistp384 、ecdsa-sha2-nistp521 、ssh-dss 、ssh-ed25519 或 ssh-rsa (默认值)。 |
<密钥字符串> | 密钥的 base64 格式字符串。 |
<注释> | 单字字符串。默认情况下,这是生成密钥的系统的名称。NVUE 使用 <comment> 字段作为密钥名称。 |
安装授权 SSH 密钥的过程因用户是 NVUE 管理用户还是非 NVUE 管理用户而异。
以下示例将名为 prod_key
的授权密钥添加到用户 admin2
。公钥文件的内容为 ssh-rsa 1234 prod_key
。
cumulus@leaf01:~$ nv set system aaa user admin2 ssh authorized-key prod_key key XABDB3NzaC1yc2EAAAADAQABAAABgQCvjs/RFPhxLQMkckONg+1RE1PTIO2JQhzFN9TRg7ox7o0tfZ+IzSB99lr2dmmVe8FRWgxVjc...
cumulus@leaf01:~$ nv set system aaa user admin2 ssh authorized-key prod_key type ssh-rsa
cumulus@leaf01:~$ nv config apply
以下示例将主机上 cumulus
帐户的授权密钥文件添加到交换机上的 cumulus
帐户
要将先前生成的公钥复制到所需位置,请运行
ssh-copy-id
命令并按照提示操作cumulus@host01:~$ ssh-copy-id -i /home/cumulus/.ssh/id_rsa.pub cumulus@leaf02 The authenticity of host 'leaf02 (192.168.0.11)' can't be established. ECDSA key fingerprint is b1:ce:b7:6a:20:f4:06:3a:09:3c:d9:42:de:99:66:6e. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys cumulus@leaf01's password: Number of key(s) added: 1
如果远程交换机上的用户名与本地交换机上的用户名不同,则
ssh-copy-id
命令不起作用。要解决此问题,请改用scp
命令cumulus@host01:~$ scp .ssh/id_rsa.pub cumulus@leaf02:.ssh/authorized_keys Enter passphrase for key '/home/cumulus/.ssh/id_rsa': id_rsa.pub
连接到远程交换机以确认身份验证密钥已就位
cumulus@leaf01:~$ ssh cumulus@leaf02 Welcome to Cumulus VX (TM) Cumulus VX (TM) is a community supported virtual appliance designed for experiencing, testing and prototyping the latest technology. For any questions or technical support, visit our community site at: http://community.cumulusnetworks.com The registered trademark Linux (R) is used pursuant to a sublicense from LMI, the exclusive licensee of Linus Torvalds, owner of the mark on a world-wide basis. Last login: Thu Sep 29 16:56:54 2016
故障排除
要显示所有当前的 SSH 服务器配置设置,请运行 NVUE nv show system ssh-server
命令
cumulus@switch:~$ nv show system ssh-server
applied
--------------------------- -----------------
authentication-retries 6
login-timeout 120
inactive-timeout 0
permit-root-login prohibit-password
max-sessions-per-connection 10
state enabled
strict enabled
login-record-period 20
max-unauthenticated
session-count 100
throttle-percent 30
throttle-start 10
要显示当前活动 SSH 会话数,请运行 NVUE nv show system ssh-server active-sessions
命令或 Linux w
命令
cumulus@switch:~$ nv show system ssh-server active-sessions
Peer Address:Port Local Address:Port State
------------------- ---------------------- -----
192.168.200.1:46528 192.168.200.11%mgmt:22 ESTAB
cumulus@switch:~$ w
11:10:46 up 19:19, 4 users, load average: 0.08, 0.05, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
cumulus ttyS0 - Wed15 19:19m 0.03s 0.02s -bash
cumulus pts/0 192.168.200.1 07:27 3:43m 0.03s 0.03s -bash
cumulus pts/1 192.168.200.1 10:01 1:09m 0.02s 0.02s -bash
cumulus pts/2 192.168.200.1 11:10 1.00s 0.03s 0.00s w
要显示哪些用户可以建立 SSH 会话,请运行 nv show system ssh-server allow-users
命令。要显示哪些用户不能建立 SSH 会话,请运行 nv show system ssh-server deny-users
命令。您还可以使用 nv show system ssh-server allow-users <user>
命令和 nv show system ssh-server deny-users <user>
命令显示特定用户的信息。
要显示侦听传入 SSH 会话的 TCP 端口号,请运行 nv show system ssh-server port
命令。您还可以使用 nv show system ssh-server port <port>
命令显示特定端口的信息。
要显示 SSH 计时器和会话信息,请运行 nv show system ssh-server max-unauthenticated
命令
cumulus@switch:~$ nv show system ssh-server max-unauthenticated
applied
---------------- -------
session-count 20
throttle-percent 22
throttle-start 5