Tmux cheatsheet

Tmux is a windowing terminal emulator that allows split windows and can keep a session running in the background – useful if your working on a remote server if your connections breaks.

Useful command line options:

Start a new tmux session

tmux

List tmux sessions:

tmux ls

Attach to an existing session

tmux a

Attach to a specific session (from the list see above):

tmux a -t <session name>

The following config (usually .tmux.conf in your home directory) file changes some of the default tmux keyboard shortcuts to ones I find more intuitative (for example the tmux command prefix is now Ctrl-a (default is Ctrl-b):

  # Send prefix
  set-option -g prefix C-a
  unbind-key C-a
  bind-key C-a send-prefix

  # Use Alt-arrow keys to switch panes
  bind -n M-Left select-pane -L
  bind -n M-Right select-pane -R
  bind -n M-Up select-pane -U
  bind -n M-Down select-pane -D

  # Shift arrow to switch windows
  bind -n S-Left previous-window
  bind -n S-Right next-window

  # Mouse mode
  setw -g mouse on

  # Set easier window split keys
  bind-key v split-window -h
  bind-key h split-window -v

  # Easy config reload
  bind-key r source-file ~/.tmux.conf \; display-message "~/.tmux.conf reloaded."

Common actions

  <prefix> h - horizontal split pane
  <prefix> v - vertical split pane
  <prefix> x - prompt to close (or simply press ctrl-d)

  <alt>-<arrow key> - move  panes left, right, up, down
  <prefix> z - zoom the current session to fill the window

  <prefix> c - create new window
  <shift>-<arrow key> move window 
  <prefix> 0-9 - move to specific window
  <prefix> & - close window (or close all panes in the window using ctrl-d)
  <prefix> , - rename window

  <prefix> d - detach session
  <prefix> $ - rename session (name display is bottom corner and on `tmux ls`)
  <prefix> s - list sessions (pressing arrow key on a session will expand it)

  <prefix> : - open command mode

To close a session or window window (can get list of sessions using <prefix> s)
enter command mode (<prefix> :) and type::

  kill-session -t <session name>

or::

  "kill-window -t <window name>"

Mouse Commands

The config above enables mouse mode which means you can enter a session by
clicking on it

The cut and paste commands from selecting the text with the mouse and paste using the middle mouse button does not work in mouse mode but::

  <shift> <drag while depressing left mouse button> - will allow text to be
                                                    copied so it can be pasted
  <shift> <middle mouse button> - paste text selected above

.. hint:: to enable the wheel button to allow scrolling in `vim` when using 
          `tmux`  set the option `set mouse=a` in your `.vimrc` 

Adding Digital Ocean Insights to droplet

The digital ocean instructions for adding their insights agent use the out of date apt-key mechanism

Installing using the latest method is simple – make sure the keyrings directory exists:

mkdir -p /etc/apt/keyrings

Then download the key and dearmour to the keyrings directory as follows:

curl https://repos.insights.digitalocean.com/sonar-agent.asc | sudo gpg --dearmour | sudo tee /etc/apt/keyrings/do-insights-repo.gpg > /dev/null

the create the repo source:

echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/do-insights-repo.gpg] https://repos.insights.digitalocean.com/apt/do-agent/ main main" | sudo tee /etc/apt/sources.list.d/digitalocean-agent.list

Then update the repos:

sudo apt update

Install the agent:

sudo apt install do-agent

Check it’s running using:

ps aux | grep do-agent

Now check on the Digital Ocean dashboard you should 5 graphs on the droplet’s Graphs tab instead of 3.

First Experience with new Pos_OS! cosmic desktop

The rust Pop_OS! cosmic desktop app written in Rust by the team an system 76 is currently in pre alpha but you can test it out. It’s probably not ready to use as your daily driver at present. Many thanks to Levi Portenier for his help via the Mattermost chat to allow me to get started with this.

Installation

make sure your system is up to date using the command:

sudo apt update && sudo apt upgrade

Enable Wayland by editing the /etc/gdm3/custom.conf and place a ‘#’ at the beginning of the line that reads WaylandEnable=false. Log out and select your username there should be a cog in the bottom right corner of the password entry page clicking on that should give the option for Pop on Wayland at the time I tested this it was not very stable and the tiled desktop did not work.

Once wayland has been enabled you can install the desktop from the standard repos using the command:

sudo apt install cosmic*

