Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi, I was hoping I could get some feedback on improving my iptables for CentOS. I've struggled to get it this far, and I'm having one particular problem (dropping all forwards with one exception). In any case, I thought I would post it here in the hopes of getting some feedback, both in terms of the final product but also the journey of learning it.
The below is in a file, firewall.sh. Thanks a ton for any improvements, suggestions, or comments!
Code:
#!/bin/bash
#
#
# Flush all current rules from iptables and so start fresh
iptables -F
#
#
# Allow SSH connections on tcp port 123 and udp port 1194 (VPN)
iptables -A INPUT -p tcp --dport 123 -j ACCEPT
iptables -A INPUT -p udp --dport 1194 -j ACCEPT
#
#
# Set default policies for INPUT, FORWARD and OUTPUT chains
iptables -P INPUT DROP
iptables -P FORWARD ACCEPT
# Above used to be set to "DROP" but then the port forwarding didn't work for the VPN, so changed to "ACCEPT".
iptables -P OUTPUT ACCEPT
#
#
# Set access for localhost - necessary for many programs
iptables -A INPUT -i lo -j ACCEPT
#
#
# Accept packets belonging to established and related connections
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#
#
# Allows proper routing of VPN subnet
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
#
#
# Make and exception to 'iptables -P FORWARD DROP', allowing specifically 1194 connections to forward.
iptables -t nat -A PREROUTING -p udp --dport 1194 -j DNAT --to 192.168.0.5
#
#
# Save settings
/sbin/service iptables save
#
#
# List rules, verbose
iptables -L -v
~
You could move frequently matched rules to the top of the file. In particulsr I'd move the loopback rule and the state match to the top of the INPUT chain. It will have a positive effect on routing performance and/or CPU load.
The "-m state" match and the related "--state" parameter is deprecated and should be changed to "-m conntrack" and "--ctstate" respectively (provided your iptables executable is reasonably recent).
You should definitely change the FORWARD policy to DROP and add any necessary rules to allow VPN traffic instead.
Ok, thanks a ton for the feedback, I've taken some of your suggestions. The conntrack and cstate didn't work with my version of IPTables however, unfortunately. I've since done the following:
Code:
#!/bin/bash
#
# iptables configuration script
#
# Flush all current rules from iptables
iptables -F
#
#
# Set access for localhost - necessary for many programs
iptables -A INPUT -i lo -j ACCEPT
#
#
# Accept packets belonging to established and related connections
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#
#
# Allow SSH connections on tcp port 123 and udp port 1194 (VPN)
iptables -A INPUT -p tcp --dport 123 -j ACCEPT
iptables -A INPUT -p udp --dport 1194 -j ACCEPT
#
#
# Allows VPN traffic to Forward
iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -s 10.8.0.0/24 -j ACCEPT
#
#
# Make and exception to 'iptables -P FORWARD DROP', allowing specifically 1194 connections to forward.
iptables -t nat -A PREROUTING -p udp --dport 1194 -j DNAT --to 192.168.0.5
#
#
# Set default policies for INPUT, FORWARD and OUTPUT chains
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
#
#
# Allows proper routing of VPN subnet
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
#
#
#
# Save settings
/sbin/service iptables save
#
#
# List rules, verbose
iptables -L -v
~
I can cannot fine to my VPN server now, that's fantastic. But I cannot connect through it to the internet - what am I not seeing with the lastest iptables configuration? It must be something with forwarding, but what am I doing wrong?
I should close out this post as "solved" with the help I received. I also wanted to point out the (admittedly embarassing) error that I made that prevented me from connecting to the internet through my VPN, especially because it's been a while since I determined the mistake. I wanted to post in case someone else runs into the same error.
I was routing to eth0, when in fact it should have been to eth4....
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.