Category Archives: Linux

Everything related to linux

IntelliJ .http/.rest cli support

I’m a big fan of the IntelliJ .http/.rest files but only being able to run them from within the IDE was not very satisfying. So I was looking for a way to run them from the cli as well and found https://github.com/restcli/restcli. This project offers a cli tool named rest-cli that is able to run IntelliJ’s files including env files. So those can also be integrated into CI/CD or other workflows that don’t use IntelliJ.

On the release page https://github.com/restcli/restcli/releases you can find the latest version of the tool as zip or brew package. If I remember correctly I was able to install the brew package with

brew install -f brew_restcli.zip

But just downloading it and putting it into the path also works fine.

nvm slows down shell startup

Recently I was wondering why my shell is taking so long to show a prompt and was able to track it to nvm – node version manager. The check which version to use as default takes quite long so I was looking for a solution that allows me to keep nvm functionality and have a fast shell.

When installing nvm it tells you to add the following statement to your shell config:

export NVM_DIR="$HOME/.nvm"
[ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh" --no-use # This loads nvm
[ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && . "/usr/local/opt/nvm/etc/bash_completion.d/nvm"  # This loads nvm bash_completion

Line to of this is causes the delay in the startup. As more on the backend side of development I don’t switch node versions and so on around very often so I found a solution that let’s me disable this check, which version should be used on nvm startup by adding a default version to the path and disable that check in the startup script:

# set default node version
export PATH="~/.nvm/versions/node/v12.18.3/bin:$PATH"
export NVM_DIR="$HOME/.nvm"
[ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh" --no-use # This loads default nvm version from above and skips default check
[ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && . "/usr/local/opt/nvm/etc/bash_completion.d/nvm"  # This loads nvm bash_completion

The first line set my default node version to 12.18.3 as default – which is stable right now. Then I added --no-use to the end of the second line. Now every time i start the shell node 12.18.3 will be used until i say nvm use. But that’s a tradeoff I can live with to have my shell start in an acceptable timespan again.

docker reset script – docker prune

I’m using docker for a lot of projects and have to switch context very often – I know – that kind of sucks, but that’s how it is…

Most containers can’t be shared between projects and I don’t want to spend more resources than necessary as my machines are on their limits. So I wrote a small bash script to get rid of all running containers, images, … on my local machine:

#!/bin/bash
echo "###########################"
echo "# clean docker"
echo "###########################"

echo "# stopping containers"
docker stop `docker ps | awk '{print $1}' | grep -v CONTAINER` >> /dev/null 2>&1

echo "# prune system"
docker system prune -f --volumes >> /dev/null 2>&1

echo "###########################"
echo "# done"

It will output a text like this

###########################
# clean docker
###########################
# stopping containers
# prune system
###########################
# done

when executed.

After that there shouldn’t be any leftovers except your images. If you also want to get rid of those you can run:

docker system prune -f --all --volumes

Or just add –all in the script above.

Attention: This will delete all your local volumes and images. So make sure you don’t run this if any data in the containers is of importance for you.

Shells starting very slow on my ubuntu and mac

My shells were starting very slow which is really bad as a dev/admin. I found out that all those version managers I installed were to blame for that. So I decided to get rid of nvm and rvm by simply removing the directories and cleaning up my .bashrc and .profile in $HOME.

#nvm
rm -rf ~/.nvm
rm -rf ~/.npm
rm -rf ~/.bower
rm .zshrc # i am using bash

#rvm
rm -rf .rvm

# cleanup configs
vim ~/.bashrc ~/.profile

After those simple steps my shell is up within an instand instead of needing some thinking time.

sdkman and brew seem to be ok and are allowed to stay installed for now.

Uninstall snapd from ubuntu 20.04

In my last post I found out who was to blame for filling my complete disk. It was this new and fu**** slow snapd added to ubuntu in the last few moments before release to get into the app market business. I decided to live without this feature and uninstalled it:

snap list | awk '{print $1}' | xargs -rn1 sudo snap remove

After letting this run a few times i tried to uninstall the remaining snaps manually with

sudo snap remove <name>

Some refused to uninstall but I continued with unmounting its volume and getting rid of snapd from the system:

sudo umount /snap/core/<replace_with_number_in_your_folder>
sudo apt purge snapd

In the end i cleaned up any stuff left from snapd:

rm -rf ~/snap
sudo rm -rf /snap
sudo rm -rf /var/snap
sudo rm -rf /var/lib/snapd

In the end removing all snaps freed about 20GB of data. I will reinstall everything based on apt as usual and will then be back at maybe 15GB of saved space without snapd.

Next to this space problem all the apt installed applications start seconds faster.

Thanks for the long years of great operating system but that’s a way I won’t go with you.

Remove old/disabled snaps from the system – ubuntu why…

The new snap system starts eating my system partition. Next to being slow it also holds old backups of upgraded snaps. Ubuntu itself has no option to disable that. You can only lower the number of old versions by using

sudo snap set system refresh.retain=2

which will limit the number to one live and one backup copy. But what if you also don’t need or want that backup copy? refresh.retain doesn’t allow 1:

sudo snap set system refresh.retain=1

error: cannot perform the following tasks:
- Run configure hook of "core" snap (run hook "configure": retain must be a number between 2 and 20, not "1")

I didn’t further check how to remove that but used the following one-liner to get rid of old versions:

sudo su
snap list --all | awk '$6~"disabled"{print $1" --revision "$3}' | xargs -rn3 snap remove

Next to those backups also the /var/lib/snapd/cache folder holds a massive amount of space on my machine. I considered it to be safe to delete:

sudo rm -f /var/lib/snapd/cache/*

This cleanup of snapd folder freed around 12GB of data on my root partition.

Get script directory in Bash

If you need to get the script directory of an executed bash script you can use the following snippet to cd to the script directory:

#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd "$DIR" || exit 1

Add docker to unattended-upgrades in ubuntu 20.04

If you want your server to install docker updates automatically then you need to add the docker repository to the list of allowed origins. Otherwise docker updates will stay untouched by unattended-update which will looks similar to this when you login and check which updates need to be installed. Had that quite some times until I decided to look into this.

sudo apt dist-upgrade 
...
The following packages will be upgraded:
  containerd.io docker-ce docker-ce-cli

As you can see all docker packages (coming from https://download.docker.com/linux/ubuntu) weren’t updated. This happens because this origin isn’t in the allowed origin list of unattended-upgrade. You can see metadata of the repository by running

apt-cache policy

There you will find an entry like this

 500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     release o=Docker,a=focal,l=Docker CE,c=stable,b=amd64
     origin download.docker.com

The important part is the o and a in this definition. Those state the origin and the archive.

To allow the docker repo as an origin you need to open /etc/apt/apt.conf.d/50unattended-upgrades with root access and add

"Docker:${distro_codename}";

to the list in Unattended-Upgrade::Allowed-Origins. The syntax is short for “origin:archive”.

Here the allowed origins list from my file as an example:

Unattended-Upgrade::Allowed-Origins {
        "${distro_id}:${distro_codename}";
        "${distro_id}:${distro_codename}-security";
        "${distro_id}ESMApps:${distro_codename}-apps-security";
        "${distro_id}ESM:${distro_codename}-infra-security";
        "${distro_id}:${distro_codename}-updates";
        "Docker:${distro_codename}";
};

I replaced the archive name focal with the variable distro_codename like the existing examples also did. This will help in a future dist upgrade if you plan to upgrade your os for example with the next lts version.

With those changes in place unattended-upgrade should also install updates for docker from now on.

The same procedure can be followed to add other repositories to this list as well.

add systray to elemtary os

Elementary OS 5 comes with a very limited systray functionality that needs to be fixed. For this you need to install wingpanel-indicator-ayatana that can be installed via a PPA and change autostart config of the indicator app.

Install wingpanel-indicator-ayatana

sudo add-apt-repository ppa:yunnxx/elementary
sudo apt update
sudo apt install indicator-application wingpanel-indicator-ayatana

Change autostart config

Open the config file with

sudo vim /etc/xdg/autostart/indicator-application.desktop

and change the line

OnlyShowIn=Unity;GNOME;

to

OnlyShowIn=Unity;GNOME;Pantheon;

Then reboot your machine and you should have a systray that displays your application icons too.