Boot a Linux CVM into single user mode

Last updated: 2019-10-14 15:52:00

PDF

Scenario

Linux users sometimes need to boot into single user mode to perform some special operations, such as password management, sshd corruption, etc. This document describes how to boot into the single user mode in mainstream Linux operating systems.

Directions

Determining the Type of the Operating System

Follow different steps based on the type of the operating system.

CentOS 6

Centos 6 uses GRUB boot loader. The following steps use CentOS 6.9 as an example. Specific steps vary slightly depending on the version of the operating system.

  1. Log in to the CVM instance remotely.
  2. Execute the following command to open the /etc/grub.conf file.
    vi /etc/grub.conf
  3. Press I to enter the insert mode.
  4. Find “GRUB_TIMEOUT”, the parameter setting the length of waiting time before the default entry is booted, and modify its value based on your needs.
    The default value of “GRUB_TIMEOUT” is 5 seconds. In order to avoid missing the boot interface because the waiting time is too short, it is recommended to modify it to 60s or longer.

    This item affects how long it takes for the system to start up. After you complete the configuration, modify it back to the default value.

  5. Press Esc to exit the insert mode, type :wq, and press Enter.
    Save your settings and exit the VI editor.
  6. Execute the following command to reboot the server.
    reboot
  7. Wait for about 1 minute, then log in to the CVM instance via VNC. The login interface is as shown below:
  8. Press any key to enter the menu as shown below:
  9. Press e to enter the kernel editing interface and enter single as shown below:
  10. Press Enter as shown below:
  11. In the interface as shown below, press b to enter the single user mode.
  12. Execute the following command to exit the single user mode.
    exec /sbin/init

CentOS 7

Unlike CentOS 6, CentOS version 7 and above use GRUB 2. The following steps use CentOS 7.5 as an example. Specific steps vary slightly depending on the version of the operating system.

  1. Log in to the CVM instance remotely.
  2. Execute the following command to open the /etc/default/grub file.
    vi /etc/default/grub
  3. Press I to enter the insert mode.
  4. Find “GRUB_TIMEOUT”, the parameter setting the length of waiting time before the default entry is booted, and modify its value based on your needs as shown below:
    The default value of “GRUB_TIMEOUT” is 5 seconds. In order to avoid missing the boot interface because the waiting time is too short, it is recommended to modify it to 60s or longer.

    This item affects how long it takes for the system to start up. After you complete the configuration, modify it back to the default value.

  5. Press Esc to exit the insert mode, type :wq, and press Enter.
    Save your settings and exit the VI editor.
  6. Execute the following command to recompile and generate the grub.cfg file.
    grub2-mkconfig -o /boot/grub2/grub.cfg
    The returned result is as follows:
  7. Execute the following command to reboot the server.
    reboot
  8. Wait for about 1 minute, then log in to the CVM instance via VNC. The login interface is as shown below:
  9. Press e to enter the kernel editing interface and add init=/bin/sh to the red box area as shown below:
  10. Press Ctrl+X to start and enter the single user mode as shown below:
  11. Execute the following command to exit the single user mode.
    exec /sbin/init

Ubuntu

The following steps use Ubuntu 16.04 as an example. Specific steps vary slightly depending on the version of the operating system.

  1. Log in to the CVM instance remotely.
  2. Execute the following command to open the /etc/default/grub file.
    sudo vi /etc/default/grub
  3. Press I to enter the insert mode.
  4. Find “GRUB_TIMEOUT”, the parameter setting the length of waiting time before the default entry is booted, and modify its value based on your needs as shown below:
    The default value of “GRUB_TIMEOUT” is 5 seconds. In order to avoid missing the boot interface because the waiting time is too short, it is recommended to modify it to 60s or longer.
    • This item affects how long it takes for the system to start up. After you complete the configuration, modify it back to the default value.
    • The default account in Ubuntu is not root, please use the sudo commands.
    sudo vi /etc/default/grub
  5. Execute the following command to recompile and generate the grub.cfg file.
    sudo update-grub
    The returned result is as follows:
  6. Execute the following command to reboot the server.
    sudo reboot
  7. Wait for about 1 minute, then log in to the CVM instance via VNC. The login interface is as shown below:
  8. Press e to enter the kernel editing interface and add rw single init=/bin/bash to the red box area as shown below:
  9. Press Ctrl+X to start and enter the single user mode as shown below: