How to open and close port in Linux?

DaRecordon

Well-known member
Registered
Joined
Oct 7, 2016
Messages
247
Points
18
I want to open or close a port in linux which is in listening mode between my client and server application. Is there any manual command line option in Linux to close or open a port? btw, how to check a port is being opened/closed or not? many thanks!
 

racksandcloud

Well-known member
Registered
Joined
May 18, 2017
Messages
89
Points
0
You can use the below command to find which all ports are listening.

netstat -lntup

-p = name of the program
-t = tcp ports
-u = udp ports
-l = only services which are listening on some port
-n = show port number, don't try to resolve the service namerewall

If you wants to open an incoming TCP port in Linux iptables firewall,

iptables -I INPUT -p tcp --dport 5555 --syn -j ACCEPT

If you want to open a UDP port

iptables -I INPUT -p udp --dport 5555 -j ACCEPT

service iptables save

for blocking a port

iptables -A OUTPUT -p tcp --dport 5555 -j DROP
 

djsmiley2k

Well-known member
Registered
Joined
Jul 10, 2016
Messages
198
Points
18

DaRecordon

Well-known member
Registered
Joined
Oct 7, 2016
Messages
247
Points
18
DaRecordon

Thanks for a detailed guide, from your commands if i want to open port 666 I need to run both these commands?

Code:
iptables -I INPUT -p tcp --dport 666 --syn -j ACCEPT

iptables -I INPUT -p udp --dport 666 -j ACCEPT
I also want to know what a UDP port or TCP port is.
 

Kaz Wolfe

Well-known member
Registered
Joined
Jul 7, 2016
Messages
614
Points
28
Kaz Wolfe
I ran this command on my vps

Code:
[root@srv ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             Stat                                                                     e       PID/Program name
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LIST                                                                     EN      1492/sshd
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LIST                                                                     EN      1571/master
tcp        0      0 :::22                       :::*                        LIST                                                                     EN      1492/sshd
udp        0      0 0.0.0.0:68                  0.0.0.0:*                                                                                                    1055/dhclient

Which column is displaying ports?
 

LJSHost

Well-known member
Hosting Provider
Registered
Joined
Jul 5, 2016
Messages
1,031
Points
63
0.0.0.0:22 <-------
127.0.0.1:25 <--------

22 and 25 are the ports
 

hostmeja

Member
Registered
Hosting Provider
Joined
May 16, 2017
Messages
50
Points
0
I recommend getting ConfigServer Security & Firewall or a firewall tool with UI to better assist you. In most cases they can advise what port to close and a simple UI to turn it off /on.
 

AlphaNine Vini

Member
Hosting Provider
Registered
Joined
Dec 8, 2016
Messages
59
Points
6
You need to use the iptables or a firewall that can enable you to block any port for the users.

Block port 80:
iptables -A INPUT -p tcp --dport 80 -j ACCEPT

Block a IP address:
iptables -I INPUT -p tcp -s 103.18.45.18 -j DROP
 

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top