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

Practical Exercise 10:  Managing services

GENERAL CONDITIONS
1- Calendar: From 7-2-19 to 14-2-2019 (3 hours)
2-
Deadline: 17-2-2019.
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 on who is your teacher
     b) File Name:

        b.1)
ASIX1 (Catalan): asix1_surname_name_m01tu01pr10.pdf and asix1_surname_name_m01tu01pr10.odt
        b2.) DAW1 (English): daw1_surname_name_m01tu01pr10.pdf and daw1_surname_name_m01tu01pr10.odt   
     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

1- What is a server program?

a) A server is a process that provides services to other computer programs, the clients,  running in the same computer or in other computers connected through the network.
b) A service provides some functionality to clients such as access to files stored in a remote computer, access to web pages, access to databases, send or receive mails, run commands in remote computers, store files in remote computers, print documents on local and remote printers and so on.
c) A server is always (or almost always) on (running).
d) 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.
e) The usual state of a server is sleeping (S).
f) The most of the times a server is a daemon, in other words, it is a process running continuosly that has been  initialized at system startup and then wait in the background until their service is required.  
g)
Most processes that you start on a Linux machine will run in the foreground. A foreground process blocks the shell and it is attached to your keyboard. A foreground process may allow user interaction or may just run and then exit. When you run a foreground process, the shell waits until the process is finished before it prompts you to enter the next command.  A background process executes independently of the shell, leaving the terminal free for other work. A background process  is not attached to your keyboard and screen and it runs without user intervention.
h) Some yypical 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.
2- SysV init vs systemd

In Linux operating systems, the init (initialization) process is the first process executed by the operating system at boot time. It has a process ID (PID) of 1, it is executed in the background until the system is shut down. The init process starts all other processes (that is services and other background processes) and therefore, it is the parent  of all other processes on the system. A process can start many other child processes on the system, but in the event that a parent process dies, init becomes the parent of the orphan process.

Booting a Linux installation involves multiple stages and software components, including firmware initialization, execution of a boot loader, loading and startup of a Linux kernel image, and execution of various startup scripts and daemons. For each of these stages and components there are different variations and approaches; for example, GRUB, LILO, SYSLINUX or Loadlin can be used as boot loaders, while the startup scripts can be either traditional SysV init, or the system configuration can be performed through modern alternatives such as systemd.

On Debian, the old and traditional init system scheme was SysV init. As years have passed by, due to some imperfections, several SysV init replacements have been developed in quests to create more efficient and perfect init systems for Linux.  SysV init  was the default system for Debian until version 7.9. Starting with version Debian 8.0, the new default init system is systemd.

Each initialization system manages services in different ways and a different tools for easy management of services have been developed for each initialization system. Commands service and update-rc.d are tools to easily manage services in the old and traditional SysV init. Command systemctl is a  tool to easily manage and keep track services in the modern systemd. Commands service and update-rc.d  will MOSTLY continue to work as expected in the systemd world but they are mostly considered deprecated. 

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

3- Managing services using the SysV init style

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:
* UNIT: The service (or unit) name
* LOAD: Whether or not the service was loaded in memory (LOAD)
* ACTIVE: and active (a way to tell if the service has started successfully or not.
* SUB: If it is running or has been exited. This value depends on the service. Running means that there is currently a process associated to the service. Exited means that the process associated to process was finished         (sometimes because this is their normal behavior but sometimes because the service had a problem).    

b) netstat  -atupn --> This command shows network services running in your system just now. This command should be run as a root user. If netstat is not installed on your system, you should install a software package  called net-tools.

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.

PRACTICAL EXERCISE

0-  Install exim4 (a mail server) running: aptitude install exim4. Do not take screenshots.
1- Find out the following features of  apache2 and cups, using the MATE System Monitor
: a) PID, b) owner, c) status and d) command-line.
2- Using a systemd command-line tool, find out if the cups service is  runnig or not. Take note of its PIDs.
3-
Using a systemd command-line tool, stop the cups service. Check if it has been stopped  or not using a command-line tool.
4- 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?.
5- 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?.
6- Using a SySV init command-line tool, find out if exim4 is runnig or stopped. Take note of its PID.
7-
Using a SysV init command-line tool, stop the exim4 service. Check if that service has been stopped using a command-line tool.
8- Using a SysV init command-line tool, start the exim4 service. Check if that service is running again using a command-line tool. Check its PID. Any change?.
9- Using a SysV init command-line tool, restart the exim4 service. Using a command-line tool, check if that service is running. Check its PID. Any change?.
10- Check the status of all your system network services. Could you tell me if apache2, ssh and exim4 ares network services?. Why?
11- Using a systemd command-line tool, show services started during the computer boot process.
12- Using a SysV init 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.
13- Using a SysV init 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.
14- 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.
15- 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.
16- Using a SysV init 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?.
17- Using a SysV init 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?.
18- 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.
19- Using the Services Administration Tool graphical tool, stop apache2 and exim4. You can start Services Administration Tool in the following way: Select System-->Administration-->Services. Using a systemd command-line tool, check if those services are running or not.
20- Using the Services Administration Tool graphical tool, start apache2 and exim4. Using a SysV init command-line tool, check if that service are running or not.