Archiv der Kategorie: Linux Howto

THINK UNBLOG GNU/Linux Tutorials Howto Workaround DevOps Code.

Block IP by ufw or iptables and firewalld

A large number of suspicious requests is going on, protection is provided by ufw, iptables or firewalld. This tutorial show how to use the commands to reject an IP address on popular Linux software Firewalls.

Block IP by ufw or iptables and firewalld

Using ufw or iptables and firewalld

Fire up a terminal and log on to the server by using SSH and then complete the steps for firewalld in the first chapter. The second chapter shows the commands for UFW, and the third shows using iptables.

firewalld tool

firewalld is on RHEL 7, CentOS 7 and later, Fedora 18 and later.

To ensure that firewalld is running on your server, run the following command. If firewalld is not running, go to the iptables chapter.

$ sudo systemctl status firewalld

Run the following command to block the IP address and to add the rule to the permanent set:

$ sudo firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='xxx.xxx.xxx.xxx' reject"

Run the following command to reload the firewalld rules:

$ sudo firewall-cmd --reload

Run the following command to list and verify the new rule:

$ sudo firewall-cmd --list-all

Run the following command to remove a blocked IP address.

$ sudo firewall-cmd --remove-rich-rule="rule family='ipv4' source address='xxx.xxx.xxx.xxx' reject"

Run the following command to verify the firewalld is running.

$ firewall-cmd --state

Uncomplicated Firewall (UFW)

ufw is available on Debian 6 and later, Ubuntu 8.04 LTS and later.

To ensure that ufw is running on your server, run the following command. If ufw is not running, go to the iptables chapter.

$ sudo systemctl status ufw

Run the following command to block the IP address:

$ sudo ufw deny from xxx.xxx.xxx.xxx to any

Run the following command to list and verify the new rule:

$ sudo ufw status

Run the following command to remove a blocked IP address.

$ sudo ufw delete 7

Run the following command to show numbered list of firewall rules.

$ ufw status numbered

iptables tool

iptables is commonly pre-installed on all Linux distributions.

Run the following command to block the IP address:

$ sudo iptables -I INPUT -s xxx.xxx.xxx.xxx -j DROP

Run the following command to save the settings. The settings persist after the server reboots.

$ sudo service iptables save

Run the following command to list and verify the new rule:

$ sudo iptables -vnL

Run the following command to delete a iptables chain.

$ sudo iptables -D INPUT 7

Run the following command to show numbered list of iptables chains.

$ sudo iptables -L --line-numbers

about ufw or iptables and firewalld

The firewall commands ufw, iptables and firewalld shown here for the corresponding kernel firewalls apply to the most common Linux operating systems.

E-Mail in Postfix Queue zu Empfänger umleiten mit postsuper

Postfix E-Mail bleibt in der Warteschlange (queue) hängen, die E-Mail soll nun mit postsuper und postcat an einen anderen Empfänger umgeleitet werden.

E-Mail in Postfix Queue zu Empfänger umleiten mit postsuper

Dieses Szenario kann in Erscheinung treten, wenn zum ursprünglichen Empfänger keine E-Mails zugestellt werden können, zum Beispiel mit Erreichen der Mailbox Speicher Limite, oder der Server mit Fehler antwortet.

Hier kann es hilfreich sein, die E-Mail vorübergehend an einen anderen Empfänger zu senden. Der Beitrag zeigt die Schritte, um eine in der Postfix Queue verbleibende E-Mail an einen alternativen Empfänger zu senden.

E-Mail auf on-hold mit postqueue postsuper

Zuerst sucht man in der Postfix Queue die Queue-ID der betreffenden E-Mail, diese man an eine andere Empfänger Adresse senden möchte.

$ postqueue -p | grep 'john@example.org' -B 2

  Der Parameter -B 2 gibt zusätzlich zwei Zeilen vor gesuchter Parse aus.

Die Ausgabe kann in etwa wie die folgende aussehen.

BCD2C3035D31!   37023 Thu Feb 17 08:59:55  suite102@mailings.daydeal.net
        (connect to 12.34.56.78[12.34.56.78]:25: Connection timed out)
                             john@example.org

Hier wurde eine E-Mail an john@example.org gesendet, die ich nun an eine andere Adresse zustellen möchte. Die Queue-ID ist BCD2C3035D31 die wir im weiteren Verlauf benötigen.

Alternativ lassen sich auch alle E-Mails jeder Postfix Queue auflisten.

$ mailq

Um zu verhindern dass Postfix versucht die E-Mail in der Zwischenzeit auszuliefern, setzen wir diese in Wartestellung mit -h auf on hold.

$ postsuper -h BCD2C3035D31
postsuper: BCD2C3035D31: placed on hold
postsuper: Placed on hold: 1 message

  Die E-Mails werden mit on hold nicht gelöscht. Das Ausrufezeichen (!) gibt an, das die Nachricht in Wartestellung ist.

Nun extrahiert man die E-Mail und speichert sie in eine temporäre Datei.

$ postcat -qbh BCD2C3035D31 > /tmp/email.eml

E-Mail aus Postfix Queue senden

Nachdem die E-Mail nun extrahiert ist, kann man diese aus der queue an einen anderen Empfänger senden, mit Postfix postsuper und sendmail.

$ sendmail -f john@example.org mike@domain.org < /tmp/email.eml

Die E-Mail wird von john@example.org an mike@domain.org gesendet.

Die Postfix Deffered Queue nach aufgeschoben E-Mails durchsuchen.

$ postqueue -vp

Nachdem die Zustellung mit postsuper und sendmail an die neue E-Mail-Adresse bestätigt wurde, kann die E-Mail aus der Postfix queue gelöscht werden.

$ postsuper -d BCD2C3035D31
postsuper: BCD2C3035D31: removed
postsuper: Deleted: 1 message

Die temporäre Datei /tmp/email.eml löschen.

$ rm -f /tmp/email.eml

Hilfreiche Postfix Kommandos

  In die E-Mail schauen mit postcat.

$ postcat -vq BCD2C3035D31

E-Mails freigeben, die in die Warteschleife gelegt wurden.

$ postsuper -H BCD2C3035D31
postsuper: BCD2C3035D31: released from hold
postsuper: Released from hold: 1 message

Warteschlange leeren, es wird versucht alle in der Warteschlange befindlichen E-Mails zuzustellen.

$ postqueue -f

Alle E-Mails in der Postfix deffered queue löschen.

$ postsuper -d ALL deferred

Conclusion

Wie in diesem Beitrag aufgezeigt wird, ist es möglich E-Mails aus der Postfix Warteschlange nach auf Hold setzen, diese temporär an andere Empfänger zu senden. Ebenfalls werden verbleibende E-Mails aus der Postfix Queue zur System Hygiene wieder gelöscht.