Linux Tips

Various list of useful information about using Linux.
Enable Javascript for TOC.

Terminator (Terminal)

Shortcut Description
Basics
ALT+[ARROW KEY] Move focus (up for up, left for left, etc)
CTRL+Shift+E Split current terminal vertically: [ | ]
CTRL+Shift+O Split current terminal horizontally: [--]
CTRL+Shift+W Closes current terminal
CTRL+Shift+Q Closes terminator window
CTRL+Shift+T Opens new Tab
CTRL+PageUp or CTRL+PageDown Swiches between tabs
CTRL+Shift+G Reset terminal and clear content
Goodies
CTRL+Shift+X Toggle current terminal to full size
CTRL+Alt+W Change window title
CTRL+Alt+X Change terminal title
Mark start of selection
>> scroll to end of selection
>> SHIFT+Click
Select more than one display page
Source: Cheatsheet

Securing SSH Public/Private Keys with Password

When exeucuting ssh-keygen, set a password. To avoid to type the password every time (e.g. git pull, push, etc) you can use the ssh-agent:
kkr@host341:~$ eval $(ssh-agent)
Agent pid 31015
kkr@host341:~$ ssh-add
Enter passphrase for /home/kkr/.ssh/id_rsa: **********************
Identity added: /home/kkr/.ssh/id_rsa (/home/kkr/.ssh/id_rsa)
kkr@host341:~$ 
Source: rabexc.org

Using Screen

Before launching screen:
Command Description
screen -S NAME Creates and attaches to new screen session
screen -ls Lists available screen sessions
screen -x NAME Attaches to existing session

To remove a session, login with screen -x NAME and type in quit or press CTRL+d.

From inside screen:
Shortcut Description
CTRL+a d Detaches from the current screen session (session not removed)
CTRL+d Shortcut for "quit" - Detatches and removes current session
Source: Screen Cheetsheet

Fix Scrolling in Screen

Adding the following lines to the ~/.screenrc fixes the scroll-problem with the mouse or SHIFT + PAGE_UP:

termcapinfo xterm* ti@:te@
Source: Stackoverflow

Fix Promt in Screen

When screen starts just with a sh-4.4# promt, you can fix this with defining the shell to use:
echo 'shell -/bin/bash' > ~/.screenrc
Source: Stackoverflow

VNC Viewer

I've used TigerVNC. The version which comes with Debian does not work, since it conflicts with the installed XServer (April 2020). You have to download the binaries from the TigerVNC webpage - then it works fine.

Pressing F8 + F (to switch to full-screen mode) allows you to pass all system keys (e.g. CTRL + ALT + ARROWs) to the remote system. With F8 + Z you can minimize the viewer - and open it again with ALT + TAB.

Start Server

If you start the server without parameters, it allows only connections from localhost. Thus - for regular use - you need the following parameters:
$ vncserver -localhost no

Access without Password

In first step you have to copy the password from your remote machine:
$ mkdir -p .vnc
$ scp 10.10.60.120:.vnc/passwd .vnc/passwd_10.10.60.120
$ chmod 600 .vnc/passwd_10.10.60.120
$ vncviewer --PasswordFile=.vnc/passwd_10.10.60.120  10.10.60.120:1

Update Alternatives

For some binaries (e.g. gcc, java, etc) there is more than one version installed on your Linux version. You can list those binaries with
$ update-alternatives --get-selections
Xvnc                           auto     /usr/bin/Xtigervnc
animate                        auto     /usr/bin/animate-im6.q16
animate-im6                    auto     /usr/bin/animate-im6.q16
aptitude                       auto     /usr/bin/aptitude-curses
automake                       auto     /usr/bin/automake-1.15
awk                            auto     /usr/bin/gawk
builtins.7.gz                  auto     /usr/share/man/man7/bash-builtins.7.gz
c++                            auto     /usr/bin/g++
...
$
When you want to change the version of the program you can do this with
$ sudo update-alternatives --config gcc
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path              Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-4.9   20        auto mode
  1            /usr/bin/gcc-4.9   20        manual mode
  2            /usr/bin/gcc-6     10        manual mode

Press <enter> to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/bin/gcc-6 to provide /usr/bin/gcc (gcc) in manual mode
$
In some cases you have to install the alternative binary first - e.g. `sudo apt install gcc-4.9`.

