暂无图片
暂无图片
1
暂无图片
暂无图片
暂无图片

使用 netstat 查看你的 Linux 服务器网络连接

原创 愤怒的蜗牛 2022-12-05
1006

安装 netstat

netstat 经常预装在 Linux 发行版上。如果 netstat 没有在你的服务器上安装,用你的包管理器安装它。下面是在基于 Debian 的系统上:

  1. $ sudo apt-get install net-tools

在基于 Fedora 的系统上:

  1. $ dnf install net-tools

使用 netstat

就其本身而言,netstat 命令显示了全部建立成功的连接。你可以使用 netstat 的参数指定进一步预期的输出。举个例子,要显示所有监听和非监听的连接,使用 --all-a 为简写)的参数。这将返回许多结果,所以在例子中我用管道符输出给 head 命令来显示输出的前 15 行:

  1. $ netstat --all | head -n 15
  2. Active Internet connections (servers and established)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State
  4. tcp 0 0 *:27036 *:* LISTEN
  5. tcp 0 0 localhost:27060 *:* LISTEN
  6. tcp 0 0 *:16001 *:* LISTEN
  7. tcp 0 0 localhost:6463 *:* LISTEN
  8. tcp 0 0 *:ssh *:* LISTEN
  9. tcp 0 0 localhost:57343 *:* LISTEN
  10. tcp 0 0 *:ipp *:* LISTEN
  11. tcp 0 0 *:4713 *:* LISTEN
  12. tcp 0 0 10.0.1.222:48388 syd15s17-in-f5.1e:https ESTABLISHED
  13. tcp 0 0 10.0.1.222:48194 ec2-35-86-38-2.us:https ESTABLISHED
  14. tcp 0 0 10.0.1.222:56075 103-10-125-164.va:27024 ESTABLISHED
  15. tcp 0 0 10.0.1.222:46680 syd15s20-in-f10.1:https ESTABLISHED
  16. tcp 0 0 10.0.1.222:52730 syd09s23-in-f3.1e:https ESTABLISHED

要只显示 TCP 端口,使用 --all 和 --tcp 参数,或者简写成 -at :

  1. $ netstat -at | head -n 5
  2. Active Internet connections (servers and established)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State
  4. tcp 0 0 *:27036 *:* LISTEN
  5. tcp 0 0 localhost:27060 *:* LISTEN
  6. tcp 0 0 *:16001 *:* LISTEN

要只显示 UDP 端口,使用 --all 和 --udp 参数,或者简写成 -au :

  1. $ netstat -au | head -n 5
  2. Active Internet connections (servers and established)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State
  4. udp 0 0 *:27036 *:*
  5. udp 0 0 10.0.1.222:44741 224.0.0.56:46164 ESTABLISHED
  6. udp 0 0 *:bootpc

netstat 命令参数常常是简单易懂的。举个例子,要显示带有全部进程 ID(PID)和数字地址的监听 TCP 和 UDP 的端口:

  1. $ sudo netstat --tcp --udp --listening --programs --numeric
  2. Active Internet connections (only servers)
  3. Proto Recv-Q Send-Q Local Address Foreign Addr State PID/Program name
  4. tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
  5. tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 2500/dnsmasq
  6. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1726/sshd
  7. tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1721/cupsd
  8. tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 4023/sshd: tux@
  9. tcp6 0 0 :::111 :::* LISTEN 1/systemd
  10. tcp6 0 0 :::22 :::* LISTEN 1726/sshd
  11. tcp6 0 0 ::1:631 :::* LISTEN 1721/cupsd
  12. tcp6 0 0 ::1:6010 :::* LISTEN 4023/sshd: tux@
  13. udp 0 0 0.0.0.0:40514 0.0.0.0:* 1499/avahi-daemon:
  14. udp 0 0 192.168.122.1:53 0.0.0.0:* 2500/dnsmasq
  15. udp 0 0 0.0.0.0:67 0.0.0.0:* 2500/dnsmasq
  16. udp 0 0 0.0.0.0:111 0.0.0.0:* 1/systemd
  17. udp 0 0 0.0.0.0:5353 0.0.0.0:* 1499/avahi-daemon:
  18. udp6 0 0 :::111 :::* 1/systemd
  19. udp6 0 0 :::44235 :::* 1499/avahi-daemon:
  20. udp6 0 0 :::5353 :::* 1499/avahi-daemon:

这个常用组合简写版本是 -tulpn 。

要显示一个指定服务的信息,使用 grep 命令过滤

  1. $ sudo netstat -anlp | grep cups
  2. tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1721/cupsd tcp6 0 0 ::1:631 :::* LISTEN 1721/cupsd
  3. unix 2 [ ACC ] STREAM LISTENING 27251 1/systemd /var/run/cups/cups.sock
  4. unix 2 [ ] DGRAM 59530 1721/cupsd
  5. unix 3 [ ] STREAM CONNECTED 55196 1721/cupsd /var/run/cups/cups.sock

接下来的步骤

一旦你运行了 netstat 命令,你就可以采取措施来保护你的系统,确保只有你主动使用的服务在你的网络上被监听。

  1. 识别通常被利用的端口和服务。一般来说,关闭你实际不使用的端口。
  2. 留意不常见的端口号,认识了解在你系统上使用的合法端口。
  3. 密切注意 SELinux 错误。有时你需要做的只是更新上下文,以匹配你对系统做的合法更改,但是要阅读错误警告,以确保 SELinux 提醒你的不是可疑或者恶意的活动。

如果你发现一个端口正在运行一个可疑的服务,或者你只是简单的想要关闭你不再使用的端口,你可以遵从以下这些步骤,通过防火墙规则手动拒绝端口访问:

如果你在使用 firewall-cmd ,运行这些命令:

  1. $ sudo firewall-cmd –remove-port=<port number>/tcp
  2. $ sudo firewall-cmd –runtime-to-permanent

如果你在使用 UFW,运行以下的命令:

  1. $ sudo ufw deny <port number>

下一步,使用 systemctl 来停止这个服务:

  1. $ systemctl stop <service>

理解 netstat

netstat 是一个快速收集你的服务器网络连接信息的有用工具。定期网络监测是了解你的系统的重要组成部分,对帮助你保持你的系统安全有着重要意义。将这一步纳入你的日常管理中,你可以使用类似 netstat 或者 ss ,以及 Nmap 等开源端口扫描器或者 Wireshark 等嗅探器 ,它们都允许设定 计划任务

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论