Linux

Autoinstall script for systemd service

The following script will install a systemd service file into /etc/systemd/system/ and enable it (i.e. start on boot) and start it immediately.

#!/bin/bash
# This script installs and enables/starts a systemd service
export NAME=MyService

# Create service file
cat >/etc/systemd/system/${NAME}.service <<EOF
# ADD SYSTEMD SERVICE FILE CONTENT HERE
EOF

# Enable and start service
systemctl enable --now ${NAME}

In order to modify the script for your systemd service, replace MyService by the desired name of your service in

export NAME=MyService

and insert the content of your .service file at

# ADD SYSTEMD SERVICE FILE CONTENT HERE

Full example

The following complete example installs a systemd service named MyService that runs /usr/bin/python3 myscript.py:

#!/bin/bash
# This script installs and enables/starts a systemd service
export NAME=MyService

# Create service file
cat >/etc/systemd/system/${NAME}.service <<EOF
[Unit]
Description=MyService

[Service]
Type=oneshot
ExecStart=/usr/bin/python3 myscript.py
WorkingDirectory=/opt/myservice

[Install]
WantedBy=multi-user.target
EOF

# Enable and start service
systemctl enable --now ${NAME}

 

Posted by Uli Köhler in systemd

How to install ruby & rubygems in Alpine Linux

Problem:

You want to install ruby and the gem package manager in Alpine linux, but running apk install ruby rubygems shows you that the package doesn’t exist

/ # apk add ruby rubygems
ERROR: unable to select packages:
  rubygems (no such package):
    required by: world[rubygems]

Solution:

gem is included in the ruby package. So the only command you need to run is

apk update
apk add ruby

Example output:

/ # apk add ruby
(1/7) Installing ca-certificates (20191127-r5)
(2/7) Installing gdbm (1.19-r0)
(3/7) Installing gmp (6.2.1-r0)
(4/7) Installing readline (8.1.0-r0)
(5/7) Installing yaml (0.2.5-r0)
(6/7) Installing ruby-libs (2.7.3-r0)
(7/7) Installing ruby (2.7.3-r0)
Executing busybox-1.32.1-r6.trigger
Executing ca-certificates-20191127-r5.trigger
OK: 928 MiB in 154 packages

After doing that, you can immediately use both ruby and gem.

Posted by Uli Köhler in Alpine Linux, Container, Docker, Linux, Ruby

How to enable SSH root login on Alpine Linux

Note: Be aware that enabling root access via SSH has security implications!

On Alpine Linux, root SSH access using passwords is disabled by default. The following tutorial shows you how to enable password-based root login via SSH when using openssh. (I have not tested whether root access is enabled when installing Alpine Linux using dropbear instead of openssh)

First, open the SSH config file using

vi /etc/ssh/sshd_config

Now look for this line:

#PermitRootLogin prohibit-password

Press I in order to activate vi editing mode.

Remove the # at the beginning of the line and change prohibit-password to yes:

PermitRootLogin yes

Now save and exit by pressing Esc and then pressing :wq and Enter.

After that, restart openssh using

service sshd restart

Now you can login as root using the password.

Posted by Uli Köhler in Alpine Linux

How to change keyboard layout in Alpine Linux

You can easily change the keyboard layout in Alpine Linux by running

setup-keymap

This will first prompt you for your generic keyboard layout:

Available keyboard layouts:
af     ara    ba     bg     by     cm     de     ee     fi     gb     gr     id     in     is     ke     kz     lk     ma     mk     mt     nl     pk     ro     se     sy     tm     ua     vn
al     at     bd     br     ca     cn     dk     epo    fo     ge     hr     ie     iq     it     kg     la     lt     md     ml     my     no     pl     rs     si     th     tr     us
am     az     be     brai   ch     cz     dz     es     fr     gh     hu     il     ir     jp     kr     latam  lv     me     mm     ng     ph     pt     ru     sk     tj     tw     uz
Select keyboard layout: [none]

In my case, I just type de to select the German keyboard layout and press Enter.

After that, It will prompt you for the keyboard variant to use. For German keyboards this will look like this:

