How to Disable a Service in Debian Linux

Need to stop a service from running continually on your server, but still leave it available to start it manually?

Why would one want to to that? I can think of 2 reasons:
You are trying to save memory on a small VPS, and you don't need the service to be running continually. An example might be webmin.
You prefer to not leave a service running continually to avoid security risks. An example might be vsftp. You might prefer to start it manually when you need it, and stop it afterwards.

These instructions use webmin as an example. We will set things up so we can start it when we want to use it, and stop it afterwards.

How is this done? Obviously we don't want to remove it completely (apt-get remove webmin).

You could simply stop the service (/etc/init.d/webmin stop), but it would get started again the next time your server is rebooted.

To automatically start services at boot time, there are symbolic links in /etc/rc?.d/ corresponding to each runlevel. As each runlevel is entered, the links are excuted. Can't we just delete the links in each runlevel?

That will work to stop webmin from restarting when the server is rebooted. But the next time "apt-get upgrade" installs a replacement webmin, those links will get replaced!

The correct way is to install "kill" (stop the service) links for webmin in each of the runlevels. To do this, we use the update-rc.d utility.

First, let's check which runlevels are supposed to start and stop webmin. To do this, we can use the -n switch to "pretend" to delete the webmin start/stop symbolic links:

Code:
crnatural:/etc/init.d# update-rc.d -f -n webmin remove
Removing any system startup links for /etc/init.d/webmin ...
/etc/rc0.d/K20webmin
/etc/rc1.d/K20webmin
/etc/rc2.d/S20webmin
/etc/rc3.d/S20webmin
/etc/rc4.d/S20webmin
/etc/rc5.d/S20webmin
/etc/rc6.d/K20webmin

The "S" in the links indicates a startup link in the corresponding runlevel. The "K" links kill the process. So we can see that webmin was installed to start in runlevels 2, 3, 4, and 5, and stop in levels 0, 1, and 6. You may want to store that information in case you ever want to put the symbolic links back later. The startup script (/etc/init.d/webmin) is a good place to store it, and in fact many init.d startup scripts have this information in a comment at the top (not webmin).

In order to use update-rc.d to remove the "start" links, we must first delete ALL the start/stop symbolic links. update-rc.d refuses to create any links if any links already exist. This is to avoid overwriting links that have been manually installed by the system administrator.

So, to delete all the start/stop links, use this command (the -f switch is necessary to force removal of the links even though the startup script /etc/init.d/webmin still exists):

Code:
crnatural:/etc/init.d# update-rc.d -f webmin remove
Removing any system startup links for /etc/init.d/webmin ...
/etc/rc0.d/K20webmin
/etc/rc1.d/K20webmin
/etc/rc2.d/S20webmin
/etc/rc3.d/S20webmin
/etc/rc4.d/S20webmin
/etc/rc5.d/S20webmin
/etc/rc6.d/K20webmin

Finally, we run the command to install "stop" links in every runlevel:

Code:
crnatural:/etc/init.d# update-rc.d webmin stop 20 0 1 2 3 4 5 6 .
Adding system startup for /etc/init.d/webmin ...
/etc/rc0.d/K20webmin -> ../init.d/webmin
/etc/rc1.d/K20webmin -> ../init.d/webmin
/etc/rc2.d/K20webmin -> ../init.d/webmin
/etc/rc3.d/K20webmin -> ../init.d/webmin
/etc/rc4.d/K20webmin -> ../init.d/webmin
/etc/rc5.d/K20webmin -> ../init.d/webmin
/etc/rc6.d/K20webmin -> ../init.d/webmin
crnatural:/etc/init.d#

That's it, we're done! Now we can stop the webmin service:

Code:
crnatural:/etc/init.d# /etc/init.d/webmin stop
Stopping webmin: webmin.

Now webmin won't ever start up again automatically, even if you upgrade webmin later! (For more information on update-rc.d, see the man page.)
 

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <br> <img> <alt> <h1> <h2> <h3>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is to prevent automated spam submissions.
        ____    _   _   ____     ___    ____  
___ | _ \ | | | | |___ \ / _ \ |___ \
/ __| | |_) | | | | | __) | | (_) | __) |
\__ \ | _ < | |_| | / __/ \__, | / __/
|___/ |_| \_\ \___/ |_____| /_/ |_____|
Enter the code depicted in ASCII art style.