OPS235 Lab 5 - CentOS7
Contents
LAB PREPARATION
Purpose / Objectives of Lab 5
The purpose of this lab is to discuss how a Linux sys admin can manage partitions including adjusting the size of their Linux systems if space is required. Various topics will be discussed including:
- Connecting and Disconnecting Directories to existing partitions (mount, umount).
- Monitoring Disk Space (df -h).
- Using LVM to resize partitions graphically and via commands.
- Create, partition and format virtual hard disks to increase the size of a file system.
- Create a Bash Shell Script to monitor and report low disk size (run periodically in crontab).
Minimum Required Materials
My Toolkit (CLI Reference)
LVM Information: | LVM Management | Miscellaneous |
INVESTIGATION 1: File System Management
We take for granted that a file-system must be mounted (for example the root partition) in order for a Linux system to be usable upon system start-up. The /etc/fstab (file system table) contains entries to mount various file systems automatically upon start-up of the Linux system.
The Linux sys admin also has the ability to manually mount (connect) and un-mount (disconnect) partitions in order to perform maintenance on the file system (for example un-mounting the /home partition to install software and prevent users from logging in during that process).
We will now learn how to perform these operations (including monitoring of disk space usage) in Part 1.
Part 1: Mounting and Un-mounting Partitions
- x
Answer the Part 1 observations / questions in your lab log book.
Part 2: Obtaining File System Information
- x
Answer the Part 2 observations / questions in your lab log book.
INVESTIGATION 2: Adjusting File System Sizes with LVM
Monitoring and ensuring adequate space for a Linux file-system is considered to be an important task for a sys admin.
Part 1: Obtaining System Information with LVM
We have seen that maintaining unneeded packages can be a security risk due to the unnecessary increase in the complexity of your system. Similarly, it is also unnecessarily hazardous, and even more so, to leave unneeded services running. In this investigation, we will learn how to control services, and turn off those services that we think are not necessary to help reduce security risks.}}
- x
Answer Part 1 observations / questions in your lab log book.
Part 2: Adjusting Partition Sizes with LVM
x
- x
Answer Part 2 observations / questions in your lab log book.
Part 3: Adding Virtual Hard Disks and Managing with LVM
x
- x
Answer Part 3 observations / questions in your lab log book.
INVESTIGATION 3: LOOKING AHEAD
Automating Routine Tasks (Shell Scripting and Using Crontab)
We will now use shell scripting to help automate the task for a Linux adminstrator to create regular user accounts.
- Download, study, and run the following shell script. Issue the command:
wget https://scs.senecac.on.ca/~murray.saul/user-create.bash
- Try to understand what these Bash Shell scripts do, and then run the script as root. After running the shell script, view the contents of the /home directory to confirm.
Although the zenity command is a "user-friendly" way to run shell scripts, Linux administrators usually create shell scripts that resemble common Linux commands. In this lab, you will learn to create a shell script using the getopts function to make your shell script behave more like actual Linux commands (including the use of options). Refer to the notes section on the right-hand-side for reference about the case statement and the getopts function.
- Open a Bash shell terminal and login as root.
- Use the wget command to download the input file called user-data.txt by issuing the command:
wget https://scs.senecac.on.ca/~murray.saul/user-data.txt
- View the contents on the user-data.txt file to confirm there are 3 fields (username, fullname, and e-mail address)which are separated by the colon (:) symbol.
- Use a text editor (such as
vi
ornano
) to create a Bash Shell script called:createUsers.bash
in /root's home directory. - Enter the following text content into your text-editing session:
#!/bin/bash
# createUsers.bash
# Purpose: Generates a batch of user accounts (user data stored in a text file)
#
# USAGE:
#
# /root/createUsers.bash [-i {input-path}]
#
# Author: *** INSERT YOUR NAME ***
# Date: *** CURRENT DATE ***
if [ $PWD != "/root" ] # only runs if in root's home directory
then
echo "You must be in root's home directory." >&2
exit 1
fi
if [ "$#" -eq 0 ] # if no arguments after command
then
echo "You must enter an argument" >&2
echo "USAGE: $0 [-i {input-path}]" >&2
exit 2
fi
- Save your editing session, but remain in the text editor.
- The code displayed below uses the getopt function set the input file pathname or check for invalid options or missing option text. Add the following code
outputFlag="n"
while getopts i: name
do
case $name in
i) inputFile=$OPTARG ;;
:) echo "Error: You need text after options requiring text"
exit 1 ;;
\?) echo "Error: Incorrect option"
exit 1 ;;
esac
done
- Save your editing session, but remain in the text editor.
- The code displayed below uses logic to exit the script if the input file does not exist. Command substitution is used to store each line of the input file as a positional parameter. There is one subtle problem here: The full names of the users contain spaces which can create havoc when trying to set each line as a separate positional parameter. In this case the sed command is used to convert spaces to plus signs (+), which will be converted back later. Finally, a for loop is used to create each account (useradd) and mail the user their account information (mail). Add the following code:
if [ ! -f $inputFile ]
then
echo "The file pathname \"$inputFile\" is empty or does not exist" >&2
exit 2
fi
set $(sed 's/ /+/g' $inputFile) # temporarily convert spaces to + for storing lines as positional parameters
for x
do
useradd -m -c "$(echo $x | cut -d":" -f2 | sed 's/+/ /g')" -p $(date | md5sum | cut -d" " -f1) $(echo $x | cut -d":" -f1)
mail -s "Server Account Information" $(echo $x | cut -d":" -f3) <<+
Here is your server account information:
servername: myserver.senecac.on.ca
username: $(echo $x | cut -d":" -f1)
password: $(date | md5sum | cut -d" " -f1)
Regards,
IT Department
+
done
echo -e "\n\nAccounts have been created\n\n"
exit 0
- Save, set permissions, and then run that shell script for the input text file user-data.txt. Did it work? Try running the script without an argument - What did it do?
- You have completed lab4. Proceed to Completing The Lab, and follow the instructions for "lab sign-off".
Answer Investigation 3 observations / questions in your lab log book.
LAB 4 SIGN-OFF (SHOW INSTRUCTOR)
Arrange proof of the following on the screen:
- ✓ Account created on centos1 VM.
- ✓ List contents of /etc/group file (ops235 group) on centos1 VM.
- ✓ List contents of /etc/passwd file (created accounts) on centos1 VM.
- ✓ Display current run-level status on centos2 VM.
- ✓ Creation of your bash shell script called createUsers.bash
Preparing for the Quizzes
- Describe all of the field in
/etc/passwd
- What is the command to create a user? What option to create a home directory for that user?
- What is the command to change the full name of an already-created user?
- What is the command to delete a user account? What option allows for the user's home directory to be removed as well?
- What is the command to create a group? What is the command (or steps) to include a user in a newly-created group?
- What is the purpose of
/etc/shadow
? - What is the purpose of
/etc/skel
? - What does the term run-level mean?
- How to set the run-level of a Linux system to text-based only? How to set to graphical mode?
- What is the command to view the status of running services?
- What is the command to start a service (like httpd, or sshd)?
- What is the command to start a service?
- Can a service be stopped and started by issuing just one command?