Multi-System Compatibility Solutions for Executing Commands as Specific Users in Linux Init Scripts

Dec 03, 2025 · Programming · 30 views · 7.8

Keywords: Linux Init Scripts | User Privilege Management | Cross-System Compatibility

Abstract: This paper comprehensively examines the multi-system compatibility issues encountered when executing commands as non-root users in Linux initialization scripts. By analyzing the differences between Ubuntu/Debian and RHEL/CentOS systems, it focuses on the usage of the daemon function from /etc/rc.d/init.d/functions and the runuser command in RHEL systems, while comparing alternative approaches such as systemd configuration, su command, and start-stop-daemon. The article provides detailed code examples and system adaptation recommendations to help developers create reliable cross-platform initialization scripts.

User Privilege Management Challenges in Linux Init Scripts

In Linux system administration, initialization scripts (init scripts) are responsible for executing specific tasks during system startup or service management. A common requirement is to run commands as non-root users, which adheres to the principle of least privilege and avoids security risks. However, significant differences in implementation across Linux distributions lead to cross-platform compatibility issues.

Analysis of Differences Between Ubuntu/Debian and RHEL/CentOS

In Ubuntu and Debian systems, using sudo -u username command typically executes successfully. However, this approach may fail on RHEL (Red Hat Enterprise Linux) and its derivatives like CentOS, manifesting as command hanging or unresponsiveness. These differences stem from variations in initialization frameworks and toolchain configurations across distributions.

Specialized Solutions for RHEL Systems

For RHEL systems, the most effective solution leverages the system's built-in initialization function library. Through source code analysis, RHEL provides standardized service management functions in /etc/rc.d/init.d/functions.

Using the daemon Function

The daemon function is one of the core tools in RHEL initialization scripts, specifically designed for starting daemon processes. Its basic syntax is:

daemon --user=username command

In actual scripts, the function library must be sourced first:

# Source RHEL initialization functions
. /etc/rc.d/init.d/functions

# Start service as specific user
daemon --user=myappuser /usr/local/bin/myapp

This approach's advantage lies in handling details like process monitoring, logging, and signal processing, making it suitable for long-running service processes.

Using the runuser Command

For scenarios not requiring full daemon process management, the runuser command provides a lighter alternative. This command is specifically designed to run commands as other users, with syntax examples:

/sbin/runuser username -s /bin/bash -c "command_to_execute"

In practical application, it can be written as:

# Using runuser for complex command chains
/sbin/runuser deployuser -s /bin/bash -c "cd /var/www && ./deploy.sh"

Note that some RHEL versions may require different parameter ordering; consult man runuser for accurate information.

Comparison of Other Compatibility Solutions

systemd Service Configuration

For modern Linux systems using systemd (like CentOS 7+), the running user can be directly specified in service unit files:

[Unit]
Description=Example Service

[Service]
User=username
ExecStart=/usr/bin/command
Restart=on-failure

[Install]
WantedBy=multi-user.target

This method separates user management from script logic, improving configuration clarity and maintainability.

Traditional su Command

As the most basic solution, the su command is available on all Linux systems:

su - username -c "command"

Compared to sudo, su doesn't rely on additional privilege configuration but requires proper handling of user environment and privilege inheritance.

start-stop-daemon Tool

Some systems provide the start-stop-daemon tool, which combines user switching and process management:

start-stop-daemon --start --quiet -u username -g usergroup --exec command

This tool is more common in Debian-based systems but may require additional installation on RHEL.

Cross-Platform Init Script Design Recommendations

To write initialization scripts compatible across multiple systems, consider the following strategies:

  1. Environment Detection: Detect system type and available tools at script start
  2. Conditional Execution: Select appropriate user switching method based on detection results
  3. Error Handling: Add proper error checking and fallback mechanisms for each method
  4. Logging: Detailed logging of execution process and any exceptions

Here's a comprehensive example:

#!/bin/bash

# System detection
if [ -f /etc/redhat-release ]; then
    # RHEL/CentOS systems
    if [ -f /etc/rc.d/init.d/functions ]; then
        . /etc/rc.d/init.d/functions
        daemon --user=appuser /opt/app/bin/start
    else
        /sbin/runuser appuser -c "/opt/app/bin/start"
    fi
elif [ -f /etc/debian_version ]; then
    # Debian/Ubuntu systems
    sudo -u appuser /opt/app/bin/start
else
    # Universal fallback
    su - appuser -c "/opt/app/bin/start"
fi

Security Considerations

When executing commands as non-root users, the following security factors must be considered:

Performance and Reliability Considerations

Different user switching methods vary in performance and reliability:

By deeply understanding the differences between Linux distributions and adopting appropriate compatibility strategies, developers can create secure and reliable cross-platform initialization scripts. In practical applications, it's recommended to select the most suitable solution based on specific requirements and conduct thorough testing and validation.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.