萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Linux 服務管理全景圖:Standalone vs xinetd、端口對應、啟停控制與開機自啟實戰

Linux 服務管理涉及啟動方式、端口對應、運行等級控制三大維度。本文系統梳理 Standalone(獨立守護進程)與 Super Daemon (xinetd) 兩大架構,提供端口追蹤、服務啟停、開機自啟的完整操作手冊。

服務啟動架構對比

特性Standalonexinetd (Super Daemon)
啟動方式系統啟動時直接常駐記憶體由 xinetd 主進程監聽,有請求才 fork 子進程
回應速度極快(無 fork 開銷)稍慢(需 fork)
資源佔用常駐佔用記憶體閒置時不佔用,適合低頻服務
管理腳本位置/etc/init.d/ 或 /etc/rc.d/init.d//etc/xinetd.d/ 設定檔
控制指令systemctl start|stop|restart sshd修改 disable = yes/no 後 systemctl reload xinetd
典型服務sshd, httpd, mysqld, named, ntpdtelnet, tftp, rsync, cvs, pop3, imap

端口與服務對應查詢

1. 系統預設對應表

cat /etc/services | grep -E '^(ssh|http|ftp|telnet|smtp|dns|dhcp)'

2. 即時端口佔用狀態

# 顯示 PID、程式名、監聽狀態
ss -tlnp
# 或
netstat -tlnp

3. 反查端口屬於哪個套件

# 找到進程執行檔路徑
which sshd
# 或
readlink -f /proc/$(ss -tlnp | grep :22 | awk '{print $NF}' | cut -d= -f2 | cut -d, -f1)/exe

# 查詢 RPM 套件
rpm -qf /usr/sbin/sshd

# 查詢該套件的啟動腳本
rpm -ql openssh-server | grep init.d

服務啟停與開機自啟控制

systemd 時代(RHEL 7+/CentOS 7+)

# 立即啟停
systemctl start|stop|restart|reload sshd

# 開機自啟
systemctl enable|disable sshd

# 查看狀態
systemctl status sshd
systemctl is-enabled sshd

# 列出所有單元
systemctl list-unit-files --type=service

SysVinit 相容指令(仍通用)

service sshd start|stop|restart|status
chkconfig sshd on|off
chkconfig --list sshd

關閉特定端口的標準流程

  1. 識別進程:ss -tlnp | grep :25 → 找到 PID/程式名
  2. 定位執行檔:readlink -f /proc//exe
  3. 查詢套件:rpm -qf /path/to/exe
  4. 停止服務:systemctl stop
  5. 禁用開機自啟:systemctl disable
  6. 防火牆封鎖(雙重保險):firewall-cmd --permanent --remove-port=25/tcp && firewall-cmd --reload

實戰案例:啟用 Telnet(xinetd 管理)

# 1. 安裝
dnf install -y telnet-server xinetd

# 2. 編輯 xinetd 設定
cat > /etc/xinetd.d/telnet <<'EOF'
service telnet
{
    flags           = REUSE
    socket_type     = stream
    wait            = no
    user            = root
    server          = /usr/sbin/in.telnetd
    log_on_failure  += USERID
    disable         = no
}
EOF

# 3. 重載 xinetd
systemctl restart xinetd

# 4. 驗證
ss -tlnp | grep :23

建議啟用的核心服務

服務功能必要性
sshd遠端安全登入必須
network / NetworkManager網路連線必須
crond排程任務必須
rsyslog / systemd-journald日誌收集必須
firewalld / iptables防火牆必須
chronyd / ntpd時間同步強烈建議
irqbalance中斷平衡多核心建議
auditd安全審計合規環境必須

其他服務依業務需求啟用,遵循最小權限原則:不需要的服務一律關閉。

copyright © 萬盛學電腦網 all rights reserved