Ubuntu



Introduction

Ubuntu is a Linux distribution with excellent support and a broad user community. Many technical projects use this distribution as their target platform. Ubuntu is based on Debian Linux. The package management tools are therefore dpkg and apt-get.

Setting up WLAN in a Headless Installation

If you are on a headless Ubuntu installation (i.e., no GUI) you can set up WLAN access via the command line.

In the /etc/netplan folder, find the network configuration definition file (most likely 50-cloud-init.yaml). Open the file in an editor. You should find an existing ethernet configuration:

network:
    ethernets:
        eth0:
            dhcp4: true
            optional: true
    version: 2

where eth0 is an example ethernet interface. To add a WiFi configuration, add the lines

    wifis:
        <wlan>:
            optional: true
            access-points:
                "<ssid>":
                    password: "<password>"
            dhcp4: true

where <wlan> is usually wlan0, and <ssid> and <password> need to be replaced with the actual WiFi SSID and password.

As this is a YAML file, make sure that indentation levels are correct and only whitespace (no tab characters) is used.

Now activate the new configuration via

sudo netplan apply

and check with

ip a


Determining the Version of Ubuntu Running

Use the lsb_release command to view version information about the running LSB-compliant Linux system.

sudo apt install lsb-release
lsb_release -a

generates the example output

No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.1 LTS
Release:	22.04
Codename:	jammy


Upgrading To A New Release

First of all, check you current version by typing

lsb_release -a

To upgrade to a new major release (for example, 16.04 to 16.10) type the command

do-release-upgrade

If there is no major release available but a point release has been issued, you can upgrade by typing

sudo apt-get update
sudo apt-get dist-upgrade


Working With Repositories

Repository Sources

Third-party repositories can be added to the list of source repositories with the command

sudo apt-add-repository <repository URL>


Source repositories that are provided by Ubuntu can be found in the file /etc/apt/sources.list. Third-party repositories that have been added afterwards can be found in the directory /etc/apt/sources.list.d/.

Querying Installed Packages

To find out which package a given file belongs to:

apt contains <file path>


To find out which files are included in a package use the Debian utility dpgk:

dpkg -L <package name>



SSH Logins Without Typing The Password

If you connect to a remote computer via ssh or copy files via scp frequently, you should consider using ssh-agent, which saves you from typing your login password each time.

First, you create an RSA public/private key pair that identifies your local machine. You may already have created such a key in your SSH keychain. It is good security practice, however, to use a separate key for each device you want to connect to. Let us assume you want to connect to a Raspberry Pi. The command to create a new SSH key would be

ssh-keygen -t rsa -b 2048 -C "for Raspberry Pi"

where '-b 2048' specifies the key length in bits. You will be prompted to enter the path and name of the file in which the key will be saved. Choose a name that makes it clear that it was created for connecting to the Raspberry Pi. Next, you will be prompted for a password. Just press the Enter key for no password.

Now that the SSH key is created, you need to copy the generated key (public) into the folder /home/pi/.ssh/ on the Raspberry Pi:

scp ~/.ssh/for-raspberry-pi_rsa.pub pi@192.168.1.10:/home/pi/.ssh/authorized_keys

The copied file is renamed to authorized_keys, which is the expected file name for the SSH server to look up the public keys of trusted clients.
If authorized_keys is to hold multiple keys, you should use ssh-copy-id instead of scp:

ssh-copy-id -i ~/.ssh/for-raspberry-pi_rsa.pub pi@192.168.1.10


Now use ssh-agent on your local machine to start a special shell session that uses the new SSH key to automatically authenticate any SSH connection from your machine to the Raspberry Pi:

eval "$(ssh-agent -s)" # starting a new shell session
ssh-add ~/.ssh/for-raspberry-pi_rsa

Done! This was the last time you had to enter the password for user pi.

By the way, you can list all the keys that were added to the SSH agent by entering

ssh-add -l



Message of the Day (MOTD)

