Linux

Generic

Kernel

Send a SysRq trigger

Remember the code B.U.S.I.E.R.

echo b > /proc/sysrq-trigger

Get a view of a mounted filesystem

To obtain a view of a mounted filesystem (e.g. /) Without anything mounted on top

mount --bind / /mnt

Obtain module information

To get some kernel module information and parameter description the command modinfo can be used.

# modinfo iTCO_wdt
filename:       /lib/modules/4.9.0-1-amd64/kernel/drivers/watchdog/iTCO_wdt.ko
alias:          platform:iTCO_wdt
license:        GPL
version:        1.11
[...]

If the module is loaded, the kernel can be queried for information using systool.

# systool -v -m r8169
Module = "r8169"

  Attributes:
    coresize            = "81920"
    initsize            = "0"
    initstate           = "live"
[...]

QEMU

Resize QCOW2 image from host system

Shutdown the machine and use the qemu-img command to resize the image. This example shows how to add 20 gigabytes to the vm-storage.img qcow2 image.

qemu-img resize vm-storage.img +20G

Once this is done, you need to resize the partitions inside. The virt-resize tool can be used for this (It's capable of resizing, shrinking or ignore different partitions inside the same image). This example shows how to expand the first partition on the vm-storage.img image to fill the available space.

virt-resize --expand /dev/sda1 vm-storage.img vm-storage-out.img

The output image (here vm-storage-out.img) must be already present. A simple way with qcow2 images is simply to copy the source disk to a new destination file and then write on that.

cp vm-storage.img vm-storage-out.img

To perform the commands on debian, install the libguestfs-tools package.

apt install libguestfs-tools

Access to system domain with virsh as standard user

To access all local system domain with virsh, first, the user must be part of the libvirt group.

usermod -a -G libvirt <username>

Then, one must set the LIBVIRT_DEFAULT_URI environment variable to qemu:///system. Here is an example for bash.

export LIBVIRT_DEFAULT_URI=qemu:///system

Disk management

Mount partition contained in a disk image

To mount a partition inside a raw disk image, one must know the offset (in bytes) of that partition from the start of the file. Then it can be mounted using the correct parameter for the mount command:

mount -o loop,ro,offset=xxxxxx disk_image.raw /mnt/image/

To find the correct offset one can use a tool like parted.

# parted disk_image.raw
GNU Parted 3.2
Using /path/to/disk_image.raw
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) unit
Unit?  [compact]? B
(parted) print
Disk /path/to/disk_image.raw: 250059350016B
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number  Start         End            Size           File system     Name  Flags
 1      1048576B      16000221183B   15999172608B   linux-swap(v1)
 2      16000221184B  250059161599B  234058940416B  ext4

The number below the Start column is the offset.

Format FAT32 partition with 32KB cluster size

mkdosfs /dev/diskpart -s 64 -S 512 -F 32

Networking

Show listening processes

The netstat tool is useful to show data on the port where processes are listening.

netstat -l

To show open TCP ports and established connections

netstat -vatn

To show open UDP ports

netstat -vaun

SSH

Setup keys for key-based auth

On the client machine (where you want to connect from)

ssh-keygen -t rsa

After key generation is completed, transfer the key to the server machine (where you want to connect to). Supposing you saved your keys in ~/.ssh

cat ~/.ssh/id_rsa.pub | ssh user@server 'cat >> .ssh/authorized_keys'

SCP with middle host jump

Copy file from host C to host A, where the connection is A-B-C and A/C have no direct communication.

With OpenSSH <7.3

A$ scp -oProxyCommand="ssh -W %h:%p B" thefile C:destination

Otherwise

A$ scp -oProxyJump=B thefile C:destination
>>>>>>> 562edbfdf64089154651be6602e39120b4c35c87

Wayland

My .profile is ignored!

Wayland ignores .profile. You can still set environment variables leveraging the files in ~/.config/environment.d.

$ cd ~/.config/environment.d/
$ cat 60-openssl.conf
OPENSSL_ENABLE_MD5_VERIFY=1
NSS_HASH_ALG_SUPPORT=+MD5

GNOME 3

Disable automatic download of updates

Disable the automatic downloading of updates in background

