🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 17.7 本章习题 ( 要看答案请将鼠标移动到“答:”下面的空白处,按下左键圈选空白处即可察看 ) * 情境仿真题:通过设置、启动、观察等机制,完整的了解一个服务的启动与观察现象。 * 目标:了解 daemon 的控管机制,以 sshd daemon 为例; * 前提:需要对本章已经了解,尤其是 systemd 的管理 部分; * 需求:已经有 sshd 这个服务,但没有修改过端口! 在本情境中,我们使用 sshd 这个服务来观察,主要是假设 sshd 要开立第二个服务,这个第二个服务的 port 放行于 222 ,那该如何处理? 可以这样做看看: 1. 基本上 sshd 几乎是一定会安装的服务!只是我们还是来确认看看好了! ``` [root@study ~]# systemctl status sshd.service sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled) Active: active (running) since Thu 2015-08-13 14:31:12 CST; 20h ago [root@study ~]# cat /usr/lib/systemd/system/sshd.service [Unit] Description=OpenSSH server daemon After=network.target sshd-keygen.service Wants=sshd-keygen.service [Service] EnvironmentFile=/etc/sysconfig/sshd ExecStart=/usr/sbin/sshd -D $OPTIONS ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target ``` 2. 通过观察 man sshd,我们可以查询到 sshd 的配置文件位于 /etc/ssh/sshd_config 这个文件内!再 man sshd_config 也能知道原来端口是使用 Port 来规范的! 因此,我想要创建第二个配置文件,文件名假设为 /etc/ssh/sshd2_config 这样! ``` [root@study ~]# cd /etc/ssh [root@study ssh]# cp sshd_config sshd2_config [root@study ssh]# vim sshd2_config Port 222 # 随意找个地方加上这个设置值!你可以在文件的最下方加入这行也 OK 喔! ``` 3. 接下来开始修改启动脚本服务档! ``` [root@study ~]# cd /etc/systemd/system [root@study system]# cp /usr/lib/systemd/system/sshd.service sshd2.service [root@study system]# vim sshd2.service [Unit] Description=OpenSSH server daemon 2 After=network.target sshd-keygen.service Wants=sshd-keygen.service [Service] EnvironmentFile=/etc/sysconfig/sshd ExecStart=/usr/sbin/sshd -f /etc/ssh/sshd2_config -D $OPTIONS ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target [root@study system]# systemctl daemon-reload [root@study system]# systemctl enable sshd2 [root@study system]# systemctl start sshd2 [root@study system]# tail -n 20 /var/log/messages # semanage port -a -t PORT_TYPE -p tcp 222 where PORT_TYPE is one of the following: ssh_port_t, vnc_port_t, xserver_port_t. # 认真的看!你会看到上面这两句!也就是 SELinux 的端口问题!请解决! [root@study system]# semanage port -a -t ssh_port_t -p tcp 222 [root@study system]# systemctl start sshd2 [root@study system]# netstat -tlnp | grep ssh tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1300/sshd tcp 0 0 0.0.0.0:222 0.0.0.0:* LISTEN 15275/sshd tcp6 0 0 :::22 :::* LISTEN 1300/sshd tcp6 0 0 :::222 :::* LISTEN 15275/sshd ``` * * * 简答题部分: * 使用 netstat -tul 与 netstat -tunl 有什么差异?为何会这样?使用 n 时, netstat 就不会使用主机名称与服务名称 (hostname & service_name) 来显示, 取而代之的则是以 IP 及 port number 来显示的。IP 的分析与 /etc/hosts 及 /etc/resolv.conf 有关, 这个在未来服务器篇才会提到。至于 port number 则与 /etc/services 有关,请自行参考喔! ^_^ * 你能否找出来,启动 port 3306 这个端口的服务为何?通过搜寻 /etc/services 内容,得到 port 3306 为 mysql 所启动的端口喔!查询 google, 可得到 mysql 为一种网络数据库系统软件。 * 你可以通过哪些指令查询到目前系统默认开机会启动的服务?systemctl list-units 以及 systemctl list-unit-files * 承上,那么哪些服务“目前”是在启动的状态?结果同上!只是若要进一步的信息,应该使用 systemctl status [unit.service] 一项一项查询!