Log out and click on your user name, the cog on bottom right of the password entry screen now has a COSMIC option. Select that and enter your password.

At the time of writing there’s no dock available, the initial screen is quite minimal. At present keyboard control is required. Here are the shortcut I found (or was pointed to by Levi).

Keyboard shortcuts

Super + / – Open the launcher and to run an application by typing it’s name

Super + A – Open the App Library

Super + B – Open the default browser

Super + F – Open the file Manager

Super + G – In Tiled mode (see Super + Y) pop the current window out of the tiled framework. I could not see any way to send the window to the background at present, even when the background window is active.

Super + H – Move workspace to the left

Super + J – When windows are tiled vertically (see Super + O) move to the window above

Super + K – When windows are tiled vertically (see Super + O) move to the window below

Super + L – Move workspace to the right

Super + M – Toggle Maximise the current window

Super + O – Toggle horizontal or vertical tiling

Super + Q – Close the current window

Super + T – Open a terminal

Super + W – Display workspaces

Super + Y – Toggle tiled window

Clicking on the Power button displays a menu from there you can log out, power off and access the Cosmic Settings app (this is still in very early development so options are limited). There does not appear to be any way to adjust the resolution of the screen. I tested this in a VM and could not see any way to make the screen larger than 1024 x 768. According to Levi, this can be configured in /etc/cosmic-comp/config.ron but the default version does not contain any settings for resolution. The standard Gnome Settings app is still available from the application library but as you would expect the settings are not applied to the COSMIC desktop.

The window chrome does not have a maximise button – you can maximise by double clicking on the title bar. However maximised at present seems to cover the top bar.

The default keyboard layout seems to English US. I could not see a way to change this.

Salt Configuration Management Cheat sheet

Delete a user:

sudo salt ‘minion-name’ user.delete username remove=True force=True

Test states:

sudo salt ‘minion-name’ state.test

Useful master configuration

sudo vim /etc/salt/master.d/state-verbose.conf

state_verbose: False

sudo vim /etc/salt/master.d/timeout.conf

timeout: 15

Troubleshooting

Run minion in debug mode

sudo systemctl stop salt-minion

sudo salt-minion -l debug

Can’t perform a sudo salt ‘minion-name’ test.ping:

Log on to minion and set to run in debug if no communication from master check the value of `source address` in /etc/salt/minion.d/base.conf

Upgrading to Onedir (on Ubuntu)

Salt classic is being retired – it will not longer be packaged after version 3005. Onedir (which packages everything, including python, required to run salt in one directory) is the future. To upgrade a new gpg key is required.

Getting started with pnpm

For a long time, I thought why use a different node package manager like yarn – “npm (the package manager that’s installed when you install node) does the job, why make it more complicated?” Then I came across a project that said it installed best with pnpm, I decided to check it out and found it does add value. I’ve switched to using pnpm because it allow management of node which was previously a bit of a faff. Also, when one includes a package in a project it stores the package version in a central repo and adds a link to that package/version so next time you reinstall or use that package / version in another project it does not need to be downloaded again. It also takes care of global installations without using sudo or extra configuration. Check out the website for more benefits.

What follows is a very basic introduction to pnpm on Linux systems partly as an aid memoir to myself but hopefully you find it helpful too! These instructions should work for most Linux distros but I’ve only tried it out on Debian based ones so far.

Installing if you don’t already have node installed

If you don’t have node installed you can install pnpm and use that to install node rather adding the node repo to your system page manger and installing from there. To install so that you can manage node use the command:

curl -fsSL https://get.pnpm.io/install.sh > pnpm-install.sh
# view the file to make sure you're happy running it on your computer
sh ./pnpm-install.sh

or if you don’t have curl you can use “wget -O- https://get.pnpm.io/install.sh | sh -” instead.

You can install the current LTS version of node using the command (other options in place of “lts” allow installing other versions, see the pnpm website for details):

pnpm env use --global lts

Installing if you already have node installed

You can use the script above if you’ve already got node installed however there are also a couple of other If you have a recent version node (versions 16.13 onwards) you can use corepack, run the commands:

corepack enable
corepack prepare pnpm@latest –activate

Or you can install using npm:

npm install -g pnpm

Getting started

Command are often similar to their equivalent npm command and should be run from the project directory.

I find tab completion very useful, to install it for bash zsh or fish shells use the following (and then reload your shell using e.g. source ~/.bashrc or log out and back in again):