Disable Host-Key Checking

When starting a complete new embedded Linux image, it generates on its start a pair of keys which are used for detecting man-in-the-middle attacks. When working on the Linux basesystem it can be quite annoying, when after replacing the image the ssh reports an man-in-the-middle attack:
$ ssh -p20160 root@10.20.60.203
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:XvRoOq+r9mRiWavIIWVhX52qYLEKCoeH83fa8O4TUvM.
Please contact your system administrator.
Add correct host key in /home/kkr/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /home/kkr/.ssh/known_hosts:222
  remove with:
  ssh-keygen -f "/home/kkr/.ssh/known_hosts" -R [10.20.60.203]:20160
ECDSA host key for [10.20.60.203]:20160 has changed and you have requested strict checking.
Host key verification failed.
$
In this case, you can disable the host-key check for local machines:
Host 10.0.*
	StrictHostKeyChecking no
	UserKnownHostsFile=/dev/null
	LogLevel ERROR
Host 10.20.60.*
	StrictHostKeyChecking no
	UserKnownHostsFile=/dev/null
	LogLevel ERROR

Share Bash History from multiple Terminals

When writing the following lines to the .bashrc the history of all terminals will be perserved - and when opening a new terminal, you can access the history of the other terminals (e.g. with CTRL+R):
# Avoid duplicates
HISTCONTROL=ignoredups:erasedups  
# When the shell exits, append to the history file instead of overwriting it
shopt -s histappend

# After each command, append to the history file and reread it
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"

XZ Compression

Compression Performance (file size if linux-v4.19.19.tar is 2.4G):
Compression Compressed Size Uncompression
Lowest Compression xz -0 linux-v4.19.19.tar 4m13.440s 1.6G Size unxz linux-v4.19.19.tar.xz 1m5.245s
Default Compression xz linux-v4.19.19.tar 15m41.213s 1.6G Size unxz linux-v4.19.19.tar.xz 1m41.542s
XZ Compression with TAR tar cfJ linux-v4.19.19.tar.xz linux-v4.19.19/ 15m34.576s 1.6G Size
Lowest Compression (with Threads) xz -0 --threads=0 linux-v4.19.19.tar 1m13.190s 1.6G Size unxz --threads=0 linux-v4.19.19.tar.xz 1m2.777s
Default Compression (with Threads) xz --threads=0 linux-v4.19.19.tar 3m1.575s 1.6G Size unxz --threads=0 linux-v4.19.19.tar.xz 1m19.941s
XZ Compression with TAR XZ_OPT="--threads=0" tar cfJ linux-v4.19.19.tar.xz linux-v4.19.19/ 3m6.042s 1.6G Size
GZIP Compression with TAR tar cfz linux-v4.19.19.tar.gz linux-v4.19.19/ 1m9.054s 1.7G Size 0m16.857s gunzip linux-v4.19.19.tar.gz
BZIP2 Compression with TAR tar cfj linux-v4.19.19.tar.bz2 linux-v4.19.19/ 3m59.512s 1.6G Size
See further benchmarks at rootusers.com.

Grep

Nice

Use nice -5 (value=5) to reduce niceness (higher is less):
$ nice -5 bitbake --read=extra.conf mrt.base.linux
[...]
$
Use nice --5 (value=-5) to increase the niceness(less is more):
$ sudo nice --5 sleep 2
[...]
$

Find

Using find and pass all parameters to one call (e.g. to archive):
find var/log/ -iname "anaconda.*" -exec tar -rvf file.tar {} '+'
Using find to execute for each result a separate instance:
find . -name "*.sh" -exec sed -i 's/#! \?\/bin\/sh/#!\/bin\/bash/' {} \;
Source: Stackoverflow

Using print0 together with xargs -0:
find . -name "2018-*" -print0 | xargs -0 ~/Dropbox/Pictures/Tools/rename_dropboximage_to_mobile.sh

>./2018-11-06 17.50.36.jpg<
>./2018-11-01 11.48.37.jpg<
>./2018-11-07 21.51.18.jpg<
>./2018-11-01 20.57.13.jpg<
>./2018-11-06 16.37.25.jpg<
Source: stackoverflow.com

Iterating over the result in a bash script:
DIR=$(realpath $1)
LOG=$DIR/hash_list.txt

