M01: Introduction to Operating Systems
TU1: Installing, configuring and exploiting a computer system
ASIX1
Practical Exercise 10: Managing services 26-02-17

Practical Exercise 10:  Managing services

GENERAL CONDITIONS
1- Deadline:
    a) ASIX1 (Catalan): 28-2-2017 at 9.30PM.
No answers sent after the deadline will be accepted.
    b
) DAW1 (English): 2-3-2017 at 6.00PM.No answers sent after the deadline will be accepted.
 2- Send your report as a PDF file attached to an e-mail with the following specifications:
     a) E-mail address:
cf(at)collados.org or jordi.binefa(at)fje.edu depending who is your teacher
     b) File Name:

        b.1)
ASIX1 (Catalan): asix1_surname_name_m01tu01pr10.pdf
        b2.) DAW1 (English): daw1_surname_name_m01tu01pr10.pdf     
     c)
Subject:
        
c.1) ASIX1 (Catalan): asix1_surname_name_m01tu01pr10
         c.2)
DAW1 (English): daw1_surname_name_m01tu01pr10
3- Make this report individually.
4- Left, right, top and bottom margins: 2cm.
5- Character format: a) Font: Arial, b) Size: 10, c) Questions typeface: Bold, d) Answers typeface: Regular
6- Page numbering on footer bar

1- Running commands with sudo
a) Description: On Linux and Unix-like operating systems, sudo allows users to run programs with the security privileges of another user, by default the  root user (also known as superuser). It originally was designed to run commands only as the superuser but the later versions added  support for running commands not only as the superuser but also as other (restricted) users. Frequently,  sudo is used for administrative tasks. 

Some ideas about sudo:

b) Basic synopsis: sudo   [-k]   command   ---> Run command with the security privileges of root user. Optional -k parameter invalidates the user's cached credentials and the next time sudo is run a password will be required.

c) Examples:

d) In order to enable the sudo command on Debian for a user:

2- What is a server program?
A server is a computer program running in a computer that provides services to other computer programs (the clients)  running in the same computer or in other computers connected through the network.
Usually, a server is a program that awaits a request from a client programs in the same or other computers. When a request is received from a client, the server program will fulfill the task requested by the client.
The most of the times a server is a daemon, therefore it is a process running continuosly that has been  initialized at system startup and then wait in the background until their service is required.

Typical servers are:
  1. Apache: It is a Web server. A client (firefox, chrome,....) can gain access to a web pages stored in the computer where Apache is running
  2. Mysql: It is Database server. A client can gain access to a database stored in the computer where MySQL is running.
  3. Postfix: It is a Mail server. A client can send e-mails through Postfix or receive e-mails stored in the computer where Postfix is running
  4. Pulseaudio: It is a sound server for Linux. A client can gain access to the sound hardware in the computer where Pulseaudio is running.
  5. CUPS: Local and network printer server for Linux.
3- Managing services with SysVinit

3.1- service

A command-line utility that can report or change the status of any of system services (a service is provided by a server program). This command should be run as a root user.The following options are available:
a) start --> Starting a service. Example: service  apache2 start
b) stop --> Stopping a service. Example: service  ssh stop
c) restart --> Restarting a service. The service will be stopped and started, its service file configuration will be read again, and its PID will be changed. Example: service apache2 restart.
d) status --> Displaying a service status (is stopped or started). Example: service cups status

Services that can be managed by the command service are found in the directory /etc/init.d.

3.2- Listing important services running in your system
a) ls  /etc/rc5.d/S* --> This command shows services started during the computer boot process if your system runlevel is 5. You should check your runlevel running: runlevel. If your runlevel were any other number, 2 for instance, you would have to run:  ls  /etc/rc2.d/S*.  Depending on your system runlevel, a different set of services will be started during the boot process.
b) netstat  -atupn --> This command shows network services running in your system just now. This command should be run as a root user.
c) service  --status-all --> It shows a list of service running in your system. Only services with +  are running in your system. Services with  - are not running in your system. The current status is no available for services with ?. This command should be run as a root user.

3.3- Adding o removing a service during the system boot process
a) If you want to totally remove a service during the system boot process you have to run the following command:

update-rc.d   -f   service_name   remove

For intance, if you want to remove apache2 during the system boot process you have to run the following command:

update-rc.d   -f   apache2   remove

b) If you want to add  a service during the system boot process you have to run the following command:

update-rc.d   service_name   defaults

For intance, if you want to add apache2 during the system boot process you have to run the following command:

update-rc.d   apache2   defaults

c) When you remove or add a service, list contents of  /etc/rc5.d  (or your current runlevel) in order to check if the service was successfully  added to (or removed from) the boot process

