Useful linux daemons for better PC experience
Tags: linux
Published: 2019-07-10 08:51:55

---
title: Useful linux daemons for better PC experience
published: true
description: 
tags: linux
---
Hi, dev.to!

Linux is great OS, but it's default behavior is usually optimized for servers. Since more and more developers use it as theirs main operating system, I thought that it would be nice to share several useful daemons that will make this experience a little bit smoother.

I intentionally didn't include any installation instructions to stay distro-agnostic. If you are interested in one of them and want to have it in your system, please go carefully through installation and configuration instructions yourself. I don't want to break your system :)

## Irqbalance
{% github Irqbalance/irqbalance %}

What does the description above mean in practice? Suppose we are running Intellij Idea and it decides that it is a good time to update indices - right in the middle of the compilation process while we are watching a nice film or peertube video.

The `irqbalance` daemon will try to keep that "heavy" process as far away from our video thread as possible. This means that despite cpu overloading the system won't "hang": the cursor will be me responsive and the video will continue playing.

Nice thing to have, right?

## haveged
{% github jirka-h/haveged %}
In linux we have two random generators:`/dev/randon` and `/dev/urandom`. The first one is really fast, but it is predictable: one should not use it for any security-related things. The second one is more reliable, but can be very slow sometimes.

The reason for that is that the kernel must collect enough entropy from outer sources (like CPU temperature, fan rpm and so on) to give you a truly random number. Even in a long time after boot the system may run out of entropy and processes will have to wait until there will be enough of it.

I faced this several times, for example when I tried to connect via `ssh` right after the system booted. The process have just "hung".

To avoid this kind of issues you may install `haveged` daemon. It uses additional algorithms to faster fill the entropy device and ensures that there is always enough of it.

## fstrim
Due to the architecture of SSDs, theirs memory chunks are "wearing out" when they are accessed. So it is a good idea to redistribute a load evenly between all chunks. To accomplish this we have to shuffle the data on the drive periodically.

This operation is called TRIM and it can seriously prolong the life of your drive. More details and instructions can be found [here](https://www.digitalocean.com/community/tutorials/how-to-configure-periodic-trim-for-ssd-storage-on-linux-servers)

## earlyoom
{% github rfjakob/earlyoom %}
Remember when we decided to launch Intellij Idea while watching a video? Let's imagine that we have only 8Gb of RAM - the amount that is totally insufficient for this (ARGH!).

The Idea's JVM will first eat all `Xms` then all `Xmx` RAM, then we'll extend `Xmx` it up to 6Gb, but there is also a `PermGen` storage eating space and a movie and also you might want to run browser. The result is simple - we are terribly running out of memory.

In cases like this, linux has a special mechanism called OOM-killer. When things goes bad like in the example above, linux just finds the most "greedy" process and kills it, so that other processes stay safe and alive. If it's not done than the computer will always be running "heavy" request that it is unable to satisfy and there will be no resources for anything else: the system will just hang.

So, OOM-killer is your friend. The problem with it: it usually comes too late. The linux will fist try to move all your memory chunks to swap partition on disk drive and from that moment your desktop environment will freeze and whole system will become unresponsive. Much later, when linux ensures that there is only one way left, he'll call for killer. And unfortunately this behavior is not configurable (but you can call it manually, refer to the next section).

`earlyoom` can help you in this case. From docs:
> earlyoom checks the amount of available memory and free swap up to 10 times a second (less often if there is a lot of free memory). By default if both are below 10%, it will kill the largest process.

## [SysRq](https://en.wikipedia.org/wiki/Magic_SysRq_key)
###### Well, technically it is not a daemon at all, but it still fits the list of "preventing your systems from becoming unresponsive" tricks.

Missing `Ctrl`+`Alt`+`Del` from Windows? It is a life-savior when things gone bad and we want to recover system somehow. In linux we've got a better solution, but you have to enable it first.


![SysRq key](https://upload.wikimedia.org/wikipedia/commons/e/ec/Printscreen1.jpg)

Remember strange `SysRq` key on your keyboard? It is magical! No, really, we call a shortcuts that involves it [Magic SysRq keys](https://en.wikipedia.org/wiki/Magic_SysRq_key) :). You have two ways to enable it:
* Add `sysrq_always_enabled=1` to kernel boot parameters
* Add `kernel.sysrq=1` to `sysctl` configuration (usually `/etc/sysctl.conf` or `/etc/sysctl.d/99-sysctl.conf`)

After that the whole list of commands from section header link becomes available. The most useful ones:
* `SysRq` + `F`: Calls for OOM-killer
* `SysRq` + `R`,`E`,`I`,`S`,`U`,`B`: Safely dumps caches to drive and performs a reboot.

These key combinations are handled directly by the kernel and will help you to recover (or safely reboot) if nothing else helps.

## fail2ban
{% github fail2ban/fail2ban %}
Once I developed a web application at work and had a web server running in developer mode with logging all request. I forgot to turn it off before heading home and when I came back to an office in the morning - I was surprised! The log file was filled with tons of strange requests like:
```
404 GET /phpmyadmin/index.php
404 GET /ldap-accont-manager/index.php
404 GET /nextcloud/index.php
```

Also, ssh log was filled with invalid authentication attempts. I notified our security expert and he confessed that it was he who scanned the network to find weak points :)

Anyway,I thought that this situation is dangerous - you can sit in Moonbucks drinking coffee and at the same time someone bruteforces your ssh password. To prevent this kind of attacks meet `fail2ban`.

This daemon monitors logs of various applications (apache, ssh and many more) for invalid auth attempts. If their count from one specific ip crosses the threshold, that ip is blocked using `iptables` rule for some time. Stay safe :)

## Conclusion
Hope your enjoyed this (my fist) post. If you'll find any typos or mistakes - please PM me. If you have suggestions or something to add - leave a comment. Have a nice day!