Skip to content



Linux »

Notes for working in Linux

Tips, hints, and tricks for developers in Linux.

Last update: 2022-06-29


Find a mirror repo#

While doing with Ubuntu on ARM, the default repo for arm64 is ports.ubuntu.com, but it’s quite slow if your location is far from it.

Are there alternative repositories to ports.ubuntu.com for ARM?

Malte Skoruppa answered that question with a nice script. I have modified it a bit:

  • change connect timeout to 5 seconds
  • add option to show connection speed

Download find_mirrors.h and set it executable.


To find mirrors:

./find_mirrors.sh arm64 bionic main
http://ftp.lanet.kr/ubuntu-ports
http://ftp.harukasan.org/ubuntu-ports
http://mirror.kumi.systems/ubuntu-ports
http://mirror.misakamikoto.network/ubuntu-ports
http://mirror.coganng.com/ubuntu-ports
http://ftp.tu-chemnitz.de/pub/linux/ubuntu-ports

To show the connection speed:

./find_mirrors.sh arm64 bionic main speed
0.203373 http://mirror.coganng.com/ubuntu-ports
0.770989 http://ftp.tu-chemnitz.de/pub/linux/ubuntu-ports
0.240577 http://ftp.lanet.kr/ubuntu-ports
0.572857 http://mirror.kumi.systems/ubuntu-ports
0.264465 http://mirror.misakamikoto.network/ubuntu-ports
0.432951 http://ftp.harukasan.org/ubuntu-ports

To sort the connection speed:

sort the first column

./find_mirrors.sh arm64 bionic main speed | sort -k 1
0.203373 http://mirror.coganng.com/ubuntu-ports
0.240577 http://ftp.lanet.kr/ubuntu-ports
0.264465 http://mirror.misakamikoto.network/ubuntu-ports
0.432951 http://ftp.harukasan.org/ubuntu-ports
0.572857 http://mirror.kumi.systems/ubuntu-ports
0.770989 http://ftp.tu-chemnitz.de/pub/linux/ubuntu-ports

Alias in scripts#

Alisa is defined for each user, either directly in .bashrc or in separated file .bash-aliases.

alias apt=apt-fast

Alias, by default, is enabled in interactive shell, and disabled in scripts.


Alias with sudo:

To use alias with sudo, define a new alias:

alias sudo="sudo "

The trailing space after sudo will cause the next word after sudo is interpreted.

sudo apt

will be:

sudo apt-fast

Alias in scripts:

Enable expand_aliases flag with shopt, and define aliases in the scripts:

Must have the shebang #!/bin/bash

#!/bin/bash
shopt -s expand_aliases
alias apt=apt-fast
alias sudo="sudo "

sudo apt install build-essential

Continue to run a bash script#

When a bash script run with set -e option, it will stop when any command returns an error. In some cases, we still need to continue the script, here is a way:

Wrap the command with true expression

umount .... || /bin/true

Ignoring exit codes isn’t really safe !!!

Fix USB Partition#

USB with wrong partition table can not be read. Erase the entire partition table is needed.

sudo dd if=/dev/zero of=/dev/sda bs=512 count=1

Then use fdisk to create GPT partition table and add new partition.

sudo fdisk /dev/sdx

The format the partition:

sudo mkfs.ext4 /dev/sdxy

Visual Studio Code#

Visual Studio Code is far better than Sublime Text. Here is a method to install it automatically:

wget -qO- https://packages.microsoft.com/keys/microsoft.asc \
    | gpg --dearmor \
    > packages.microsoft.gpg \
&& \
sudo install -o root -g root -m 644 \
    packages.microsoft.gpg \
    /etc/apt/trusted.gpg.d/ \
&& \
rm -f packages.microsoft.gpg \
&& \
sudo sh -c \
    'echo "\
        deb [signed-by=/etc/apt/trusted.gpg.d/packages.microsoft.gpg] \
        https://packages.microsoft.com/repos/code stable main\
    " \
    > /etc/apt/sources.list.d/vscode.list' \
