LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Fedora
User Name
Password
Fedora This forum is for the discussion of the Fedora Project.

Notices


Reply
  Search this Thread
Old 07-25-2007, 05:25 PM   #1
MikeyCarter
Member
 
Registered: Feb 2003
Location: Orangeville
Distribution: Fedora
Posts: 492

Rep: Reputation: 31
Question iptables


Is it possible to configure my firewall to only allow specific IP addresses in? ie only 192.168.0.*

what's the command?
 
Old 07-25-2007, 06:30 PM   #2
TylerD75
Member
 
Registered: Aug 2004
Location: Norway
Distribution: Gentoo
Posts: 96

Rep: Reputation: 18
iptables -P INPUT DROP # Set the default policy to DROP
iptables -A INPUT -i eth0 -s 192.168.0.0/24 -j ACCEPT # Add a rule to allow only 192.168.0.1-254 to connect.

The above should work, although you might need some additional settings (like setting policy for OUTPUT, FORWARD etc...)
 
Old 07-25-2007, 07:25 PM   #3
MikeyCarter
Member
 
Registered: Feb 2003
Location: Orangeville
Distribution: Fedora
Posts: 492

Original Poster
Rep: Reputation: 31
Is there something I should put in after to get it to work? I've tried a few things but none of the new commands are working.
 
Old 07-25-2007, 08:33 PM   #4
TylerD75
Member
 
Registered: Aug 2004
Location: Norway
Distribution: Gentoo
Posts: 96

Rep: Reputation: 18
Is this a gateway/firewall?
Do you have more than one network interface?

What exactly are you trying to do?

If it's just a workstation, then try this BASH script:
Code:
#!/bin/bash

IPT="/sbin/iptables"  # Location of iptables command
LAN="192.168.0.0/24"  # IP-range of LAN
IF0="eth0"            # Name of interface

$IPT -F               # Flush all rules
$IPT -t nat -F        # Flush nat table (only neccessary on a gateway/firewall with nat'ing)
$IPT -X               # Delete any chains present
$IPT -Z               # Zero counters in all chains

$IPT -P INPUT DROP    # Set default INPUT policy to DROP.  This will be used on all attempted connections without a specific rule.
$IPT -P OUTPUT ACCEPT # Allow all outbound connections.  This is so that ALL programs locally can connect as needed to the internet etc.

# Already established connections, and connections initiated from your PC, will be allowed:
$IPT -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# OUTBOUND connections will be allowed:
$IPT -A OUTPUT -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

# Everything to and from loopback interface has to be allowed:
$IPT -A INPUT -i lo -j ACCEPT
$IPT -A OUTPUT -o lo -j ACCEPT

# We also want to allow everyhing from the LAN to be able to connect:
$IPT -A INPUT -i $IF0 -s $LAN -j ACCEPT

# If you want certain ports open for everyone (ex. If you have an Apache server or SSHd running on this computer), then add them here:
$IPT -A INPUT -i $IF0 -p tcp --dport 22 -j ACCEPT  # Allows all TCP connections to port 80
$IPT -A INPUT -i $IF0 -p tcp --dport 80 -j ACCEPT  # Same as above, but for port 22 (SSHd)

# The next few lines will enable logging of dropped packages (might not work, depending on your kernel configuration):
$IPT -A INPUT -j LOG --log-prefix "INPUT_DROP: "
$IPT -A OUTPUT -j LOG --log-prefix "OUTPUT_DROP: "
Copy and paste the above code into an empty file. Call it whatever you like, then "chmod +x <name_of_file>".
Now you're ready to run it:
./<name_of_file> # (In a shell/console/terminal

If you get errors, they're probably related to kernel configuration...

Also, if you're configuring a firewall/gateway, you need some more lines in the above script (to enable forwarding, masquerading and setting up internal and external interfaces etc...).

One thing to remember is that when a package comes to your interface, it goes through all rules until it finds one that applies.
This means that the order you add rules means everything.
An example:
Code:
iptables -A INPUT -i eth0 -s 192.168.0.4 -j DROP       # Drop everything coming from 192.168.0.4
iptables -A INPUT -i eth0 -s 192.168.0.0/24 -j ACCEPT  # ACCEPT all from LAN 192.168.0.0/24
# Even though you've basically allowed everything from the LAN, 192.168.0.4 will always be blocked.
Another example:
Code:
iptables -A INPUT -i eth0 -s 192.168.0.0/24 -j ACCEPT  # ACCEPT all from LAN 192.168.0.0/24
iptables -A INPUT -i eth0 -s 192.168.0.4 -j DROP       # Drop everything coming from 192.168.0.4
# The second rule will NEVER be used, as you have ACCEPTed everything from the LAN before the DROP rule
Hope this helps?
 
Old 07-27-2007, 02:11 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Just to clarify, adding/changing the ip tables rules is fine, but that's just a config file.
You have to restart the network software, so it re-reads the file.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
An error occured getting IPtables status from the command /etc/rc.d/init.d/iptables s CrazyMAzeY Linux - Newbie 10 08-12-2010 05:25 AM
iptables v1.2.9: Unknown arg `/sbin/iptables' Try `iptables -h' or 'iptables --help' Niceman2005 Linux - Security 4 12-29-2005 08:20 PM
Iptables - Couldn't load target `ACCPET':/lib/iptables/libipt_ACCPET.so: z00t Linux - Security 3 01-26-2004 02:24 AM
IPtables Log Analyzer from http://www.gege.org/iptables/ brainlego Linux - Software 0 08-11-2003 06:08 AM
My iptables script is /etc/sysconfig/iptables. How do i make this baby execute on boo ForumKid Linux - General 3 01-22-2002 07:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Fedora

All times are GMT -5. The time now is 10:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration