oriolrius.cat

Des del 2000 compartiendo sobre…

Tag: kernel

rp_filter Linux kernel feature

Reading time: 1 – 2 minutes

It’s just an IP spoofing protection which is by default enabled on Linux kernels. When it’s value is ‘1’ means that all IP address which are not directly routable and received form a network interface they are directly discarded.

So, if you want to scan a range of IP address in your LAN which not belong to that interface address space when packets from IP addresses are received they are going to be discarded by the kernel. So, take that into account when you have those “unusual” requirements.

It can be enable/disabled by all interfaces or just one:

root@mini9:/proc/sys/net/ipv4# cat ./conf/all/rp_filter
1
root@mini9:/proc/sys/net/ipv4# cat ./conf/ztly5q4n37/rp_filter
1

Remove old kernels when there is no space in /boot

Reading time: 1 – 2 minutes

The first step is get some space in the partition “/boot” because without that it’s impossible to do anything.

So go to /boot and remove some “initrd” files as they are the biggest ones. A few of them will be enough.

After that a good point is to ensure there is no partial installation pending to finish:

apt-get -f install

Now it’s a good idea to purge all kernels except the running one:

dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge

To avoid that in future before filling the partition, it’s a good idea to install and run periodically: purge-old-kernels. Installation and example of use are:

# installation
apt-get install bikeshed
# keep three old kernels:
purge-old-kernels --keep 3
# if you want to put that in the crontab use that command
purge-old-kernels --keep 3 -qy

If you’re a Grub user don’t forget to run:

update-grub2

Personally I have a nightmare with that problem and Ubuntu, especially with version 12.04 which is installed in a lot of servers that I manage. I repeated the previous process a lot of times and in the end, I decided to document it because I always have to go to Google and find the proper steps to solve that problem.

Enabling linux kernel to open LOTS of concurrent connections

Reading time: < 1 minute Just a small recipe about how to enable linux kernel to open tons of concurrent connections. Really simple and useful post entry.

echo “10152 65535″ > /proc/sys/net/ipv4/ip_local_port_range
sysctl -w fs.file-max=128000
sysctl -w net.ipv4.tcp_keepalive_time=300
sysctl -w net.core.somaxconn=250000
sysctl -w net.ipv4.tcp_max_syn_backlog=2500
sysctl -w net.core.netdev_max_backlog=2500
ulimit -n 10240

parprouted – ARP daemon

Reading time: 1 – 2 minutes

parprouted és un dimoni que fa funcions de proxy ARP, idea per fer unir dues xarxes a nivell 3 quan no estan unides per la capa 2. O sigui, no cal que fem WDS (usant en xarxes Wi-Fi) o que posem un bridge de capa 2 per unir dues xarxes separades físicament però unides a través de routing.
El funcionament del dimoni és el següent: quan es rep una petició ARP i la resposta de la petició és desconeguda llavors aquesta petició ARP es re-envia a la resta d’interficies en busca de la MAC requerida per la IP demanada en la petició ARP. Quan es localitza la IP es creen rutes estàtiques de tipus host (/32) per interconnectar aquella IP que esta en una altre interficie de xarxa diferent de la interficie que la requeria. Així aconseguim fer visible la IP entre les dues interficies de xarxa.
Totes les entrades que es creen amb el dimoni es refresquen cada 50s enviant peticions ARP que validin que encara són vigents. En cas de que les peticions fallin el propi kernel borrarà les entrades de la taula ARP i el dimoni s’encarregarà de borrar les rutes estàtiques.
Normalment es triguen uns 60ms per aconseguir fer visible un host que no esta al propi segment de xarxa degut als processos que s’han comentat, però es considera un temps marginal en comparació al benefici que se’n obté.

inotify, inotify-tools i incron: un gran descobriment per substituir dnotify

Reading time: 2 – 3 minutes

Fa forces anys, vaig parlar del dnotify és una eina que ens permetia saber quan hi havia un canvi en els fitxers d’un directori, llavors aprofitabem per llençar un procés al produir-se l’event. Com que l’arquitectura interna del programa comentat no era massa bona (controlava els canvis a base de fer pooling) i degut a les millores dels nous kernel, s’ha aprofitat el concepte de l’inotify: inotify is a Linux kernel subsystem that provides file system event notification..

Així doncs, les inotify-tools són les eines que ens permeten usar aquesta funcionalitat del kernel des de l’espai d’aplicacions d’usuari. Realment un passada. Permeten vigilar un fitxer, per generar-ne estadístiques, llençar processos associats quan es detecti un determinat canvi en el fitxer, etc. Però potser la funcionalitat que més m’ha interessat és la de la llibreria que ofereix per programar aplicacions usant les funcionalitats de l’inotify.

Concretament aquestes llibreries són les necessaries per compilar el incron. Per mi l’aplicació final més genial de totes: es tracta d’un ‘inotify cron’. Bàsicament l’eina es composa de dues parts d’un dameon i d’una taula d’accions. Aquesta taula s’usa d’una forma semblant al servei de cron. La diferència és que els events no són senyals horaris, sinó captures d’evenets del sistema de fitxers.

Un exemple d’especte que pot tenir el incrontab -l:

/var/mail IN_CLOSE_WRITE abc $@/$#

Això captura l’event cada cop que s’hagi canviat un fitxer (IN_CLOSE_WRITE) dins del directori /var/mail, després llença el programa abc passant com a paràmetre el path/file_name. Com podeu veure la potència és infinita i la simplicitat d’ús és impressionant. Potser el que més m’agrada és el fàcil que és tenir centrilitzats tots els directoris que estic controlant i les accions associades. Per exemple, és ideal per mantenir un RSS de les novetats de música, video, descàrregues, etc.

InterScan Messaging de TrendMicro problematic amb kernel 2.4 i ECN

Reading time: 2 – 2 minutes

Tenia un problema amb un servidor de correu SMTP al qual no podia accedir des del servidor de correu de la feina. Encanvi des d’altres IPs del nostre mateix rang si que tenia accés al port 25/tcp d’aquest servidor remot. Doncs resulta que el servidor en qüestió té instal·lat un InterScan Messaging Security Suite de la casa TrendMicro i al servidor de correu de l’empresa jo hi tenia activat el ECN (Explicit Congestion Notification). El problema esta en que quan s’envia un “SYN” en el handshake TCP entre els nostres servidors el paquet TCP té activats dos flags més a part del ‘SYN’, el ‘W’ i el ‘E’.

Aquí podeu veure el que envia el meu servidor amb ECN, flags SWE:

14:12:31.257939 IP 1.1.1.1.44435 > 2.2.2.2.25: SWE 2986809236:2986809236(0) win 5840 <mss 1460,sackOK,timestamp 338497211 0,nop,wscale 0>

El que envia un altre servidor que no té ECN, flag S:

14:13:02.225358 IP 1.1.1.2.37876 > 2.2.2.2.25: S 3871099907:3871099907(0) win 5840 <mss 1460,sackOK,timestamp 3419720577 0,nop,wscale 2>

Per comprovar si realment tenim el ECN activat al kernel:

# cat /proc/sys/net/ipv4/tcp_ecn

Per desactivar el ECN del nostre kernel:

echo 0 > /proc/sys/net/ipv4/tcp_ecn

Només fent això la cosa ja ha començat a funcionar perfectament. Així doncs, dubto que mai a algú li passi algo tan enravassat com el que m’acaba de pasar però aquí queda explicat.

Linux Tips: Modules, Who Are They, And How Do They Work?

Reading time: 2 – 3 minutes

[ Via Migraine Heartache ] A very nice feature of the Linux kernel is the modules. Modules are pieces of software that can be loaded and unloaded from the kernel at runtime. That way you can save kernel memory (and that’s important for small machines, since kernel memory is never swapped out on disk), and keep your kernel clean when you’re not working with some of your hardware.

Especially sound and network cards are really good to compile as modules, since sometimes they are a bit hard to configure, and when you have a module, you don’t have to recompile and reboot your kernel to configure some irq or io setting.

The Tools Used

The tools you use to handle modules are all located in /sbin. They are insmod, modprobe, lsmod, depmod and rmmod.. Notice the names, and compare them to some often used unix commands such as ls and rm and you’ll immediatelly understand what each command will do for you. As always I recommend reading the manual pages. Anyway, here’s what they do:

  • lsmod – List the modules currently loaded. A completely harmless command, even a normal user can run it.
  • rmmod – Removes a loaded module from memory. Checks dependencies, ie if a module needs another module, the other module may not be unloaded before the first one.
  • insmod – Inserts a specific module in memory. You may give a path, or just a filename with or without extension. In the latter case, insmod will search some standard module paths to find the module.
  • modprobe – Inserts a module in memory, but checks dependencies. If you modprobe module A, and module A needs module B to run, modprobe will insert module B and then module A.
  • depmod – Calculates dependencies between modules needed by the other module commands. This is most probably done by your init scripts at system startup, but some time you may need to execute a depmod -a.
  • modinfo – ens dona informació sobre les funcions del mòdul, els paràemtres que suporta, etc.(se l’havien deixat)

How do I make modules?

When you compile your kernel, you choose to make things as modules, instead of into the kernel itself.

The modules will be installed when you do a make modules ; make modules_install after your kernel is compiled. They will reside in a directory called /lib/modules/xx.yy.zz where xx.yy.zz is your kernel version.