If a Ubuntu system is used in a hardware setup without a connected display (e.g., embedded in a robot) it is usually still accessible through a terminal session. To help the user successfully juggle multiple terminal sessions to multiple devices, you can set a custom message of the day, or MOTD, that will be shown at each login. Have a look at the files in /etc/update-motd.d/. This is where you can edit the parts of the dynamically generated MOTD. The files are shell scripts which are executed in alphabetical order. The output of the shell commands form the MOTD. The names of the script files start with a number between 1 and 99, which makes ordering of the scripts immediately visible. You can edit the scripts to customize their output. You can delete a script file or flip its execution bit via chmod in order to disable it. You can also add a new script file to insert your custom message between two existing MOTD items. Please refer to the Internet knowledge for the details.

Additionally you can create the file /etc/motd whose static contents will be appended to the dynamically generated MOTD. You can embellish your MOTD with ASCII art, for example, like the Robo.Fish logo below

                                       .';cldxxkkkxxdolc;,..
                                   .;oOXWWMMMMMMMMMMMMMMWWXKOxo:,.
                                 'o0WMMMMMMMMMMMMMMMMMMMMMMMMMMMWX0xl:'.
                              .;xXMMMMMMMMMMMMMMMMMMMMMMMMMWWWNNXXKKK0kd:.
                            .:ONMMMMMMMMMMMMMMMMMMWXKOxdoc:;;,'..........
.                         'o0WMMMMMMMMMMMMMMWN0xl:,..
Xx:.                   .:kXWMMMMMMMMMMMMMN0d:'.
MMWKxc'.           .'cxXWMMMMMMMMMMMMNOo:,.
MMMMMMN0xc,.   .,cd0NWMMMMMMMMMMMMN0o,.
MMMMMMWWNX0d:;ckKNWMMMMMMMMMMWN0xl,.
lllcc:;,'..    ..,;:clloollc:,..


The MOTD can be previewed without needing to start a new shell session:

sudo run-parts /etc/update-motd.d/



Switching to GUI-less (Multi-user) Mode

Ubuntu uses systemd to control whether the graphical user interface is loaded and shown. To turn off the graphical desktop immediately enter

sudo systemctl isolate multi-user.target

To prevent the GUI from being loaded during the system startup, type

sudo systemctl set-default multi-user.target

To return to the graphical desktop, type

sudo systemctl isolate graphical.target

and to make the graphical desktop the default again, type

sudo systemctl set-default graphical.target


Setting the Screen Resolution for GUI-less Mode

  • Restart the machine to the Grub boot menu.
  • Press 'c' to get into the Grub command console.
  • Run the command 'vbeinfo' to list possible resolutions.
  • Note down the desired resolution. For example, 1024x768x32, where 32 is the color mode.
  • Type exit to return to the Grub boot menu and boot into Ubuntu.
  • Open /etc/default/grub in an editor to add the line
GRUB_GFXPAYLOAD_LINUX=1024x768x32
  • Optionally, add the line
GRUB_GFXMODE=1024x768

to set the screen resolution for the GRUB boot menu itself.

  • Finally, run the Grub updater
sudo update-grub



NFS (Network File System)

Setting up an NFS Server

sudo apt install nfs-kernel-server

then edit /etc/exports to add lines in the following format:

<directory>    <hostname>(<options>) [<hostname>(<options>)]*

where <directory> is the directory that you want to make available over NFS, followed by one or more pairs of client host names and access options. <hostname> can be an IP address, a domain name with wildcards, or a subnet. of the allowed client machine, <options> is a list of options that determine whether the directory is, e.g., writable. The file format is better explained in this article. Examples are

/home/james/photos    192.168.178.62(r,async) 192.168.178.55(r, async)
/mnt/storage2         192.168.178.0/24(rw,sync,no_subtree_check)

After editing /etc/exports, apply the NFS configuration via /usr/sbin/exportfs:

sudo exportfs -ar


Query the allowed NFS protocol versions for the clients via

sudo cat /proc/fs/nfsd/versions


Mounting an NFS Drive

sudo apt install nfs-common

and mount the NFS with the usual /usr/bin/mount command using the -t nfs option like in this example:

 
sudo mount -t nfs -o vers=4 192.168.178.8:/storage /backups

where vers=4 indicates the NFS protocol version, 192.168.178.8 is the address of the NFS server, /storage is the path of the served directory on the NFS server (in NFS version 4 the path is relative to the served NFS root directory), and /backups is the mount point on the client machine

