What Does the 'service: command not found' Error Mean
The service: command not found (or bash: service: command not found) error in Linux occurs when you try to run the service command, but the system cannot find it in the directories listed in the PATH variable. The service command is used to manage system services (start, stop, restart) in distributions with a System V or Upstart init system. In modern systemd-based systems, its role is often fulfilled by systemctl, but service may be retained for backward compatibility.
A typical scenario: you type sudo service nginx start and get an error message instead of managing the service. This does not indicate a problem with the service itself (nginx), but points to the absence of the service utility on your system.
Causes
- The
sysvinit-utils(or equivalent) package is not installed. In Debian/Ubuntu, theservicecommand is provided by thesysvinit-utilspackage, which is not installed by default in minimal or cloud images. In CentOS/RHEL, the equivalent functionality is provided by theinitscriptspackage. - The package is corrupted or removed. If the package was accidentally removed or its files are corrupted, the command stops working.
- The
PATHvariable does not include system directories. Theservicecommand is usually located in/sbinor/usr/sbin. If these directories are missing fromPATH(e.g., in a user environment without administrator privileges), the shell cannot find it. - The system uses a pure systemd without compatibility. Some modern distributions (e.g., a fresh Ubuntu 22.04 install) may not include
serviceby default, assuming the use ofsystemctl. - Symbolic link error. In systemd systems,
serviceis often a symbolic link tosystemctl. If the link is broken, the command fails.
Solutions
Solution 1: Install the package that provides service
This is the most direct approach. Install the package containing the service utility for your distribution.
For Ubuntu/Debian:
sudo apt update
sudo apt install sysvinit-utils
For CentOS/RHEL (versions 7 and below, which use System V):
sudo yum install initscripts
For Fedora or CentOS 8+ (with systemd): The initscripts package may be available, but often service is already present. If not, install sysvinit-tools (if available) or use systemctl.
After installation, verify:
which service
Expected output: /usr/sbin/service or a similar path. The command should now work.
Solution 2: Use systemctl instead of service
If your system runs under systemd (likely for Ubuntu 16.04+, Debian 8+, CentOS 7+), it is recommended to use systemctl—a more modern and functional alternative.
Examples of command replacement:
# Instead of:
sudo service nginx start
# Use:
sudo systemctl start nginx
# To check status:
sudo systemctl status nginx
# To enable autostart:
sudo systemctl enable nginx
systemctl supports all service functions and adds capabilities like unit management, log viewing via journalctl, and dependencies. Switch to systemctl to avoid future issues with a missing service.
Solution 3: Check and fix the PATH variable
If the package is installed but the command is still not found, the issue may be with the PATH variable.
- Check the current
PATH:echo $PATH
Ensure the output includes/sbinand/usr/sbin. They are typically present inPATHfor root but may be missing for regular users. - If the directories are missing, add them to
PATHtemporarily:export PATH=$PATH:/usr/sbin:/sbin
This command only affects the current terminal session. - To add them permanently, edit the
~/.bashrcfile (for the user) or/etc/profile(for all users):nano ~/.bashrc
Add the line:export PATH=$PATH:/usr/sbin:/sbin
Then apply the changes:source ~/.bashrc - Restart the terminal and check
which serviceagain.
Solution 4: Reinstall a corrupted package
If the package is installed but the service file is missing or corrupted, reinstall it:
For Ubuntu/Debian:
sudo apt install --reinstall sysvinit-utils
For CentOS/RHEL:
sudo yum reinstall initscripts
After reinstalling, verify the file's integrity:
ls -l /usr/sbin/service
It should be executable (permissions -rwxr-xr-x).
Prevention
To avoid encountering the service command not found error again:
- Install basic service management packages when deploying minimal systems (e.g., in Docker containers or cloud images). For Debian/Ubuntu, include
sysvinit-utilsin your package list. - Use
systemctlin new projects. Since most modern distributions have switched to systemd, get accustomed tosystemctl—this ensures compatibility. - Update your system regularly.
sudo apt update && sudo apt upgradeorsudo yum updatewill help prevent package conflicts. - Check
PATHwhen customizing your environment. If you modifyPATHin~/.bashrcor/etc/environment, ensure you do not remove system directories. - Use full paths in scripts. In automated scripts, specify
/usr/sbin/serviceorsystemctlinstead of justserviceto avoidPATHdependencies.