我是 CentOS Linux 服务器的新用户。如何在 CentOS Linux 上启动、停止或重新启动 OpenSSH (SSHD) 服务器?如何在 CentOS Linux 7.x 或 8.x 上启动/停止/重新启动 SSHD?你能告诉我如何在 CentOS Linux 上启动 SSHD 吗?
教程详情 | |
---|---|
难度级别 | 简单的 |
根权限 | 是的 |
要求 | 没有任何 |
美东时间。阅读时间 | 4分钟 |
您需要根据您的 CentOS Linux 系统版本使用以下命令来启动、停止或重新启动 sshd 服务器(OpenSSH 服务)。
CentOS Linux命令启动/停止/重启SSHD服务
- service 命令 –仅适用于 CentOS v4.x/5.x/6.x停止、启动、重新启动或查找系统服务的状态。
- chkconfig命令 – 仅在启动时打开或关闭服务CentOS v4.x/5.x/6.x。
- systemctl命令 –仅在 CentOS v7.x/8.x 或更高版本上管理和启动/停止/重新启动 sshd (请参阅此处的 CentOS 7.x/8.x 命令)。
CentOS Linux 上的 sshd 服务名称
服务的名称通常也sshd由/etc/init.d/sshd脚本或service命令或systemctl
CentOS 在启动时命令打开 OpenSSH SSHD 服务器
对于CentOS 版本 4.x/5.x/6.x 或更早版本,在启动时打开 SSHD 的语法如下:
## 您必须以 root 用户身份登录。换句话说,尝试 'sudo -i' 或 'su -' ## # chkconfig sshd on
例如,要在启动时关闭 SSHD 服务,请输入:
# chkconfig sshd 关闭
要查看每个运行级别类型中服务的当前状态:
# chkconfig --list sshd
输出示例:
sshd 0:关闭 1:关闭 2:打开 3:打开 4:打开 5:打开 6:关闭
CentOS启动sshd服务命令
CentOS 4.x/5.x/6.x 或更早版本的语法如下:
# sshd服务启动
或者
# /etc/init.d/sshd启动
CentOS停止sshd服务命令
语法适用于CentOS 版本 4.x/5.x/6.x 或更早版本:
警报:不要通过基于 ssh 的会话运行以下命令,您最终会收到“网络连接丢失”错误。
# sshd 服务停止
或者
# /etc/init.d/sshd停止
CentOS重启sshd服务命令
语法适用于CentOS 版本 4.x/5.x/6.x 或更早版本:
#重新启动 sshd 服务
或者
# /etc/init.d/sshd重新启动
CentOS查找sshd服务命令的状态
语法适用于CentOS 版本 4.x/5.x/6.x 或更早版本:
#服务 sshd 状态
或者
# /etc/init.d/sshd状态
上述命令的会话输出示例
![图 01:服务和 chkconfig 命令的实际应用](https://www.alaica.com/wp-content/uploads/linux-20.jpg)
Fig. 01: service and chkconfig command in action for CentOS version 4.x/5.x/6.x and older only
A note about CentOS version 7.x/8.x or above
CentOS Linux version 7.x and 8.x come with systemd as init. In other words, the above command may not work. You need to use the systemctl command to manage services on a CentOS Linux box. Hence, I recommend avoiding using the service command on the latest version of CentOS Linux. Let us see all commands and examples to restart sshd on CentOS 7.x or 8.x
Enable service at boot time
To enable SSHD service at boot time on CentOS version 7.x/8.x or above, run:
# systemctl enable sshd
Sample outputs:
Created symlink from /etc/systemd/system/multi-user.target.wants/sshd.service to /usr/lib/systemd/system/sshd.service.
Start SSHD on CentOS version 7.x/8.x+ or above
# systemctl start sshd.service
Restart SSHD on CentOS version 7.x/8.x+ or above
# systemctl restart sshd.service
Stop SSHD on CentOS version 7.x/8.x+ or above
# systemctl stop sshd.service
Reload SSHD on CentOS version 7.x/8.x+ or above after config changes
# systemctl reload sshd.service
Want to get status SSHD on CentOS version 7.x/8.x+ or above?
Try the following systemctl command:
# systemctl status sshd.service
Sample outputs:
* sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2017-08-08 21:01:45 IST; 1min 5s ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 25701 (sshd) CGroup: /system.slice/sshd.service ??25701 /usr/sbin/sshd -D Aug 08 21:01:45 centos7-box systemd[1]: Starting OpenSSH server daemon... Aug 08 21:01:45 centos7-box sshd[25701]: Server listening on 0.0.0.0 port 22. Aug 08 21:01:45 centos7-box sshd[25701]: Server listening on :: port 22. Aug 08 21:01:45 centos7-box systemd[1]: Started OpenSSH server daemon.
Conclusion
您成功学习了如何在 CentOS Linux 版本 4.x/5.x/6.x/7.x 和 8.x 上启动、停止和重新启动 sshd 服务器。简而言之,对于 CentOS 7.x/8.x 使用 systemctl 命令,对于旧版本使用 service 命令。请参阅此处的CentOS 项目页面以及如何保护 OpenSSH (SSHD) 服务器以获取更多信息。