gsettings set org.gnome.software download-updates false

Benchmarks

sysbench

The sysbench tool can be used to stress I/O, RAM access or CPU.

sysbench --test=fileio --file-total-size=512M prepare
sysbench --test=fileio --file-total-size=512M --file-test-mode=rndrw --init-rng=on --max-time=600 --max-requests=0 run
sysbench --test=fileio --file-total-size=512M cleanup
sysbench --test=memory run --memory-total-size=1G
sysbench --test=memory run --memory-total-size=1G --memory-oper=read
sysbench --test=cpu --cpu-max-prime=20000 run

User management

Force logout an user

The skill tool can be used to forcefully logout an user.

skill -KILL -u <username>

Or the session can be terminated by killing the parent process

$ who
fbattaglia pts/0        2017-02-23 18:50 (2001:470:c89a:0:3655:ee9c:2c74:1282)
$ ps -dN|grep pts/0
17904 pts/0    00:00:00 bash
$ kill -9 17904

Misc

GPG Pinentry issues on command line

Check ~/.gnupg/gpg-agent.conf and set pinentry to

pinentry-program /usr/bin/pinentry-tty

or

pinentry-program /usr/bin/pinentry-sh

Then restart the agent with

gpg-connect-agent reloadagent /bye

Converting from hex representation back to bytes

One can use the xxd tool

echo 5C1840 | xxd -p -r > data.bin

Printing an hex dump of a binary file splitting at byte length

We can use hexdump

cat filename.bin | hexdump -v -C

Fix fontconfig ugliness in Visual Studio Code

Create the following file

/usr/local/share/fontconfig/conf.avail/99-vscode.conf

With this content:

<?xml version='1.0'?>
<!DOCTYPE fontconfig SYSTEM 'fonts.dtd'>
<fontconfig>
  <!-- Force RGBA subpixel aliasing to none in code and code-insiders -->
  <match target="pattern">
    <or>
      <test name="prgname">
        <string>code</string>
      </test>
      <test name="prgname">
        <string>code-insiders</string>
      </test>
    </or>
    <edit name="rgba" mode="assign">
      <const>none</const>
    </edit>
  </match>
</fontconfig>

And then create a link to the following path

/etc/fonts/conf.d/99-vscode.conf

Progress of "sync" operation

You can check the progress of the sync op by using /proc/meminfo

watch grep -e Dirty: -e Writeback: /proc/meminfo

Debian

Generic

Enable local namespace for users

Enable this to allow some applications to work in a sandbox (e.g. iridium). This applies to both Debian 9 and current (as the time of writing) sid.

echo kernel.unprivileged_userns_clone=1 > /etc/sysctl.d/00-local-userns.conf
service procps restart

Add an arch to dpkg

This works from dpkg 1.16.2 onwards.

dpkg --add-architecture <arch>

List installed or residual kernel images

To list installed kernel images/headers and residual configurations, this can be used.

dpkg --list | egrep -i --color 'linux-image|linux-headers'

Find which package a file belongs to

To find which package owns a file, the tool apt-file can be used.

apt-file search /path/to/file

But first, the tool must be installed and the local database updated.

apt install apt-file
[...]
apt-file update
[...]

Copy all files of a single type into a directory

The find command can be used for this

find . -name "*.extension" -type f -exec cp -v {} ./destination-dir \;

Debian 8 - Jessie

Debian 9 - Stretch

Where did 'reboot' go?

Debian 9 uses systemctl to handle reboots, shutdowns, etc. The old SysV style commands can be restored by installing a compatibility package.

apt install systemd-sysv

Where did 'ifconfig' go?

Debian 9 uses the new ip tool to handle interface configuration. The old style ifconfig can be restored by installing a package.

apt install net-tools

Missing '/etc/resolv.conf'

The resolv.conf file is now managed by systemd-resolved service: to leverage the file updated by this service one can create a link this way

ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf

Example network interface using dhcp

# cat /etc/systemd/network/wired.network
[Match]
Name=eth0

[Network]
DHCP=ipv4
IPv6AcceptRA=true

[DHCP]
UseDNS=true
UseNTP=true
SendHostname=true
UseDomains=true

Back to Menu