Controlling Bluetooth

On most Linux systems, Bluetooth functionality is provided by the kernel module, libraries and utilities of the BlueZ project. So, make sure that BlueZ is installed.

sudo apt install bluez


Bluetooth Low Energy (BLE) Control in Terminal

Set the Bluetooth controller to operate in BLE mode only by editing /etc/bluetooth/main.conf to make sure that the value of ControllerMode is set to le.

ControllerMode = le

If a change had to be made, you need to reboot the system.

Now issue the command

sudo systemctl start bluetooth

to start the bluetooth service. You can also stop, restart and query the status with by replacing start with stop, restart, status, respectively.

With the bluetooth service running, enter the command

bluetoothctl

which runs a Bluetooth control program in the terminal. In this program you can, for example, display the list of commands, query the controller, power on the controller, scan for devices with RSSI 80 or better, advertise with the local name RPi3 and, finally, power off and quit the program with the following commands:

help
show
power on
menu scan
rssi 80
back
scan on
...
scan off
discoverable on
advertise on
menu advertise
name RPi3
back
...
advertise off
power off
quit


Bluetooth Programming via D-Bus

Here is an example of programming D-Bus in C++ using the sdbus-c++ C++ binding, for which you need to install libsdbus-c++-dev.

sudo apt install libsdbus-c++-dev


A simple example that creates a BLE peripheral:



Installing NVIDIA Drivers

Follow the instructions from Ubuntu to install device drivers for NVIDIA graphics cards and configure for your system. There are also instructions for manually installing a driver downloaded from NVIDIA, which is not recommended.

Alternative packages with newer drivers are available via Launchpad after adding the graphics-drivers repository by typing

sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update


If your monitor cable (HDMI, DisplayPort, etc.) is connected to the output of the integrated graphics solution of your computer (e.g., Intel Skylake Graphics) you should shut down your computer and your monitor, unplug the monitor cable from the integrated graphics port and plug it into one of the ports of the NVIDIA card. Restart your computer, boot into the BIOS utility provided by your motherboard and check that Secure Boot is disabled, otherwise it will prevent kernel modules from being loaded.

Make sure nvidia-prime is installed and use it to select the NVIDIA card as the prime graphics output:

sudo apt install nvidia-prime
sudo prime-select nvidia

This modifies /etc/ld.so.conf.d/x86_64-linux-gnu_GL.conf and /etc/ld.so.conf.d/x86_64-linux-gnu_EGL.conf to use the latest installed nvidia driver. To check the status of the graphics card and the driver assigned to it you can enter

inxi -G

The output should look like this:

Graphics:  Card: NVIDIA Device 1b81
           Display Server: X.Org 1.18.4 drivers: nvidia (unloaded: fbdev,vesa,nouveau)
           Resolution: 3840x2160@60.00hz
           GLX Renderer: GeForce GTX 1070/PCIe/SSE2 GLX Version: 4.5.0 NVIDIA 370.28

Note that nvidia is the selected driver in the line that starts with Display Server:.

You may have to add the string nomodeset to the line that starts with GRUB_CMDLINE_LINUX_DEFAULT= in the file /etc/default/grub. For example,

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash nomodeset"

where quiet and splash were the default options on my system. Be sure to run

sudo update-grub

so that the change to /etc/default/grub has an effect on /boot/grub/grub.cfg. Beware, however, that adding nomodeset will also prevent your display mode from being detected and set automatically if anything should go wrong. Finally, if your boot disk contains multiple bootable Linux system partitions, make sure that the bootloader that you just updated is the one installed in the Master Boot Record (MBR). You can install a Grub bootloader in the MBR via

$ sudo grub-install --boot-directory=/boot /dev/sda
$ sudo update-grub 

where /dev/sda is an example and needs to be replaced with your specific boot disk device file.

Other useful commands to check the device status are:

ubuntu-drivers devices

for showing a list of packages from the Ubuntu repository that are drivers for the installed hardware,

sudo lshw -c display

for showing information about installed hardware in the display category,

lspci -nnk | grep -iA3 vga

for showing information about graphics cards connected to the PCI bus, and

xrandr

for showing a list of possible display modes in which the connected screen can operate.