pnpm install-completion

To start a new project:

pnpm init

To convert an existing project using a package-lock.json:

pnpm import

To install an existing project – this read from an existing pnpn-lock.yaml to get version information (if it exists, it it does not exist it will be created – you should add this file to your :

pnpm install

Adding dependencies to a project is slightly different to npm. To add a development only dependency use the add command and append “–save-dev” as below, you can optionally specify a tag, version or version range:

pnpm add <package name>[@<tag>|@version>|@<version range>] –save-dev

Add a production dependency (the default add action)

pnpm add <package name>[@<tag>|@version>|@<version range>]

Removing the installed packages is similar to npm (though as pnpm creates links to a central repository it only removed the links to these packages)

pnpm remove <package name>

To run a script you can use the same syntax as npm (pnpm run <script defined in package.json>) but unless your script is a keyword used by pnpm you can simply use:

pnpm <script defined in package.json>

Thanks for reading

I find pnpm much is an improved package manager compared to npm, hopefully you’ve found this useful as an introduction.

Enjoy!

Update your apt keys for Ubuntu and other Debian based operation systems

You may get a warning from a modern Debian based operating system which says:

Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).

Essentially the apt-key framework is being retired and you need to store your keys in separate files in the /etc/apt/trusted.gpg.d/ directory rather than using apt-key to manage

If when you run:

sudo apt-key list

Any keys listed under the heading of

/etc/apt/trusted.gpg

should be migrated. This procedure is described below. Before you start make sure the keyrings directory exists (not required if you are using Ubuntu 22.04)

sudo mkdir -p /etc/apt/keyrings

From the list produced by the “apt-key list” command, a key can be uniquely identified by the last 2 sets of hexidecimal numbers on the second line of the pub section. For example if the numbers are DE57 BFBE you can use the string DE57BFBE to identify the key. So you can export it using the following command (replace the DE57BFBE with the actual last digits of the key and <repo-name> with a unique name in that directory):

sudo apt-key export DE57BFBE | sudo gpg --dearmour -o /etc/apt/keyrings/<repo-name>.gpg

You can then update the repository definition to use this key. for example if your repo was:

deb [arch=amd64] https://repo.awesome.io/repo/py3/ubuntu/22.04/amd64/latest jammy main

and you used the filename awesome.gpg then you’d enter:

deb [signed-by=/etc/apt/keyrings/awesome.gpg arch=amd64] https://repo.awesome.io/repo/py3/ubuntu/22.04/amd64/latest jammy main

You can then list the key using “sudo apt-key list” – and use “sudo apt update” to update your apt indexes.

With all that working delete the old key from the trusted.gpg using the command:

sudo apt-key del DE57BFBE

Enjoy!

Using chroot to install the GRUB2 bootloader

You can use chroot from a live CD or USB to update or install GRUB on a Linux system where, for example, your master boot record has been overwritten by installing windows. This post explains how… If you need to chroot to do something other than fix GRUB you can use the relevant sections of this procedure.

Boot from live CD and access the partition where Linux is installed.

Boot the system from a live CD (you can obtain one from the download section of www.ubuntu.com or www.debian.org, it does not really matter which version or even which Linux distribution as long as it gives you access to a terminal session).  Once booted, on Ubuntu / Debian you can usually open a terminal window using Ctrl-Alt-T or from the Accessories menu or type terminal into the Dash.  I’ve shown all command that need to be run as root proceeded by “sudo” if you have logged in as root or become root using “sudo -i” or “su -” you don’t need to include sudo in each command.

Many modern distros provide the facility to mount the local hard drive automatically using the file manager.  If your system does not automatically mount partitions you’ll need to create a mount point e.g

sudo mkdir /root-partition

You will then need to find the partition containing your linux root partition “sudo fdisk -l” may provide this information.

sudo mount /dev/sda1 /root-partition

