2

I want to block all IPs except for my own home IP from doing anything on my server.

How can I do this with iptables? For the example lets say my home IP is 1.2.3.4

My server still needs to be able to connect to various IPs. Also by doing this will this cause any general problems?

Something like this? (doesn't work)

/sbin/iptables -A INPUT -s 1.2.3.4 -j ACCEPT
iptables -A INPUT -s 0.0.0.0/0 -j DROP
iptables -A OUTPUT -d 0.0.0.0/0 -j DROP
  • 1
    If you do this, you'd better hope that your home IP address never changes. – Greg Hewgill Jun 10 '15 at 20:12
5

Leave the OUTPUT chain untouched. Put these in your INPUT chain

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -s 1.2.3.4 -j ACCEPT
iptables -A INPUT -j DROP    # or REJECT

The first rule allows your iptables configuration to accept traffic for established connections (i.e. those initiated by your own server to other destinations).

2

You can use /etc/hosts.deny and /etc/hosts.allow.

/etc/hosts.allow:

ALL: allowed_ip

/etc/hosts.deny:

ALL: ALL

This configuration access to your server only from allowed ip (this might be a problem in some cases). May be you have to use some firewall to specify access to your server.

But above will not handle httpd and ICMP requests you can try this:

# Allow your server to other destinations
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Default Policy to DROP all incoming traffic
iptables -P INPUT DROP

# Allow traffic from source 1.2.3.4
# Add an additional rule for each allowed source
iptables -A INPUT -s 1.2.3.4 -j ACCEPT
  • Hi, this did not block an IP from accessing my website hosted on my VPS – Teddy291 Jun 10 '15 at 21:30
  • I want to block everything sent to my server http, ftp, ping everything. – Teddy291 Jun 10 '15 at 21:34
  • @Teddy291 in that case it's likely you already have some rules in your iptables chains that accept such traffic. Check with iptables -nvL. – roaima Jun 12 '15 at 16:24
  • @roaima, Teddy291 pointed about hosts.deny and it was edited after that but something does not work as expected and we do not know why. – taliezin Jun 12 '15 at 16:27
  • @taliezin, you added the iptables rules after the comment about it not blocking an IP? Ah. I couldn't see that chronology. – roaima Jun 12 '15 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.