Runlevel Configuration: Customizing System Behavior

How can I customize my system's behavior by configuring runlevels or their systemd equivalents?

1 Answers

βœ“ Best Answer

Understanding Runlevel Configuration βš™οΈ

Runlevels define the state of a Unix-like operating system. Traditionally, they dictate which services start when the system boots. Systemd uses targets, which are similar to runlevels but more flexible.

Traditional Runlevels (SysVinit) πŸ“œ

In SysVinit, runlevels are numbered 0-6 and S/1 (single-user mode). Common runlevels include:

  • 0: Halt
  • 1: Single-user mode
  • 2-5: Multi-user mode (typically 3 is the default)
  • 6: Reboot

Configuration files are located in /etc/inittab and /etc/rc[0-6].d/.

Example: Changing Default Runlevel

Edit /etc/inittab and modify the initdefault line:


sudo nano /etc/inittab
# id:3:initdefault:

Change the 3 to your desired runlevel. This method is deprecated in favor of systemd.

Systemd Targets 🎯

Systemd uses targets instead of runlevels. Targets are more flexible and can be dependencies for other targets.

Common Systemd Targets

  • poweroff.target: System shutdown
  • rescue.target: Single-user mode
  • multi-user.target: Multi-user mode, non-graphical
  • graphical.target: Multi-user mode with a graphical interface
  • reboot.target: System reboot

Changing Default Target

To change the default target, use the systemctl command:


sudo systemctl set-default multi-user.target

This sets the default target to multi-user mode (no GUI).

Checking Current Target

To check the current target, use:


systemctl get-default

Isolating a Target

To switch to a different target during runtime, use:


sudo systemctl isolate graphical.target

This command switches to the graphical target (GUI).

Customizing Services πŸ› οΈ

Whether using SysVinit or systemd, you can customize which services start in a particular runlevel/target.

SysVinit

Scripts in /etc/rc[0-6].d/ determine which services start or stop in each runlevel. Scripts starting with S start the service, and scripts starting with K stop the service. The numbers indicate the order in which the scripts are executed.

Systemd

Systemd uses unit files (usually in /etc/systemd/system/) to define services. To enable or disable a service:


sudo systemctl enable your_service.service
sudo systemctl disable your_service.service

Example: Creating a Custom Systemd Service

Create a unit file (e.g., /etc/systemd/system/mycustom.service):


[Unit]
Description=My Custom Service
After=network.target

[Service]
ExecStart=/usr/local/bin/my_script.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target

Then, enable and start the service:


sudo systemctl enable mycustom.service
sudo systemctl start mycustom.service

Conclusion πŸŽ‰

Understanding runlevels and systemd targets is crucial for customizing system behavior. Systemd offers more flexibility and control compared to traditional SysVinit. Proper configuration ensures your system starts in the desired state with the necessary services running.

Know the answer? Login to help.