Top 10 Linux tricks

Hello! Today we bring you the 10 best tricks for linux, especially at the system administration level. These tricks will help you in everyday administration tasks.

1- Increasing the size of a virtual disk in RAW format

When we are virtualizing with virtualization systems that allow RAW format and we need to increase the size of the disk we can do it in the following way: “truncate -s+00XX path” where 00 is the size to increase and XX is the format (GB, TB, etc). With a more practical example in which we have to increase 90 GB, we would do it like this:

truncate -s+90GB file_path

2- When we run out of RAM and can’t reboot

When we run out of memory, we can’t reboot and need to keep a service active. If we have enough storage, we have a temporary solution. We can increase the available swap by adding a virtual swap. We have to do it like this:

  • dd if=/dev/zero of=/PATH_DESTINATION bs=1M count=1024 -> This will create a 1GB file, if we want 2GB modify 1M to 2M.
  • mkswap /PATH_DESTINATION -> format the file we have created as SWAP
  • Add the line “/PATH_DESTINATION none swap sw 0 0” to the fstab file so that it will be mounted again if we reboot.
  • Execute “swapon -a”, this will mount the virtual swap and we will have this new space so that when we start to run out of RAM the system does not crash.

3- Gain up to 5% disk space without reboot

In EXT3 and EXT4 file systems, 5% of the space of the default partition is reserved by default. That is, when the partition is at 100%, it is actually at 95%. If we need this space, for example when we are close to 100% on a production system, to save time and not having to reboot, we can run tune2fs.

  • The first thing to run is: tune2fs -l /dev/sda2 | grep “^Reserved”.
    • With this we have the number of reserved blocks, each block occupies 4096, so if we divide it 3 times by 1024, we can get the size in GB that we will free up.
  • Then we have to execute: “tune2fs -m PERCENTAGE” where the maximum percentage that we can put is 5 and the minimum 1 and that percentage will be released when executing the command.

4- Delete ARP table

Sometimes the ARP table is cached storing MAC addresses that no longer correspond to an IP and this can lead to connection problems. To clear the ARP table and make linux “learn” again all the relationship between MAC and IP you can execute: “ip -s -s neigh flush all dev DEVICE”.

5- Change the maximum number of descriptors

When all the descriptors of the system are occupied, problems can be experienced when making connections, saving or creating files or running applications. The solution to this is to increase the number of descriptors allowed by the system. Below is a table with the commands for handling descriptors:

ComandoDescripción
ulimit -Snsee maximum soft descriptors
ulimit -Hnsee maximum hard descriptors
ulimit -aList limits
ulimit -n 94000change max open files to 94000, can be put in /etc/rc.local to run at system startup

6- Prevent OOM Killer from killing a process

Sometimes there can be problems due to excessive memory consumption in a system, it is possible that OOM Killer kills a critical process for our production system. You can prevent this from happening by modifying the process score so that it is not chosen to die: “echo -17 > /proc/$PID/oom_adj”.

7- Locate the correct MTU

Sometimes networks do not work as optimally as they should and it may be due to the MTU, to find out what MTU we should establish, we can execute: “ping -M do -s 1500 -c 1 HOST-NAME”.

We execute the command, the option “-s” is the MTU to test. If we get an error, we have to lower the amount until we get a correct ping:

ger@portatil:~$ ping -M do -s 1473 -c 1 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 1473(1501) bytes of data.
ping: local error: Message too long, mtu=1500
^C
--- 192.168.1.1 ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms

ger@portatil:~$ ping -M do -s 1472 -c 1 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 1472(1500) bytes of data.
1480 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=2.36 ms

--- 192.168.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 2.365/2.365/2.365/0.000 ms

8- How to redirect a port to another one

We may need to redirect one port to another either because we are migrating to another host and we want to redirect the traffic to the new one or because we want to redirect a public port to a private port of another host in the internal network. For this we can use socat. This is a software that allows to redirect only TCP ports, the way to use it is the following: socat TCP4-LISTEN:8080,bind=x.x.x.x.x,fork,reuseaddr TCP4:y.y.y.y.y.y:80

In this example port 8080 of x.x.x.x.x is redirected to port 80 of y.y.y.y.y.

9- Knowing the hardware data of a drive

Sometimes we need to identify physical data of a disk such as the serial number. For example if we have a software RAID on a machine that does not have hot swap disks and a disk crashes, to recover the RAID we will need to remove the correct disk. This can only be known by knowing the serial number. You can find this out by running this command: “hdparm -i /dev/DISPOSITIVE”.

10- Know the 20 most memory consuming processes

With the “top” command we can know the most consuming processes but not as precisely as with ps. We can execute it in the following way to have the 20 most memory consuming processes:

ps aux | awk '{print $2, $4, $11}' | sort -k2r | head -n 20

 

For today has been all, these are the 10 best tricks for linux for us.

I hope you liked it. If you liked it, please share it on your social networks.

See you soon!

Leave a Reply