UnixUbuntuAdmin - henk52/knowledgesharing GitHub Wiki

Ubuntu administration

Introduction

Purpose

Describe Ubuntu administrator quick notes.

General commands

  • lsb_release -sc : get the name of the release.

Repo / Packages

Tools

  • scanpackages: Generate the Packages db file used by the apt-get install.
    • sudo apt-get install dpkg-dev
      • Uses quite a lot of packages; like gcc
  • apt
    • sudo apt clear: Clear the /var/cache/apt/archives
  • apt-cache search : search for packages
  • dpkg

Install local deb file: sudo apt install path_to_deb_file

  • ssh-keygen -t ed25519 -f ~/.ssh/some_key -C "key comment"

Update

  1. sudo apt update
  2. sudo apt --with-new-pkgs upgrade

Upgrade

If you need to force it:

  • sudo do-release-upgrade

Clean up /boot

  • What is the safest way to clean up /boot partition?

  • sudo uname -r

  • sudo dpkg --list 'linux-image*'|awk '{ if ($1=="ii") print $2}'|grep -v uname -r

  • sudo apt purge linux-image-5.15.0-78-generic

  • sudo apt-get -f install

  • sudo apt autoremove

  • if all else fails delete some of the old files in /boot

  • then

    • sudo apt-get -f install
    • sudo apt purge linux-image-5.15.0-78-generic
    • sudo apt autoremove

Core dumps

To do a core dump:

  • ulimit -c unlimited
  • ulimit -c
    • Should state 'unlimited'
  • sudo systemctl status apport.service
  • run your failing command

If it does not generate a 'core' file in the current dir, try:

  1. ls /var/run/apport.lock
  2. sudo systemctl stop apport.service
  3. sudo rm /var/run/apport.lock
  4. sudo systemctl start apport.service
  5. Try again

/var/lib/systemd/coredump/

Try coredumpctl

  1. sudo apt install systemd-coredump
  2. sudo systemctl stop apport.service
  3. sudo rm /var/run/apport.lock
  4. ./runner
  5. coredumpctl list
  6. coredumpctl debug

Repo Cookbook

package information

Find out what package provides a file

  • dpkg -S FILE
    • e.g.: dpkg -S /usr/sbin/anacron

get information on a package

  • apt info PACKAGE
    • e.g.: apt info anacron

list installed packages

  • apt list --installed | grep docker-ce

Get list of package versions

  • apt-cache policy docker-ce

Install specific pkg version

  • /etc/apt/preferences

Package: docker-ce*
Pin: version 5:19*
Pin-Priority: 990

See also:

dry-run installation

  • apt install --dry-run docker-ce

Create your own repo

Downloading packages to a local repo

  • sudo apt-get install --downloadonly PKG
  • /var/cache/apt/archives/
    • This is where the downloaded files are stored.

e.g.:

wget -q -O - https://jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install --download-only jenkins

Generate the Packages index file for the local repo

Adding a key to the package key chain

Cron jobs

Contab

hourly, daily, weekly, monthly cron jobs

See:

  • Put your script in one of: /etc/cron.hourly /etc/cron.daily etc.
  • Make sure that the filename has only alphanumeric characters, not dots, dashes etc.
  • For daily and up make sure /usr/sbin/anacron is installed (see: /etc/crontab)

Network

IP addresses

Hardcode IP addresses in Ubuntu 20.04

  • sudo vi /etc/netplan/00-installer-config.yaml
    • See below for content
  • sudo netplan apply
# This is the network config written by 'subiquity'
network:
  ethernets:
   ens160:
    dhcp4: true
  ens192:
    dhcp4: false
    addresses: [10.1.2.3/24, 10.2.3.3/24]
 version: 2

Change NIC names to ethX

See:

  • sudo vi /etc/default/grub
    • GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"
    • There can only be one line with 'GRUB_CMDLINE_LINUX' otherwise it seems the last GRUB_CMDLINE_LINUX is the one being used.
  • sudo update-grub
    • or?: sudo grub-mkconfig -o /boot/grub/grub.cfg
  • sudo vi /etc/netplan/00-installer-config.yaml
    • Change the ethernet names to eth0, eth1 etc.

SSH server

Enable SSH server

  1. sudo apt-get install openssh-server
  2. sudo systemctl enable ssh

NFS server

  • How to Install and Configure an NFS Server on Ubuntu 22.04

  • sudo apt install nfs-kernel-server

  • sudo mkdir -p /hdd1/airsonic/music

    • sudo chown -R nobody:nogroup /hdd1/airsonic/music
  • sudo chmod 777 /hdd1/airsonic/music

  • sudo vi /etc/exports

  • sudo exportfs -a

  • sudo systemctl restart nfs-kernel-server

  • showmount -e

/hdd1/airsonic/music 192.168.49.0/24(rw,sync,no_subtree_check)

Other tools

Scheduling

  • Check current policies for a PID:
    • chrt -p 29522
  • List available priorities
    • chrt -m
  • Set PID to RR top? priority
    • Set sudo chrt --rr --pid 99 29522

Installation

USB installation

USB installation with swap partition

  • Copy the ISO image to local dir
mkdir ~/ubuntu-custom
cd ~/ubuntu-custom
sudo mkdir mnt iso
sudo mount -o loop ~/Downloads/ubuntu-22.04-desktop-amd64.iso mnt
rsync -a mnt/ iso/
sudo umount mnt
  • Create the autoinstall config file