Available variants: de-T3 de-deadacute de-deadgraveacute de-deadtilde de-dsb de-dsb_qwertz de-dvorak de-e1 de-e2 de-mac de-mac_nodeadkeys de-neo de-nodeadkeys de-qwerty de-ro de-ro_nodeadkeys de-ru de-sundeadkeys de-tr de-us de
Select variant (or 'abort'):

If you don’t particularly care about the variant or if you don’t know what than means, just enter whatever you entered in the first step: If you entered de before, enter de again and press Enter.

Now Alpine will configure your Keyboard:

* WARNING: you are stopping a boot service
* Caching service dependencies ... [ ok ]
* Setting keymap ... [ ok ]

The new keymap will be effective immediately and it will also persist after a reboot.

Note that the Alpine Installer (which you can start using setup-alpine) will automatically ask you for the correct keymap – hence, it’s not neccessary to run setup-keymap before running the installer.

Posted by Uli Köhler in Alpine Linux

How to run mkpasswd with yescrypt on Ubuntu/Debian

Currently the Ubuntu/Debian mkpasswd command does not support yescrypt.

In order to use it anyway, we can use the ulikoehler/mkpasswd docker image to run the proper version of mkpasswd:

docker run --rm -it ulikoehler/mkpasswd

This will prompt you for a password and then echo the yescrypt encrypted and salted password:

$ docker run --rm -it ulikoehler/mkpasswd
Password:
$y$j9T$YzrfO5lQkDWahpz5pwYzg/$HzQoMYt.7E1jj.sd6OyYCGI/Qk6oGehNgz5uvY1qp59

 

Posted by Uli Köhler in Docker, Linux

How to install XCP-NG Guest Utilities on Alpine Linux

At the time of writing this, the XCP-NG guest tools ISO does not support Alpine Linux, but you can install the guest utilities using

apk add xe-guest-utilities

Additionally, you need to enable starting the Xen guest utilities on startup:

rc-update add xe-guest-utilities default

Now we can start the utilities manually in order to avoid having to reboot:

/etc/init.d/xe-guest-utilities start
Posted by Uli Köhler in Alpine Linux

How to fix Alpine Linux ERROR: ‘install’ is not an apk command

Problem:

You are trying to install an apk package using e.g.

apk install python

but you see this error message:

ERROR: 'install' is not an apk command. See 'apk --help'.

Solution:

Use apk add instead of apk install to install packages on Alpine Linux !

So instead of

apk install python

use

apk add python

 

Posted by Uli Köhler in Alpine Linux

How to fix Alpine Linux -ash: sudo: not found

Many users who are used to classical Linux distributions will see the following error message when using sudo on Alpine Linux:

-ash: sudo: not found

You don’t need sudo in many use cases!

The easiest way to fix that is to just run the command as root.

In order to get into a root shell (if you are not already logged in as root), use

su

which expects you to enter the root password.

Now just run the original command without sudo.

Still want to install sudo?

If you still want to install sudo, just run

apk add sudo

This is often the best approach if you have e.g. scripts that are running sudo commands.

Posted by Uli Köhler in Alpine Linux

How to fix docker.errors.DockerException: Error while fetching server API version: (‘Connection aborted.’, FileNotFoundError(2, ‘No such file or directory’))

Problem:

While running a docker command like docker-compose pull, you see an error message like

