Linux

How to fix Alpine Linux wg-quick (no such package)

Problem:

While trying to install wg-quick on Alpine Linux, you see the following error message:

ERROR: unable to select packages:
  wg-quick (no such package):
    required by: world[wg-quick]

Solution:

You need to install the wireguard-tools package which also contains wg-quick:

apk add wireguard-tools

 

Posted by Uli Köhler in Alpine Linux, Wireguard

How to install OpenCascade on Ubuntu 22.04

The following command installs a recommended subset of the OpenCascade packages (and development headers) using apt. Since the packages are now available from the official repositories, you don’t need to add extra repositories.

sudo apt install -y xfonts-scalable libocct-data-exchange-dev libocct-draw-dev libocct-foundation-dev libocct-modeling-algorithms-dev libocct-modeling-data-dev libocct-ocaf-dev libocct-visualization-dev

 

Posted by Uli Köhler in Linux, OpenCASCADE

How to fix Ubuntu /usr/bin/env: ‘python’: No such file or directory

In recent Ubuntu versions, you can encounter the error message

/usr/bin/env: 'python': No such file or directory

for some scripts that want to be executed using python where only python3 is available (python2 has long been deprecated).

In order to fix the issue, install the python-is-python3 package which makes python and alias of python3:

sudo apt -y install python-is-python3

 

Posted by Uli Köhler in Linux

How to install Gmsh (mesh generator) on Ubuntu 22.04

The best way to install Gmsh on any recent Ubuntu version is to use pip:

sudo pip3 install --upgrade gmsh

In case you don’t have pip3 installed, run

sudo apt -y install python3-pip

After installation, you can open gmsh by just entering

gmsh

in the terminal of your choice. In case you see an error message such as

/usr/bin/env: 'python': No such file or directory

see our post How to fix Ubuntu /usr/bin/env: python: No such file or directory for more details on how to fix this issue.

Posted by Uli Köhler in Linux

How to get domain names (SANs) for TLS certificate using OpenSSL

Use the following command to extract the X509 Subject Alternative Names in human-readable form from any SSL certificate file (cert.pem in this example):

openssl x509 -in cert.pem -noout -text | grep "Subject Alternative Name" --after=1

Example output:

X509v3 Subject Alternative Name: 
    DNS:*.techoverflow.net, DNS:techoverflow.net

 

Posted by Uli Köhler in Linux

How to remove container_name: … statements from docker-compose.yml automatically using sed

This script will remove alle container_name statements from a docker-compose.yml config file:

sed -zie 's#container_name:\s*[a-zA-Z0-9_-]*\s*##g' docker-compose.yml

Example input:

services:
    inventree-proxy:
        container_name: inventree-proxy
        image: nginx:stable
        depends_on:
            - inventree-server
[...]

 

Example output:

services:
    inventree-proxy:
        image: nginx:stable
        depends_on:
            - inventree-server
[...]

 

Posted by Uli Köhler in Docker, Linux

How to generate random port number on the command line

The following Linux shell command will generate a random port number between 1024 and 65535 (inclusive):

shuf -i 1024-65535 -n 1

Example:

$ shuf -i 1024-65535 -n 1
45508

 

Posted by Uli Köhler in Linux, Networking

How to set and verify v4l2-ctl parameters in Python using subprocess

The following code uses the v4l2-ctl executable to get and set v4l2 parameters such as exposure_absolute. It also provides means of writing a parameter and verifying if it has been set correctly.

def v4l2_set_parameters_once(params, device="/dev/video0"):
    """
    Given a dict of parameters:
    {
        "exposure_auto": 1,
        "exposure_absolute": 10,
    }
    this function sets those parameters using the v4l2-ctl command line executable
    """
    set_ctrl_str = ",".join([f"{k}={v}" for k,v in params.items()]) # expsosure_absolute=400,exposure_auto=1
    subprocess.check_output(["v4l2-ctl", "-d", device, f"--set-ctrl={set_ctrl_str}"])

