Red Hat Training
A Red Hat training course is available for RHEL 8
34.2. 配置并启动 OpenSSH 服务器
使用以下步骤执行您的环境以及启动 OpenSSH 服务器所需的基本配置。请注意,在默认 RHEL 安装后,sshd
守护进程已经启动,服务器主机密钥会自动被创建。
先决条件
-
已安装
openssh-server
软件包。
流程
在当前会话中启动
sshd
守护进程,并在引导时自动启动:# systemctl start sshd # systemctl enable sshd
要为
/etc/ssh/sshd_config
配置文件中的ListenAddress
指令指定默认地址0.0.0.0
(IPv4)或::
(IPv6),并使用较慢的动态网络配置,将network-online.target
目标单元的依赖关系添加到sshd.service
单元文件中。要做到这一点,使用以下内容创建/etc/systemd/system/sshd.service.d/local.conf
文件:[Unit] Wants=network-online.target After=network-online.target
-
查看
/etc/ssh/sshd_config
配置文件中的 OpenSSH 服务器设置是否满足您的情况要求。 另外,还可通过编辑
/etc/issue
文件来更改您的 OpenSSH 服务器在客户端验证前显示的欢迎信息,例如:Welcome to ssh-server.example.com Warning: By accessing this server, you agree to the referenced terms and conditions.
确保
/etc/ssh/sshd_config
中未注释掉Banner
选项,并且其值包含/etc/issue
:# less /etc/ssh/sshd_config | grep Banner Banner /etc/issue
请注意:要在成功登录后改变显示的信息,您必须编辑服务器上的
/etc/motd
文件。详情请查看pam_motd
man page。重新载入
systemd
配置,并重启sshd
以应用修改:# systemctl daemon-reload # systemctl restart sshd
验证
检查
sshd
守护进程是否正在运行:# systemctl status sshd ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2019-11-18 14:59:58 CET; 6min ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 1149 (sshd) Tasks: 1 (limit: 11491) Memory: 1.9M CGroup: /system.slice/sshd.service └─1149 /usr/sbin/sshd -D -oCiphers=aes128-ctr,aes256-ctr,aes128-cbc,aes256-cbc -oMACs=hmac-sha2-256,> Nov 18 14:59:58 ssh-server-example.com systemd[1]: Starting OpenSSH server daemon... Nov 18 14:59:58 ssh-server-example.com sshd[1149]: Server listening on 0.0.0.0 port 22. Nov 18 14:59:58 ssh-server-example.com sshd[1149]: Server listening on :: port 22. Nov 18 14:59:58 ssh-server-example.com systemd[1]: Started OpenSSH server daemon.
使用 SSH 客户端连接到 SSH 服务器。
# ssh user@ssh-server-example.com ECDSA key fingerprint is SHA256:dXbaS0RG/UzlTTku8GtXSz0S1++lPegSy31v3L/FAEc. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'ssh-server-example.com' (ECDSA) to the list of known hosts. user@ssh-server-example.com's password:
其他资源
-
sshd(8)
和sshd_config(5)
手册页。