19 Ekim 2010 Salı

Enabling and disabling services during start up in Linux

Here I will explain different ways of enabling and disabling the system services.







1) Red Hat Method







Red Hat and Red Hat based Linux distributions make use of the script called chkconfig to enable and disable the system services running in Linux.











For example, to enable the apache webserver to start in certain run levels, you use the chkconfig script to enable it in the desired run levels as follows:















# chkconfig httpd --add



# chkconfig httpd on --level 2,3,5







This will enable the apache webserver to automatically start in the run levels 2, 3 and 5. You can check this by running the command:







# chkconfig --list httpd







One can also disable the service by using the off flag as shown below:







# chkconfig httpd off



# chkconfig httpd --del







Red Hat also has a useful script called service which can be used to start or stop any service. Taking the previous example, to start apache webserver, you execute the command:







# service httpd start







and to stop the service...







# service httpd stop







The options being start, stop and restart which are self explanatory.







2) Debian Method







Debian Linux has its own script to enable and disable services across runlevels. It is called update-rc.d. Going by the above example, you can enable apache webserver as follows:







# update-rc.d apache2 defaults







... this will enable the apache webserver to start in the default run levels of 2,3,4 and 5. Of course, you can do it explicitly by giving the run levels instead of the "defaults" keyword as follows:







# update-rc.d apache2 start 20 2 3 4 5 . stop 80 0 1 6 .







The above command modifies the sym-links in the respective /etc/rcX.d directories to start or stop the service in the destined runlevels. Here X stands for a value of 0 to 6 depending on the runlevel. One thing to note here is the dot (.) which is used to terminate the set which is important. Also 20 and 80 are the sequence codes which decides in what order of precedence the scripts in the /etc/init.d/ directory should be started or stopped.







And to disable the service in all the run levels, you execute the command:







# update-rc.d -f apache2 remove







Here -f option which stands for force is mandatory.







But if you want to enable the service only in runlevel 5, you do this instead:







# update-rc.d apache2 start 20 5 . stop 80 0 1 2 3 4 6 .







3) Gentoo Method



Gentoo also uses a script to enable or disable services during boot-up. The name of the script is rc-update . Gentoo has three default runlevels. Them being: boot, default and nonetwork. Suppose I want to add the apache webserver to start in the default runlevel, then I run the command:







# rc-update add apache2 default







... and to remove the webserver, it is as simple as :







# rc-update del apache2







To see all the running applications at your runlevel and their status, similar to what is achieved by chkconfig --list, you use the rc-status command.







# rc-status --all







4) The old fashioned way



I remember the first time I started using Linux, there were no such scripts to aid the user in enabling or disabling the services during start-up. You did it the old fashioned way which was creating or deleting symbolic links in the respective /etc/rcX.d/ directories. Here X in rcX.d is a number which stands for the runlevel. There can be two kinds of symbolic links in the /etc/rcX.d/ directories. One starts with the character 'S' followed by a number between 0 and 99 to denote the priority, followed by the name of the service you want to enable. The second kind of symlink has a name which starts with a 'K' followed by a number and then the name of the service you want to disable. So in any runlevel, at any given time, for each service, there should be only one symlink of the 'S' or 'K' variety but not both.







So taking the above example, suppose I want to enable apache webserver in the runlevel 5 but want to disable it in all other runlevels, I do the following:







First to enable the service for run level 5, I move into /etc/rc5.d/ directory and create a symlink to the apache service script residing in the /etc/init.d/ directory as follows:







# cd /etc/rc5.d/



# ln -s /etc/init.d/apache2 S20apache2







This creates a symbolic link in the /etc/rc5.d/ directory which the system interprets as - start (S) the apache service before all the services which have a priority number greater than 20.







If you do a long listing of the directory /etc/rc5.d in your system, you can find a lot of symlinks similar to the one below.







lrwxrwxrwx 1 root root 17 Mar 31 13:02 S20apache2 -> ../init.d/apache2







Now if I start a service, I will want to stop the service while rebooting or while moving to single user mode and so on. So in those run levels I have to create the symlinks starting with character 'K'. So going back to the apache2 service example, if I want to automatically stop the service when the system goes into runlevel 0, 1 or 6, I will have to create the symlinks as follows in the /etc/rc0.d, /etc/rc1.d/, /etc/rc6.d/ directories.







# ln -s /etc/init.d/apache2 K80apache2







One interesting aspect here is the priority. Lower the number, the higher is the priority. So since the starting priority of apache2 is 20 - that is apache starts way ahead of other services during startup, we give it a stopping priority of 80. There is no hard and fast rule for this but usually, you follow the formula as follows:







If you have 'N' as the priority number for starting a service, you use the number (100-N) for the stopping priority number and vice versa.



AddThis Social Bookmark Button







Posted by Ravi







23 comments:







Anonymous said...







thank you, this is useful



on SuSE 10.0, chkconfig method works



4/02/2006 06:13:00 PM



Anonymous said...







You missed CRUX/Archlinux's way of doing this:



startup scripts are not stored in /etc/init.d, but rather in /etc/rc.d. A list of the services you want started is put into /etc/rc.conf and each is started in the order listed (and shutdown in the reverse order in which they were started); it looks like "SERVICES=(net inetd crond named sshd apache)" on my server running an old copy of CRUX for example. It will also run the script /etc/rc.local which can manually start services if you don't want to bother writing a special script; this is much more BSD-like than the SYSV-like init systems you've described. There are no runlevels (or rather, only one runlevel).