From here on I’ll assume your mount point is /root-partition if your distro mounted the drive for you automatically you’ll need to substitute the mount point it created in the following commands.  Alternatively you can create a link to it (this is useful if your system mounts using the very long uuid of the disk. for example if your system mounted the partition as /mount/point/created/automatically create a link to it as follows

sudo ln -s /mount/point/assigned/automatically /root-partition

Prepare for chroot

When you chroot the directory that you specify becomes the new root partition hiding the current one.  To access the devices and processes currently on the system you need to rebind /proc /sys /dev from the existing root partition to the new one this is done using “mount – o bind”

sudo mount -o bind /proc /root-partition/proc
sudo mount -o bind /sys /root-partition/sys
sudo mount -o bind /dev /root-partition/dev
sudo mount -o bind /dev/pts /root-partition/dev/pts

Updating GRUB does not require network connectivity but if you’re using chroot for some other reason you may require it, assuming your your live CD has set up network connectivity you’ll need to the save the resolv.conf and copy the one created on the live cd for DNS to work.

sudo cp /root-partition/etc/resolv.conf /root-partition/etc/resolv.conf-save

sudo  cp /etc/resolv.conf /root-partition/etc/resolv.conf

Now execute the chroot opening a bash session

sudo chroot /root-partition   /bin/bash

Update or install GRUB

Grub is often configured to use a separate partition which is mounted as /boot.  If this is how your system is set up you’ll need to mount this partition (e.g. on the system I’m using to write this tutorial it’s a the fifth partition on the first disk or /dev/sda5, your will almost certainly be different “sudo fdisk -l” may help you indentify it’s usually a small ext2 partition) This is mounted as follows

mount /dev/sda5 /boot

Note you don’t need to use sudo as when you chroot you become root.  Substitute the actual partition that /boot is located on your system.

I’m assuming GRUB was set up correctly before your master boot record was overwritten, if you need to set up grub you should use one of the excellent tutorials out there.  If you’ve installed another linux version on your system you may already have grub setup in the master boot record in which case you can simply run update-grub other wise you’ll need to run grub-install.  Most linux distros provide a GRUB configuration that does a good job of detecting other linux distros and windows.

Before you run update-grub may want to save the current grub.cfg file as follows

cp /boot/grub/grub.cfg /boot/grub/grub.cfg-save

Run update-grub to create the new configuration (this will not change the master boot record however if your system has GRUB installed on the master boot record this will alter the configuration)

update-grub

You can then examine the configuration created by looking at the file /boot/grub/grub.cfg.  Once you are happy that it’s created correctly

grub-install /dev/sda

The master boot record that you need to update is often on the first disk (device name /dev/sda) Note /dev/sda is the entire disk partitions are names from 1 e.g. /dev/sda1)

 Exit from chroot

To exit from chroot it’s the same as exiting from any shell, simply type

exit

or use Ctrl – D

When you reboot, you should now be able to access your linux partition again.

df displays UUID rather than the device name

Several distros set up your boot device in grub using the UUID.  Unfortunately this can cause the output from the df command to display the long uuid (e.g. /dev/disk/by-uuid/0ebcf386-83eb-4639-b1ba-be73a7f60efc) rather than the device name (e.g. /dev/sda1)

To change this edit the file /etc/default/grub on Debian use the command

sudo vi /etc/default/grub

find the line which contains

GRUB_DISABLE_LINUX_UUID=true

If it has a # as the first character on the line this character should be deleted then save the file.

Now update the grub using the command

sudo update-grub

After you reboot the df command will display the device name.

Apache .htaccess for basic authentication

The Apache documentation recommends that where possible the use of htaccess files should be avoided.  It recommends for performance and security reasons that Directory specific configuration should be put in the main configuration files, via an include if a large number of directories are specified.  The caveat it specifies is where the user does not have access to the main configuration files, as is usually the case where the site is hosted on a shared hosting environment.  As a web developer you may want your test environment to be as similar to the clients site as possible.  Therefore you may want to configure your local web server to use .htaccess files.

The file and apache directory (/etc/apache2 and /var/www) names used in this post are those from recent versions of Ubuntu with the default site.  If you are using a virtual host you’ll need to edit the configuration file for the that virtual host.  If your are using another distribution the directory containing the configuration files may be different.  The editior used in the post is gedit you can substitute any text editor.

Check basic authentication is enabled

To ensure that basic authentication is enabled on the server, check for the auth_basic.load file in the mods-enabled directory, to do this using the command line type the following command

ls - l /etc/apache2/mods-enabled/auth_basic.load

Alternatively you can browse to the directory using your favourite file manager.  If this file does not exist you will need to execute the following commands

cd /etc/apache2/mods-enabled
sudo ln -s ../mods-available/auth_basic.load  .

The module will not be active until will not take effect until the apache server is restarted, see below for the command.

