How do I configure iptables firewall under CentOS / Fedora / RHEL / Redhat Linux to allow access to the Samba server? How do I open TCP ports # 137, 138, 139 and 445 under Linux so that all Microsoft Windows machine can access files and printer on a Linux host?
The Samba server can be configured to allow access to certain hosts. However, iptables prevent the access over the Internet. You must allow only the systems on your network as clients of the Samba Linux server.
Iptables Open Port 137, 138, 139 and 445
Edit /etc/sysconfig/iptables file, enter:
# vi /etc/sysconfig/iptables
To allow access to 192.168.1.0/24 network only add the following before the final LOG & DROP statements:
-A RH-Firewall-1-INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 137 -j ACCEPT -A RH-Firewall-1-INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 138 -j ACCEPT -A RH-Firewall-1-INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 139 -j ACCEPT -A RH-Firewall-1-INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 445 -j ACCEPT
Save and close the file.
Restart Firewall
Type the following command:
service iptables restart
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop











{ 8 comments… read them below or add one }
Great writeup! I think this may put me on the right track to figuring out my system at home.
I am running Ubuntu 9.04 and recently enabled connection sharing for my 3G USB modem. My problem is that when I enabled ICS I was no long able to access my shared folders from my Windows machines. At first I thought it had to do with my routing tables but after I read this I figured it was my iptables.
Is there a similar article for this on Ubuntu? I’m sure I can work through it and get it running but it would be nice if there were something to walk me through it (I’m very new).
You can use GUI tool called Firestarter (package is included with Ubuntu) is quite popular and easy to use.
OR UFW
Is there a UFW package for CentOS ?
This is incorrect.
Should be:
UDP/137,138
TCP/139,445
I cannot restart my samba after applying the iptables rules
Jim -
The original post is over two years old. Here are the correct firewall commands. Change 192.168.1.0/24 to your internal LAN subnet if different. After executing, use the command ‘service iptables save’, or edit /etc/sysconfig/iptables directly.
-A INPUT -s 192.168.1.0/24 -m state –state NEW -p udp –dport 137 -j ACCEPT
-A INPUT -s 192.168.1.0/24 -m state –state NEW -p udp –dport 138 -j ACCEPT
-A INPUT -s 192.168.1.0/24 -m state –state NEW -p tcp –dport 139 -j ACCEPT
-A INPUT -s 192.168.1.0/24 -m state –state NEW -p tcp –dport 445 -j ACCEPT
Additionally, firewall rules should not affect the functionality of the samba service… recheck your configuration and logs (/var/log/samba/*.log) for relevant error messages and repost any errors you see here.
Depending on how your browser renders fonts, the firewall rules shown above may not work. In my browser (Firefox 3.6.24 on CentOS 6.2) the characters before ‘state NEW’ and ‘dport nnn” parameters render as a single long dash (somtimes called an ‘em’ dash). In order to work correctly the long dash must be changed to 2 consecutive short dashes, then everything is fine. I discovered this doing a copy & paste of the above to modify /etc/sysconfig/iptables.
Hope this helps!
Oldan
You can put it all in one line:
iptables -I INPUT 1 -p tcp -m conntrack –ctstate=NEW -m multiport –dports 137,138,139,445 -j ACCEPT