Loading

网络连接查询命令:netstat(Linux)

一、前言

基于tcp或udp的应用程序启动后,想要查看程序对应的端口号是否打开。

netstat 是一个告诉我们操作系统中所有 tcp/udp/unix socket 连接状态的命令行工具

二、netstat

netstat 命令是一个监控 TCP/IP网络 的非常有用的工具,它可以显示 路由表、实际的网络连接 以及 每一个网络接口设备的状态信息

  • 查看所有tcp端口

    netstat -ant

  • 查看所有udp端口

    netstat -anu

  • 查看具体tcp端口

    已知 ssh 默认端口是 22 ,基于 tcp 协议

    netstat -ant | grep 22

  • 查看所有的tcp以及对应的PID/Program name

    netstat -antp

帮助文档

[root@localhost ~]# netstat --help
usage: netstat [-vWeenNcCF] [<Af>] -r         netstat {-V|--version|-h|--help}
       netstat [-vWnNcaeol] [<Socket> ...]
       netstat { [-vWeenNac] -I[<Iface>] | [-veenNac] -i | [-cnNe] -M | -s [-6tuw] } [delay]

        -r, --route              display routing table
        -I, --interfaces=<Iface> display interface table for <Iface>
        -i, --interfaces         display interface table
        -g, --groups             display multicast group memberships
        -s, --statistics         display networking statistics (like SNMP)
        -M, --masquerade         display masqueraded connections

        -v, --verbose            be verbose
        -W, --wide               don't truncate IP addresses
        -n, --numeric            don't resolve names
        --numeric-hosts          don't resolve host names
        --numeric-ports          don't resolve port names
        --numeric-users          don't resolve user names
        -N, --symbolic           resolve hardware names
        -e, --extend             display other/more information
        -p, --programs           display PID/Program name for sockets
        -o, --timers             display timers
        -c, --continuous         continuous listing

        -l, --listening          display listening server sockets
        -a, --all                display all sockets (default: connected)
        -F, --fib                display Forwarding Information Base (default)
        -C, --cache              display routing cache instead of FIB
        -Z, --context            display SELinux security context for sockets

  <Socket>={-t|--tcp} {-u|--udp} {-U|--udplite} {-S|--sctp} {-w|--raw}
           {-x|--unix} --ax25 --ipx --netrom
  <AF>=Use '-6|-4' or '-A <af>' or '--<af>'; default: inet
  List of possible address families (which support routing):
    inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25) 
    netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP) 
    x25 (CCITT X.25) 
posted @ 2023-01-28 10:07  eiSouthBoy  阅读(305)  评论(0)    收藏  举报