Linux Tricks

Mort Yao

(Note: This page contains tips and tricks which are specific to the Linux kernel or a certain Linux distribution. For cross-platform utilities that may run on Linux, e.g., GNU, see CLI tricks instead.)

1 Kernel

1.1 Useful information

Show the parameters passed to the kernel at the time it was started:

$ cat /proc/cmdline

Show the config options of the current running kernel:

$ cat /proc/config.gz | gunzip

Or:

$ zcat /proc/config.gz

2 Kernel modules

2.1 Disable the touchpad

psmouse seems to be the module designated for the touchpad on many laptops. If it annoys when typing, simply remove the module with:

# rmmod psmouse

To bring it back:

# modprobe psmouse

3 util-linux

(Wikipedia: https://en.wikipedia.org/wiki/Util-linux)

3.1 Display system shutdown entries and run level changes

$ last -x | less

3.2 Show bad login attempts

# lastb | less

3.3 View kernel message buffer (and follow it)

In human-readable timestamp: (may be inaccurate!)

$ dmesg -Tw

3.4 Force releasing the swap space

Swapping generally downgrades the overall performance; it is sometimes good to manually release everything back into the RAM.

(Warning: To run this command safely, freely available RAM is assumed to be sufficient; otherwise, the system may kill some processes to make room.)

# swapoff -a && swapon -a

3.5 Synchronize hardware clock with an NTP server

# ntpd -qg
# hwclock --systohc

4 SysV init

(Wikipedia: https://en.wikipedia.org/wiki/Init)

4.1 Start a system service (e.g., httpd)

# /etc/rc.d/httpd start

Or:

# rc.d start httpd

5 systemd

(Wikipedia: https://en.wikipedia.org/wiki/Systemd)

5.1 Start a system service (e.g., httpd)

# systemctl start httpd

5.2 View systemd journal (reverse chronologically)

$ journalctl -r

5.3 View kernel message log from current boot (and follow it)

$ journalctl -kf

5.4 Write a simple service

Create a file foobar.service:

[Unit]
Description=My dumb daemon

[Service]
ExecStart=/bin/sh -c "foo bar"

[Install]
WantedBy=multi-user.target

Copy the file to systemd’s designated place and enable the service:

# cp -v foobar.service /etc/systemd/system/
# systemctl enable foobar

6 Distro-specific

6.1 Arch Linux

Find out which package owns the program:

$ which cpp | pacman -Qo -

List all installed packages, in descending order of size:

$ expac -s -H M "%-30n %m" | sort -rhk 2