PFA
Lab 8-1: Use Systemctl Utility to Manage Services Introduction In this first lab, you will work with the systemctl command to start, stop, restart, reload, and enable/disable services running on a Linux system. Learning Outcomes After completing this exercise, you will be able to: · Log into a Linux System · Install Apache2 · Use Systemctl to manage services · Use daemon-reload Exercise 1 - Use Systemctl utility to manage services There are numerous services that run on a Linux system. Some of them are available by default and others are installed when a particular package is added to the system. In this exercise, you will understand how to manage services on a Linux system. Task 1 - Install Apache2 Apache is a Web server that works on the Linux and Windows systems. In Ubuntu, Apache2 is a software package that you can install. This lab will use the apache2 package. You can, however, choose to use any package. In this task, you will use the hostnamectl command to change the hostname. To do this, log into your Ubuntu system using the Admin account and perform the following steps: Step 1 On the desktop, right-click and select Open in Terminal. Note: If you are prompted with the Software Updater dialog box, click Remind Me Later. This dialog box may occur before or after this step. Figure 1: Open a terminal Step 2 Type the following command: sudo apt update && sudo apt install apache2 -y Figure 2: Installing Apache Notes: The above command is actually two commands. The first command updates the apt database. If the update is successful (represented by the AND or &&) then the second command runs and installs Apache2. If the update fails, then the second command won’t run. The -y option tells apt not to prompt the user if they want to install Apache. This just answers yes to that prompt. In commands, you can also use the OR ( || ) which will try to run the first command. If it is successful, the second command does not run. If it fails, then the second command runs. Step 3 When the installation completes, verify Apache is running by entering the following command. sudo systemctl status apache2 Figure 3: Verifying Apache2 is running NOTE: There are two lines to pay attention to. The line that reads Loaded: contains the entry “enabled”. If the service was disabled, then this would read “disabled”. The second line to note is the line that begins with Active. It should be in green and should read active (running). If this is not the case, then you will need to troubleshoot the reason it failed. Take a screenshot showing the apache2 service is running and enabled and paste it into your document. Task 2 - Use Systemctl to Manage Services The systemctl command is a management tool for managing and controlling the init system. We’ve already seen how we can check the status of a service using the systemctl command. Using the systemctl command, you can manage services, check service statuses, or change the service states. In this task, you will use the systemctl command to perform various actions on the apache2 service. Step 1 Press q to break out of systemctl. (You can also use Ctrl+c.) First, clear the screen then use systemctl to stop the apache2 service. clear sudo systemctl stop apache2 Figure 4: Stopping the Apache2 Service Note: You can use the systemctl command to either stop the service directly or executing instructions in the service’s unit file, which is suffixed with .service. The result will be the same in both the cases. Step 2 Using the command replacement, start the apache2 service, type the following command: ^stop^start Figure 5: Using command replacement NOTE: The command above, ^stop^start locates the first instance of stop in the previous command and replaces it with start. This can be used when you need to stop and start service and don’t want to keep typing the whole command over or arrowing up through the history. Step 3 An alternative to stopping and starting a service is by using the restart option. Often when you make a change to a configuration file, you will need to force the service to re-read the configuration file. One method is to restart the service. Restart Apache using the following command. sudo systemctl restart apache2 Figure 6: Restarting Apache2 NOTE: We will discuss configuration files later in the semester. Just know that all services work off of configuration files (they have a .conf extension) and when a system or service starts, they read the file and then close it. The service does not re-read the file until the system restarts, or the service restarts. Step 4 There may be scenarios in which you need to reload an application’s configuration files without restarting the system or the service. To reload a service, use the following command: sudo systemctl reload apache2 Figure 7: Reloading the Apache2 configuration Review You have completed this portion of the lab. Shutdown your Ubuntu system and start your CentOS VM and continue with the Lab 8-2. Summary You completed the following exercise: · Exercise 1 - Use Systemctl utility to manage services You should now be able to: · Log into a Linux System · Install Apache2 · Use Systemctl to manage services Lab 8-2: Change Runlevels and Shutdown or Reboot System Introduction In this lab, you will change runlevels to manage a Linux system. In this module, you will work with the various runlevels to administer a system, restart, and shutdown a system. Learning Outcomes After completing this exercise, you will be able to: · Log into a Linux System · Configure a runlevel · Switch among runlevels · Shutdown and reboot the system from the command line · Alert users before switching runlevels or other major system events · Understand terms and utilities Exercise 1 - Change Runlevels and Shut down or Reboot System A run level on a Linux system is the definition of what all services are running on the system. To run each service, relevant parameters need to be initialized in the init file. Therefore, each level defines the state of various parameters in the init file. Each run levels is referred to by a number. You can change between run levels to activate or shut down various services, as required. Note: You can manage the services individually as well, without changing between the run levels. There are 7 runlevels numbered 0 through 6. The following table lists runlevels (or targets) and their meaning. SystemV init Runlevel Systemd target Function 0 runlevel0.target poweroff.target System halt/shutdown 1 runlevel1.target rescue.target Single-user mode (only basic services running 2, 4 runlevel2.target, runlevel4.target multi-user.target User-defined/site-specific runlevels 3 runlevel3.target multi-user.target Multi-user mode non-graphical 5 runlevel5.target graphical.target Multi-user mode 6 runlevel6.target reboot.target Reboot emergency emergency.target Emergency Mode In this exercise, you will understand how to change runlevels to shut down or reboot systems. Task 1 - Changing Runlevels You can add, delete, and manage services to configure a runlevel. In this task, you will learn how to change runlevels using the runlevel and systemctl commands. Step 1 Open a terminal window by clicking on Activities then selecting Terminal. Figure 8: Opening a terminal window Step 2 From the prompt, type the following command to become root and enter the root password: su - Figure 9: Changing to root Step 3 You can view a list of services running on the system using the systemctl command. There are a lot of options you can use with this command. Use the list-unit-files option to view the current list of services running on the system. systemctl list-unit-files Figure 10: Listing services Step 4 When changing runlevels, systemctl uses targets. You can list the targets using the list-units and setting the type to equal target. systemctl list-units --type=target Note: The type option uses a double hyphen (-). If you use a single hyphen, Linux will complain. Some options take a single hyphen (i.e. ls -h) and often have a word equivalent (ls --human). These word equivalents often use a double hyphen (--). Figure 11: Listing targets Note: From this list of targets, you can see the multi-user.target as well as the graphical.target. Take a screenshot displaying the list of targets and paste it into your document. Step 5 A Linux system will default to a specific runlevel, this is typically runlevel 5. To view the default runlevel, use the get-default option. systemctl get-default Figure 12: Displaying the default target Step 6 You can change do a different runlevel by identifying the target. For example, to reboot the system, isolate the reboot.target. Use the following command to reboot CentOS. Once the system has restarted, log in and open a terminal window. Switch to root using the command from step 2. systemctl isolate reboot.target Figure 13: Rebooting the system Step 7 You may find that switching to single user mode (single.target) cannot be done from your current login. You can use the init command or the telinit command to switch to single user mode. telinit 1 Figure 14: Switching to single user mode Note: You will need to enter the root password once you get to this runlevel. When the system gets to runlevel 1, take a screenshot and paste it into your document. Step 8 You can move back to graphical mode specifying the default runlevel. systemctl default Figure 15: Switching back to graphical mode Note: You will need to log back in when the system gets to graphical mode. Enter your password and then open a terminal window and change users to root. Task 2 - Alert Users Before Switching Runlevels or Other Major System Events and SSH You will often reboot your systems during non-critical hours of operation, or during scheduled down times. However, you may find times when there are users connected to your system and you need to reboot or change runlevels. When this occurs, you will need to alert your users before switching runlevels so they have the opportunity to save their work. During this task, you will learn how to message your users so you can change runlevels or shutdown the system. Prior to beginning this task, make sure your VM is set for NAT. Then open a Windows Command Prompt by clicking on the Windows button then typing cmd. Click on the Command Prompt to open the Command Prompt window. Enter the IP address of your VM (use the command ip -br a to get your IP address. It will be something like ens## or enp0s#.): ssh
[email protected] NOTE: Replace 192.168.0.131 with the IP address from the command ip -br a. You will be prompted to type yes to accept the fingerprint and connect to the system. Leave the window open as you work through this part of the lab. Alternatively you can download PuTTY from https://www.putty