def v4l2_get_parameters(params, device="/dev/video0"):
    """
    Query a bunch of v4l2 parameters.
    params is a list like
    [
        "exposure_auto",
        "exposure_absolute"
    ]
    
    Returns a dict of values:
    {
        "exposure_auto": 1,
        "exposure_absolute": 10,
    }
    """
    get_ctrl_str = ",".join([f"{k}" for k in params])
    out = subprocess.check_output(["v4l2-ctl", "-d", device, f"--get-ctrl={get_ctrl_str}"])
    out = out.decode("utf-8")
    result = {}
    for line in out.split("\n"):
        # line should be like "exposure_auto: 1"
        if ":" not in line:
            continue
        k, _, v = line.partition(":")
        result[k.strip()] = v.strip()
    return result

def v4l2_set_params_until_effective(params, device="/dev/video0"):
    """
    Set V4L2 params and check if they have been set correctly.
    If V4L2 does not confirm the parameters correctly, they will be set again until they have an effect
    
    params is a dict like {
        "exposure_auto": 1,
        "exposure_absolute": 10,
    }
    """
    while True:
        v4l2_set_parameters_once(params, device=device)
        result = v4l2_get_parameters(params.keys(), device=device)
        # Check if queried parameters match set parameters
        had_any_mismatch = False
        for k, v in params.items():
            if k not in result:
                raise ValueError(f"Could not query {k}")
            # Note: Values from v4l2 are always strings. So we need to compare as strings
            if str(result.get(k)) != str(v):
                print(f"Mismatch in {k} = {result.get(k)} but should be {v}")
                had_any_mismatch = True
        # Check if there has been any mismatch
        if not had_any_mismatch:
            return

Usage example:

v4l2_set_params_until_effective({
    "exposure_auto": 1,
    "exposure_absolute": 1000,
})

 

Posted by Uli Köhler in Audio/Video, Linux, OpenCV, Python

How are OpenCV CAP_PROP_… mapped to V4L2 ctrls / parameters?

From both the OpenCV documentation and the V4L2 documentation, it is unclear how all the CAP_PROP_... parameters are mapped to v4l2 controls such as exposure_absolute.

However, you can easily look in the source code (int capPropertyToV4L2(int prop) in cap_v4l.cpp) in order to see how the parameters are mapped internally. Github link to the source code

Continue reading →

Posted by Uli Köhler in Audio/Video, Linux, OpenCV

How to make Jupyter Lab open using Chrome instead of Firefox on Linux

Note: This specific post only covers Jupyter Lab – not Jupyter Notebook. I have no post for Jupyter Notebook so far, but you can use a similar method here, just with slightly different config names etc.

On Ubuntu, my Jupyter Lab always opens using firefox whereas I generally want to use chrome.