Enable Apache to read .htaccess file

Earlier version of Apache web server enabled the use of .htaccess files by default, however in recent versions it is disabled.  To enable it edit the site file to include at least  AuthConfig in the AllowOverride directive for the highest level directory you wish to use a .htaccess file, to enable for the entire site include this directive for the DocumentRoot.

Open the site file using your favourite editor ensuring that you have super user privilege.  This is achieved from the command line as follow.

gksu gedit /etc/apache2/sites-enabled/default

This contains the DocumentRoot directory (by default on the Ubuntu apache2 package this is /var/www).  Below this is the directory section, within this is the AllowOverride directive.  This should be changed from

AllowOverride None

to

AllowOverride AuthConfig

or if you want to allow changes to more than authentication in the .htaccess file you may want to specify.

AllowOverride All

the relevant sections of your site file will now be as follows

   ...
   DocumentRoot /var/www
   ...
   <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride AuthConfig
        Order allow,deny
        allow from all
    </Directory>
   ...

Again this change to the main configuration file will not take place until apache is restarted

Restart Apache web server

On Ubuntu services are restarted using the service command.

service apache2 restart

On other distributions you will probably use the

/etc/init.d/apache2 stop; /etc/init.d/apache2 start.

Configuring the .htaccess file

With Apache configured to read .htaccess files we can now create a .htaccess file in the directory that we want to restrict access for the purposes of this post we are using a directory called private in the DocumentRoot (/var/www).

Create the directory if it does not already exist, using the command

cd /var/www
mkdir private

Change to the directory

cd private

Create the .htaccess file

gedit .htaccess

add the following content to the file

AuthType Basic
AuthName "Private Area"
AuthUserFile /var/www/private/.htpasswd
Require valid-user

Save the file and create the password file specified by adding the first user

htpasswd -c .htpasswd   newusername

You will then be prompted to enter a password for the user.  Type the password and press enter and repeat the password

Now you can create content in the directory that is password protected.

 

Ubuntu 12.10 on AMD64 hardware reports missing microcode

On upgrading to Ubuntu 12.10 and rebooting the system (which has a AMD Phenom II processor) the system reported “failed to load file amd-ucode/microcode_amd.bin”.

This indicates that the the microcode kernel module has been unable to find the amd-ucode directory in “/lib/firmware”

Fortunately this is easily fixed:

  • create the directory “/lib/firmware/amd-ucode” if it does not already exist using the command 

mkdir /lib/firmware/amd-ucode

  • Browse to http://www.amd64.org/support/microcode.html.  Here you will find information on AMD’s Linux support.
  • Download the file “amd-ucode-latest.tar” you should see a link to this at the bottom of the page.  (It’s good practice when downloading critical software like this to verify the PGP signature of the file, the above page provide conveniently provides instructions so I won’t repeat them here)
  • Extract the file using the Archive manager open a command prompt navigate to the directory where you downloaded the file and type 

tar xf amd-ucode-latest.tar

  • This creates a directory (at the time of writing it is called “amd-ucode-2012-09-10” but you may have a later version)
  • From this directory you need to copy or move the files “microcode_amd.bin” and “microcode_amd_fam15h.bin” the first supports several older generations of AMD hardware and the second supports the 15h family.  There are several readme files and some which provide support for Solaris X86.
  • Copy the microcode_amd.bin files and microcode_amd_fam15h.bin files using the following command (the microcode module will use only the section of these files which are needed for your hardware)

cp microcode_amd.bin   /lib/firmware/amd-ucode

cp microcode_amd_fam15h.bin    /lib/firmware/amd-ucode

  • To activate these modules you can reboot or use the modprobe command to remove and reinstall the microcode kernel module as follows
  • modprobe -r microcode modprobe microcode
  • Next time you boot the “failed to load” message should no longer be displayed and the processor microcode should be updated correctly.
  • To check that it is updated type the following at a command prompt

dmesg | grep microcode

This will display output similar to the following:

[ 14.174582] microcode: CPU0: patch_level=0x010000c8
[ 14.222343] microcode: CPU1: patch_level=0x010000c8
[ 14.223834] microcode: CPU2: patch_level=0x010000c8
[ 14.225326] microcode: CPU3: patch_level=0x010000c8
[ 14.226881] microcode: Microcode Update Driver: v2.00 <tigran@aivazian.fsnet.co.uk>, Peter Oruba