mkdir -p ~/ubuntu-custom/iso/autoinstall
nano ~/ubuntu-custom/iso/autoinstall/user-data
  • user-data yaml file
#cloud-config
autoinstall:
  version: 1

  interactive-sections:
    - identity
    - storage.crypto
    - storage.config.crypto

  identity:
    hostname: ubuntu-host
    username: user
    password: "$6$rounds=4096$PLACEHOLDER$PLACEHOLDER..."  # placeholder only; user will override interactively

  locale: en_US
  keyboard:
    layout: us
  timezone: Etc/UTC

  storage:
    layout:
      name: direct
    config:
      - id: disk0
        type: disk
        match:
          size: largest
        ptable: gpt
        wipe: superblock-recursive
        preserve: false
        grub_device: true
        partitions:
          - id: efi
            size: 512M
            flag: boot
            type: fat32
            mount: /boot/efi
          - id: luks-part
            size: -1
            flag: ''
            type: crypto_LUKS
            wipe: superblock
            preserve: false
            crypto:
              dm_name: cryptroot
              # no password specified → prompt user for it interactively
              volume:
                type: lvm_volgroup
                name: vg0
                volumes:
                  - id: lv-root
                    name: root
                    type: lvm_lv
                    size: -1
                    filesystem: ext4
                    mount: /
                  - id: lv-swap
                    name: swap
                    type: lvm_lv
                    size: 18G
                    filesystem: swap
                    mount: none

  late-commands:
    # late-commands section (runs in target system chroot, after install but before reboot.
    
    # Mount the installed system (already done by installer)
    # Find swap partition UUID and update GRUB for hibernation
    - curtin in-target --target=/target bash -c '
        # Identify swap logical volume (inside LUKS)
        SWAP_PATH="/dev/mapper/vg0-swap"
        if [ -b "$SWAP_PATH" ]; then
          sed -i "s|GRUB_CMDLINE_LINUX_DEFAULT=.*|GRUB_CMDLINE_LINUX_DEFAULT=\"quiet splash resume=$SWAP_PATH\"|" /etc/default/grub
          echo "RESUME=$SWAP_PATH" > /etc/initramfs-tools/conf.d/resume
          update-grub
          update-initramfs -u -k all
        fi
      '
  • rebuild the ISO
cd ~/ubuntu-custom/iso
sudo mkisofs -D -r -V "UBUNTU-AUTO" -cache-inodes -J -l \
-b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot \
-boot-load-size 4 -boot-info-table -o ../ubuntu-22.04-auto.iso .
  • Write to USB
sudo dd if=~/ubuntu-custom/ubuntu-22.04-auto.iso of=/dev/sdX bs=4M status=progress && sync

Troubleshooting

Trobuleshoot package handling

WARNING: The following packages cannot be authenticated

  1. cd /vagrant/repo/binary
  2. wget
  3. wget
  4. apt-get update
  5. dpkg-scanpackages . /dev/null > Packages

W: Failed to fetch file:/vagrant/repo/binary/Packages Hash Sum mismatch

  1. cd /vagrant/repo/binary
  2. wget
  3. apt-get update E: Some index files failed to download. They have been ignored, or old ones used instead.

Errors were encountered while processing: openmpi-bin

After update from 20.04 to 21.04

sudo apt-get purge --auto-remove openmpi-bin  
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
  libevent-core-2.1-7* libevent-pthreads-2.1-7* libfabric1* libhwloc-plugins* libhwloc15* libopenmpi3* libpmix2* libpsm-infinipath1* libpsm2-2* libucx0* libxnvctrl0*
  openmpi-bin* openmpi-common*
0 upgraded, 0 newly installed, 13 to remove and 1 not upgraded.
1 not fully installed or removed.
After this operation, 18.6 MB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 376328 files and directories currently installed.)
Removing openmpi-bin (4.1.0-7ubuntu2) ...
update-alternatives: error: /var/lib/dpkg/alternatives/mpi corrupt: slave link same as main link /usr/bin/mpicc
dpkg: error processing package openmpi-bin (--remove):
 installed openmpi-bin package pre-removal script subprocess returned error exit status 2
dpkg: too many errors, stopping
Errors were encountered while processing:
 openmpi-bin
Processing was halted because there were too many errors.

Fix:

See:

  1. sudo rm /var/lib/dpkg/alternatives/mpi
  2. sudo apt-get purge --auto-remove openmpi-bin

The following packages have been kept back: dh-python

sudo apt-get --with-new-pkgs upgrade
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following packages have been kept back:
  dh-python
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.

See:

Fix: sudo apt-get install --reinstall dh-python

OR

  • apt-cache policy python3-distupgrade

Network troubleshooting

NIC troubleshooting

ens192: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN group default qlen 1000

In a ESXi VM, another VM has connection on the same VM network.

I had to create a new vNIC, delete the other second one and then reboot, then it worked.

upgrade troubleshooting

Troubleshoot upgrade from 21.10 to 22.04

could run do-release-update because 21.10 was no longer supported

Key is stored in legacy trusted.gpg keyring

Fix:

sudo apt-key export 5044912E | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/dropbox.gpg

the '5044912E' was seen in sudo apt-key list

W: http://linux.dropbox.com/ubuntu/dists/disco/Release.gpg: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.