//Cloud notes from my desk -Maheshk

"Fortunate are those who take the first steps.” ― Paulo Coelho

Linux firewall and connectivity issues

Src: Todd Hammer, Microsoft

All Linux Distributions have a firewall in place, just because it comes with the kernel, but it’s inactive by default.  If we’re having connectivity issues, then, the firewall is something we should check. Netfilter is the name for firewall in the Linux kernel, and we can talk with it using a command-line utility called iptables

List currently configured iptablesiptables -L
List main tablesiptables -t security -L
iptables -t mangle -L
iptables -t filter -L
iptables -t nat -L
Clear all currently configured rulesiptables -F
Block connection to specific ip addressiptables -A INPUT -s <ip_address> -j DROP
Block connection to a ip addresses rangeiptables -A INPUT -s <ip>/<netmask> -j DROP
Block connection to a specific port and ip addressiptables -A INPUT -p tcp –dport <port> -s <ip_address> -j DROP
Block connections to a specific port from any ip addressiptables -A INPUT -p tcp –dport <port> -j DROP
Add port in firewall(22 in example)/sbin/iptables -A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT
Save changes you madeUbuntu: sudo /sbin/iptables-save Red Hat/CentOS:  sudo /sbin/service iptables save Others:  /etc/init.d/iptables save
Listen open portssudo lsof -i -P -n | grep LISTEN sudo lsof -i:22 ## see a specific port such as 22 ## sudo ss -tulpn

Now, some people’s distributions has their firewall in place so, you can use these commands (of course there are more options you can apply, read the manuals please) to check on them:

Red Hat/CentOS/SuSE

Get current firewall status:sudo systemctl status firewalld
Start/Stop Firewalldsudo systemctl start firewalld/sudo systemctl stop firewalld
Enable/disable firewalld at system startupsudo systemctl enable firewalld/sudo systemctl disable firewalld
List zonesfirewall-cmd –get-zones
Check active zonesfirewall-cmd –get-active-zones
List configurationfirewall-cmd –list-all
List allowed servicesfirewall-cmd –list-services
List allowed portsfirewall-cmd –list-ports
Allow a servicefirewall-cmd –add-service=<servicename> –zone=<zonename> –permanent
Allow a portfirewall-cmd –add-port=<portnumber/protocol> –zone=<zonename> –permanent
Reload the firewallfirewall-cmd –reload

Ubuntu

Firewall statussudo ufw status [verbose]
Enable/Disable firewallsudo ufw enable/sudo ufw disable
Allow port for tcp/udpsudo ufw allow <port_number>
Allow port for tcpsudo ufw allow <port_number>/tcp
Allow port by service name (will check in /etc/services file)sudo ufw allow <service>
Block outgoing trafficsudo ufw reject out <service/port>
Delete a rulesudo ufw delete reject out <service/port>
Deny tcp traffic from specific ip on specific portsudo ufw deny proto tcp from <ip> to any port <port_number>
Reset firewall to its default statesudo ufw reset
See application profiles availablesudo ufw app list
View information about a profile and its included rulessudo ufw app info <app_name>
Allow an application profilesudo ufw allow <app_name>
Enable logging to print firewall messages to -system logsudo ufw logging on

There is something called Security-enhanced Linux(SELinux) that can also block connections,

More information: 

2022-05-27 - Posted by | Linux, Open Source |

No comments yet.

Leave a comment