port 端口

ss

  • 推荐,比 netstat 更快。现代 Linux 发行版的首选工具,效率高。
  • 参数:
    • -t: TCP协议
    • -l: 监听中的端口
    • -n: 显示端口号 (而非服务名)
    • -p: 显示进程信息
# 查看所有端口
ss -tlnp
# 搜索指定端口
ss -tlnp | grep :8080

netstat

  • 传统工具,仍广泛使用
# Ubuntu/Debian 安装
sudo apt update
sudo apt install net-tools
# CentOS/RHEL/Fedora
sudo yum install net-tools
# sudo dnf install net-tools
# 查看所有端口
netstat -tlnp
# 搜索指定端口
netstat -tlnp | grep :8080

lsof

  • 功能强大,侧重文件与进程
  • 参数:
    • -i :端口号: 查看指定端口
    • -iTCP: 仅查看TCP
    • -sTCP:LISTEN: 仅查看监听状态
    • -n/-P: 不解析主机名/端口名
# 查看所有端口
lsof -i
# lsof -iTCP -sTCP:LISTEN -nP
# 搜索指定端口
lsof -i :8080