Open main menu

CDOT Wiki β

Changes

OPS235 Lab 5 - CentOS7

3,618 bytes added, 12:15, 27 November 2019
no edit summary
{{Admon/caution|THIS IS AN OLD VERSION OF THE LAB|'''This is an archived version. Do not use this in your OPS235 course.'''}}
[[Category:OPS235]]
=LAB PREPARATION=
==Purpose / Objectives of Lab 15==[[Image:disk-space.png|thumb|right|150px|Monitoring disk space and taking corrective action before problems arise is a critical operation for a system administrator.]]
There are many other tasks that a Linux system administrator must perform other than installing Linux and installing software.
A few additional tasks are user management The purpose of this lab is to demonstrate how a Linux system administrator can monitor hard disk space availability, and managing servicesto manage file system size via the Logical Volume Manager (LVM) application. This lab will also demonstrate how to manually mount (i.e. connect) and unmount (disconnect) partitions to file system directories, and demonstrate how to have partitions automatically mounted to directories upon Linux system startup.
<u>Main Objectives</u>
<u>Main objectives</u>:<br>:* Administer Using '''(add, remove, modify) usersLVM''' on a Linux system.:* Save time while adding new users using a template of to '''startresize partitions graphically and via command-up filesline'''.:* Create , partition and manage format '''groupsvirtual hard disks''' on to increase the size of a Linux file system.:* Manually connect and disconnect directories (mount points) to existing partitions ('''mount''', '''Start and Stop servicesumount''' on a Linux system).:* Display the Monitoring Disk Space ('''status of running servicesdf -h''' on ).:* Create a Linux systemBash Shell script to '''monitor and report low disk size''' (run periodically in '''crontab''').
|width="10%" |[[Image:ubs-key.png|thumb|left|85px|<b>USB key</b><br>(for backups)]]
|width="10%" |[[Image:log-book.png|thumb|left|70px|<b>Lab4 Lab5 Log Book</b>]]
|}
{|width="50%" cellpadding="15"
|- valign="top"
|width="10%" |<u>User ManagementLVM Information:</u>*[http://unixhelpman7.org/linux/man-pages/man8/vgs.ed8.achtml vgs]<br>*[http://man7.ukorg/CGIlinux/man-cgi?useradd+pages/man8/pvs.8 useradd.html pvs]<br>*[http://unixhelp.ed.acman7.ukorg/CGIlinux/man-cgi?userdel+pages/man8/lvs.8 userdel.html lvs]<br>*[http://unixhelp.ed.acman7.ukorg/CGIlinux/man-cgi?usermod+pages/man8/vgdisplay.8 usermod.html vgdisplay]<br>*[http://unixhelp.ed.acman7.ukorg/CGIlinux/man-cgi?groupadd+pages/man8/pvdisplay.8 groupadd.html pvdisplay]<br>*[http://unixhelp.ed.acman7.ukorg/CGIlinux/man-cgi?groupdel+pages/man8/lvdisplay.8 groupdel.html lvdisplay]<br>|width="10%" |<u>Managing ServicesLVM Management</u>*[http://unixhelpwww.centos.edorg/docs/5/html/Deployment_Guide-en-US/s1-system-config-lvm.achtml system-config-lvm]<br>*[http://man7.ukorg/CGIlinux/man-cgi?chkconfig+pages/man8/lvextend.8 chkconfig.html lvextend]<br>*[http://unixhelpman7.edorg/linux/man-pages/man8/lvcreate.ac8.ukhtml lvcreate]<br>*[http:/CGI/man7.org/linux/man-cgi?service+pages/man8/lvreduce.8 service.html lvreduce]<br>*[http://wwwman7.dsmorg/linux/man-pages/man8/pvcreate.fordham8.eduhtml pvcreate]<br>*[http://cgi-binman7.org/linux/man-cgipages/man8/vgextend.pl?topic=systemctl systemctl8.html vgextend]<br>
|width="10%" |<u>Miscellaneous</u>
*[http://man7.org/linux/man-pages/man5man8/passwdmount.58.html /etc/passwdmount]<br>*[http://man7.org/linux/man-pages/man5man8/groupumount.58.html /etc/groupumount]<br>*[http://man7.org/linux/man-pages/man5man1/shadowdf.51.html /etc/shadowdf]<br>*[http://archive.linuxfromscratchman7.org/blfslinux/man-museumpages/man1/du.1.0html du]<br>*[http://www.lehman.cuny.edu/cgi-bin/BLFSman-cgi?awk+1awk]<br>*[http://tldp.0org/HOWTO/postlfsPartition/skelfdisk_partitioning.html /etc/skelfdisk]<br>*[http://www.cyberciti.biz/faq/howto-format-create-linux-filesystem/ mkfs]*[http://zenitman7.senecacorg/linux/man-pages/man5/fstab.on5.cahtml /etc/fstab]*[http:/wiki/indexcode.phptutsplus.com/Init_vs_systemd init vs systemdtutorials/scheduling-tasks-with-cron-jobs--net-8800 Using crontab
|}
=INVESTIGATION 1: MANAGING FILE-SYSTEMS USING LVM=
{| width="40%" align="right" cellpadding="10"
|- valign="top"
|{{Admon/note|VM Backups and Recovery|Most of these investigations will take place in your virtual machines. If you make a significant mistake, your virtual machine may not boot. Remember that you created backups of your virtual machines in Lab 2, and you can restore them if something goes wrong.<br /><br />'''Remember: if you did not create backups for all of your VMs, then you don't have any restoration points to fall-back to!'''|}}
|}
Monitoring and ensuring adequate space for a Linux file-system is considered to be a critical task for any system administrator. An application called '''LVM''' is a very useful tool for Linux system administrators to easily manage file systems - even when the computer system is running!
= INVESTIGATION 1: User'''LVM''' ('''Logical Volume Management''') is used to manage hard disk drives /Group Management =partitions for Linux and Unix systems. LVM provides more flexibility than just partitioning hard disks. '''Volume Groups''' are areas used to define '''Physical Volumes''' (i.e. hard disks, disk partitions, or other forms of storage devices). '''Logical Volumes''' are then used to relate directories (mount points) to a specific physical volume or for a "range" or "span" of physical volumes.
User account management is a very important operation that a LVM allows more flexibility and growth potential for Linux sysadmin does on a consistent basissystems (for example, having Logical volumes span multiple hard disks). The sysadmin not only needs to add or remove user accounts CentOS uses LVM by issuing commands, but default upon installation. Other Linux distributions may need to automate user account creations a large number (batch) of potential employees. There are many features with provide the Linux command capacity to create new users including: specification of a home directoryinstall LVM, type of shell used, name, password and time-limit (referred to as "aging") for a new user account. Remove user accounts also have options such as removing the user account but keeping the home directory for reference or evidence of "wrong-doing"
In your ULI101 course, you learned to change permissions for directories and files relating to user, same group members and other group members. In this course, since you are the sysadmin with root privileges, you can create or remove groups as well as change the ownership of directories and files! We will now learn to perform key user account management operations in this section.
=== Part 1: The /etc/passwd file Manage LVM Graphically===
[[Image:lvm1.png|thumb|500px|right|The '''system-config-lvm''' application allows the Linux system administrator to manage LVMs Graphically. Your LVM information for '''centos2''' VM should be similarly displayed.]]# Let's learn to administer (manage) our LVM graphically for our '''centos2''' Virtual Machine.# Launch your '''centos2''' VM and open a shell terminal.# CentOS provides a tool called '''system-config-lvm''' to graphically administer LVM.<br><br>'''NOTE:''' This tool may be replaced by a future graphical LVM management tool and is NOT currently available in the default repositories. We will now perform a "work-around" in order to make the system-config-lvm utility work in our system.<br><br># Look at Issue the following command to download the system-config-lvm rpm:<br><b><code><span style="color:#3366CC;font-size:1.2em;">wget https://kojipkgs.fedoraproject.org//etcpackages/passwdsystem-config-lvm/1.1.18/1.fc19/noarch/system-config-lvm-1.1.18-1.fc19.noarch.rpm</span></code></b> <br>(Although it is an rpm filefor the Fedora Distribution, it will also work for Centos7).<br><br># Make note of certain that you are logged in as '''root'''.# Try issuing the contents of that filecommand: <b><code><span style="color:#3366CC;font-size:1.2em;">rpm -i ./system-config-lvm-1.1.18-1.fc19.noarch.rpm</span></code></b><br>You should notice an error.# Read about Use the '''yum install''' command to resolve the filefollowing dependencies:<ul><li><b><code><span style="color: http#3366CC;font-size:1.2em;">yum install gnome-python2-bonobo</span></code></b></man7li><li><b><code><span style="color:#3366CC;font-size:1.org2em;">yum install gnome-python2-gnome</span></code></linuxb></manli><li><b><code><span style="color:#3366CC;font-size:1.2em;">yum install usermode-pagesgtk</span></code></b></man5li></passwdul><br><ol> <li value="8">Re-issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">rpm -i ./system-config-lvm-1.1.518-1.fc19.noarch.rpm</span></code></b><br>It should work this time.html </li><li>Run the command: <b><code><span style="color:# Make sure 3366CC;font-size:1.2em;">system-config-lvm</span></code></b></li><li>On the left-hand side, you know what information each field containscan click on the '''Volume Group''', '''Physical Volume''' and '''Logical Volumes''' and view their properties on the on the right-hand side.</li><li>Determine the current LVM configuration by clicking on the appropriate element and reading the properties in the right-hand panel -- write down the answers: <ol type="a"># Why do you think there <li>What are so many usersthe names and sizes of the '''Volume Group'''?</li> <li>What is the name and size of the '''Physical Volumes'''?</li># Look at <li>What are the names and sizes of the users'''Logical Volumes'''?</li> <li>Is there any space in the VG which is not allocated to a LV?<br><br></li> </ol> </li> <li>Click on '''Logical View''' (i.e. above home, root, swap) and then click '''Create New Logical Volume''' button. What happens? Why do you think these user names represent? Are they peoplethis happened?<br><br>We will now create another partition ('''/dev/vda3''') using the '''fdisk''' utility, format this newly-created partition so we can create another logical volume called '''archive'''.<br><br> </li> <li>Quit the '''system-config-lvm''' application.</li> <li>In the terminal as root, issue the command: <b><code><span style="color:# 3366CC;font-size:1.2em;">ls /dev/vd*</span></code></b><br><br>'''NOTE:''' If nothing displays, issue the command: '''ls /dev/sd*''' and use that device pathname '''/dev/sda''' instead.<br><br> <li>Issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">fdisk /dev/vda</span></code></b><br>(or ''fdisk /dev/sda'' if the previous command didn't work)</li> <li>At the fdisk prompt issue the command: '''p'''. What is does this do?</li> <li>Now issue the commands '''n''' (new partition), '''p''' (primary partition), '''3''' (i.e. next available partition number). When prompted for initial block, '''press enter to accept the numeric user ID default beginning block size''', and '''type: +2G''' at ending block (UIDcreate 2GB for partition) of and '''press enter'''. </li> <li>At the fdisk prompt, issue the command '''p''' to review the partition information, then type '''w''' to save partition table and exit (ignore WARNING message).</li> <li>You <u>'''must'''</u> restart your centos2 VM to allow changes to take effect</li> <li>Open a terminal as root user, and format your newly-created partition by issuing the command: <b><code><span style="color:#3366CC;font-size:1.2em;">mkfs -t ext4 /dev/vda3</span></code></b><br>(or ''mkfs -t ext4 /dev/sda3'')</li></ol>{| width="40%" align="right" cellpadding="10"|- valign="top"|[[Image:lvm3.png|thumb|right|500px|What '''system-config-lvm''' display should appear after using '''fdisk''' to create new partition (/dev/sda3), '''formatting''' partition, '''adding partition to LVM''', and '''allocating partition''' to our default volume group.]]|[[Image:lvm4.png|thumb|right|200px|Creating a new logical volume from our newly created physical volume]]|}<ol> <li value="21">'''Restart the system-config-lvm utility'''. Do you see a new /dev/sda3 partition under Physical Volumes?</li> <li>To add the newly created partition, you need to add it into LVM to be used. '''Exit the system-config-lvm utility''' and '''issue the following command to add the partition into LVM:'''<br><b><code><span style="color:# 3366CC;font-size:1.2em;">pvcreate /dev/vda3</span></code></b> (or ''pvcreate /dev/sda3'' ) (enter '''y''' to proceed - ignore warning)</li> <li>'''Restart system-config-lvm'''. You should notice a section at the bottom indicating <u>Unallocated</u> Physical Volumes. Click on the <u>unallocated</u> partition /dev/vda3 and then click '''Add to Existing Volume Group''' (click on other button in next screen to continue). The user IDs of real users screen should now show the partition ''/dev/vda3'' contained in our default Volume Group (refer to diagram on right-side for reference).</li> <li>Click on Logical View (peoplei.e. above home, root, swap) are different from , and then click on '''Create New Logical Volume''' button and fill out the details for this new logical volume including: size: '''1GB''', LV Properties: '''linear''', file system: '''ext4''' file system name: '''archive''', select '''mount''' and mount it at: '''/archive'''. Have the user IDs of system accountscreate the directory ''/archive'' if it does not exist (Refer to diagram on right for reference).</li> <li>Confirm that this new Logical Volume now exists by changing to the directory /archive. What Did you need to restart your centos2 VM to confirm that the '''/archive''' directory is now on your '''centos2''' VM?</li> <li>Use system-config-lvm to resize (i.e. edit) the pattern?'''archive''' logical volume to '''1.5 GB''' and verify your results.</li></ol>
'''Answer the Part 1 observations / questions in your lab log book.'''
== '''Answer Part 2: Adding users ==1 observations / questions in your lab log book.'''
===Part 2: Managing LVM Via Command Line & Adding Virtual Hard Drives===
[[Image:add_virtual_disk.png|thumb|400px|right|You can add virtual hard disks for a VM by changing to the '''Details''' section for the VM (as opposed to ''console''), click '''Add Hardware''', fill information in the '''Add New Virtual Hardware''' dialog box and clicking '''Finish'''.]]
Let's say that you have run out of disk space on your '''centos3''' VM, and you need more space on the root file-system in order to host more web-pages, or to support a larger database. What are your options? Getting a replacement hard-drive would probably require re-installation of the operating system and/or backing up and restoring data on the old hard disk.
#Perform Because we're using LVM we can avoid this part in your problem. We can '''centos1''' VM.# Read the man page for the <b>add a new <code><span style="color:#3366CC;font-size:1.2em;">useradd</span></codeu>virtual</b> command.# Create three fictitious users (make-up their userids and full names. Give each of these newly-created users a password.# Grep the <bu><code><span style="color:#3366CC;fonthard-size:1.2em;">/etc/passwd</span></code></b> file for each of the new users.#* What is the '''home''' directory of each user?#* What drive'''(which will serve as a physical volume) to the volume group''' is each user in?#* What other information can you provide regarding these users?#* Where are the '''passwords''' stored?# Look at the man page for '''/etc/shadow''' using the command: <b><code><span style="color:#3366CC;font-size:1.2em;">man 5 shadow</span></code></b>#* Grep the <b><code><span style="color:#3366CC;font-size:1.2em;">/etc/shadow</span></code></b> file for each of the new users.#* Make note of this information. # Create two new dummy users, <b><code><span style="color:#3366CC;font-size:1.2em;">ops235_1</span></code></b> and <b><code><span style="color:#3366CC;font-size:1.2em;">ops235_2</span></code></b>.# Investigate extend the home directory of one root logical volume to make use of your new users.#* What files are there? Be sure to include hidden files.#* What do you think these files are used for?#* How does the operating system determine which files are created in a new home account? The answer can be found here:<br>http://www.linuxhowtos.org/Tips%20and%20Tricks/using_skel.htm#* Look at the files (including hidden files) in the template directory referred to in the articleavailable space. Compare them to what Creating virtual hard drives is in not only inexpensive, but a home directory great way for a new user. What do you notice?#* Create a new file in this directory with the following command: <b><code><span style="color:#3366CC;font-size:1.2em;">touch foo</span></code></b>#* Create a new user named <b><code><span style="color:#3366CC;font-size:1.2em;">foobar</span></code></b>, with the option students to automatically create a home directory.#* Look at the contents of foobar's home directory. What do you notice?# Be sure learn now to record your observations in your lab notes.#Issue simulate growing the man pages for the '''useradd''' command. Explain the purpose size of using the '''-e''' option for the ''useradd'' command. Try to think what would be the purpose for a Linux sysadmin to use this option when creating new usersfile system.
'''Answer Perform the Part 2 observations / questions following operations to increase the size of lv_root in your lab log book.centos3:'''
== Part 3: Managing Groups ==
#Remain Perform this Part in your '''centos1centos3''' VM for this section.# Read Run the man page for following commands and make note of the output:::<b><code><span style="color:#3366CC;font-size:1.2em;">groupaddls /dev/vd*</span></code></b> and ::<b><code><span style="color:#3366CC;font-size:1.2em;">groupdelpvs</span></code></b> commands.# Note which option allows you to set the Group ID number ('''GID''') when you create a new group.# Examine the file ::<b><code><span style="color:#3366CC;font-size:1.2em;">/etc/groupvgs</span></code></b>#* Which values of GID are reserved for system accounts?#* Which values of GID are reserved for non-system user accounts?#* What is the lowest available GID number for non-system users?#* What is the default group name of a new user?#* Add a new group named ::<b><code><span style="color:#3366CC;font-size:1.2em;">ops235lvs</span></code></b> with a GID of ::<b><code><span style="color:#3366CC;font-size:1.2em;">600df -h</span></code></b>.#* The management at <ol> <li value="3">Record the size of the volume group and the amount of free space</li> <li>At the top of your organization have concerns regarding some irresponsible users on your systemVM window (make certain <u>not</u> full screen mode) click the '''view''' menu and change view from '''Console''' to <u>Details</u>''' .</li>#** <li>At the bottom left-hand corner, click '''Add Hardware''' and add a new group named storage device of '''2GBs''', make sure the '''Bus type''' is selected as: '''investigationVirtIO disk'''.<br><br>'''NOTE: Make certain to click "select managed or other existing storage", and fill in the pathname circled in red as shown in the diagram on the right-hand-side.<br><br>'''</li> <li>Go back to the '''console''' view</li> <li>Issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">ls /dev/vd** Look at </span></code></b>, what has changed?</li> <li>Use '''fdisk''' (''refer to how to use in Part 1'') to create a new single primary partition for '''/etcdev/groupvdb''' that fills the entire disk, save partition table (accepting defaults prompts would work), restart your '''centos3''' VM and note the GID of group called then '''format''' that partition for file type: '''investigationext4'''.</li>#** What GID is given to <li>Now we'll make the new device a new group if you do not specify it?#** In the file'''physical volume''', add those users it to the end of the concerned '''volume group (separate each user-name with a comma).#** Those individuals have explained their actions to management ''', and the crisis has been resolved. Delete the extend '''investigationlv_root''' group.:</li></ol>:: <b><code><span style="color:#** Look at 3366CC;font-size:1.2em;">pvcreate /dev/vdb1</span></code></b> (enter '''/etc/groupy''' again and note the change.to proceed - ignore warning)
:: <b><code><span style="color:#3366CC;font-size:1.2em;">vgs</span></code></b> (to determine: '''Answer the Part 3 observations / questions in your lab log book.'name_of_volumegroup'')
:: <b><code><span style== Part 4"color:#3366CC;font-size: Deleting 1.2em;">vgextend ''name_of_volumegroup'' /dev/vdb1</ Modifying Users ==span></code></b>
#Remain in your '''centos1''' VM for this section.# Read the man page for the '''userdel''' command. Note which option automatically removes the users home directory when that user is deleted.# Delete the user '''ops235_1''' using the command <b><code><span style="color:#3366CC;font-size:1.2em;">userdel ops235_1</span></code></b># Delete the user '''ops235'''_2 using the same command with the option which removes the home directory of the user.# Check the contents of the /home directory. What do you notice?# Check the contents of the <b><code><span style="color:#3366CC;font-size:1.2em;">/etc/group</span></code></b> file. What do you notice?# Read the man page for the usermod command. Note which options change the user's full name, primary group, supplementary groups, and shell.# Create a new user account called '''noobie''' for the employee: '''"Really Green"''' . Assign a password for that newly created user.# Management has indicated that this employee be on on probation for 3 months. Use the '''usermod''' command to set the account for noobie to expire in 3 months from this day as part of the security policy of this organization.# Add each of your new users to the group ops235 (in other words, add ops235 to each user as a supplementary group).# Examine <b><code><span style="color:#3366CC;fontlvextend -L +2G -size:1.2em;">/etc/group</span></code></b>. What has changed?# Use the '''usermodr ''name_of_volumegroup' command to change the full name of the user account '''noobie''' from '''"Really Green"''' to '''"Outstanding Employee"'''. Examine the result of running that command in the <b><code><span style="color:#3366CC;font-size:1.2em;">/etc/passwdlv_root</span></code></b> file. What has changed?# Use the '''usermod''' command to extend the use of their account for 5 years as of today.# Be sure to record your observations in your lab notes.
{| width="40%" align="right" cellpadding="10"|- valign="top"|{{Admon/note|LVM Safeguards When Reducing File System Size|In older versions of LVM, the system administrator had to be careful when reducing the size of Linux file-systems using LVM. This usually required to separate operations:<ul><li>Reducing the file-system to make room (''resize2fs'Answer ')</li><li>Reduce the logical volume by using the Part 4 observations '''lvreduce''' command</li></ul>This used to be performed to prevent a catastrophic loss of data if the logical volume was resize smaller than the available space on the file-system.<br>The newer LVM commands have "safeguards" (eg. option '''-r''' to prevent destroying the file-system (thus making the ''resize2fs'' command <u>obsolete</ questions in your lab log booku>).'''|}}|}
<ol> <li value=INVESTIGATION 2"13">Now rerun the '''ls /dev/vd*''' , '''pvs''' , '''vgs''' , '''lvs''' and '''df -h''' commands.</li> <li>Record the size of the volume group and the amount of free space. What has changed and what caused those changes?</li> <li>Among the changes, note that your root file-system is now 2GB bigger, and you have not even rebooted your machine since you used fdisk to create a partition!</li> <li>Reduce the size of the '''lv_root''' partition by '''1GB''' by issuing the following command:<br><b><code><span style="color:#3366CC;font-size: Managing System Services and Run1.2em;">lvreduce ''name_of_volumegroup''/lv_root --size -levels1G</span></code></b></li>Confirm that the file system has been reduced.</li><li>Increase the size of the '''lv_root''' partition by another '''1GB''' by issuing the command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">lvextend ''name_of_volumegroup''/lv_root --size +1G</span></code></b></li>Confirm that the file system can been increased.</li><li>Record the LVM Management commands in your lab log-book.</li></ol>
Many students may think that the following topic is small and "not a big deal". Those students may say, '''"How hard is running and stopping services?"'''
The process may not be hard, but knowing how to stop, start, restart and check the status of services is absolutely critical to a Linux server. '''Aside from learning to trouble-shoot problems''' by checking the status of running services, '''understanding how to manage services is critical to help protect a Linux server from penetration''' (this term is referred to as "'''Hardening a system'''"). Sometimes it is "what we don't know" that can harm us. One key element Answer Part 2 observations / questions in hardening a computer system is to disable non essential networkng services to allow IDSs ('''Intrusion Detection Systems''') to focus on a narrower range of policy violationsyour lab log book. A Debian-based penetration testing distribution called Kali (formerly referred to as '''BackTrax''') allows sysadmins and security professionals to identify vulnerabilities in their computer systems, and thus improve (harden) their systems against penetration. Learning to monitor the status, enable and disable networking services underlies the '''Backtrax''' motto:<br><br>'''''"The quieter you are, then more you will hear..."'''''<br><br>
=== Part 1: How do we Manage System Services? ===
= INVESTIGATION 2: ADDITIONAL FILE-SYSTEM OPERATIONS = We have seen take for granted that maintaining unneeded a file-system must be mounted (for example the root partition) in order for a Linux system to be usable upon system start-up. This was create automatically for the '''packages can be a security risk/archive''' due mount point automatically when we used the system-config-lvm utility in the previous investigation. We need to the unnecessary increase learn now to do this manually by editing or adding an entry in the complexity ''/etc/fstab'' file. The '''/etc/fstab''' (file system table) contains entries to mount various file systems automatically upon start-up of your the Linux system. Similarly, it is  The Linux system administrator also unnecessarily hazardous, and even more so, to leave unneeded services running. In this investigation, we will learn how has the ability to manually '''mount'''control services, (connect) and turn off those services that we think are not necessary '''un-mount''' (disconnect) partitions in order to help reduce security risksperform maintenance on the file system (for example un-mounting the '''/home'''partition to install software and prevent users from logging in during that process).}}
#Use your '''centos2''' VM for this part.
<ol>
<li value="2">Use the '''man''' pages to learn about the '''service''' command.</li><li>Issue the following Linux command:
<ul>
<li><b><code><span style="color:#3366CC;font-size:1.2em;">service --status-all</span></code></b></li>
</ul>
</li>
<li>Note the services that are currently running.</li>
<li>Use the command <b><code><span style="color:#3366CC;font-size:1.2em;">service iptables stop</span></code></b> to stop the service named '''iptables'''</li>
<li>Run a command to verify that the '''iptables''' service has stopped.</li>
<li>A newer method of managing services is by using '''systemd'''. It has the ability to manage dependent service in parallel and allow one service to be stopped without disrupting the other services. Here is a link that briefly explains how to use '''systemd''' (as opposed to tradition method: ''init'') and the '''systemctl''' command: [http://zenit.senecac.on.ca/wiki/index.php/Init_vs_systemd init vs systemd]</li>
<li>If you reboot now - the iptables service will be turned back on. We don't want it on though, it causes students headaches. To turn it off permanently we need to use the '''chkconfig''' command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">chkconfig iptables off</span></code></b></li>
<li>Use the '''systemctl''' command (from the link above - showing examples) to verify that the '''iptables''' service is off ('''hint:''' issue command, and pipe to grep "'''iptables'''").
<li>Reboot and confirm that it's no longer running.</li>
</ol>
== Part 1: Mounting and Un-mounting Partitions ==[[Image:mount.png|thumb|700px|right|Using the '''mount''' command with no arguments displays file-systems that are already mounted. The Linux system administrator can use the '''mount''' and '''umount''' commands to connect and disconnect different partitions from the file-system to perform maintenance.]]# Perform this part in your '''centos2'''Answer VM.# As you may recall in Part 1 observations of Investigation 1, we created another logical volume called '''archive''' using the graphical application '''system-config-lvm'''. This logical volume should be mounted (connected) to our existing file-system (2GB).# Issue the following command: <b><code><span style="color:#3366CC;font-size:1.2em;">mount</span></code></b># What is the purposed of issuing this command without arguments? Can you see the '''/archive''' mount point?# Confirm that the directory named '''/ questions archive''' is mounted.# You can use the '''umount''' command to unmount a file-system (eg. for maintenance purposes). Note the the name of the command is umount, <u>not</u> unmount.# Make certain you are not located in the /archive directory, and issue the following command to unmount the /archive directory:<br><b><code><span style="color:#3366CC;font-size:1.2em;">umount /archive</span></code></b># Issue the '''mount''' command (without arguments) to confirm it has been unmounted.# View the contents of the file-system table /etc/fstab by issuing the following command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">cat /etc/fstab</span></code></b># Note the line that automatically mounts a file-system (''/dev/sda3'', type ''ext4'') to '''/archive'''. This was automatically performed for you via the '''system-config-lvm''' utility.# View and record the fields for the '''/archive''' mount in your lab log booklogbook, and then issue the following command to reconnect or mount '''/archive''':<br><b><code><span style="color:#3366CC;font-size:1.2em;">mount &nbsp; -t ext4 &nbsp; /dev/centos_centos2/archive &nbsp; /archive</span></code></b><br><br>'''NOTE:''' If you are having problem mounting, use the /dev pathname listing in your /etc/fstab file.<br><br># Confirm that this file-system has been properly mounted. Note: You could have also issued the command: '''mount -a''' to <u>automatically</u> mount the file-systems contained in the '''/etc/fstab''' file.<br><br>Tip: If you had to do manually configure /etc/fstab (eg. only in text-based mode - centos3), you could have issued the command (as root):<br> <b><code><span style="color:#3366CC;font-size:1.2em;">echo "/dev/centos_centos2/archive /archive ext4 defaults 1 2" >> /etc/fstab</span></code></b><br>(although we don't need to do this, since it was already done).<br><br># '''A curious question''': If root needs to unmount the '''/home''' directory for maintenance but we had to first login in graphically as a regular user, then su to "root", isn't our regular user still logged in so we can't un-mount the /home directory?!? How can we as a Linux System Administrator get around the problem (hint: you already learned it somewhere in lab3!!! Wow, using what we already taught can pay-off in the future ... lol!).
===Part 2: How do we Manage Runlevels?===
Running servers '''Answer the Part 1 observations / questions in graphical mode will make your system most likely to be penetrated. The X-windows framework can be vulnerable to attacks when these servers are connected to the Internet. This is why when you install server versions of Linux, they work in text-based mode only. Desktop versions of Linux are then installed on workstations (working in graphical mode) that connect to the server (for security reasons)lab log book.'''
The Linux sysadmin can also change the run-level (or state) of a graphical Linux server to run in text-based mode and run the graphical mode by issuing a command when graphic mode is required. The run-level term is now deprecated in Fedora, and will likely be deprecated in RHEL/CentOS at some point as well, but for now this is what the industry is using.
== Part 2: Monitoring Disk Space ==
[[Image:disk_usage.png|thumb|500px|right|The '''df''' and '''du''' commands are useful tools for Linux system administrators to flag disk space issues and investigate their causes.]]
Another essential duty of a Linux system administrator is to anticipate problems and take preventative measures to avoid computer system problems <u>before</u> that occur.
Monitoring disk space activity helps provide Linux system administrators information to help take corrective action before problems can occur.
#Perform this part Remain in both your '''centos2''' and '''centos3''' VMsVM for this section.<ol> <li value="2"># Issue the following Linux command: <ul> <li><b><code><span style="color:#3366CC;font-size:1.2em;">runleveldf -h</span></code></b></li> </ul> </li> <li># Note the difference in output between disk space usage for '''centos2/''' and , '''centos3/home'''.</li> <li>You can use the , and '''init/archive''' command to change the current run-levelpartitions. See # If a list partition is running out of runlevels [https://www.centos.org/docs/5/html/5.2/Installation_Guide/s2-init-boot-shutdown-rl.html here].</li><li> Use available space, the '''man''' command to learn how to use Linux System Administrator can reallocate space among partitions or add another disk and grow the '''init''' commandfile system (like you did in a previous investigation). Use this command to change The administrator also can investigate the current run-level in '''centos2''' cause of low disk space. Two examples immediately come to '''3'''mind: excessive use of space from users, and potential penetration from hackers. What happened?</li> <li>Issue # To investigate excessive disk usage by regular users, you can obtain a total amount of disk usage for that user by issuing the following Linux command: <ul> <libr><b><code><span style="color:#3366CC;font-size:1.2em;">startxdu -hs /home/regularuserid</span></code></b># To provided a more detailed list of usage (file-by-file), issue the command:</libr> </ulb> </licode> <lispan style="color:#3366CC;font-size:1.2em;">What happens?du -h /home/regularuserid | more</lispan> <li/code>Log-off your graphical system. You should return to your shell prompt.</lib> <li>Using systemd requires # If there is a different recurring space usage problem with regular users, the Linux system administrator can impose quotas (caps on disk usage). This method of setting text mode is not taught in this course.# The methods to monitor potential penetration to a Linux system are too numerous, and graphical modeare taught in other coures (for example: SEC520). You can refer On method of monitoring potential penetration is to this link look for future referenceabnormally large files. Issue the command: [http<br><b><code><span style="color:#3366CC;font-size://fedoraproject1.org2em;">find -P /wiki/Systemd#How_do_I_change_the_runlevel.3F How to Change Run-Levels with Systemd]size +100000k</lispan><li/code>Restart your centos2 machine, and make certain that it runs in '''graphical''' mode</lib> </li>Why would # Did you want see any files that are that size?# The next section will apply some of these tools we have discussed into a shell script and crontab entry to make periodically monitor and contact the system administrator of potential disk space issues (before they become a graphical Linux system run in text-based mode?</li></ol>serious problem).
'''Answer the Part 2 observations / questions in your lab log book.'''
= INVESTIGATION 3: LOOKING AHEAD =
==Automating Routine Tasks (Scheduling The Running of Shell ScriptingScript Via Crontab)==
{|width="40%" align="right" cellpadding="10"
|- valign="top"
|{{Admon/tip|Bash Shell Scripting Tips:|<br><ul><li>'''The case statementUsing awk to Manipulate Text:'''<br><br>The case statement is a control-flow statement Legend has it that works in a similar way as the if-elif-else statement (but is more concise). This statement presents scenerios or "cases" based on values or regular expressions (not ranges of values like if-elif-else statements). After action(s) are taken for a particular scenerio (or "case"), a break statement (''';;awk''') is used command was invented by three C programmers that wanted to "break-out" of create a utility in Unix that had programming syntax more like C programming. People started to use the statement (command and found it very useful for report generation and not perform other actions). A default case (*) is also used to catch exceptionsfile repair or manipulation.<br><br><u>The command mimics a C program, with braces '''Examples (try in shell script):{ }'''</u><br><br>''read -p "pick that surround the action to perform based on records from a door (1 or 2): " pick<br>case database file matching either test conditions, regular expressions, etc. Fields appear as numbers with $pick in.<br>&nbsp; 1) echo "You win a car!" ;;<br>&nbsp; 2) echo "You win a bag of dirt!" ;;<br>&nbsp; *) echo "Not a valid entry"<br>&nbsp;&nbsp;&nbsp;&nbsp; exit 1 ;;<br>esac''<br><br>''read -p "enter a single digit: " digit<br>case $digit in<br>&nbsp; [0-9]) echo "Your single digit is: $digit" ;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *)&nbsp;echo "not a valid single digit"<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit 1 ;;<br>esac''<br><bru>Examples</li><liu>'''The getopts function:'''<br><br></li></ul>The getopts function allows the shell scripter to create scripts that accept options (like options for Linux commands). This provides the Linux administrator with scripts that provide more flexibility and versatility. A built-in function called ''awk 'getopts{print}''' (idata-file.e. get command options) is used in conjunction with a '''while''' loop and a ''txt'case''' statement to carry out actions based on if certain options are present when the shell script is run. The variable '''$OPTARG''' can be used if an option accepts text (denoted in the getopts function with an option letter followed by a colon. Case statement exceptions use the ''':)''' and '''\?)''' cases for error handling.<br><br>''awk -F";" '<u>Example of getopts</u>{print $5,$3}'data-file.txt'' (try in script and run with options)<br><br>''while getopts abc: name<br>do<br>&nbsp; case $name in<br>&nbsp; &nbsp; a) echo awk -F"Action for option \,"a\"" ;;<br>'$4 &nbspgt; &nbsp#61; b) echo "Action for option \"b\"" ;;<br>&nbsp; &nbsp; c) echo "Action for option \"c\""<br>&nbsp; &nbsp; &nbsp; &nbsp; echo Value is: 10000 {print $OPTARG" ;;<br>&nbsp; &nbsp; :) echo "Error: You need text after -c option"<br>&nbsp; &nbsp; &nbsp; &nbsp; exit 1 ;;<br>&nbsp; &nbsp; \?) echo "Error: Incorrect option"<br>&nbsp; &nbsp; &nbsp; &nbsp; exit 1 ;;<br>esac, $2}' salary.txt''<br/li><br/ul>}}
|}
We will now use This emphasis on this section focuses on how to run useful shell scripts or shell scripting script that we have created at specific dates/times. It would be silly to help automate the task for expect a Linux adminstrator system administrator to create regular user accountsstay up late (eg. 2 a.m.  #Download, study, and ) to manually run the following a shell script. Issue the command:<br><b><code><span style=" pointerto terminate processes or to re-events:none;cursor:default;color:#3366CC;font-size:1.2em;">wget https://scs.senecacboot Linux servers.Database files (tables) are used to provide instructions onhow frequent shell scripts or commands can be run.ca/~murray.saul/user-create.bash</span></code></b>#Try The '''cron''' daemon is used to refer to understand what these Bash Shell scripts do, files and then to run the script as rootthem on a pre-determined basis. After running the shell script, view the contents of The term '''cron''' comes from the old word '''chronograph'''meaning a special type of watch (actually a '/home'stop-watch'' directory ) to confirmhelp monitor and schedule routine tasks.
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.
#Perform this section in your '''c7host''' machine
#Make certain you are logged in as '''root'''.
#Download, study, and run the following shell script. Issue the command:<br><b><code><span style=" pointer-events:none;cursor:default;color:#3366CC;font-size:1.2em;">wget https://scs.senecac.on.ca/~murray.saul/monitor-disk-space.bash</span></code></b>
#Try to understand what this Bash Shell script does (refer to man pages for the '''awk''' command), and then run the script as root.
<ol><li value="3">Open a Bash shell terminal and login as root.</li><li>Use the wget command to download the input file called user-data.txt by issuing the command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">wget https://scs.senecac.on.ca/~murray.saul/user-data.txt</span></code></b></li><li>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.<li><li>Use a text editor (such as <b><code><span style="color:#3366CC;font-size:1.2em;">vi</span></code></b> or <b><code><span style="color:#3366CC;font-size:1.2em;">nano</span></code></b>) to create a Bash Shell script called: <b><code><span style="color:#3366CC;font-size:1.2em;">createUsers.bash</span></code></b> in /root's home directory.</li><li>Enter the following text content into your text-editing session:</li></ol>
<code style="color:#3366CC;font-family:courier;font-size:.9em;margin-left:20px;">
<br>
&#35;!/bin/bash <br>
<br>
&#35; createUsers.bash<br>
&#35; Purpose: Generates a batch of user accounts (user data stored in a text file)<br>
&#35;<br>&#35; USAGE:<br>
&#35;<br>&#35; /root/createUsers.bash [-i {input-path}] <br>
&#35;<br>
&#35; Author: *** INSERT YOUR NAME ***<br>
&#35; Date: *** CURRENT DATE ***<br>
<br>
if [ $PWD != "/root" ] # only runs if in root's home directory<br>
then<br>&nbsp;echo "You must be in root's home directory." >&2<br>
&nbsp;exit 1<br>
fi<br>
if [ "$#" -eq 0 ] # if no arguments after command<br>
then<br>
&nbsp;echo "You must enter an argument" >&2<br>
&nbsp;echo "USAGE: $0 [-i {input-path}]" >&2<br>
&nbsp;exit 2<br>
fi<br>
</code>
<br>
<ol><li value="6">Save your editing session, but remain in the text editor.</li><li>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</li></ol>
<br>
<code style="color:#3366CC;font-family:courier;font-size:.9em;">
<br>
outputFlag="n"<br>
while getopts i: name<br>
do<br>
&nbsp;case $name in<br>
&nbsp; &nbsp;i) inputFile=$OPTARG ;;<br>
&nbsp; &nbsp;:) echo "Error: You need text after options requiring text"<br>
&nbsp; &nbsp; &nbsp; &nbsp;exit 1 ;;<br>
&nbsp; &nbsp;\?) echo "Error: Incorrect option"<br>
&nbsp; &nbsp; &nbsp; &nbsp; exit 1 ;;<br>
&nbsp;esac<br>
done<br>
</code>
<ol><li value="6">Save your editing session, but remain in the text editor.</li><li>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:</li></ol>
<br>
<code style="color:#3366CC;font-family:courier;font-size:.9em;">
<br>
if [ ! -f $inputFile ]<br>
then<br>
&nbsp; echo "The file pathname \"$inputFile\" is empty or does not exist" >&2<br>
&nbsp; exit 2<br>
fi<br>
<br>
set $(sed 's/ /+/g' $inputFile) # temporarily convert spaces to + for storing lines as positional parameters<br>
<br>
for x<br>
do<br>
&nbsp; &nbsp; useradd -m -c "$(echo $x | cut -d":" -f2 | sed 's/+/ /g')" -p $(date | md5sum | cut -d" " -f1) $(echo $x | cut -d":" -f1)<br>
&nbsp; &nbsp; mail -s "Server Account Information" $(echo $x | cut -d":" -f3) <<+<br>
&nbsp; &nbsp; Here is your server account information:<br>
&nbsp; &nbsp; servername: myserver.senecac.on.ca<br>
&nbsp; &nbsp; username: $(echo $x | cut -d":" -f1)<br>
&nbsp; &nbsp; password: $(date | md5sum | cut -d" " -f1)<br>
&nbsp; &nbsp; Regards,<br>
&nbsp; &nbsp; IT Department<br>
+<br>
done<br>
<br>
echo -e "\n\nAccounts have been created\n\n"<br>
exit 0<br>
</code>
In order to automatically run the above-mentioned script periodically, you use the scheduler in Linux called '''crontab'''. The term crontab stands for '''Chronograph Tables''', where a chronograph is the old term for a timepiece (the forerunner of the modern watch). You can run the crontab command to schedule commands and shell script to be run in a number of different ways.
<br><br>
<ol>
<li value="85">Save, Quickly view the tutorial about the <b>[http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800 Using crontab]</b> file to understand the purpose of this file and how to basically set permissions, and then up a schedule to run that a shell script .</li> <li>Issue the following command to setup a crontab entry for root:<br><b><code><span style=" pointer-events:none;cursor:default;color:#3366CC;font-size:1.2em;">crontab -e</span></code></b></li><li>Enter the input text file following line in order to run at 6:00 on the first day of every month:<br><b><code><span style=" pointer-events:none;cursor:default;color:#3366CC;font-size:1.2em;">0 6 1 * * /root/monitor-disk-space.bash #Runs first day of each month (6:00 am)</span></code></b></li><li>'''user-data.txtSave'''the crontab entry. Did it work? Try running the script without an argument - What did it do? </li><li>You have completed lab4. Proceed to Completing The Lab, and follow Confirm that the entry was properly saved by issuing the instructions for following command:<br><b><code><span style="lab signpointer-offevents:none;cursor:default;color:#3366CC;font-size:1.2em;".>crontab -l</span></code></b></li></ol>
'''Answer Investigation 3 observations / questions in your lab log book.'''
= LAB 4 5 SIGN-OFF (SHOW INSTRUCTOR) ={{Admon/important|Time for a new backup, INCLUDING YOUR VIRTUAL HARD DRIVE!|If you have successfully completed this lab, make a new backup of your virtual machines. Remember to also make a backup of the new second virtual disk drive on <br><br>''centos1'' Virtual hard-- you now have two virtual disks on ''centos1'', and therefore two drives created in this lab are image files, and therefore will now need two backup filesto be backed up as well!'''.}}
'''Arrange proof of the following on the screen:'''
::<ol><li><span style="color:green;font-size:1.5em;">&#x2713;</span> Account created on '''centos1centos2''' VM.::<span style="color:green;font-size:1.5em;"blockquote><ul><li>&#x2713;'''Logical Volume''', '''Physical Volume''', and '''Volume Group''' information </spanli><li> List contents Contents of '''/etc/groupfstab''' file (ops235 group) on </li><li>Proof that '''centos1/archive''' VM.::has been mounted</li></ul></blockquote><li><span style="color:green;font-size:1.5em;">&#x2713;</span> List contents '''centos3''' VM:<blockquote><ul><li>Proof of virtual hard disk creation</li><li>'''Logical Volume''', '''/etc/passwdPhysical Volume''' file (created accounts) on , and '''centos1Volume Group''' VM.::information </li></ul></blockquote></li><li><span style="color:green;font-size:1.5em;">&#x2713;</span> Display current run-level status on '''centos2c7host''' VM.Machine:<blockquote><ul><li>Proof of creation of the shell script:'''monitor-disk-space.bash'''</li><li>Crontab entry for '''root''' account</li></ul></blockquote><li><span style="color:green;font-size:1.5em;">&#x2713;</span> Creation of your bash shell script called '''createUsers.bashLab5'''log-book filled out.</li></ol> 
= Preparing for the = Practice For Quizzes , Tests, Midterm &amp; Final Exam ==
# Describe all of the field in <code>'''/etc/passwd'''</code># What is the command to create a userVG? What option to create a home directory for that userPV? LV?# What is the command to change total size of the full name of an already-created user"main" VG on your system?# What is the command to delete a user account? What option allows for the user's home directory to be removed as wellHow do you create an LV?# What is the command to create a group? What is the command (or steps) to include a user in a newly-created groupHow do resize an LV?# What is How would you add the purpose of <code>disk partition '''/etcdev/shadowsdb7'''</code>to your volume group "main"?# What is How would you increase the purpose size of <code>'''/etc/skel'''</code>the root filesystem by 50 MB?# What does the term run-level meanHow can you determine if a partition has been mounted onto a system?# How to set can you unmount an existing partition from the runfile-level of a Linux system to text-based only? # How to set to graphical modecan you temporarily mount a partition on a file-system?# What is the command to view the status of running servicesHow can you permanently mount a partition on a file-system upon boot-up?# What is are the command to start a service separate elements (like httpd, or sshdfields)of the '''/etc/fstab''' file?# What is Describe the command tools that a Linux system administrator have to start a service?# Can a service be stopped and started by issuing just one command?monitor disk space usage.
[[Category:OPS235]]
[[Category:OPS235 Labs]]