How can I allow SSH and SMTP only using IPTables?

BubbleMonster

I've got a raspberry pi set up to send me periodic emails. As it's connected to the internet 24/7, I need IPTables set up properly.

I want to allow incoming SSH and allow emails to send out on port 587 via SMTP. I've came up with this IPTables script, is it correct? If not, can you tell me why. Thanks.

sudo iptables -P INPUT DROP
sudo iptables -P OUTPUT DROP

sudo iptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
sudo iptables -A OUTPUT -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

sudo iptables -A OUTPUT -p tcp --dport 587 -j ACCEPT
sudo iptables -A INPUT -p tcp --sport 587 -j ACCEPT
elbarna

A iptables rule like this works fine

*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [1:156]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 
-A INPUT -i lo -j ACCEPT 
-A INPUT  -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT 
-A INPUT  -p tcp -m state --state NEW -m tcp --dport 587 -j ACCEPT 
-A INPUT -j REJECT --reject-with icmp-host-prohibited 
-A FORWARD -j REJECT --reject-with icmp-host-prohibited 
COMMIT

The first rule DROP by default all incoming connection the second DROP by default all forwarding the third ACCEPT the output,why accept?Is not too unsafe imho to make open the output connections,close it can make the firewall configuration a little difficult.

-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 

Accept the connection with state RELATED and established state

the rest is easy

    -A INPUT  -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT 
    -A INPUT  -p tcp -m state --state NEW -m tcp --dport 587 -j ACCEPT 
    -A INPUT -j REJECT --reject-with icmp-host-prohibited 
    -A FORWARD -j REJECT --reject-with icmp-host-prohibited 
    COMMIT

accept 22 tcp,accept 587 tcp and forbid all the other connections, you can save on file and then do

iptables-restore < firewall.file

And check it with nmap -sS your host

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to allow only ssh and internet access with iptables?

How to Allow Outgoing SMTP on iptables Debian Linux

How do I allow only internal network to access a particular UDP Port using iptables

How can I only allow the input of numbers using exception handling?

How can I allow SSH password authentication from only certain IP addresses?

iptables: How to allow SSH through debian router?

How to allow only subnet for port with iptables

How to allow traffic for SSH 22 using iptables for one specific ip address and internal networks

How do I allow browser in IPtables?

How can I allow access to both SSH and SFTP?

How can I allow ssh host keys to be added again?

Can iptables allow DNS queries only for a certain domain name?

How I can block specific IP's using iptables?

How can I generate AWS SES SMTP credentials using the CDK?

how can i send mail using SMTP Gmail

How do I allow DNS through interface filtering using iptables in Ubuntu Headless

How can I connect to my server through SSH using only a browser?

How can I allow merging to master only from testing branch?

How can I allow numeric only in Swal Sweetalert

How can I only allow whitelisted queries with Graphene?

How can I allow an employee to edit only certain tables?

How can I allow controller access only for form submission?

How can i allow only positive number in my code

How can I allow single minus only with preg_match?

How can I allow only numbers and letters on Vue Js?

how can i allow aws IAM permission only specific time

C# How can I make an exception to allow only numbers?

How can I only allow type imports from a module in TypeScript?

How to allow only IPs ending with a specific number in IPTABLES?

TOP Ranking

HotTag

Archive