리눅스 포트 오픈 - linugseu poteu opeun

안녕하세요? 서버 개발을 진행하면서 포트 및 방화벽에 대해서 설정할 일이 많아 정리를 해보았습니다.
개발하시는데 다들 도움이 되셨으면 좋겠습니다.

A. 포트 상태 확인

1. 열려있는 모든 포트 표시
$ netstat -nap

-n: host명으로 표시 안함
-a: 모든소켓 표시
-p: 프로세스ID와 프로그램명 표시 

2. LISTEN중인 포트 표시
$ netstat -nap | grep LISTEN

3. 확인하려는 포트번호 상태확인
$ netstat -nap | grep 포트번호

4. 특정 호스트 포트 상태 확인
특정 호스트로 접속이 불가할 때, netcat(nc) 네트워크 유틸리티를 이용하여 포트가 막혀 있는지 확인할 수 있다.

4.1 특정 포트 상태 확인
$ nc -z 호스트주소 포트
$ nc -z www.google.com 80 

Sample Result)

Connection to www.google.com 80 port [tcp/http] succeeded! 

4.2 특정 호스트의 포트 범위를 지정하여 열린 포트 확인
$ nc 호스트주소 -z 시작포트-끝포트
$ nc 10.20.30.40 -z 19-21

Sample Result)

Connection to 10.20.30.40 21 port [tcp/ftp] succeeded!
Connection to 10.20.30.40 22 port [tcp/ssh] succeeded!
Connection to 10.20.30.40 23 port [tcp/telnet] succeeded! 

B. 포트 열기
리눅스 방화벽 설정 명령어인 iptables을 사용하여 포트를 오픈할 수 있다.

  방화벽 설정 정보 확인하기
# iptables -nL

1. 특정포트 외부에서 접속할 수 있도록 열기 (외부에서 접속할 수 있도록 포트 OPEN)
아래는 외부에서 들어오는(INBOUND) TCP포트 12345의 연결을 받아들인다는 규칙을 1번 방화벽 규칙으로 추가한다는 의미이다.

1.1 TCP PORT일 경우
# iptables -I INPUT 1 -p tcp --dport 12345 -j ACCEPT

1.2 UDP PORT일 경우
# iptables -I INPUT 1 -p udp --dport 12345 -j ACCEPT

-I: 새로운 규칙을 추가한다.
-p: 패킷의 프로토콜을 명시한다.
-j: 규칙에 해당되는 패킷을 어떻게 처리할지를 정한다. 

 내부에서 외부로 나갈 수 있도록 포트 열기

# iptables -I OUTPUT 1 -p tcp --dport 9002 -j ACCEPT
# iptables -I OUTPUT 1 -p udp --dport 9002 -j ACCEPT

2. 추가한 설정 조회
# iptables -L -v

3. 추가한 설정 삭제
a) 추가한 규칙의 번호로 삭제하는 방법과 
b) 추가했을 때의 명령어에서 "-I"를 "-D"로 바꾸어 주는 방법이 있다.

a) 규칙번호로 삭제
# iptables -D INPUT 1

b) 추가한 규칙으로 삭제
# iptables -D INPUT -p tcp --dport 12345 -j ACCEPT
# iptables -D INPUT -p udp --dport 12345 -j ACCEPT

4. 변경사항 저장
# service iptables save
# /etc/init.d/iptables restart

C. 방화벽 활성화&비활성화

1. 켜기
# /etc/init.d/iptables start

2. 끄기
# /etc/init.d/iptables stop

------------------------------------------------------------------------------------------
도움 되셨다면 하단의 광고 클릭 해주시면 감사하겠습니다:)

리눅스서버에서 리모트서버로 포트가 오픈되어 있는지 확인하는 2가지 방법에 대해 알아보겠습니다.

1) nc (넷캣, Netcat) 추천!!!

실습으로 리모트 서버 10.0.0.1, 8883 포트로 확인해보겠습니다.

[포트 오픈된 경우]

$ nc -vz 10.0.0.1 8883 [결과] Connection to 10.0.0.1 8883 port [tcp/*] succeeded!

[포트 미오픈된 경우] (리모트 서버에서 서비스를 하고 있는지 또는 방화벽 설정을 확인하세요)

$ nc -vz 10.0.0.1 8884 [결과] connect to 10.0.0.1 port 8884 (tcp) failed: Connection refused 또는 connection to 10.0.0.1 failed: Connection timed out.

실습으로 리모트 서버 10.0.0.1, 8883 포트로 확인해보겠습니다.

[포트 오픈된 경우]

$ telnet 10.0.0.1 8883 Trying 10.0.0.1... Connected to 127.0.0.1.

[포트 미오픈된 경우] (리모트 서버에서 서비스를 하고 있는지 또는 방화벽 설정을 확인하세요)

$ telnet 10.0.0.1 8884 Trying 10.0.0.1... telnet: Unable to connect to remote host: Connection refused

끝.

nc 명령어 도움말입니다. 참고하세요.

usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl] [-m minttl] [-O length] [-P proxy_username] [-p source_port] [-q seconds] [-s source] [-T keyword] [-V rtable] [-W recvlimit] [-w timeout] [-X proxy_protocol] [-x proxy_address[:port]] [destination] [port] * Port numbers can be individual or ranges: lo-hi [inclusive] Command Summary: -4 Use IPv4 -6 Use IPv6 -b Allow broadcast -C Send CRLF as line-ending -D Enable the debug socket option -d Detach from stdin -F Pass socket fd -h This help text -I length TCP receive buffer length -i interval Delay interval for lines sent, ports scanned -k Keep inbound sockets open for multiple connects -l Listen mode, for inbound connects -M ttl Outgoing TTL / Hop Limit -m minttl Minimum incoming TTL / Hop Limit -N Shutdown the network socket after EOF on stdin -n Suppress name/port resolutions -O length TCP send buffer length -P proxyuser Username for proxy authentication -p port Specify local port for remote connects -q secs quit after EOF on stdin and delay of secs -r Randomize remote ports -S Enable the TCP MD5 signature option -s source Local source address -T keyword TOS value -t Answer TELNET negotiation -U Use UNIX domain socket -u UDP mode -V rtable Specify alternate routing table -v Verbose -W recvlimit Terminate after receiving a number of packets -w timeout Timeout for connects and final net reads -X proto Proxy protocol: "4", "5" (SOCKS) or "connect" -x addr[:port] Specify proxy address and port -Z DCCP mode -z Zero-I/O mode [used for scanning]