前言与netstat的弃用现状
前言
在Linux服务器的日常运维与管理中,netstat是排查网络问题的核心工具之一。笔者常借助该命令检查当前服务器上哪些进程正在监听端口,以此评估网络服务的运行状态。
然而,近期在部署Ubuntu发行版时,发现系统默认并未预装netstat。手动安装后查阅手册页(man pages),得知该命令已被标记为deprecated(已弃用),官方推荐使用ss命令作为替代方案。
This program is mostly obsolete. Replacement for netstat is ss. Replacement for netstat -r is ip route. Replacement for netstat -i is ip -s link. Replacement for netstat -g is ip maddr.
netstat man pages
netstat 的用法
netstat拥有众多参数,笔者通常只使用一组固定组合,以至于后来连具体参数的含义都难以回忆起来:
netstat -npl
执行上述命令后,输出结果如下:
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 655/systemd-resolve
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 890/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 30790/cupsd
tcp 0 0 0.0.0.0:18025 0.0.0.0:* LISTEN 890/sshd
tcp6 0 0 :::22 :::* LISTEN 890/sshd
tcp6 0 0 ::1:631 :::* LISTEN 30790/cupsd
tcp6 0 0 :::9090 :::* LISTEN 15415/./prometheus
tcp6 0 0 :::18025 :::* LISTEN 890/sshd
udp 0 0 127.0.0.53:53 0.0.0.0:* 655/systemd-resolve
udp 0 0 0.0.0.0:631 0.0.0.0:* 30792/cups-browsed
udp 0 0 0.0.0.0:5353 0.0.0.0:* 757/avahi-daemon: r
udp 0 0 0.0.0.0:42360 0.0.0.0:* 757/avahi-daemon: r
udp6 0 0 :::58232 :::* 757/avahi-daemon: r
udp6 0 0 :::5353 :::* 757/avahi-daemon: r
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node PID/Program name Path
unix 2 [ ACC ] STREAM LISTENING 35116 1304/gnome-session- @/tmp/.ICE-unix/1304
unix 2 [ ACC ] SEQPACKET LISTENING 1448 1/init /run/udev/control
unix 2 [ ACC ] STREAM LISTENING 34277 1270/systemd /run/user/1000/systemd/private
unix 2 [ ACC ] STREAM LISTENING 34282 1270/systemd /run/user/1000/gnupg/S.gpg-agent.ssh
unix 2 [ ACC ] STREAM LISTENING 33510 1270/systemd /run/user/1000/gnupg/S.gpg-agent
unix 2 [ ACC ] STREAM LISTENING 33511 1270/systemd /run/user/1000/pulse/native
unix 2 [ ACC ] STREAM LISTENING 33512 1270/systemd /run/user/1000/gnupg/S.gpg-agent.extra
这组命令是最常用的组合,输出分为两部分:第一部分展示TCP和UDP协议的监听状态,第二部分展示Unix socket文件的监听情况。其中,参数-n表示以数字格式显示IP地址,避免解析主机名或域名;参数-p用于显示关联的进程名称(部分情况下可能无法显示);参数-l则专门筛选处于LISTENING状态的Socket。
通过该命令,可以查看系统中所有打开的Socket。无论是启动现有网络服务,还是开发自定义网络服务并开放端口,均可在此看到相应记录。若未观察到预期端口,则说明端口未能正确开启,需进一步排查原因。因此,这是一个极为实用的调试工具。
ss 的用法
前文介绍了netstat最基础的用法,尽管其功能丰富,但此处暂不展开。若要使用ss命令实现类似效果,应如何操作?
ss -atlp
这是笔者自行摸索出的一组参数组合,目前记忆尚不牢固,每次使用时仍需查阅文档:
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.53%lo:domain 0.0.0.0:* users:(("systemd-resolve",pid=655,fd=13))
LISTEN 0 128 0.0.0.0:ssh 0.0.0.0:* users:(("sshd",pid=890,fd=5))
LISTEN 0 5 127.0.0.1:ipp 0.0.0.0:* users:(("cupsd",pid=30790,fd=7))
LISTEN 0 128 0.0.0.0:18025 0.0.0.0:* users:(("sshd",pid=890,fd=3))
LISTEN 0 128 [::]:ssh [::]:* users:(("sshd",pid=890,fd=6))
LISTEN 0 5 [::1]:ipp [::]:* users:(("cupsd",pid=30790,fd=6))
LISTEN 0 128 *:9090 *:* users:(("prometheus",pid=15415,fd=3))
LISTEN 0 128 [::]:18025 [::]:* users:(("sshd",pid=890,fd=4))
ss命令的输出结果如上所示。可以看出,其格式与netstat截然不同,不如后者紧凑直观。这也是许多用户批评该命令的主要原因之一。当然,也有观点认为这种批评源于用户对旧习惯的固守,不愿接受新事物。此言虽有理,但以笔者为例,自2010年参加工作以来,一直使用netstat近十年,早已习以为常,看起自然格外顺眼。
此外,有国外用户提出ss命名不佳的理由。ss可能是“socket statistics”的缩写,但仅两个字母难以联想,不如netstat直观。当然,这是笔者的解读,并非外方抱怨的真实原因。他们抱怨的是,提及“SS”时,往往会联想到希特勒及其领导的党卫队(Schutzstaffel)。这一点令人匪夷所思。作为80后,笔者对此并无深刻印象,且中文语境下大家更可能联想到其他含义。但该纳粹组织的德语简称确为SS。
闲话少叙,回归技术细节。参数-a表示显示所有连接,-t表示仅显示TCP协议,-l代表处于LISTENING状态的连接,-p显示进程信息。对比前述表格,ss输出的进程信息虽比netstat更为详尽,包含了进程名、PID及文件描述符(FD),但采用了双重小括号包裹的key/value格式并加引号,视觉上显得杂乱。虽然可通过其他命令进行格式化,但操作较为繁琐。
更换的原因是什么?
这是笔者最为好奇的问题。然而,网上搜索到的资料大多语焉不详,令笔者感到无奈。
总体而言,主要趋势是net-tools包将被iproute2包所取代。理由大致包括:1. net-tools包年代久远;2. 不支持Linux内核的新特性(具体未指明);3. 界面优化不足,自动化集成困难,对命令行不够友好;4. 其中的ifconfig命令存在诸多缺陷;5. 未来不再计划维护net-tools。
Luk Claes and me, as the current maintainers of net-tools, we've been thinking about it's future. Net-tools has been a core part of Debian and any other linux based distro for many years, but it's showing its age.
It doesnt support many of the modern features of the linux kernel, the interface is far from optimal and difficult to use in automatisation, and also, it hasn't got much love in the last years.
On the other side, the iproute suite, introduced around the 2.2 kernel line, has both a much better and consistent interface, is more powerful, and is almost ten years old, so nobody would say it's untested.
Hence, our plans are to replace net-tools completely with iproute, maybe leading the route for other distributions to follow. Of course, most people and tools use and remember the venerable old interface, so the first step would be to write wrappers, trying to be compatible with net-tools.
At the same time, we believe that most packages using net-tools should be patched to use iproute instead, while others can continue using the wrappers for some time. The ifupdown package is obviously the first candidate, but it seems that a version using iproute has been available in experimental since 2007.
https://serverfault.com/questions/633087/where-is-the-statement-of-deprecation-of-ifconfig-on-linux
从原理层面分析,现有的netstat和ifconfig命令通过读写/proc目录下的虚拟文件来完成任务。这在小型业务系统中尚可,但在大规模系统中可能影响性能。相比之下,ss和ip命令利用Linux内核的Netlink sockets特性,实现了根本性的不同。虽然老命令也可用新原理重写,但因不同程序员团体间的政治因素及意见分歧,无人愿意这么做。
更深层次的原因在于,使用此类调试命令的本质目的是获取内核状态。事实上,内核已彻底改变了Networking模块的原理,而用户仍要求命令以旧方式展示信息,导致展示格式与真实原理背离。从长远看,替代这两个旧命令是必然趋势。
结论
技术人员应保持与时俱进的态度。尽管旧命令熟悉好用、信手拈来,甚至难以遗忘,但仍需保持对新事物的学习热情。许多发行版已默认不再包含net-tools包,虽可手动安装,但背后的态度已十分明确。此外,做技术需防止思维僵化,应对新事物保持好奇心与热情。
好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对本站的支持。