&& \
sudo apt install -y apt-transport-https \
&& \
sudo apt update \
&& \
sudo apt install -y code

GDB for Multiple Architectures#

Assuming that the target is running on ARM machine, and we need to run GDB on X64 machine.

Install gdb-multiarch, and run it:

gdb-multiarch

Enable core-dump and set target directory on /mnt/xhci-sda1:

ulimit -c unlimited
echo "/mnt/xhci-sda1/coredump_%e_%p_%t" > /proc/sys/kernel/core_pattern

Re-run application to get core-dump.

Copy the executable binary, e.g. ivilauncher, and the core-dump to an Ubuntu host.

Load the core-dump file to see missing libraries:

(gdb) core coredump_ivilauncher_xxxx
(gdb) info shared

That will print out path of dynamic libraries, such as /usr/lib, /lib, /opt/share, etc.

Copy the rootfs from the target device, for faster, only copy the folders containing missing libraries, for example:

mkdir /mnt/xhci-sda1/rootfs
cp -rf /usr /mnt/xhci-sda1/rootfs
cp -rf /lib /mnt/xhci-sda1/rootfs
cp -rf /opt /mnt/xhci-sda1/rootfs

Then copy the rootfs to a new folder on the host machine, such as /home/vqtrong/Desktop/issue/rootfs.

In GDB, set the sysroot and solib-search-path:

set sysroot /home/vqtrong/Desktop/issue/rootfs
set solib-search-path /home/vqtrong/Desktop/issue/rootfs

Finally, use backtrace to see all call stacks:

(gdb) bt full
#4 0x00118884 in QList<RADIO_UPDATE_LIST_FM_t>::operator(int) ()
#5 0x00111f68 in HacRadio::updatePresetList() ()
#6 0x001113a4 in HacRadio::setRadioMode(BAND_e) ()
#7 0x0010f8fc in HacRadio::prepareRequestAudio() ()
#8 0x000533f0 in HAppManager::requestHac(HAppComponent::VIEW_MODE_LIST, bool, bool) ()
#9 0x00070c08 in HAppManager::onAgreeHide() ()

Take screenshot over SSH terminal#

One of unique screenshot utilities is scrot (short for “SCReen shOT”), which is a command-line screenshot utility.

sudo apt install scrot

It is easy to screen-capture the entire desktop. Simply run scrot command without any argument, and it will save a screenshot of the entire desktop as a (date-stamped) .png file in the current directory. Add a filename if you want to save to a specific one.

scrot

From SSH, you have to set the DISPLAY environment variable before taking a screenshot:

export DISPLAY=:0 && \
scrot

Automount USB on Xubuntu#

Install below packages:

sudo apt install thunar-volman gvfs udisks2

Then enable Volume Manager feature in Thunar Preferences, and select Automount features:

Thunar Volume Manager - Automount features

Share keyboard and mouse#

Barrier is a software that mimics the functionality of a KVM switch, which would allow you to use a single keyboard and mouse to control multiple computers.


Linux

On Ubuntu 18.04, install via Ubuntu Snap Software.

On Ubuntu 20.04+, install via Ubuntu APT:

sudo apt install barrier

Run and select Client mode. You have to set the Server’s IP manually because the auto-detect function may not work well.

Barrier can not auto-start at boot if it is installed from snap, unless you manually add an entry to Startup Application list. For example:

Ubuntu 18.04
/snap/barrier/682/usr/bin/barrierc -f --no-tray --debug INFO --name 2ff17p2 --disable-crypto [192.168.100.221]:24800

or

Ubuntu 20.04
/usr/bin/barrierc -f --no-tray --debug INFO --name 2ff17p2 --disable-crypto [192.168.100.221]:24800


Windows

Download the setup file on the GitHub. The installer also automatically install Bonjour service.

Run and select Server mode. Click on Configure and add clients by their names.

Setting up Barrier on Ubuntu and Windows

Comments