In order to fix this, I first needed to generate a default config file (the echo -e "\n" part is to automatically answer no when prompted if any existing config file should be overwritten:

echo -e "\n" | jupyter lab --generate-config

Now the config file in ~/.jupyter/jupyter_lab_config.py contains this line:

# c.ServerApp.browser = ''

which we can automatically un-comment and set to chrome using:

sed -i -e "s/# c.ServerApp.browser = ''/c.ServerApp.browser = 'google-chrome'/g" ~/.jupyter/jupyter_lab_config.py

The resulting line looks like this:

c.ServerApp.browser = 'google-chrome'

Full script to use google-chrome instead of firefox

This is a script which you can copy & paste directly into your command line:

echo -e "\n" | jupyter lab --generate-config
sed -i -e "s/# c.ServerApp.browser = ''/c.ServerApp.browser = 'google-chrome'/g" ~/.jupyter/jupyter_lab_config.py
Posted by Uli Köhler in Linux, Python

How to install avidemux3 on Ubuntu 22.04

You can install Avidemux 3 (QT) from the xtradebs PPA:

sudo apt -y install software-properties-common apt-transport-https -y
sudo add-apt-repository -y ppa:xtradeb/apps
sudo apt -y install avidemux-qt

After that, run

avidemux3_qt5

to run Avidemux 3.

Posted by Uli Köhler in Audio/Video, Linux

How I fixed Visual Studio Code Remote SSH “Waiting for server log…” SSH

Problem:

When you try to connect Visual Studio Code to a computer (such as a Raspberry Pi) using SSH, you see log messages like

[16:23:13.444] > Waiting for server log...
[16:23:13.480] > Waiting for server log...
[16:23:13.520] > Waiting for server log...
[16:23:13.560] > Waiting for server log...
[16:23:13.598] > Waiting for server log...
[16:23:13.638] > Waiting for server log...

and VS code restarts the connection process repeatedly without connecting

Solution:

In my case, the ~/.vscode-server on the device where you’re connecting to was corrupted.

Therefore, the solution was to manually login using ssh to the device as the same user you’re using for VS code remote SSH and then running

rm -rf ~/.vscode-server

After that, just reconnect using VS code – it will automatically re-install the server.

Posted by Uli Köhler in Linux

How I fixed my old Sony Laptop backlight under Ubuntu 22.04

Until recently, my old Sony laptop was running Ubuntu 20.04 with the nvidia-340 proprietary graphics drivers. After upgrading to Ubuntu 22.04, the nvidia proprietary graphics driver didn’t work, so I uninstalled it – the noveau graphics driver worked fine, except that the backlight buttons didn’t work (the corresponding popup was shown but the backlight was stuck at full brightness).

In /sys/class/backlight there were both the nv_backlight and the sony directory. Writing to nv_backlight using

sudo echo 20 > /sys/class/backlight/nv_backlight/brightness

worked perfectly.

In order to permanently fix the issue and make the backlight buttons work again, I had to modify the kernel command line used at boot. By adding acpi_backlight=video in /etc/default/grub by appending it to the GRUB_CMDLINE_LINUX_DEFAULT= line.

Now said line looks like this:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash acpi_sleep=nonvs acpi_backlight=video"

After that, I ran sudo update-grub and rebooted the computer. After the reboot, the backlight worked fine.

Posted by Uli Köhler in Linux

How to install mosquitto_sub on Ubuntu 22.04

You can install the mosquitto client (mosquitto_sub) by running

sudo apt -y install mosquitto-clients

 

Posted by Uli Köhler in Linux, MQTT

How to use custom port for ZeroTier instead of 9993 on Linux

You can create /var/lib/zerotier-one/local.conf – by default it doesn’t exist, if it does, just add these settings:

{
  "settings": {
    "primaryPort": 9994
  }
}

 

Posted by Uli Köhler in Linux, Networking, ZeroTier

How to install gcsfuse on Ubuntu 22.04 (jammy)

If you try to install gcsfuse on Ubuntu 22.04, you will face two issues. First, the signature can’t be verified due to the new method of managing APT keys. Secondly, the gcsfuse repo does not have packages for Ubuntu 22.04 even 5 months after its release:

Err:10 http://packages.cloud.google.com/apt gcsfuse-jammy Release                                                                                                                  
  404  Not Found [IP: 64.233.161.101 80]

You can work around both issues by force-installing the bionic packages which appear to work fine for me.

export GCSFUSE_REPO=gcsfuse-bionic
echo "deb [signed-by=/usr/share/keyrings/gcsfuse.gpg] http://packages.cloud.google.com/apt $GCSFUSE_REPO main" | sudo tee /etc/apt/sources.list.d/gcsfuse.list
wget -qO- https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor --yes --output /usr/share/keyrings/gcsfuse.gpg

 

Posted by Uli Köhler in Linux

How to fix apt error: NO_PUBKEY FEEA9169307EA071 NO_PUBKEY 8B57C5C2836F4BEB

Problem:

When running apt update, you see the following error message:

Err:12 http://packages.cloud.google.com/apt gcsfuse-bionic InRelease
  The following signatures couldn't be verified because the public key is not available: NO_PUBKEY FEEA9169307EA071 NO_PUBKEY 8B57C5C2836F4BEB

Solution:

This error occurs due to the VirtualBox repo using the outdated (as of April 2022) apt-key method of importing keys.

You can import the key by just installing the gcsfuse repo again. The following commands are based on the official installation guide.

export GCSFUSE_REPO=gcsfuse-`lsb_release -c -s`
echo "deb [signed-by=/usr/share/keyrings/gcsfuse.gpg] http://packages.cloud.google.com/apt $GCSFUSE_REPO main" | sudo tee /etc/apt/sources.list.d/gcsfuse.list
wget -qO- https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor --yes --output /usr/share/keyrings/gcsfuse.gpg

and then running

apt update

again. Note that at the time of writing this post, gcsfuse is not available for Ubuntu 22.04 in the official repository.

You can work around this by installing the bionic packages instead of jammy. For more details, see our post

Posted by Uli Köhler in Linux

How to generate random hex string using OpenSSL

The following command will generate a 32-bytes long random hex string

openssl rand -hex 16

Example:

$ openssl rand -hex 16
122166dd9e2d617246da9af9d7d3cd6f

The command lists 16, not 32 because OpenSSL will generate 16 random binary bytes and then convert these 16 bytes into 32 hex characters.

Posted by Uli Köhler in Linux

How to print fingerprints of all SSH host keys

In oder to print new-style (SHA256) fingerprints such as Is7oLjYcKue/zRjPKy9gQma/9F24KGaMPHbGberPGAw, use

for i in /etc/ssh/ssh_host_*_key; do sudo ssh-keygen -l -E sha256 -f "$i"; done

Example output:

1024 SHA256:SAhM78NWwqoYS/nRByGs1oqJxrX8fZL/PR/F5oX3QWs root@myserver (DSA)
256 SHA256:V/+9872Yg1i2Q4i7Nt4ef5R06lAuzT6qKemaQEt8EJQ root@myserver (ECDSA)
256 SHA256:Is7oLjYcKue/zRjPKy9gQma/9F24KGaMPHbGberPGAw root@myserver (ED25519)
2048 SHA256:AORSBitRPpwXrbTQzP0Mb/OBXyK+V6gyWf0S4bIh7Ys root@myserver (RSA)

In order to print old-style (MD5) fingerprints such as 5c:19:8b:94:44:5e:b6:15:e6:32:cc:3c:9b:38:6b:4c, use this command:

for i in /etc/ssh/ssh_host_*_key; do sudo ssh-keygen -l -E md5 -f "$i"; done

Example output:

1024 MD5:6d:f8:17:9b:4a:f5:ec:14:04:68:22:9c:ca:2c:a1:19 root@myserver (DSA)
256 MD5:5c:19:8b:94:44:5e:b6:15:e6:32:cc:3c:9b:38:6b:4c root@myserver (ECDSA)
256 MD5:45:dd:7f:91:89:67:ab:2d:ee:a2:f8:9e:18:68:5b:0d root@myserver (ED25519)
2048 MD5:1b:b2:54:3a:10:51:17:b4:49:af:73:c2:da:d6:fc:df root@myserver (RSA)
Posted by Uli Köhler in Linux

How to fix Ubuntu PHP Uncaught Error: Call to undefined function simplexml_load_file()

Problem:

Your wordpress page or other PHP application displays a critical error, with the following message:

2022/09/17 04:11:49 [error] 98082#98082: *1053 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Error: Call to undefined function simplexml_load_file() in /var/sites/mysite.com/wp-content/plugins/otgs-installer-plugin/vendor/otgs/installer/includes/class-wp-installer.php:870

Solution:

Install the php[VERSION]-xml package for the PHP version that is running wordpress.

For example:

sudo apt -y install php8.1-xml
Posted by Uli Köhler in Linux