본문 바로가기

IT/리눅스

리눅스 리스닝 포트, 연결된 포트 확인하기(netstat, listen state port, connected state port)

반응형

개발이나 시스템 관리를 하다보면 현재 사용중인 서버에서 외부 접속 대기를 위한 리슨 포트가 제대로 열려 있는지 혹은 원하는 포트에 연결이 제대로 되어 있는지 확인하고 싶을 때가 있습니다. 

 

이럴 때 간단하게 사용하는 간단한 cmd가 있습니다. 

 

1. 리스닝 포트 확인하기 

 

 netstat -lnp

 

[root@localhost ~]# netstat -lnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:4330          0.0.0.0:*               LISTEN      3462/pmlogger
tcp        0      0 0.0.0.0:139             0.0.0.0:*               LISTEN      2021/smbd
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
tcp        0      0 0.0.0.0:7860            0.0.0.0:*               LISTEN      35484/python
tcp        0      0 0.0.0.0:7861            0.0.0.0:*               LISTEN      563865/python
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      2922/dnsmasq
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1579/sshd
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1572/cupsd
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      938229/sshd: root@p
tcp        0      0 127.0.0.1:6011          0.0.0.0:*               LISTEN      939736/sshd: root@p
tcp        0      0 0.0.0.0:1020            0.0.0.0:*               LISTEN      20698/sshd
tcp        0      0 0.0.0.0:445             0.0.0.0:*               LISTEN      2021/smbd
tcp        0      0 127.0.0.1:44321         0.0.0.0:*               LISTEN      2336/pmcd
tcp        0      0 0.0.0.0:1122            0.0.0.0:*               LISTEN      735328/sshd
tcp        0      0 127.0.0.1:2023          0.0.0.0:*               LISTEN      738385/python3

:

:

Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path
unix  2      [ ACC ]     STREAM     LISTENING     26889    1/systemd            /run/avahi-daemon/socket
unix  2      [ ACC ]     STREAM     LISTENING     26896    1/systemd            /var/run/docker.sock
unix  2      [ ACC ]     STREAM     LISTENING     50451    1452/lsmd            /var/run/lsm/ipc/simc
unix  2      [ ACC ]     STREAM     LISTENING     26899    1/systemd            /run/libvirt/virtlockd-sock
unix  2      [ ACC ]     STREAM     LISTENING     50453    1452/lsmd            /var/run/lsm/ipc/sim
unix  2      [ ACC ]     STREAM     LISTENING     28940    1455/sssd            /var/lib/sss/pipes/private/sbus-monitor
unix  2      [ ACC ]     STREAM     LISTENING     26903    1/systemd            /run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     272544   28783/Xorg           /tmp/.X11-unix/X0
unix  2      [ ACC ]     STREAM     LISTENING     41125    1533/sssd_be         /var/lib/sss/pipes/private/sbus-dp_implicit_files.1533
unix  2      [ ACC ]     STREAM     LISTENING     26907    1/systemd            /run/libvirt/virtlogd-sock

:

:

 

 

2. 연결된 포트  확인하기  

 

 netstat -np

 

[root@localhost ~]# netstat -np
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 192.168.10.218:445      192.168.10.111:1328     ESTABLISHED 941288/smbd
tcp        0      0 192.168.10.218:445      192.168.10.60:3386      ESTABLISHED 943120/smbd
tcp        0      0 192.168.10.218:22       192.168.10.111:11917    ESTABLISHED 938195/sshd: root [
tcp        0      0 192.168.10.218:445      192.168.10.58:64413     ESTABLISHED 942261/smbd
tcp        0      0 192.168.10.218:445      192.168.10.58:49675     ESTABLISHED 942261/smbd
tcp        0      0 192.168.10.218:22       192.168.10.111:13641    ESTABLISHED 939732/sshd: root [
tcp        0      0 192.168.10.218:445      192.168.10.58:50240     ESTABLISHED 944327/smbd
tcp       25      0 192.168.10.218:51092    104.18.6.192:443        CLOSE_WAIT  563865/python
tcp       25      0 192.168.10.218:41136    151.101.65.91:443       CLOSE_WAIT  28933/gnome-shell
tcp        0    272 192.168.10.218:22       192.168.10.25:8820      ESTABLISHED 944697/sshd: root [

:

:

Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path

unix  3      [ ]         STREAM     CONNECTED     280918   28933/gnome-shell
unix  3      [ ]         STREAM     CONNECTED     202347   1/systemd            /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     261628   28909/dbus-daemon    @/tmp/dbus-spMuX2VqwU
unix  3      [ ]         STREAM     CONNECTED     37537    1540/platform-pytho
unix  3      [ ]         STREAM     CONNECTED     15982    1452/lsmd
unix  3      [ ]         STREAM     CONNECTED     249554   29042/gsd-wacom
unix  3      [ ]         STREAM     CONNECTED     44998    2336/pmcd
unix  3      [ ]         STREAM     CONNECTED     249551   29042/gsd-wacom
unix  3      [ ]         STREAM     CONNECTED     202343   1/systemd            /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     40204    1441/dbus-daemon     /run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     43207    1/systemd            /run/systemd/journal/stdout

:

:

 

원하는 포트만 보려면 grep 과 함께 사용하면 되겠죠?

 

[root@localhost ~]# netstat -np |grep -w 22
tcp        0      0 192.168.10.218:22       192.168.10.111:11917    ESTABLISHED 938195/sshd: root [
tcp        0      0 192.168.10.218:22       192.168.10.111:13641    ESTABLISHED 939732/sshd: root [
tcp        0     64 192.168.10.218:22       192.168.10.25:8820      ESTABLISHED 944697/sshd: root [

반응형