find $DIR -not -type d -print0 | while IFS='' read -r -d '' FILE; do 
	echo -n "Check '$FILE': "
	if grep -q "$FILE" $LOG ; then
		echo "already in list"
	else
		md5sum "$FILE" | tee -a "$LOG"
	fi
done

XFCE Shortcuts

Shortcut Meaning Comment
ALT + F10 Toggle maximize
G H I

Installation Proposal

Installing the program command-not-found will print install proposals like this:
$ ITGSend
The program 'ITGSend' is currently not installed.  You can install it by typing:
apt install d-itg
ITGSend: command not found
$
or
$ dislay

Command 'dislay' not found, did you mean:

  command 'display' from deb graphicsmagick-imagemagick-compat

Try: sudo apt install <deb name>

$
Source: Stackoverflow

One reason for a crash of the command-not-found tool is a wrong encoding:

$ dislay
Sorry, command-not-found has crashed! Please file a bug report at:
http://www.debian.org/Bugs/Reporting
Please include the following information with the report:

command-not-found version: 0.3
Python version: 3.7.3 final 0
Distributor ID:	Debian
Description:	Debian GNU/Linux 10 (buster)
Release:	10
Codename:	buster
Exception information:

unable to open database file
Traceback (most recent call last):
  File "/usr/share/command-not-found/CommandNotFound/util.py", line 23, in crash_guard
    callback()
  File "/usr/lib/command-not-found", line 90, in main
    cnf = CommandNotFound.CommandNotFound(options.data_dir, do_raise=True)
  File "/usr/share/command-not-found/CommandNotFound/CommandNotFound.py", line 86, in __init__
    self.db = SqliteDatabase(dbpath)
  File "/usr/share/command-not-found/CommandNotFound/db/db.py", line 12, in __init__
    self.con = sqlite3.connect(filename)
sqlite3.OperationalError: unable to open database file

This can be fixed by setting the locale to UTF-8 in the .bashrc:

export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

and recreate your system locals:

$ sudo locale-gen en_US.UTF-8
$ sudo dpkg-reconfigure locales
Source: Stackoverflow

SSH Connection Check (on Server)

Sometimes you need to know (e.g. in scripts) whether the caller is connected via SSH or not:
if [ -n "$SSH_CONNECTION" ] ; then
	if [ "$(readlink /proc/$$/fd/0)" != "/dev/null" ] ; then
		echo
		echo "The update does fail when it is started via SSH (since the network"
		echo "and the sshd will be shut down)."
		echo
		echo "Please trigger script by software (e.g. via web-browser), start it"
		echo "on the serial console or via SSH with"
		echo
		echo "   nohup /usr/bin/flashupdate &"
		echo
		sleep 10
		exit 1
	fi
fi

VNC Sessions

On Windows and Linux the TigerVNC works quite well.

There is only one pitfall on Linux/Debian - you have to start the serve with -localhost no, otherwise it binds only on the loopback device and is not reachble from outside.

$ vncserver -alwaysshared -localhost no

Password File

When setting up the vncserver initially, a password must be set. This password must be strong and unique, to avoid security flaws. Strong passwords have the drawback, that they can be hardly remembered - thus we can copy the password (stored encrypted in a file) from our remote to the local workstation and use it for authentification:
$ scp $REMOTEIP:.vnc/passwd ~/.vnc/passwd_$REMOTEIP
password: ****
$ vncviewer --PasswordFile=$HOME/.vnc/passwd_$REMOTEIP $REMOTEIP:1

Shortcuts

Supported on TigerVNC from v1.4.90.
Shortcut Description
F8 Show popup menu
F8 + Z Minimize
F8 + F Toggle Fullscreen
F8 + X Exit
Source: stackoverflow.com

The repository '...' does not have a Release file

If you see the following error message:
$ sudo apt-get update
[...]
Reading package lists... Done                      
E: The repository 'http://ppa.launchpad.net/octave/stable/ubuntu bionic Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
$
Open /etc/apt/sources.list.d/octave-ubuntu-stable-bionic.list and add [trusted=yes]:
deb [trusted=yes] http://ppa.launchpad.net/octave/stable/ubuntu bionic main
# deb-src http://ppa.launchpad.net/octave/stable/ubuntu bionic main