linux

How to fix terminal prompt not wrapping correctly

Submitted by luxian on Tue, 04/03/2018 - 13:23

On all the servers I manage I tend to change PS1 (Prompt String One) to add a memo of the environment you are on. For example I will display a red [ PROD ] in front of command prompt on production servers to remind ssh users they are on a production server. Development servers get the same treatment, with a [ DEV ] prefix and a blue background. Example with the value used on Ubuntu dev server:

PS1="\e[104m\e[30m[DEV]\e[0m "'${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

Unfortunately, after doing this, the command prompt didn't wrap correctly at the end of the line. This wasn't noticed in the beginning and then I was too lazy to fix it until today, when I found the fix quite fast on Unix Stackexchange: Terminal prompt not wrapping correctly. The problem is caused by the color sequenced I used:

Non-printable sequences should be enclosed in \[ and \].

And the explanation why this happens:

The reason for the behavior is because bash believes the prompt is longer then it actually is.

Basically, the line will wrapp later than expected because bash thinks the line is longer.

The correct version of command prompt above is:

PS1="\[\e[104m\e[30m\][DEV]\[\e[0m\] "'${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

 

Tags

How to use git as root with your SSH keys and Git name and email.

Submitted by luxian on Sun, 01/14/2018 - 01:56

This is definitely not best practice, but on some servers I have some server configuration in Git and I deploy and update them manually. Git is used more as versioning system and less for deployment.

There are two main issue with this approach once you switch to root account (sudo su):

  1. you loose you ssh key chain and you can not pull/push changes
  2. if you commit changes, your Git name and email are empty and using git config it's not OK since multiple users might do the same thing

It's possible to fix this using sudo by using something like this:

 

sudo -Es GIT_AUTHOR_NAME=Your\ Name GIT_COMMITTER_NAME=Your\ Name GIT_COMMITTER_EMAIL=your.email@example.com GIT_AUTHOR_EMAIL=your.email@example.com su
I added an alias for this and whenever you use the alias, you will forward your ssh key chain and set Git username and password by send temporary environment variables.

Cleanup free space on Linux, delete journalctl logs

Submitted by luxian on Thu, 08/03/2017 - 07:13

Today my dev server ran out of space. Which was a little bit weird - 20GB should be enough for a bunch of websites. I managed to free up to 6 GB of space just doing the following:

1. Aptitude cache

sudo apt-get autoremove

I had to run this 2 or 3 times until nothing could be removed anymore. Not sure why it didn't clean up everything from the first pass.

2. Logs

In my case journal logs (/var/log/journal) used more than 1GB of space. I found a cool thread on Arch forum: Is it safe to delete /var/log/journal log files? -- with two useful commands:

journalctl --disk-usage # See how much space your journal logs  use
systemctl kill --kill-who=main --signal=SIGUSR2 systemd-journald.service # Force journal to execute cleanup procedure

In my case /etc/systemd/journald.conf didn't have any values, so I had to un-comment the defaults and change the following:

SystemMaxUse=16M
ForwardToSyslog=no

After that I tried the second command, which didn't help. Being a dev server, few minutes of downtime are ok so I restarted and then the journal logs are down to ~ 135M now (not sure why not 100M, but I'm happy anyway).

3. Find biggest folders

Cleaning up unused packages and logs didn't free up as much space as I wanted. I wanted to check what folders had biggest disk usage, but I had no idea how to do it. After some searching ended up on How to Find Out Top Directories and Files (Disk Space) in Linux which is exactly what I needed. I used the following command to find top 20 biggest folders in the current directory. I started from root (/) and ended up in /var/lib.

du -hs * | sort -rh | head -n 20

Using this I was able identify other folders that grew out of proportion:

Tags

Ulauncher is my new favorite app launcher for Linux.

Submitted by luxian on Fri, 07/14/2017 - 08:19

Some time ago I wrote about Mutate. After a few weeks of using it I decided I need something better. Mutate had a major drawback, it couldn't handle properly .desktop files. That means that some apps were displayed with the wrong or missing icon and Chrome Apps did not work. For example when I tried to open Wunderlist (Chrome App) it will open just another Chrome window. It works well if I use normal app menu.

After trying to fix it myself for a while I gave up. The project is almost abandoned on Github.

This is how I ended up installing Ulauncher which works great with .desktop files. So no more issues with opening Chrome Apps or wrong app icons. It's also fast and reliable.

It was very easy to install following the instruction from the Github repository. The only problem I had was the autostart. By default it adds only the following line to autostart:

ulauncher --hide-window

But in my case that location was not in the $PATH variable, so I had to write the full path to Ulauncher in there.

 

Tags

Extract SSH public key from private key

Submitted by luxian on Thu, 03/09/2017 - 11:03

In the last couple of days I spent a lot of time debugging the deployment script called via GitLab CI. The script worked well on one server (dev), but failed on the second one (prod).

GitLab CI was using a docker container with an injected SSH private key to connect to server and run some scripts. The whole setup was exactly as described in the official tutorial. From the beginning I was surprised to see that public key was never added to the container. But since it did the job well  I didn't spend too much time to see how it actually works.

When I tried to make deployment work for a second server, things didn't work so smoothly. It refused to connect over SSH. After some digging and with ssh debugging enabled it was clear that the public key used by CI container was missing from the authorized_keys file on that server. The authorized_keys file was big on the first server, the one that work, and I had no idea which key was the one I needed to copy over. So I tried to see if I can extract the public key from the private key. To my delight, this seems to be a very easy task. You just need to run the following command:

ssh-keygen -y -f ~/.ssh/id_rsa

Where ~/.ssh/id_rsa is the file with your private key. This command will print the associated public key. With this key in place, the deployment script worked without issues.

Lessons learned:

  • private keys contain the public key so in case you lose it you can re-create it
  • I need a better systems for deploying and managing authorized_keys across all the servers

Tags

Mutate - a simple Launcher inspired by Alfred

Submitted by luxian on Wed, 12/07/2016 - 21:25

Later update: I stopped using Mutate and switched to Ulauncher. You can find out more here: Ulauncher is my new favorite app launcher for Linux.


I'm using a Macbook when I'm in the office, but at home I'm a happy Linux user. My distro of choice is Linux Mint Debian 2 (LMDE2).

While the lack of keyboard shortcuts on OS X bothers me from time to time, when I'm on Linux I miss Alfred a lot. Today I spent some time looking for alternatives. I remembered that a few years ago I saw a friend using something similar for Ubuntu, so there must be something for my distro as well.

That's how I discovered Mutate. LMDE doesn't support PPAs, so I tried the .deb package and it worked. The only thing I had to configure was the shortcut. By default it uses Ctrl + D for general search and Ctrl + T for Google Translate. I found the second one quite annoying (that's the shorcut to open tabs) and the first one only after I opened preferences. To change them, open Mutate (any of the shortcuts above) and type "Preferences". I removed both shortcuts and added Ctrl + Space for default search. 

Update: If you also added the program to startup list, you might find it annoying that the mutate UI opens after you login. Apparently this can be solved by calling the following command:

mutate --hidden

Found on GitHub issue 64.

Tags