Traceback (most recent call last):
  File "/usr/bin/docker-compose", line 33, in <module>
    sys.exit(load_entry_point('docker-compose==1.27.4', 'console_scripts', 'docker-compose')())
  File "/usr/lib/python3.8/site-packages/compose/cli/main.py", line 67, in main
    command()
  File "/usr/lib/python3.8/site-packages/compose/cli/main.py", line 123, in perform_command
    project = project_from_options('.', options)
  File "/usr/lib/python3.8/site-packages/compose/cli/command.py", line 60, in project_from_options
    return get_project(
  File "/usr/lib/python3.8/site-packages/compose/cli/command.py", line 131, in get_project
    client = get_client(
  File "/usr/lib/python3.8/site-packages/compose/cli/docker_client.py", line 41, in get_client
    client = docker_client(
  File "/usr/lib/python3.8/site-packages/compose/cli/docker_client.py", line 170, in docker_client
    client = APIClient(**kwargs)
  File "/usr/lib/python3.8/site-packages/docker/api/client.py", line 197, in __init__
    self._version = self._retrieve_server_version()
  File "/usr/lib/python3.8/site-packages/docker/api/client.py", line 221, in _retrieve_server_version
    raise DockerException(
docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

Solution:

This means you haven’t started your docker service!

First, try to start it using

sudo systemctl start docker

or

sudo service docker start

or

sudo /etc/init.d/docker restart

(whatever works with your distribution).

After that, retry the command that originally caused the error message to appear.

In case it still shows the same error message, try the following steps:

  • First, check /var/log/docker.log using
    cat /var/log/docker.log

    Check that file for errors during docker startup.

  • Also check if the user you’re running the command as is a member of the docker group. While insufficient permissions will not cause a FileNotFoundError(2, 'No such file or directory')), but a Permission denied, the error message might look similar in some cases.
Posted by Uli Köhler in Container, Docker, Linux

How to install docker & docker-compose on Alpine Linux

In this post, we’ll show how to install docker-compose (and the docker backend) on Alpine Linux.

Step 1: Enable the apk community repository

Edit the repository config as root using

vi /etc/apk/repositories

It should look like this (the exact URLs will vary according to your selected mirror)

#/media/cdrom/apks
http://ftp.halifax.rwth-aachen.de/alpine/v3.13/main
#http://ftp.halifax.rwth-aachen.de/alpine/v3.13/community
#http://ftp.halifax.rwth-aachen.de/alpine/edge/main
#http://ftp.halifax.rwth-aachen.de/alpine/edge/community
#http://ftp.halifax.rwth-aachen.de/alpine/edge/testing

Now find the line that ends in /communityand does NOT end with /edge/communityTypically, this is the second line in the file:

#http://ftp.halifax.rwth-aachen.de/alpine/v3.13/community

Navigate to this line by using the arrow keys, press the Insert key in order to activate Editing.

Then remove the # at the beginning of the line. The resulting file should look like this:

#/media/cdrom/apks
http://ftp.halifax.rwth-aachen.de/alpine/v3.13/main
http://ftp.halifax.rwth-aachen.de/alpine/v3.13/community
#http://ftp.halifax.rwth-aachen.de/alpine/edge/main
#http://ftp.halifax.rwth-aachen.de/alpine/edge/community
#http://ftp.halifax.rwth-aachen.de/alpine/edge/testing

Now save the file an exit the editor by pressing the Esc key, entering :wq and pressing enter.

Step 2: Install docker & docker-compose

Now we’ll fetch the package lists using

apk update

After that, we can install docker and docker-compose using

apk add docker docker-compose

Step 3: Enable docker daemon autostart & start docker daemon

Enable autostart on boot using

rc-update add docker default

and then start docker using

/etc/init.d/docker start
Posted by Uli Köhler in Alpine Linux

How to check / enable DHCP in Alpine Linux installer

Once you have booted from the Alpine Linux installer CD and logged in using root<no password> as described in What is the Alpine linux default login & password?, one often wants to test if DHCP works before going through the installer and potentially having to repeat the process.

First enable Ethernet using

ifconfig eth0 up

Then run the DHCP client using

udhcpc eth0

This will show you the IP address of the lease that your Alpine Live CD did acquire.

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

How to install python3 pip / pip3 in Alpine Linux

Problem:

You want to install pip3 (also called python3-pip) in Alpine linux, but running apk install python3-pip shows you that the package doesn’t exist

/ # apk add python3-pip
ERROR: unable to select packages:
  python3-pip (no such package):
    required by: world[python3-pip]

Solution:

You need to install py3-pip instead using

apk add py3-pip

Example output:

/ # apk add py3-pip
(1/35) Installing libbz2 (1.0.8-r1)
(2/35) Installing expat (2.2.10-r1)
(3/35) Installing libffi (3.3-r2)
[...]

 

Posted by Uli Köhler in Alpine Linux, Container, Docker, Linux

How to fix Alpine Linux fatal error: stdio.h: No such file or directory

Problem:

When trying to compile a C/C++ program or library on Alpine Linux, you see an error message like

/home/user/test.cpp:5:10: fatal error: stdio.h: No such file or directory
  123 | #include <stdio.h>
      |          ^~~~~~~~~

Solution:

Install the libc headers using

apk add musl-dev

 

Posted by Uli Köhler in Alpine Linux, C/C++, GCC errors, Linux

Mini systemd command cheat-sheet

These are the most common commands I use if my systemd service file is placed in /etc/systemd/system/myservice.service.

Enable (i.e. start at boot) and also start the service right now (--now):

sudo systemctl enable --now myservice

Start by

sudo systemctl start myservice

Restart by

sudo systemctl restart myservice

Stop by

sudo systemctl stop myservice

View status:

sudo systemctl status myservice

View & follow logs:

sudo journalctl -xfu myservice

View logs in less:

sudo journalctl -xu myservice

 

Posted by Uli Köhler in Linux

What is the Alpine linux default login & password?

The Alpine Linux installation ISO uses root as the default user and an empty password. In order to login, just enter the username root and press return.

Posted by Uli Köhler in Alpine Linux, Linux

How to install Podman on Ubuntu 20.04 in 25 seconds

Run this one-liner to install podman on your Ubuntu system:

wget -qO- https://techoverflow.net/scripts/install-podman-ubuntu.sh | sudo bash /dev/stdin

This is the code that is being run, which is exactly the code taken from the Podman installation page.

. /etc/os-release
echo "deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_${VERSION_ID}/ /" | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list
curl -L https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_${VERSION_ID}/Release.key | sudo apt-key add -
sudo apt-get update
sudo apt-get -y upgrade
sudo apt-get -y install podman

 

Posted by Uli Köhler in Container, Linux, Podman

How to fix Jigasi config file getting overwritten

Problem:

My .jitsi-meet-cfg/jigasi/sip-communicator.properties is getting overwritten every time I start Jigasi, but I need to set

net.java.sip.communicator.impl.protocol.sip.acc1.AUTHORIZATION_NAME=abc123abc

in order for my SIP communication to work.

Solution:

Run this script after starting the jigasi container. It will fix the overwritten config and then restart the Jigasi Java process without restarting the container

#!/bin/sh

sed -i -e "s/# SIP account/net.java.sip.communicator.impl.protocol.sip.acc1.AUTHORIZATION_NAME=abc123abc/g" .jitsi-meet-cfg/jigasi/sip-communicator.properties

# Reload config hack
docker-compose -f docker-compose.yml -f jigasi.yml exec jigasi /bin/bash -c 'kill $(pidof java)'

 

Original source: This GitHub ticket which provides a similar solution for a similar problem

Posted by Uli Köhler in Linux

How to use xargs in parallel

A good start is to use -P 4 -n 1 to run 4 processes in parallel (-P 4), but give each instance of the command to be run just one argument (-n 1)

These are the xargs options for parallel use from the xargs manpage:

-P, --max-procs=MAX-PROCS    run at most MAX-PROCS processes at a time

-n, --max-args=MAX-ARGS      use at most MAX-ARGS arguments per command line

Example:

cat urls.txt | xargs -P 4 -n 1 wget

This command will run up to 4 wget processes in parallel until each of the URLs in urls.txt has been downloaded. These processes would be run in parallel

wget [URL #1]
wget [URL #2]
wget [URL #3]
wget [URL #4]

If you would use -P 4 -n 2 these processes would be run in parallel:

wget [URL #1] [URL #2]
wget [URL #3] [URL #4]
wget [URL #5] [URL #6]
wget [URL #7] [URL #8]

Using a higher value for -n might slightly increase the efficiency since fewer processes need to be initialized, but it might not work with some commands if you pass multiple arguments.

Posted by Uli Köhler in Linux