6
Configuring services in Linux. Page contents Overview of services in Linux. chkconfig and service commands. system-config-services chkconfig command. service command. Which services to run? Overview of services in Linux. Services are programs (called daemons) that once started run continuously in the background and are ready for input or monitor changes in your computer and respond to them. For example the Apache server has a daemon called httpd (the d is for daemon) that listens on port 80 on your computer and when it receives a request for a page it sends the appropriate data back to the client machine. Many services are required to run all the time however many can be safely turned of for both security reasons as running unnecessary services opens more doors into your computer, but also for performance reasons. It may not make much difference but your computer should boot slightly faster the less services it has to start on boot. Even though I use my laptop as a development machine running Apache, PHP and MySQL and I am also connected to a home network I choose to start Apache, MySQL and Samba manually as required. This page explains how to start and stop services and how to control which services start automatically at boot time. chkconfig and service commands. There are 2 commands used to control services: /sbin/chkconfig - this controls which services are set to start on boot, by their nature these setting are saved and are applied at next boot. Changing these settings will not start the service immediately, it will just flag them to be started from the next boot. /sbin/service - this controls the starting and stopping of services during a session, these setting are not saved. If you start Apache this way but it is not set to start on boot using the above method then it will continue to run but on next boot will not start automatically. GUI tool - system-config-services. First of all we need to know which services are running and which are stopped. There are two ways of doing this, the graphical way is to open up system-config- services. If you look in the start menu it should be under Desktop > System >

Configuring Services in Linux

Embed Size (px)

DESCRIPTION

Configuring Services in Linux

Citation preview

Page 1: Configuring Services in Linux

Configuring services in Linux.Page contents

Overview of services in Linux. chkconfig and service commands. system-config-services chkconfig command. service command. Which services to run?

Overview of services in Linux.Services are programs (called daemons) that once started run continuously in the background and are ready

for input or monitor changes in your computer and respond to them. For example the Apache server has a

daemon called httpd (the d is for daemon) that listens on port 80 on your computer and when it receives a

request for a page it sends the appropriate data back to the client machine.

Many services are required to run all the time however many can be safely turned of for both security reasons

as running unnecessary services opens more doors into your computer, but also for performance reasons. It

may not make much difference but your computer should boot slightly faster the less services it has to start on

boot.

Even though I use my laptop as a development machine running Apache, PHP and MySQL and I am also

connected to a home network I choose to start Apache, MySQL and Samba manually as required. This page

explains how to start and stop services and how to control which services start automatically at boot time.

chkconfig and service commands.

There are 2 commands used to control services:

/sbin/chkconfig - this controls which services are set to start on boot, by their nature these setting are saved and are applied at next boot. Changing these settings will not start the service immediately, it will just flag them to be started from the next boot.

/sbin/service - this controls the starting and stopping of services during a session, these setting are not saved. If you start Apache this way but it is not set to start on boot using the above method then it will continue to run but on next boot will not start automatically.

GUI tool - system-config-services.

First of all we need to know which services are running and which are stopped. There are two ways of doing

this, the graphical way is to open up system-config-services. If you look in the start menu it should be under

Desktop > System > Administration > Services. You will be asked for your root password and then be

presented with a screen like below. You can also start it from a terminal by typing 'system-config-services'.

Page 2: Configuring Services in Linux

From here you are able to tell which services are set to start on boot, those denoted with a tick in the checkbox,

and also which are currently running by looking at the information in the bottom right panel. You can also start,

stop and restart services from here. Note that if you start a service it will not be set to start on next boot unless

you click the checkbox and then save your settings. Note that this graphical front end uses both chkconfig and

service commands since you are able to control both in session starting and starting at next boot.

This is a good place to start looking at services but like many things in Linux it's actually easier to control

services from the command line.

Finding which services start on boot using chkconfig

To get this information we use the chkconfig command, if you wish to see the status of all services then type:

$ /sbin/chkconfig --list

NetworkManager 0:off 1:off 2:off 3:off 4:off 5:on 6:off

NetworkManagerDispatcher 0:off 1:off 2:off 3:off 4:off 5:on 6:off

acpid 0:off 1:off 2:off 3:on 4:on 5:on 6:off

anacron 0:off 1:off 2:on 3:on 4:on 5:on 6:off

apmd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

atd 0:off 1:off 2:off 3:on 4:on 5:on 6:off

ati-fglrx 0:off 1:off 2:on 3:on 4:on 5:on 6:off

auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

autofs 0:off 1:off 2:off 3:on 4:on 5:on 6:off

bluetooth 0:off 1:off 2:on 3:on 4:on 5:off 6:off

cpuspeed 0:off 1:on 2:on 3:on 4:on 5:on 6:off

Page 3: Configuring Services in Linux

......................

As you can see this returns a long list of all the services, each column refers to a different run level. In most

