[Fiexed] system has not been booted with systemd as init system (pid 1). can’t operate

Encountering the error message “system has not been booted with systemd as init system (pid 1). can’t operate.” can be confusing, especially for users accustomed to systemd-based environments. This issue typically arises in specific scenarios where systemd is not the default init system, such as in Windows Subsystem for Linux (WSL) or Docker containers. In this guide, we’ll explore the causes of this error and provide effective solutions to manage your system effectively, whether you can enable systemd or need to work around its absence.

Understanding the Error

An init system is the first process started by the kernel during the booting process and is responsible for initializing the system. It manages system initialization, starts and stops services, and handles system shutdown. The init system plays a crucial role in ensuring that all necessary services and processes are up and running for the system to function correctly.

The Role of Systemd

Systemd is a popular init system used by many modern Linux distributions, including Ubuntu, Fedora, and Debian. It offers several advanced features, such as parallel service startup, on-demand starting of daemons, and dependency-based service control logic. These features contribute to faster boot times and more efficient resource management. Systemd also provides a unified interface for managing system services, logs, and configuration, making it a powerful tool for system administrators.

Common Scenarios for the Error

This error commonly occurs in environments where systemd is not the default init system. Two prevalent scenarios include:

  • Windows Subsystem for Linux (WSL): WSL allows users to run a Linux environment directly on Windows without the overhead of a virtual machine. However, WSL traditionally uses a simpler init system rather than systemd, which can lead to compatibility issues when running commands or services expecting systemd.
  • Docker Containers: Docker containers are designed to be lightweight and often use minimal init systems to reduce overhead. As a result, they may not include systemd by default, causing errors when trying to use systemdspecific commands or configurations.

Checking Your Current Init System

Determining the init system in use is a straightforward process but crucial for effective system management and troubleshooting.

Using the ps Command

The ps command is a versatile tool for displaying information about active processes. To check your init system, execute the following command in the terminal:

ps -p 1 -o comm=

This command outputs the name of the process with PID 1, which is the init system. If the output is systemd, it indicates that systemd is the active init system. If the output is init, it likely signifies that SysVinit is in use.

The init system can also be identified by examining the symbolic link /sbin/init. Running the following command reveals the target of this link:

ls -l /sbin/init

The output will point to the executable managing the init process. For example:

  • systemd will point to /lib/systemd/systemd
  • SysVinit will point to /sbin/init

Using the systemctl Command

If you suspect that systemd is in use, you can leverage systemctl, a systemd utility, to confirm:

systemctl

If systemd is active, this command will display a list of system services managed by systemd. If it is not installed or active, the command will fail, indicating that an alternative init system is in use.

WindowsTerminal Y0OVN4hVee

How to Install and Enable Systemd in WSL

Enabling systemd in Linux Distro running on Windows Subsystem for Linux (WSL) involves several steps.

Pre-requisites

Before you begin, ensure the following:

WSL 2 Installed

Ensure you are running WSL 2, as it provides better compatibility with systemd due to its full Linux kernel.

wsl --list --verbose
Updated WSL and Ubuntu

Make sure both WSL and your Ubuntu distribution are up to date.

sudo apt update && sudo apt upgrade -y

Install Necessary Packages

Ensure your system has the latest updates and install systemd packages:

sudo apt install systemd

Modify WSL Configuration

To enable systemd in WSL (Windows Subsystem for Linux), you need to modify the WSL configuration file.

Open or create the WSL configuration file in your preferred text editor. Typically, this file is located at /etc/wsl.conf:

sudo nano /etc/wsl.conf

Add the following lines to enable systemd:

[boot]
systemd=true

Save and exit the text editor.

Restart WSL

Restart your WSL instance to apply the changes. You can do this by shutting down WSL and then starting it again.

wsl --shutdown
wsl

After restarting, systemd should be active in your WSL environment.

Verifying Systemd Installation

To confirm that systemd is running as the init system, use the following command:

ps -p 1 -o comm=

If the output is systemd, it indicates that systemd is successfully installed and running as PID 1.

Handling the Lack of Systemd

In environments where systemd cannot be enabled, such as some Docker containers or WSL instances, it’s essential to explore alternative methods for managing services and processes.

Service Management

Use service or rc-service (for OpenRC) to start, stop, and restart services.

sudo service <service-name> start
sudo service <service-name> stop
sudo service <service-name> restart

Scheduling Tasks

Replace systemd timers with cron jobs for scheduling recurring tasks.

crontab -e

Add a cron job in the format: * * * * /path/to/command

Running Services in WSL Without Systemd

When systemd isn’t available, managing services in WSL requires alternative methods. Let’s look at a practical example:

Starting Services Manually

You can start services manually using the service command:

sudo service apache2 start
sudo service mysql start

Creating Custom Scripts

Automate service startup with a custom script. Create a file start-services.sh:

#!/bin/bash
sudo service apache2 start
sudo service mysql start

Make the script executable and run it at startup:

chmod +x start-services.sh
./start-services.sh

Using Cron for Automated Tasks

Schedule tasks with cron to ensure services start at boot. Edit the crontab:

crontab -e

Add a line to start services at reboot:

@reboot /path/to/start-services.sh

Using Docker Without Systemd

In Docker environments, containers often lack systemd to keep them lightweight. Here’s how to manage services and processes effectively.

Supervisord

Use supervisord to manage processes within a container.

Install supervisor

sudo apt-get install supervisor

Starting Supervisord

Start supervisord to launch and monitor services:

sudo supervisord -c /etc/supervisor/supervisord.conf

Monitoring and Control

Use supervisorctl to interact with running services:

sudo supervisorctl status
sudo supervisorctl start <service-name>
sudo supervisorctl stop <service-name>

Docker Configuration Without Systemd

In Docker environments, it’s essential to manage services efficiently without systemd. Here’s how you can set up a multi-service container using supervisord:

Creating the Dockerfile

Use the following Dockerfile to install and configure supervisord:

FROM ubuntu:latest
RUN apt-get update && apt-get install -y supervisor nginx mysql-server
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
CMD ["/usr/bin/supervisord"]

Supervisord Configuration

Create a supervisord.conf file to manage services:

[supervisord]
nodaemon=true

[program:nginx]
command=/usr/sbin/nginx -g "daemon off;"
autostart=true
autorestart=true

[program:mysql]
command=/usr/sbin/mysqld
autostart=true
autorestart=true

Building and Running the Docker Container

Build the Docker image and run the container:

docker build -t my-supervised-container .
docker run -d my-supervised-container

This setup ensures that both Nginx and MySQL are managed and monitored by supervisord within the container, providing a robust alternative to systemd.

Conclusion

Handling the “system has not been booted with systemd as init system (pid 1)” error requires understanding the role of init systems and the environments where systemd is not the default. By identifying your current init system and adapting your approach, you can effectively manage services and processes whether or not systemd is available. With the steps provided, you can enable systemd in WSL or utilize alternatives in Docker, ensuring smooth and efficient system operations.

Share your love

Newsletter Updates

Stay updated with our latest guides and tutorials about Linux.

Leave a Reply

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