Everyone knows about “Safe Mode” in Windows. The days when a misconfigured driver could bring your system crashing down, and the only way to fix the OS was to press the “F8” or “Ctrl” key when Windows was starting up. Good days…While it’s hardly ever required any more, the concept of safe mode is sound. Often, systems need to be placed into a restricted state in order to fix some urgent problem or misconfiguration. A state where only the essential services are running. Kind of like certain types of bypass surgery, were you need to stop the heart in order to fix the problem.
SysVinit to Systemd
In CentOS, we used to have the concept of “runlevels”. These were different modes which had specific services running in each. We could switch between the different runlevels depending on the requirements. However, that was part of the old “SysVinit” system. A few years ago, administrators started shifting to a new mechanism called “systemd”.
From the user’s perspective, the biggest change was the replacement of the commands “service”, “chkconfig”, and “init.d” with the catch-all “systemctl”. When searching for help with various Linux commands, you’ve surely seen different responses from various people asking you to use different commands for the same task. Sometimes it’s chkconfig, sometimes “service start/stop”, and sometimes systemctl. Now you know why. Systemctl is the new mechanism to replace init.d and should always be used, going forward.
Runlevels and Targets
The old init.d mechanism used the idea of “runlevels”. These were numbered levels from 1 to 5, and we could switch between them with simple commands. In the new systemd, we have the concept of “targets”. There is a direct mapping between the old “runlevels” and targets. Here they are:
|Old Runlevels||New Targets|
You can see that three runlevels (2,3, and 4) have been compressed into a single runlevel – multi-user.target. By default, this is the target at which CentOS runs. Let’s take a quick look at what we can do with different targets.
“Target” Based Commands
You can get information about the current “targets” at which your system is running with the following command:
systemctl list-units --type=target
You can see in the screenshot below, that this returns “multi-user.target”, which is the expected value.
Next, we can switch between targets using this:
systemctl isolate [targetname]
Replace [targetname] with the one you want. For example, if you want to switch to the erstwhile “runlevel 5” which maps to “graphical.target”, you would use:
systemctl isolate graphical.target
Finally, you can also change the default target at which the system starts up each time. Here is the command for that:
systemctl set-default [targetname]
As before, replace [targetname] with the one you actually want to set as the default.
Using the CentOS “Safe Mode”
Now that we have the background, you can use the hidden “safe mode”, or as it’s called in CentOS, “Rescue mode”. Referring to the table above, you can see that the old “Runlevel 1” in the init.d framework is now called rescue.target. So if you want to switch your system to a minimalistic configuration with the lowest chance of conflicts, use the following command:
This is exactly the same as using the isolate command with:
systemctl isolate rescue.target
The only difference between these two is that the former will send a warning message to all users on the system like this:
Be very careful while using this command when logged in via remote SSH. It will disconnect you immediately! You’ll need to find a way to access your console directly to revert the target back to what it was, or reboot the system. Remember that rescue mode disables all networking access, so be prepared!
So “rescue mode” is the equivalent of “safe mode” in a Linux environment. Use it to perform low level maintenance and troubleshoot problems which require a minimum of running services.