我在上一篇文章中分享了一些帮助您管理个人 Linux 服务器的重要初步步骤。我简要提到了监控监听端口的网络连接,并且我想通过使用 Linux 系统的 netstat
命令来扩展这一点。
服务监控和端口扫描是标准的行业实践。有一些非常好的软件,例如 Prometheus 可以帮助自动化该过程,还有 SELinux 可以帮助情境化和保护系统访问。但是,我认为了解您的服务器如何连接到其他网络和设备是建立服务器正常运行基线的关键,这有助于您识别可能表明错误或入侵的异常情况。作为初学者,我发现 netstat
命令为我的服务器提供了重要的洞察力,无论是在监控还是网络故障排除方面。
Netstat 和类似的网路监控工具,在 net-tools 软件包中组合在一起,显示有关活动网络连接的信息。由于在开放端口上运行的服务通常容易受到攻击,因此定期进行网络监控可以帮助您及早发现可疑活动。
安装 netstat
Netstat 通常预装在 Linux 发行版中。如果您的服务器上未安装 netstat,请使用您的软件包管理器安装它。在基于 Debian 的系统上
$ sudo apt-get install net-tools
对于基于 Fedora 的系统
$ dnf install net-tools
使用 netstat
在没有其他选项的情况下,netstat
命令会显示所有已建立的连接。您可以使用上面的 netstat
选项来进一步指定所需的输出。例如,要显示所有监听和非监听连接,请使用 --all
(简写为 -a
)选项。这将返回大量结果,因此在本例中,我将输出通过管道传递给 head
以仅显示输出的前 15 行
$ netstat --all | head -n 15
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:27036 *:* LISTEN
tcp 0 0 localhost:27060 *:* LISTEN
tcp 0 0 *:16001 *:* LISTEN
tcp 0 0 localhost:6463 *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 localhost:57343 *:* LISTEN
tcp 0 0 *:ipp *:* LISTEN
tcp 0 0 *:4713 *:* LISTEN
tcp 0 0 10.0.1.222:48388 syd15s17-in-f5.1e:https ESTABLISHED
tcp 0 0 10.0.1.222:48194 ec2-35-86-38-2.us:https ESTABLISHED
tcp 0 0 10.0.1.222:56075 103-10-125-164.va:27024 ESTABLISHED
tcp 0 0 10.0.1.222:46680 syd15s20-in-f10.1:https ESTABLISHED
tcp 0 0 10.0.1.222:52730 syd09s23-in-f3.1e:https ESTABLISHED
要仅显示 TCP 端口,请使用 --all
和 --tcp
选项,或简写为 -at
$ netstat -at | head -n 5
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:27036 *:* LISTEN
tcp 0 0 localhost:27060 *:* LISTEN
tcp 0 0 *:16001 *:* LISTEN
要仅显示 UDP 端口,请使用 --all
和 --udp
选项,或简写为 -au
$ netstat -au | head -n 5
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 *:27036 *:*
udp 0 0 10.0.1.222:44741 224.0.0.56:46164 ESTABLISHED
udp 0 0 *:bootpc
netstat 的选项通常很直观。例如,要显示所有监听 TCP 和 UDP 端口以及进程 ID (PID) 和数字地址
$ sudo netstat --tcp --udp --listening --programs --numeric
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Addr State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 2500/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1726/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1721/cupsd
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 4023/sshd: tux@
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::22 :::* LISTEN 1726/sshd
tcp6 0 0 ::1:631 :::* LISTEN 1721/cupsd
tcp6 0 0 ::1:6010 :::* LISTEN 4023/sshd: tux@
udp 0 0 0.0.0.0:40514 0.0.0.0:* 1499/avahi-daemon:
udp 0 0 192.168.122.1:53 0.0.0.0:* 2500/dnsmasq
udp 0 0 0.0.0.0:67 0.0.0.0:* 2500/dnsmasq
udp 0 0 0.0.0.0:111 0.0.0.0:* 1/systemd
udp 0 0 0.0.0.0:5353 0.0.0.0:* 1499/avahi-daemon:
udp6 0 0 :::111 :::* 1/systemd
udp6 0 0 :::44235 :::* 1499/avahi-daemon:
udp6 0 0 :::5353 :::* 1499/avahi-daemon:
此常用组合的简写版本是 -tulpn
。
要显示有关特定服务的信息,请使用 grep
过滤
$ sudo netstat -anlp | grep cups
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1721/cupsd tcp6 0 0 ::1:631 :::* LISTEN 1721/cupsd
unix 2 [ ACC ] STREAM LISTENING 27251 1/systemd /var/run/cups/cups.sock
unix 2 [ ] DGRAM 59530 1721/cupsd
unix 3 [ ] STREAM CONNECTED 55196 1721/cupsd /var/run/cups/cups.sock
下一步
运行 netstat
命令后,您可以采取措施来保护您的系统,方法是确保只有您主动使用的服务在您的网络上监听。
- 识别常用的被利用的端口和服务。作为一般规则,关闭您实际不使用的端口。
- 注意不常见的端口号,并学习识别系统中正在使用的合法端口。
- 密切关注 SELinux 错误。有时您只需要更新上下文以匹配您对系统进行的合法更改,但请阅读错误信息以确保 SELinux 没有警告您可疑或恶意活动。
如果您发现某个端口正在运行可疑服务,或者您只是想关闭不再使用的端口,您可以通过以下步骤手动通过防火墙规则拒绝端口访问
如果您正在使用 firewall-cmd
,请运行以下命令
$ sudo firewall-cmd –remove-port=<port number>/tcp
$ sudo firewall-cmd –runtime-to-permanent
如果您正在使用 UFW,请运行以下命令
$ sudo ufw deny <port number>
接下来,使用 systemctl
停止服务本身
$ systemctl stop <service>
学习 netstat
Netstat 是一个有用的工具,可以快速收集有关服务器网络连接的信息。定期网络监控是了解您的系统的重要组成部分,它可以帮助您保持系统的安全。要将此步骤纳入您的管理例程,您可以使用网络监控工具(如 netstat 或 ss)以及开源端口扫描器(如 Nmap)或嗅探器(如 Wireshark),这些工具允许计划任务。
随着服务器存储越来越多的个人数据,确保个人服务器的安全性变得越来越重要。通过了解您的服务器如何连接到互联网,您可以降低机器的漏洞,同时仍然可以从数字时代日益增长的连接性中受益。
评论已关闭。