Also, I'm not really sure what the startup scripts of various distributions have to do with either GNU or Linux (as referenced in the title of the post), since I don't think either provides or even recommends one sort of startup script or another (well, I suppose Hurd must have something, but I don't know if it is packaged seperately).



4/03/2006 12:32:00 AM



Soumyadip Modak said...







Well, re: Debian, you could perhaps consider the use of rcconf. While not as granular as the method that you listed here, it is nevertheless easier for newcomers.







apt-get install rcconf



4/03/2006 02:02:00 AM



HackMan said...







There is one other type of system initialization which is also used. For example in Slackware all startup scripts are located at /etc/rc.d/ and there is no /etc/init.d directory. For most of the services there are separate rc files such as /etc/rc.d/rc.httpd or /etc/rc.d/rc.inetd. There are 2 tings that you can do to disable a service.



1. You can chmod 644 /etc/rc.d/rc.servicename



2. You can remove or comment the lines which call this file. Depending when the things are called you can find the lines calling the applications in:



rc.S - system initialization



rc.M - multy user mode(including grafics mode)



rc.K - single user mode







In Slackware you can start pkgtool and use the Setup option to reset the default configurations which also include what should be started at boot time.







Regards



Marian Marinov



4/03/2006 02:28:00 AM



Rugmonster said...







You're generalization about all distros using /etc/init.d is false. Slackware and it's derivatives use the BSD style /etc/rc.d for it's init scripts.



4/03/2006 03:55:00 AM



tommahu said...







slackware method :







chmod a-x /etc/rc.d/rc.yourservicename



4/03/2006 03:59:00 AM



Komakino said...







It is not true to say that "all linux distributions have startup scripts in /etc/init.d as Slackware (the oldest distro still going) uses BSD style init scripts which are in /etc/rc.d and are FAR easier to work with than the sys-v style used by the other distros, as disabling them only requires making them non-executable.



4/03/2006 04:04:00 AM



Anonymous said...







> But one thing is common for all Linux distributions which is that all the start-up scripts are stored in the '/etc/init.d/' directory.







One word: Slackware.



4/03/2006 08:44:00 AM



ashterix said...







good explanation!



just to add, for RH you can list out all services by typing:



# chkconfig --list







by default if you type:



# chkconfig httpd on



it will enable httpd in runlevel 2,3,5



4/03/2006 03:02:00 PM



Iulian said...







Slackware doesn't conform with what you are saying ( /etc/rc.d )







Cheers !



4/03/2006 04:48:00 PM



Stealth said...







You know, you could take an easier route by installing a program called: sysv-rc-conf







Its a slightly more graphical command-line utility (ncurses? idk) that allows you see the list of possible start up scripts, and disable what you please. :)



4/03/2006 04:55:00 PM



Luka Marinko said...







Slackware way:







all the scripts are in /etc/rc.d/ directory.



The ones you wanted started at boot just make them executable ie.







code:



chmod +x /etc/rc.d/rc.httpd



/code







You can also start/stop/restart services manualy:



ie.







code:



sh /etc/rc.d/rc.httpd start



/code







If you check you will find this are wery simple shellscripts, that you can easily create/modify for your own purposes.



4/03/2006 05:29:00 PM



Anonymous said...







Slackware (and derivatives) don't use /etc/init.d. Instead, they use /etc/rc.d and there reside every init script (BSD style). To enable/desable you just set/reset the executable bit for the script.



4/03/2006 06:38:00 PM



Anonymous said...







For SuSE use the insserv(8) function.







-d, --default Use default runlevels a defined in the scripts. This may restore an edited runlevel link scheme.







-r, --remove Remove the listed scripts from all runlevels.



4/03/2006 08:36:00 PM



Jeff said...







A quick note. Some distributions, notably Slackware and derivatives still use the older /etc/rc.d/ directory as the container for their startup scripts. It's really simple to enable / disable services on these distributions.



The files in this directory are ususally named "rc.SERVICE". ie. "rc.sendmail" for the sendmail service.



To enable / disable the service, simply add / remove the execute bit on the script.



For apache: "chmod -x /etc/rc.d/rc.httpd" to disable Apache from loading at startup or "chmod +x /etc/rc.d/rc.httpd" to enable it. Simple no?



4/03/2006 08:39:00 PM



Anonymous said...







Great! Thanks for the info. Just a small detail between "3) Gentoo Method" and "5) The Old .." there was no "4)". :)



4/04/2006 03:37:00 PM



breun said...







I prefer running ntsysv under Red Hat-style systems (or run 'setup' and choose Services) and just selecting there which services should and shouldn't start automatically. If you don't want to differentiate between runlevels I think this is the easiest way.



4/17/2006 01:05:00 AM



Anonymous said...







Excellent...thanks Ravi



4/23/2006 06:24:00 AM



Anonymous said...







It would be nice to also know how to run these services as a different user



4/25/2006 10:29:00 PM



Anonymous said...







It would be nice indeed. I use to do that in KDE using SysV-Init Editor but that application won't start anymore for some reason. So I need a way to set the user runnning a service from the command line :)



5/10/2006 04:37:00 AM



rajat swarup said...







BootUpManager (a.k.a bum) is also a good tool to do this configuration.







sudo apt-get install bum



sudo bum







Then simply check or uncheck the services you want to remove.



12/04/2006 01:02:00 AM



Free File Hosting said...







Hi,







In Centos there is RunLevels for single user mode and Multi User Mode, what are difference and in which time do I need to use Single User Mode?







P.S: Cool website.



1/06/2007 04:18:00 AM



Kevin said...







In response to what Free File Hosting said...







Since centos is just a free distribuation of RHEL.







Just follow what ashterix said...







by default if you type:



# chkconfig httpd on



it will enable httpd in runlevel 2,3,5

Hiç yorum yok:

Yorum Gönder