cases we boot into level 5 so this is the column we are most interested in (see below for the meaning of the

different run levels). The on and off refer to whether the service is set to start on boot, it does not tell us

whether the service is currently running.

Run levels.

0: Halt 1: Single User Mode 2: Basic Multi-user Mode 3: Full Multi-user without X 4: Not Used 5: Mutli-user with X 6: Reboot

This table just shows the meaning of different run levels. As you can see we normally boot into run level 5,

multi-user with X.

To query the status of just one service we can use grep to filter the returned data. Here we use chkconfig but

only want to see the Apache service.

$ /sbin/chkconfig --list|grep httpd

httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off

So this shows that Apache is not set to start on boot in all run levels.

View only services not started on boot.

We can use grep again to see only those services set to start on boot for a particular run level. Here we see

services not set to start in run level 5.

$ /sbin/chkconfig --list|grep -v 5:on

bluetooth 0:off 1:off 2:on 3:on 4:on 5:off 6:off

cups-config-daemon 0:off 1:off 2:off 3:on 4:on 5:off 6:off

dc_client 0:off 1:off 2:off 3:off 4:off 5:off 6:off

dc_server 0:off 1:off 2:off 3:off 4:off 5:off 6:off

dhcdbd 0:off 1:off 2:off 3:off 4:off 5:off 6:off

diskdump 0:off 1:off 2:off 3:off 4:off 5:off 6:off

httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off

irda 0:off 1:off 2:off 3:off 4:off 5:off 6:off

lirc 0:off 1:off 2:off 3:off 4:off 5:off 6:off

mDNSResponder 0:off 1:off 2:off 3:on 4:on 5:off 6:off

.........................

Page 4: Configuring Services in Linux

This command uses the -v option for grep, meaning non matching lines. What this line does is get all services

from chkconfig but then only shows the lines that do not include the term 5:on, i.e. it will only display lines

where 5:off occurs meaning the service is not set to start on boot. You could replace '5:on' with '5:off' to get all

services set to start on boot or just ':off' would return all lines where the service is set to start in all run levels.

Changing whether a service starts on boot.

If you want to alter which services get started at boot time then we use chkconfig with different arguments. For

example to set Apache to start on boot in run level 5:

$ /sbin/chkconfig --level 5 httpd on

To then stop it Apache starting on boot replace 'on' with 'off'.

$ /sbin/chkconfig --level 5 httpd off

If you wish to set more run levels just replace '5' with the run level required without spaces, e.g. '345'.So you

can see that it's simple to configure which services start on boot, the next step is to manually start a service

when required. This way we can turn off many services not actually required for the normal operation of the

computer and just start them when the need arises.

Starting and stopping services using the service command.

To start a service simply use the service command, this example uses Apache (httpd service) as an example

but the command is the same for any service.

$ /sbin/service httpd start

Starting httpd: [ OK ]

Stopping a service is just as easy

$ /sbin/service httpd stop

Stopping httpd: [ OK ]

As you may have guessed restarting just uses restart in place of start or stop. Note that this will start a service

for this session but after rebooting this service will not automatically restart. In my case this is what I want,

many times I start my laptop to check email but have no need for Apache, MySQL, Samba and so on. I find it

much easier just to start them as required. It's also very easy to write a simple bash script that starts Apache

and MySQL from one command.

Get all running services.

To see a list of all the currently running services we can use grep again to filter the data for the word 'running'.

Conversely we could use 'stopped' to see a list of all stopped services.

$ /sbin/service --status-all|grep running

acpid (pid 2146) is running...

anacron (pid 2236) is running...

atd (pid 2243) is running...

Page 5: Configuring Services in Linux

Init-Script is running.

auditd (pid 1944) is running...

cpuspeed (pid 1880) is running...

crond (pid 2206) is running...

cupsd (pid 2156) is running...

..........................

If we just use /sbin/service --status-all then we get a list of all services whether they are running or not.

If we just want to query the status of one service then do the following. In this case I'm getting the status of the

Samba (smb) service.

$ /sbin/service smb status

smbd is stopped

nmbd is stopped

Which services do I need to run?

As I said earlier I tend to turn off as many services as I can while still being able to use my computer for day to

day things. For this reason I've included the list off all services on my computer and whether they are set to

start at boot or not. Also note that even though I have a laptop I have turned off pcmcia since I do not have any

need for it, but if you use a pcmcia network card for example then you will need this service running.

Remember that it's easy to test this for yourself, since if you turn off a service and lose some functionality you

can just turn it on again without doing any harm. This list of my running services allows me to do all basic

activities including internet browsing and email. I do use my laptop as a web server running Apache and

MySQL but I just start them as required, the same goes for SSH sessions. In this way I get a faster boot up

time, increased security from less services open to attack and fewer ports open.