Practical Record Book - Linux
Practical Record Book - Linux
ON
LINUX ADMINISTRATION
By
Katabalwa John Vianney
Bsc.AIT
012220073
2023
1 Introduction to Linux
4 Redirection of output
6 Links to files
2) Ubuntu Linux - Linux operating system distro used for the lab activities.
3) Bash terminal emulator – command line terminal used to interact with the system shell.
Screen shot of the Terminal prompt and GUI icons under the Ubuntu distro using Oracle Virtualbox:
1.0 : Introduction to Linux.
➢ Computer operating system assembled under the license of free and open-source software
development and distribution. The original core system of Linux was written by Linus
Torvalds, and this was based on the UNIX operating system.
➢ Linux has been further developed, through numerous system utilities developed by the
Linux community developers, as an open-source software under the GNU project pioneered
by Richard Stallman.
➢ Linux now comes in several distributions (distros) to serve different purposes. These include
Ubuntu, Redhat, Fedora, Centos, Debian, LinuxMint and others. Linux is a very stable system
with high security, and supports multi-tasking and multi-user operations. This has rendered
Linux as the best choice for high performance computers such as web- based servers.
1) The kernel: This is the core component of Linux OS, and comprises of programs that
interact with the computer hardware and resources so as to provide a service to a
computer user. These programs receive interpreted commands from the Shell
component.
2) The shell: This component interfaces with the Linux users, and takes users’ instructions
or commands and interprets them for the Kernel.
3) The file system: This component manages the organization of users’ files and related
data storage. Everything in Linx is managed as a file and related files are grouped as
directories.
➢ Linux avails the below two modes for users to interact with the operating system (Shell):
1) Terminal emulators – Command line interaction with the Shell using the Terminal
(keyboard shortcut “Ctrl + Alt + T”) as the standard input and output. The default or
commonest terminal emulator is the “bash” (bourne-again-shell). This user mode and
the bash emulator has been adopted throughout the study course.
2) Graphical User Interface (GUI) – Program or application icons that support access to
system services and resources by way of the user clicking on the specific icon of interest.
➢ To get help about the command line usage in the terminal mode, we can use the below
command: man man. This displays the detailed list and description of system commands as
per below screen shot. To exit the display and return to the terminal prompt, simply press
the key q.
Screen shot of the Terminal and GUI icons under the Ubuntu distro using Oracle Virtualbox:
3.5: Creating Files, viewing and editing content (touch, cat, head, tail, gedit)
The below commands can be used for the above operations:
1) touch : used to create an empty file
2) cat : used to view the contents of a file:
3) gedit : used to edit a file using a text editor window.
4) head -<lines> : View the first several lines of a file, for multi-lines files.
5) tail -<lines> : View the last several lines of a file, for multi-lines files.
Illustration for the above commands (touch, cat, gedit):
➢ Comparing files:
1) cmp <file1> <file2> : used to compare contents in 2 or more files.
2) diff <file1> <file2>: used to display the differences between 2 or more files.
Illustration for cmp and diff:
➢ File compression using the tar command (Tape Archive). Common options include:
1) tar -cf : create a new archive.
2) tar -xf : extract files from an archive.
3) tar -tf : list the contents of an archive.
4) -v : verbosely list files processed.
Illustration for tar:
Illustration for hard link and original file having same content:
7.0 Users and permissions
The ls -l command is used to display the below detailed information about files or
directories.
➢ So, in order to read a file, you must have execute permission on the directory containing
that file, and hence on any directory containing that directory as a subdirectory, and so on,
up the tree. Some examples:
1) -rwxrwxrwx : a file that everyone can read, write and execute (and delete).
2) -rw------- : a file that only the owner can read and write - no-one else can read or write
and no-one has execution rights.
7.2 Changing access rights (chmod)
➢ Only the root (super user) or the file owner can use chmod to change the permissions of a
file.
➢ The options of chmod are as follows:
1) u : user
2) g : group
3) o : other
4) a : all
5) r : read
6) w : write (and delete)
7) x : execute (and access directory)
8) + : add permission
9) - : take away permission
➢ In a Linux system, users refer to individuals or entities that interact with the operating
system by logging in and performing various tasks. User management plays a crucial role in
ensuring secure access control, resource allocation, and system administration.
➢ A user in Linux is associated with a user account, which consists of several properties
defining their identity and privileges within the system. These properties include: a
username, UID (User ID), GID (Group ID), home directory, default shell, and password.
➢ Linux supports two main categories of users: system users and regular users:
1) System users are created by the system during installation and are used to run system
services and applications.
2) Regular users are created by the administrator and can access the system and its
resources based on their permissions.
➢ The above two categories of users can be grouped to create the third category of users
called Groups.
➢ Use the ls -l command to find the owner, and group of a file or directory.
➢ To know more about the current user, use the id command.
/etc/passwd file: contains all the users available in the system. This is a plain text file and
can be viewed using cat command as illustrated below.
➢ From the above illustration, each line has seven entries separated by “:” as below:
username:password:uid:gid:gecos:/home/dirname:shell
/etc/shadow file: The actual user passwords are stored in an encrypted form in /etc/shadow
file. To view the file details, one needs to be a root user (superuser).
/etc/group file: Each user has one primary group, and zero or more supplementary groups.
To view the file details, one needs to be a root user.
To become root user, use the su - command, and provide the root password for
authentication. To exit super user, use shortcut crtl + d or use the command exit.
To become root user temporarily, use the sudo command, and you must have your user
recorded in the /etc/sudoers file. Only the super user can add another user to the
/etc/sudoers file. Below illustration refers:
We can also use the command “ls /home” to view the users created on the system:
Illustration to confirm new user (kjv2) added and deleted, but user home directory is retained (manual
deletion required by system administrator):
Illustration to create a group, add users to groups and know users’ groups and delete groups:
8.0 Processes and Jobs
A process is an executing program identified by a unique PID (process identifier).
When a process is running, backgrounded or suspended, it will be entered onto a list along
with a job number. To examine this list, we can use the commands jobs or ps
To kill a process (terminate or signal a process), for example, when an executing program is
in an infinite loop:
➢ To kill a job running in the foreground, type ^c (control-c). For example, run sleep 60 ^c
➢ To kill a suspended or background process, type kill % <jobnumber>
➢ Processes can also be killed by finding their process numbers (PIDs) using ps command, and
using the command kill <PID>
➢ If a process refuses to be killed, we can use the -9 option, i.e. type kill -9 <PID>
➢ To kill off other users' processes, root user must be used.
Summary of key process actions:
➢ command & - run command in background
➢ ^C - kill the job running in the foreground
➢ ^Z - suspend the job running in the foreground
➢ bg - background the suspended job
➢ jobs - list current jobs
➢ fg %1 - foreground job number 1
➢ kill %1 - kill job number 1
➢ ps - list current processes
➢ kill <1000> - kill process number 1000
➢ top – view all processes running on the system
➢ Whenever calling at, you have to define the interval of running the desired task. For
example:
1) To run a certain command after an hour, use at now + 1 hour
2) To run the desired task at 6 P.M., six days from now, use at 6pm + 6 days
3) To execute a bash script containing all the tasks that need to be performed, one can run
the command at 6pm + 6 days -f <script>
➢ To list all the pending at jobs, run the command atq. Here, the output will list all the jobs
with a specific reference number in the left column.
➢ To delete a queued job, run the command atrm <reference_number>
➢ To exit the at prompt, press "Ctrl + d.", and a summary of the tasks scheduled and the time
they'll be executed will be displayed.
➢ To set a scheduled task, we use the crontab file. This file allows any user to define their
personal scheduled tasks. You can use it to configure cron schedules different from the
above pre-defined schedulers. it is not a must that a user has to be super user or root in
order to run cron jobs. Every user on Linux can use a crontab file to run their own set of
cron jobs.
➢ By default, a user doesn’t have a crontab file on Linux. The user can create a crontab file
with the command crontab -e
➢ Running the command crontab -e for the first time prompts the user to pick a text editor
from the available list. The crontab file should be created (if not available already) and
opened with the chosen text editor. The user can add own cron jobs at the end of this file,
and save it, and exit out of the text editor.
➢ The possible variables for the above crontab fields are listed below. Using an asterisk (*) or
wildcard in place of the numeric value for any of the five variables will repeat the job on all
possible or new instances of that variable.
1) minute: 0 to 59
2) hours: 0 to 23
3) day of the month: 1 to 31
4) month: 1 to 12
5) day of the week: 0 (Sunday) to 6 (Saturday)
➢ Below are examples of cron tasks or scheduled jobs for periodically taking screenshots of a
user’s desktop and storing them in home folder:
1) 13 15 2 3 2 scrot: Job entry to take a screenshot once at 15:13 hours on 2nd of March
plus every Tuesday in March at 15:13 hours.
2) * * * * scrot: Job entry to take a screenshot every minute.
3) 0 * * * * scrot: Job entry to take a screenshot Every hour.
4) 0 18 * * * scrot: Job entry to take a screenshot Every day at 18:00 hours.
5) 0 18 6 * * scrot: Job entry to take a screenshot Every month on 6th at 18:00 hours.
6) */30 * * * * scrot: “/” indicates Job entry to take a screenshot at a fixed periodic interval
of 30 minutes (runs every 30 minutes).
7) 1,4,5 * * * * scrot: comma separated values for a given variables to define repeated
execution of the job at regular intervals. For this example, a screenshot will be taken on
every 1st, 4th, and 5th minute of a new hour.
➢ Besides the five scheduling variables, cron also uses some special string to schedule jobs.
These strings include:
1) @hourly: job is run every hour
2) @daily / @midnight: job is run every day
3) @weekly: job runs once a week
4) @monthly: job runs once a month
5) @yearly / @anually: job runs once a year
6) @reboot: job runs once every reboot / login
➢ For example, the crontab entry @reboot scrot will take screenshot once on every reboot.
You can replace “@reboot” with any other special strings mentioned above.
9.0: Shell programming using scripts
Shell is a command-line interpreter that allows the user to interact with the system. It is
responsible for taking inputs from the user and displaying the output.
Shell scripts are a series of commands written in order of execution. These scripts can
contain functions, loops, commands, and variables. Scripts are useful for simplifying a
complex series of commands and repetitive tasks.
Illustration of a basic shell script created using cat command, and with user defined variable and function:
10: System maintenance and monitoring – using utilities, tools and packages
The Advanced Package Tool, aptly named the 'apt' package manager can handle the
installation and removal of software. Below are some simple examples of use:
➢ Install software or utility tools - we can use sudo apt-get install <package or software>
➢ Update existing software - sudo apt-get update
➢ Upgrade existing software - sudo apt-get upgrade