Linux: Booting in single-user mode

Sometimes it might be necessary to start in single-user mode to do some administration work, or even reset an existing password.

Normally this can be achieved via the GRUB boot loader.

CentOS / RedHat (with root account enabled)

Switch on your system.

Press Esc until the GRUB menu shows up.

This will bring up the GNU GRUB menu. If the CentOS/RedHat logo/boot messages show up you will need to restart (Ctrl-Alt-Del) and try again.

Select the OS/boot you want to edit. Normally the first line. Press e to edit it.

CentOS Linux (3.10.0-1160.53.1.el7.x86_64) 7 (Core)
CentOS Linux (3.10.0-1160.45.1.el7.x86_64) 7 (Core)
CentOS Linux (3.10.0-1160.42.2.el7.x86_64) 7 (Core)
CentOS Linux (3.10.0-1160.41.1.el7.x86_64) 7 (Core)
CentOS Linux (3.10.0-1160.36.2.el7.x86_64) 7 (Core)
CentOS Linux (0-rescue-d0401f7cdedb4955a0a262b3e0054323) 7 (Core)


Use the ↑ and ↓ keys to change the selection.
Press 'e' to edit the selected item, or 'c' for command prompt.

You will need to find the entry for the kernel. Normally starts with linux16.

[...]
linux16 /vmlinuz-3.10.0-1160.53.1.el7.x86_64 root=/dev/mapper/centos_centos7-root ro rd.lvm.lv=centos_centos7/root rd.lvm.lv=centos_centos7/swap rhgb quite LANG=en_US.UTF-8
[..]

At the end of the line you can choose to add one of the following:

single
systemd.unit=rescue.target
systemd.unit=emergency.target

So that it looks like this:

[...]
linux16 /vmlinuz-3.10.0-1160.53.1.el7.x86_64 root=/dev/mapper/centos_centos7-root ro rd.lvm.lv=centos_centos7/root rd.lvm.lv=centos_centos7/swap rhgb quite LANG=en_US.UTF-8 single
[..]

Type Ctrl-X to exit and the system will start in single- user mode.

If required, remount the root filesystem:

# mount -o remount,rw /

[If there are other filesystems you need to mount from fstab:]

# mount --all

CentOS / RedHat (without root account enabled)

It might be that your system didn’t have a root account enabled, in which case the above steps will fail. There is a workaround.

Switch on your system.

Press Esc until the GRUB menu shows up.

This will bring up the GNU GRUB menu. If the CentOS/RedHat logo/boot messages show up you will need to restart (Ctrl-Alt-Del) and try again.

Select the OS/boot you want to edit. Normally the first line. Press e to edit it.

CentOS Linux (3.10.0-1160.53.1.el7.x86_64) 7 (Core)
CentOS Linux (3.10.0-1160.45.1.el7.x86_64) 7 (Core)
CentOS Linux (3.10.0-1160.42.2.el7.x86_64) 7 (Core)
CentOS Linux (3.10.0-1160.41.1.el7.x86_64) 7 (Core)
CentOS Linux (3.10.0-1160.36.2.el7.x86_64) 7 (Core)
CentOS Linux (0-rescue-d0401f7cdedb4955a0a262b3e0054323) 7 (Core)


Use the ↑ and ↓ keys to change the selection.
Press 'e' to edit the selected item, or 'c' for command prompt.

You will need to find the entry for the kernel. Normally starts with linux16.

[...]
linux16 /vmlinuz-3.10.0-1160.53.1.el7.x86_64 root=/dev/mapper/centos_centos7-root ro rd.lvm.lv=centos_centos7/root rd.lvm.lv=centos_centos7/swap rhgb quite LANG=en_US.UTF-8
[..]

At the end of the line add this:

rd.break

So that it looks like this:

[...]
linux16 /vmlinuz-3.10.0-1160.53.1.el7.x86_64 root=/dev/mapper/centos_centos7-root ro rd.lvm.lv=centos_centos7/root rd.lvm.lv=centos_centos7/swap rhgb quite LANG=en_US.UTF-8 rd.break
[..]

Type Ctrl-X to exit and the system will start emergency mode in read-only mode.

Remount the root filesystem as read/write:

# mount -o remount,rw /sysroot

Switch to the sysroot jail.

# chroot /sysroot

Reset the password, or do any required tasks.

If you have SELinux enforcing mode enabled it will protect the OS from any changes. After you change the password type the following to overcome this:

# touch /.autorelabel

Restart.

# reboot -f

Ubuntu / Debian

Switch on your system.

Press and hold the Shift key.

In some instances pressing the Esc key several times (instead of holding it) achieves the same result. Just be aware that if you press it too many times it will bring you to the GRUB CLI. You can type normal and you will get to the menu described below.

This will bring up the GNU GRUB menu. If the Ubuntu logo/boot messages show up you will need to restart (ctrl-alt-del) and try again.

Select Advanced Options on the GRUB menu.

			GNU GRUB version 2.04

 Ubuntu 20.04.4 LTS
*Advanced options for Ubuntu 20.04.4 LTS
 History for Ubuntu 20.04.4 LTS
 UEFI Firmware Settings

And select the recovery mode option. Normally the latest kernel installed on your system.

			GNU GRUB version 2.04

 * Ubuntu 20.04.4 LTS, with Linux 5.13.0-37-generic
** Ubuntu 20.04.4 LTS, with Linux 5.13.0-37-generic (recovery mode)
 Ubuntu 20.04.4 LTS, with Linux 5.13.0-35-generic
 Ubuntu 20.04.4 LTS, with Linux 5.13.0-35-generic (recovery mode)

This will boot the system and show a series of options. Select root.

