SE업무/OS

Netstat 명령어

에륜 2021. 6. 6. 14:32
반응형

Netstat 명령어란 리눅스,윈도우에 쓰이며

현재 서버의 네트워크 통신상태를 확인할때 쓰이는 명령어입니다.

오늘은 리눅스에서 사용할때에대해 이야기하겠습니다.

 

리눅스 최소설치를 진행한 경우 없을수도있으니

yum install net-tools

이용하여 설치를 진행하자.

 

어떤 옵션이 있는지 확인하자.

[root@infra-01 ~]# netstat -?
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)

기본적인 프로그램인 만큼 다양한 옵션들이 있다.

옵션없이 netstat만 쓴다면 많은 프로세스가나와 확인하기 힘들다.

 

주로 많이쓰는 옵션들을 소개하겠다.

-n : ssh나 http 처럼 알려진 이름대신 22,80 와 같이 숫자로 표기하는 옵션이다.

-l : 현재 리스닝중인 서버 소켓으 보여준다

-t : tcp 포트를 보여준다.

-p : 그 소켓의 PID와 프로그램명을 보여준다.

 

이렇게 -nltp를 합쳐서쓰면

[root@infra-01 ~]# netstat -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      676/rpcbind
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      985/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1310/master
tcp6       0      0 :::111                  :::*                    LISTEN      676/rpcbind
tcp6       0      0 :::22                   :::*                    LISTEN      985/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      1310/master

22,111번 포트가 돌아가는게 보인데 왜 유용하냐면

예를 들어 서버를 처음 접속하게되면 그 서버가 어떤 서비스를 실행시키는지 확인해야한다.

 

[root@infra-01 ~]# netstat -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      676/rpcbind
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1727/nginx: master
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      985/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1310/master
tcp6       0      0 :::111                  :::*                    LISTEN      676/rpcbind
tcp6       0      0 :::22                   :::*                    LISTEN      985/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      1310/master
[root@infra-01 ~]#

 

nginx를 설치하고 서비스를 실행하였다.

80포트가 생긴것을 알 수 있다. 이로서 우리는 처음접속하는 서버이지만 이 서버가 nginx가 돌아가고 웹서버or프록시를 하고 있겠구나라고 예상할 수 있다.

 

udp를 확인하고싶으면 t대신 u를 사용해서 확인하면된다.

 

본인이 관리하는 서비스가 아닌 다른분이 관리하는 서버를 대타로 작업할때 항상쓰는 명령어이다.

서버를 처음 접속할때 netstat를 사용하여 용도,도커를 사용하는지를 파악하고 빠르게 접근가능하다.

반응형