4- Managing services with systemd

4.1- systemctl
A command-line utility that can report or change the status of system services (a service is provided by a server program). This command should be run as a root user.The following options are available:
a) start --> Starting a service. Example: systemctl  start ssh
b) stop --> Stopping a service. Example: systemctl  stop ssh
c) restart --> Restarting a service. The service will be stopped and started, its service file configuration will be read again, and its PID will be changed. Example: systemctl restart apache2.
d) status --> Displaying a service status (is stopped or started). Example: systemctl status apache2.

Services that can be managed by the command service are found in the directory /lib/systemd/system.

4.2- Listing services loaded and active in your system
a) systemctl  list-units --type=service --> This command shows services loaded an active. Service can be running or exited (This value depends on service type)q
b) netstat  -atupn --> This command shows network services running in your system just now. This command should be run as a root user.

4.3- Adding o removing a service during the system boot process
a) If you want to totally remove a service during the system boot process you have to run the following command:

systemctl  disable  service_name

For intance, if you want to remove apache2 during the system boot process you have to run the following command:

systemctl   disable apache2

b) If you want to add  a service during the system boot process you have to run the following command:

systemctl  enable  service_name

For intance, if you want to add apache2 during the system boot process you have to run the following command:

systemctl  enable  apache2

c) In order to get a list showing services enabled and disabled, run the following command:

systemctl   list-unit-files  --type=service

NOTE: The meanings of static and masked are beyond the scope of the prsent exercise.

5- SysVinit vs systemd

SysVinit is the traditional system of managing services on Debian. It was the default system for Debian until version 7.9 (but you could install  systemd if you wanted). Starting with version 8.0 the new default system of managing services is systemd but The service and update-rc.d commands will MOSTLY continue to work as expected in the systemd world and, if you want, you can install the SysVinit system. 

To check if your Debian works with systemd, search for a directory called /run/systemd/system. To check if your Debian works with SysVinit, search for a file called /etc/inittab.

PRACTICAL EXERCISE

1- Enable the sudo command to  your user by default.
2-
Find out the following features of  apache2 and cups, using the GNOME System Monitor
: a) PID, b) owner, c) status and d) command-line.
3- Using a systemd command-line tool, find out if the cups service is  runnig or not. Take note of its PIDs.
4-
Using a systemd command-line tool, stop the cups service. Check if it has been stopped  or not using a command-line tool.
5- Using a systemd command-line tool, start the cups service. Check if it is running again using a command-line tool. Check its PID. Any change?.
6- Using a systemd command-line tool, restart the cups service. Using a command-line tool, check if that service is running. Check its PID. Any change?.
7- Using a SySVinit command-line tool, find out if exim4 is runnig or stopped. Take note of its PID.
8-
Using a SySVinit command-line tool, stop the exim4 service. Check if that service has been stopped using a command-line tool.
9- Using a SySVinit command-line tool, start the exim4 service. Check if that service is running again using a command-line tool. Check its PID. Any change?.
10- Using a SySVinit command-line tool, restart the exim4 service. Using a command-line tool, check if that service is running. Check its PID. Any change?.
11- Check the status of all your system network services. Could you tell me if apache2, ssh and exim4 ares network services?. Why?
12- Check the status of apache2, exim4  and ssh using netstat and one single command  (*1).
13- Using a systemd command-line tool, show services started during the computer boot process.
14- Using a SySVinit command-line tool, remove apache2 and exim4 as services started by the computer during the boot process. Reboot your system and check that both services are not running.
15- Using a SySVinit command-line tool, add apache2 and exim4 as services started by the computer during the boot process. Reboot your system and check that both services are running.
16- Using a systemd command-line tool, remove apache2 and exim4 as services started by the computer during the boot process. Reboot your system and check that both services are not running.
17- Using a systemd command-line tool, add apache2 and exim4 as services started by the computer during the boot process. Reboot your system and check that both services are running.
18- Using a SySVinit command-line tool, remove ssh as a service started by the computer during the boot process. Check that it i has been removed from /etc/rc5.d. Reboot your system and check whether ssh is running or not. What is happening?.
19- Using a SySVinit command-line tool, add ssh as a service started by the computer during the boot process. Check that it i has been added to /etc/rc5.d.Using a systemd command-line tool, remove ssh as a service started by the computer during the boot process. Using a systemd command-line tool, check that it i has been removed. Reboot your system and check whether ssh is running or not. What is happening?.
20- Using a systemd command-line tool, add ssh as a service started by the computer during the boot process. Using a systemd command-line tool, check that it i has been added. Reboot your system and check whether ssh is running or not.

(*1) --> Help:  grep -E '(apache|exim4|ssh)'.