Recovery Menu (filesystem state: read only)

	resume			Resume normal boot
	clean			Try to make free space
	dpkg			Repair broken packages
	fsck			Check all file systems
	grub			Update grub bootloader
	network			Enable networking
	root			Drop to root shell prompt
	system-summary	System summary

					<OK>

This message will show. Press Enter.

Press Enter for maintenance
(or press Ctrl-D to continue)

If your / volume is ZFS it will be already read and write. Other filesystems might start in read only mode. If so, remount:

# mount -o remount,rw /

[If there are other filesystems you need to mount from fstab:]

# mount --all

Changing an user’s password

# passwrd <username>

Adding a new user

In the rare event of not having an user, you can add one and give it sudo privileges.

# useradd <username>

# usermod -a -G sudo <username>

# passwd <username>

Other OS

You can find description on how to get to single-user mode for other Linux distros in this Microsoft’s article.

https://docs.microsoft.com/en-us/troubleshoot/azure/virtual-machines/serial-console-grub-single-user-mode




Linux: Configure locale and keyboard layout when remotely accessing from a Mac

At work I have to remote into several different Linux systems from a Mac and there is always the pain of having to handle different keyboard layouts if using Synergy or VMs.

The conversion from a Mac keyboard layout doesn’t translate correctly when the Linux system has the keyboard configured as a PC.

First find out what the current configuration is.

$ localectl

The output in a British system is:

  System Locale: LANG=en_GB.UTF-8
                 LANGUAGE=en_GB:en
     VC Keymap : n/a
     X11 Layout: gb
      X11 Model: pc105
    X11 Options: terminate:ctrl_alt_bksp

Note that the last line might not show in Ubuntu systems.

Unfortunately if you are accessing from a Mac this layout will not work and basic things like |pipe| will not be easy to find.

You can edit the locale and keyboard layout of the system you are accessing so the mapping matches your Mac keyboard layout.

The ideal configuration should be like this:

  System Locale: LANG=en_GB.UTF-8
                 LANGUAGE=en_GB:en
     VC Keymap : n/a
     X11 Layout: gb
      X11 Model: macintosh
    X11 Variant: mac
    X11 Options: lv3:alt_switch

Ubuntu 18.04 / Ubuntu 20.04 / Debian 9 / Debian 10

Edit the following file:

/etc/default/keyboard

With these entries:

XKBMODEL="macintosh"
XKBLAYOUT="gb"
XKBVARIANT="mac"
XKBOPTIONS="lv3:alt_switch"

The XKBOPTIONS I have here are for Synergy to keep the Control and Alt keys on the Mac working the same on the Linux systems. You might not need or want it. Just remove it from the commands if that is the case.

You can also do a text GUI configuration of the keyboard with:

# dpkg-reconfigure keyboard-configuration

If your environment isn’t in English the menus won’t be either. You can force the language output of the application launched to be in the default one. That would be English in most cases. The same command as above but forcing the output to be in English:

# LC_ALL=C dpkg-reconfigure keyboard-configuration

An internet search of LC_ALL will show you more languages and options if needed.

CentOS 6

I have only been able to change the keyboard to a British layout, the Mac layout doesn’t seem to work.

Edit the following file:

/etc/sysconfig/i18n

with the following:

LANG="en_GB.UTF-8"
SYSFONT="latarcyrheb-sun16"
SUPPORTED="en_GB.UTF-8:en_GB:en"

Or type the following command:

# loadkeys uk

You might need to restart if editing i18n, but the change should be automatic with loadkeys.

CentOS 7

Edit the following file:

/etc/locale.conf

with the following:

LANG="en_GB.UTF-8"

Or type the following command:

# localectl set-locale LANG=en_GB.UTF-8

Set the keymap:

# localectl set-x11-keymap gb macintosh mac lv3:alt_switch

In CentOS 7 it isn’t necessary to reboot, the above command automatically loads the key mappings.

CentOS 8

The same commands used for CentOS 7 fail. I suspect that there is a file or folder with the keyboard mappings that has been moved. It might be a bug or a deprecated feature.

Just in case I opened a bug report with CentOS.

Regardless, the following will set the locale and the keyboard mappings correctly:

# localectl set-locale LANG=en_GB.UTF-8
# localectl set-keymap gb-mac

There is no need to reboot.




Linux: Adding a GUI to headless/server installs

Server and minimal installs are normally headless and have no graphical interface.

If needed you can add a GUI manually. The process is slightly different depending on the distro.

RedHat / CentOS 7.x

# yum update
# yum groupinstall "Server with GUI"

RedHat / CentOS 8.x

# dnf update
# dnf groupinstall workstation

Ubuntu 18.04.x LTS

# apt update

[Install minimum GNOME desktop]
# apt install --no-install-recommends ubuntu-desktop

[Install full desktop with associated applications]
(Long process and too many extras installed)

# apt install ubuntu-desktop

[There are other alternative desktops and installations possible:]

[Generic Gnome desktop]
# apt install vanilla-gnome-desktop

[Mate]
# apt install ubuntu-mate-desktop

[Xfce]
# apt install xubuntu-desktop

[KDE]
# apt install kubuntu-desktop

[LightDM]
# apt install --no-install-recommends lightdm

Debian 9.x

# apt update
# apt install gnome-core

Debian 10.x

# apt update
# apt install gnome-core

All the above distros use systemd as their init system and you set the default run level with the same set of commands.

[Enable run level 5 by default]
systemctl  set-default graphical.target

[Enable run level 3 by default]
systemctl  set-default multi-user.target

Despite systemd you can still use init to start the graphical interface without having to reboot.

# init 5