Category: Security

Simplifying iptables management on a Debian (Squeeze) server

My basic management concept for iptables (easy to maintain when you're the owner and administrator of a server). I don't like native iptables management way, so with little help from Google I've recently figured out an alternative way to handle rules loading and applying. Here's how it's done.

First, create (as a su) iptables file which will contain all our rules. File must be executable:

# Rules from this file will be applied on system boot
touch /etc/iptables.rules
chmod +x /etc/iptables.rules

Stuff from the file above will be loaded on system boot. To tell the OS, that he needs to load them, lets use /etc/network/interfaces file. Edit it and paste:

# vim /etc/network/interfaces
# Load our rules automatically
pre-up iptables-restore < /etc/iptables.rules

Now still as a su, create in your home dir file called iptables.rules. Yup - with the same name (but different directory!) as a first one:

touch /home/mylogin//iptables.rules
chmod +x /home/mylogin//iptables.rules

Now let's put our rules into /home/mylogin/iptables.rules:

# vim ~/iptables.rules
# Reset all (block all) and start from zero
# Warning! If you apply only those 3, you're screwed! So watch out!
iptables -F INPUT
iptables -F FORWARD
iptables -F OUTPUT
 
# Reject incoming traffic
iptables -P INPUT DROP
 
# Reject forwarded traffic
iptables -P FORWARD DROP

# Accept loopback traffic
iptables -A INPUT -i lo -j ACCEPT
iptables -A FORWARD -o lo -j ACCEPT

# Accept all connections started by us 
# (this can be dangerous if you install shitty stuff on your server)
iptables -P OUTPUT ACCEPT
 
# Accept data related to our requests 
iptables -A INPUT -j ACCEPT -m state --state ESTABLISHED,RELATED
iptables -A FORWARD -j ACCEPT -m state --state ESTABLISHED,RELATED
  
# http port can be setup with limits or without - here's the example with:
# (not used right now so commented out - 2 lines, should be in 2):
# iptables -A INPUT -p tcp --dport 80 -m state --state NEW 
# -m limit --limit 50/minute --limit-burst 5 -j ACCEPT

# Open ports related to our services (http, https, ssh, etc)
iptables -A INPUT -p tcp --dport 10000 -j ACCEPT # Example: webmin port
iptables -A INPUT -p tcp --dport 22 -j ACCEPT # Example: ssh port
iptables -A INPUT -p tcp --dport 80 -j ACCEPT # Example: http port

# After executing and checking, to save rules, perform (as su) command:
# iptables-save > /etc/iptables.rules

In order to check your settings. you can use nmap, a simple (yet powerful) port scanner:

sudo apt-get install nmap
nmap IP/domain

Starting Nmap 5.21 ( http://nmap.org ) at 2012-02-04 15:17 CET
Nmap scan report for domain (IP)
Host is up (0.032s latency).
Not shown: 998 filtered ports
PORT      STATE  SERVICE
80/tcp    open   http

Nmap done: 1 IP address (1 host up) scanned in 8.89 seconds

If everything is ok, save your rules permanently (execute as su):

iptables-save > /etc/iptables.rules

Ubuntu i “No init found. Try passing init= bootarg”

Nieprawidłowe wyłączanie komputera może być dość dramatyczne w skutkach. Część z was wie, że jestem właścicielem małego serwera (który tak naprawdę jest zwykłym komputerem ;) ), na którym wykonuję testy aplikacji oraz stawiam wersje developerskie (alfy, bety, itd). Oczywiście robię mu regularne backupy, jednak sami dobrze wiecie, że "pad" jakiegokolwiek komputera to po prostu masa pracy z postawieniem go na nowo. W przypadku serwerka dochodzi konfiguracja na nowo, itd.

Wczoraj w skutek nagłego spadku zasilania - zresetował mi się wspomniany wyżej serwer i niestety... już nie wstał. Dostawałem za to piękną wiadomość:

mount: mounting /dev/disk/uuid/***************************** on /root
failed: Invalid argument
mount: mounting /sys on /root/sys failed: No such file or directory
mount: mounting /dev on /root/dev failed: No such file or directory
mount: mounting /sys on /root/sys failed: No such file or directory
mount: mounting /proc on /root/proc failed: No such file or directory
Target file system doesn't have /sbin/init
No init found. Try passing init= bootarg

Busybox v1.13.3 (Ubuntu 1:1.13.3-1ubuntu7) built-in shell (ash)
Enter 'help' for a list of built-in commands
(initramfs) _

Co się stało? Po prostu "padła" jedna z partycji. Naprawa tego typu problemów nie jest trudna (o ile wysypała się partycja a nie np. padł dysk). Musimy mieć bootowalną wersję Linuksa (na pendrivie lub na płycie CD). Odpalamy takie Live CD a następnie przechodzimy do terminala. Wpisujemy komendę:

sudo fdisk -l 

aby dowiedzieć się jakie partycje mamy na dysku i mamy (przykładowo):

Dysk /dev/sda: 80.0 GB, bajtów: 80026361856
głowic: 255, sektorów/ścieżkę: 63, cylindrów: 9729
Jednostka = cylindrów, czyli 16065 * 512 = 8225280 bajtów
Rozmiar sektora (logiczny/fizyczny) w bajtach: 512 / 512
Rozmiar we/wy (minimalny/optymalny) w bajtach: 512 / 512
Identyfikator dysku: 0x85f285f2

Urządzenie Rozruch   Początek      Koniec   Bloków   ID  System
/dev/sda1               1         125     1004031   82  Linux swap
/dev/sda2   *         126        1993    14999552   83  Linux
/dev/sda3            1993        3860    14999552   83  Linux
/dev/sda4            3860        9730    47145985    5  Rozszerzona
/dev/sda5            3860        5728    14998528   83  Linux
/dev/sda6            5728        9730    32146432   83  Linux

Następnie uruchamiamy program fsck na każdej z partycji (tak dla pewności), wykonując następujące polecenie:

# Zmieniamy kolejno na #sda1, sda2, sda3, itd
sudo fsck /dev/sda2

Po tym zabiegu restartujemy serwer i cieszymy się działającym systemem. Następnie zamawiamy UPSa aby taka sytuacja więcej się nie zdarzyła.

Dla pewności, możemy wymusić autouruchomienie fsck przy starcie systemu, wpisując:

sudo touch /forcefsck

Utworzoenie pliku forcefsck w głównym katalogu - spowoduje, że system wstając wykryje go i wykona jeszcze raz testy na partycjach. Po tych operacjach wszystko powinno wrócić do normy.

Copyright © 2025 Closer to Code

Theme by Anders NorenUp ↑