Technology

#How Linux Runlevels Affect Running Services – CloudSavvy IT

“#How Linux Runlevels Affect Running Services – CloudSavvy IT”

power button
Shutterstock/Allexxandar

Linux runlevels distinguish between system operating states. Switching runlevel will change the services which are running. You can customise runlevels so each level starts with a specific set of services.

Standard Runlevels

The Linux kernel defines seven standard runlevels. Individual operating system distributions may customise these runlevels or add additional ones. Users are also free to create and modify runlevels.

  • Runlevel 0 – No activity, equivalent to shutdown. The system is ready for a physical loss of power.
  • Runlevel 1 – Running in single-user (rescue) mode.
  • Runlevel 2 – Running in multi-user mode without networking and graphics.
  • Runlevel 3 – Multi-user mode with the addition of networking support.
  • Runlevel 4 – Left for users/distributions to define.
  • Runlevel 5 – Multiple user mode with a graphical user interface.
  • Runlevel 6 – System reboot mode.

Most Linux systems will boot into Runlevel 5. This is the runlevel you’re probably most familiar with. The broadest range of services are available, including a display server and full networking stack. If you’re on a headless server without a display stack, you might be in Runlevel 3.

Some single-user systems will start in Runlevel 1.. You’ll also encounter this runlevel if you use a recovery mode to rescue your system. This lets you obtain shell access without logging in as a regular user.

Runlevels 2, 3 and 4 can vary across distribution. Some distributions will create a clear distinction between each runlevel; in others, all three may have the same effect. Generally, you can expect any of these three to give you a single-user text shell with networking available.

Your system can only be in one runlevel at a time. A single runlevel will be selected after boot. You won’t transition been runlevels unless you explicitly initiate a change. Usually, the only runlevel transitions that occur are from levels 1-5 into level 0 or 6, when your system shuts down or reboots.

Higher runlevels indicate increasing levels of available functionality. This convention is followed by all distributions. A low runlevel usually means that only a subset of the system’s services are running.

Runlevels and Startup Services

Startup services are coupled to runlevels. Linux traditionally adds startup services to runlevels. When your system enters a runlevel, all the services associated with that level will be started.

The way in which services are actually handled depends on the service manager you’re using. Individual distributions ship with different service managers. The original service manager, init, defines its services within /etc/init.d. Scripts created here get symlinked into /etc/rc where they’re sorted by runlevel:

  • /etc/rc0.d – Scripts placed in here are executed by runlevel 0…
  • /etc/rc1.d – Scripts for runlevel 1…
  • /etc/rc2.d – …etc.

To add new startup services, create or edit a script in /etc/init.d:

ms:345:respawn:/usr/bin/service_executable

Next, use chkconfig to enable the service:

sudo chkconfig service_executable on

The 345:respawn in the script line instructs init to run service_executable when the system enters runlevels 3, 4 or 5.

Many newer distributions have replaced init with more modern alternatives. These can provide a higher-level abstraction over runlevels and the init system.

The majority of Linux distributions, including Debian, Ubuntu, CentOS, Arch and Red Hat, now use systemd. This doesn’t reference runlevels directly; instead, they’re converted to “targets” which are identified by name. While Linux can only be in one runlevel at a time, systemd supports multiple concurrently active targets. The mapping between runlevels and targets is only approximate; it’s provided for compatibility purposes.

The special default target defines what the system will boot into. The default target is usually linked to the multi-user target – equivalent to a runlevel of 2 or higher.
Each target’s services are stored in a “wants” directory. This will contain .service files. These are also symbolic links that point back to service definitions in /usr/lib/systemd/system.

ls -l /etc/systemd/system/multi-user.target.wants/*.service


This allows service definitions to be created once and shared with multiple “targets”. When Linux boots, systemd will select the target indicated by the runlevel. The services wanted by that target will then be loaded up. systemd supports service dependencies, so individual services can be made to wait on other services before they start.

Inspecting Your System’s Runlevel

You can find out your system’s runlevel using the runlevel command. This will print two characters to the terminal. They show your system’s previous and current runlevels.

N 5 is a typical output for a desktop Linux system. The 5 indicates you’re in a multi-user graphical session with networking available. N means the previous runlevel couldn’t be determined, usually because the system was shutdown.

On some systems, you may be able to get the current and previous runlevels using the $RUNLEVEL and $PREVLEVEL environment variables. If these variables are set, the runlevel command simply emits their values.

You can also inspect your current runlevel using the who command. Run who -r to see the runlevel number and the time at which the runlevel was entered.

Switching Between Runlevels

You can switch between runlevels using the telinit command. This lets you manually trigger different modes, such as single-user mode or rescue mode.

Running telinit 0 will shutdown your machine; telinit 6 should initiate a reboot. Using telinit with runlevels 1 – 5 will activate the corresponding operating mode. If you’re currently in text-only mode, telinit 5 will try to enable the display server and enter graphical mode.

When using systemd, the systemctl isolate command lets you activate a different “target”. All running services will be stopped; services associated with the new target then get started up.

Here’s how to activate the multi-user target:

sudo systemctl isolate multi-user.target

If you want to make a permanent change to the runlevel, use the systemctl set-default command:

sudo systemctl set-default multi-user.target

Now the multi-user target will be selected whenever your system reboots.

Summary

Linux runlevels describe different states within the operating system. Each runlevel layers additional functionality in the form of extra running services.

Low runlevels are commonly used for recovery modes and text-only operation. A desktop Linux system with a graphical environment will usually be in runlevel 5. You should check your distribution’s documentation for a detailed description of available runlevels, as implementations can vary.

If you liked the article, do not forget to share it with your friends. Follow us on Google News too, click on the star and choose us from your favorites.

For forums sites go to Forum.BuradaBiliyorum.Com

If you want to read more like this article, you can visit our Technology category.

Source

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button
Close

Please allow ads on our site

Please consider supporting us by disabling your ad blocker!