Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
44 views

Practical Record Book - Linux

lab book using linux

Uploaded by

ilanjv40
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Practical Record Book - Linux

lab book using linux

Uploaded by

ilanjv40
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

PRACTICAL MANUAL

ON
LINUX ADMINISTRATION

By
Katabalwa John Vianney
Bsc.AIT
012220073

2023

FACULTY OF INFORMATION AND COMMUNICATION TECHNOLOGY


INTERNATIONAL BUSINESS SCIENCE AND TECHNOLOGY UNIVERSITY
Plot 11A, Rotary Avenue, Lugogo Bypass, Kololo, Kampala, Uganda
List of Activities

1 Introduction to Linux

2 Using Linux: The Terminal and Getting Help

3 Working with files and directories.

4 Redirection of output

5 Wildcards and file naming conventions

6 Links to files

7 Users and permissions

8 Processes and Jobs

9 Basic Shell programming using scripts

10 System maintenance and monitoring – using utilities, tools and packages


Tools used for the Lab activities:

1) Oracle Virtualbox - Virtualization hypervisor to install Linux operating system as a virtual


machine (guest OS) on Microsoft Windows Operating system (host OS).

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.

4) Default user (kjv) created upon installation of Ubuntu as a virtual machine:

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.

➢ Linux is available as an open-source distribution or commercial distribution. The Open-


source distribution is supported by the Linux community under the GNU project while the
commercial distribution is supported by a dedicated enterprise team such as Redhat
Enterprise (RHE) distribution.

➢ Linux is built of three key broad components as below:

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.

2.0 : Using Linux: The GUI, Terminal and Getting Help.

➢ Linux operating system can be deployed or installed on a computer in two ways:


1) Clean installation directly onto the computer.
2) Concurrently deployed as a virtual machine (guest OS) on top of the existing (host OS)
operating system. Here, a software called a Hypervisor is required to support the setup
of the virtual machine. Such software includes Oracle virtualbox, VMware.

➢ 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:

➢ Additional helpful commands include:


1) date - prints the current date time.
2) cal - displays the existing calendar of the shell.
3) whoami – displays the user account for the current user using the system.
4) who - displays which users are logged onto the system.
5) id - prints real user id, and various other details related to the account.
6) pwd - print working directory, displays the absolute path of the current directory.
7) cd - change directory. Used to change the user’s current directory.
8) ls - to list the files and directories inside any given directory.
9) clear – to clear the terminal and return to the initial prompt.
10) history – to display the past user activity under the terminal.
11) exit – to exit the terminal.
12) shutdown – to schedule shutdown of the virtual machine.
13) shutdown now – to immediately shut down the virtual machine.

Illustration for the above additional helpful commands (1-8):


3.0 : Working with files and directories.

3.1: Listing files and directories (ls)


Upon first login, the user’s current working directory is the user’s home directory. This has the
same name as user’s name, for example, kjv, and it is where the user’s personal files and
subdirectories are saved. Key command formats or behavior used include:
1) ls : lists the contents of your current working directory.
2) ls -a : list all files including hidden file starting with '.'
3) ls -i : list file's inode index number.
4) ls -l : list with long format - show permissions.

Illustration for the above commands:

3.2: Making Directories (mkdir)


Directories hold the files created by a user. Common command formats include:
1) mkdir : to create directory or directoties.
2) mkdir -p : to create subdirectory within a directory ( nesting)

Illustration for the above commands:


3.3: Changing to a different directory (cd)
The cd command means change the current working directory, which is the user’s current
position in the file-system tree. Common command formats include:
1) cd : change current directory. By default, returns to the user’s home directory.
2) cd . : change current directory to previous directory for subdirectories.
3) cd .. : change current directory to parent directory for subdirectories.
4) cd / : change current directory to the root directory. Root (/) is the top most directory in
the file system tree hierarchy.

Illustration for the above commands:

3.4: Pathnames (pwd)


➢ Pathnames enable a user to establish the current directory in relation to the whole file-
system. The absolute pathname starts at the root (/) while the relative pathname starts
from any other directory but not the root.
➢ Using the absolute pathname enables access to a file or directory from any other location or
directory.

Illustration for the above commands:

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):

3.6: Copying Files and Directories (cp)


The cp command copies the files or directories from source to destination. Common command
formats include:
1) cp <source> <destination> : copies the file from source to destination. This overwrites
the destination file.
2) cp -p <source> <destination> : copies the file from source to destination and preserves
the original attributes of file like file owner, timestamp, group, permissions etc.
3) cp -R <source> <destination> : copies source directory to specified destination
recursively. –R means ‘recursive’ and will copy the contents of the directory as well as
the directory itself.

Illustration for the above cp:

Illustration for the above cp -R


3.7: Moving files and Directories (mv)
The mv command has two key uses.
1) It can be used to move a file or files to a different location (i.e. a different directory).
2) It can also be used to rename a file or a directory.

Illustration for mv command, and head and tail commands:

3.8: Removing Files (rm) and Directories (rmdir)


rm : used to delete (remove) a file.
rmdir : used to delete an empty directory.
rm -r : removes directories that already have files in them. This should be used sparingly as
there is no Recycle bin!.

Illustration for rm and rmdir:

3.9: cat command


cat (concatenate) command can be used to:
1) cat <file> : view or display the contents of a file on the screen.
2) cat > <file> : create a file by redirecting the output to that file, press ctrl + d after editing
or adding the file items.
3) cat >> <file> : Append items to a file, press ctrl + d after editing or adding the file items.
4) cat <file1> <file2> > <file3> : Concatenate or merge 2 or more files and redirect the
output to a new file.
Illustration for cat, cat> and cat>>:

Illustration for cat concatenation:

3.10: Other useful file management commands include:


➢ Searching for content or files:
1) find <pathname> -name <name of file> : used to search for files by name or the location
or pathname for files.
2) grep <display options> <pattern> <name of file> : stands for general regular expression
parser, used to search files as well as for specified words or patterns in files.

Illustration for find and grep:

➢ 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:

➢ Sort, unique display and count of content:


1) sort : used to sort file contents.
2) uniq : used to display only unique items in a file, excluding duplicates.
3) wc : short for word count. Used to do a word count on file. To find out how many lines
the file has, use wc -l <file>. To find out how many characters the file has, type wc -m
<file>.

Illustration for sort, uniq and wc:

➢ 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:

4.0: Redirection using the > and >> operators


1) command > file : redirect standard output to a file.
2) command >> file : append standard output to a file.
➢ For illustration on usage, refer to the cat command above.

4.1: Piping (|) commands


➢ The pipe (|) is used to combine 2 or more commands and their execution or output.

Illustration for piping:

5.0: Wildcards (* and ? )


➢ For illustration on usage, refer to the above commands find and grep.
1) The character * will match against one or more character(s) in a file (or directory) name.
2) The character ? will match exactly one character.

5.1: Filename conventions


➢ In naming files or directories, characters with special meanings such as / * & % , should be
avoided. Also, avoid using spaces within names.
➢ The safest way to name a file is to use only alphanumeric characters, that is, letters and
numbers, together with _ (underscore) and . (dot).
➢ File names conventionally start with a lower-case letter, and may end with a dot followed by
a group of letters indicating the contents of the file (suffix). For example, all files consisting
of C code may be named with the ending .c, for example, prog1.c. Then in order to list all
files containing C code in your home directory, you need only type ls *.c in that directory.
6.0: Creating links to files
➢ The below two kinds of links are supported in Linux:
1) Soft link or symbolic links, using the ln -s command
2) Hard links, using the ln command

6.1 Creating soft link to a file


➢ Soft link or symbolic links are a special kind of files which point (pointers) to some other file
using either related or absolute paths.
➢ To create soft links, use ln -s command.
➢ To view details about the soft, use ls -l command.
➢ If you remove the original file the soft link is pointing to, then the soft link will become
useless, because it’ll point to a file that does not exist. Soft links can also point to file which
is in a different file system.

Illustration for soft link:

6.2 Creating hard link to a file


➢ Hard links are a default for any file created, that is, every file has at least one link upon
creation in the file system.
➢ Hard links can also be created as special kind of files which point (pointers) to some other
file using either related or absolute paths. To create hard links, use ln command.
➢ To view details about the hard link, use ls -l command.
➢ If you remove the original file the hard link is pointing to, the hard link will persist with the
contents of the original file.

Illustration for hard link creation:

Illustration for hard link and original file having same content:
7.0 Users and permissions

7.1 Viewing file and directory permissions

The ls -l command is used to display the below detailed information about files or
directories.

➢ In the left-hand column is a 10-symbol string consisting of the symbols d or l, r, w, x, -, and,


occasionally, s or S. If d or l is present, it will be at the left-hand end of the string, and
indicates a directory or link: otherwise - will usually be the starting symbol of the string,
indicating a normal file. The 9 remaining symbols indicate the permissions, or access rights,
and are indicated as groups of 3:
1) The left group of 3 gives the file permissions for the user that owns the file (or directory)
(kjv in the above example);
2) The middle group of 3 gives the permissions for the group of people to whom the file (or
directory) belongs (kjv in the above example);
3) The rightmost group of 3 gives the permissions for everyone else.
➢ The next digit indicates the links associated with file or directory.
➢ The next group of characters indicates the user or owner of the file or directory.
➢ The next group of characters indicates the group to which the owner of the file belongs to.
➢ The next group of digits indicates the size of the file in bytes.
➢ The next group of characters and digits indicates the date and time when the file was
created.
➢ The last group of characters indicates the name of the file.

Each file or directory has associated access rights as below:


➢ Access rights on files:
1) r (or -), indicates read permission (or otherwise), that is, the presence or absence of
permission to read and copy the file
2) w (or -), indicates write permission (or otherwise), that is, the permission (or otherwise)
to change a file
3) x (or -), indicates execution permission (or otherwise), that is, the permission to execute
a file, where appropriate

➢ Access rights on directories:


1) r allows users to list files in the directory;
2) w means that users may delete files from the directory or move files into it;
3) x means the right to access files in the directory. This implies that you may read files in
the directory provided you have read permission on the individual files.

➢ 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

➢ Also, octal digits can be used in the format below:

Decimal / Octal format Symbol Permission


0 (000) --- no access
1(001) --x execute only
2(010) -w- write access only
3(011) -wx write and execute
4(100) r-- read only
5(101) r-x read and execute
6(110) rw- read and write
7(111) rwx read, write and execute (full access)

Illustration for chmod:

7.3: Users and Groups


➢ In Linux everything is associated to a user and a group. Based on these values, the system
figures out, who can access what part of the system. That includes files, directories, network
ports etc.

➢ 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

➢ Meaning of each field or entry:


1) Username : the username (kjv)
2) Password : the password of the user (encrypted form – x)
3) uid : Numeric user id (1000)
4) gid : Numeric group id of user (1000)
5) gecos : arbitary field (kjv,,,)
6) /home/dirname : Home directory of the user (/home/kjv)
7) Shell : Which shell to use for the user (/bin/bash)

/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.

Illustration for shadow file and group file:


7.4 Becoming superuser or root user (su and sudo)
➢ The root user is also known as the superuser of the system. This user has all privileges to
change anything on the system, and it is the administrator account of any Linux system.

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:

7.5 Adding a new user (adduser or useradd)


➢ Two commands can be used, useradd or adduser.
➢ For security purposes, both commands must be executed as root, otherwise any user can
add random user accounts in the system.

Illustration for adduser:


Illustration to confirm new user (kjv1) added to /etc/passwd file: 3 users on the system (root, kjv, kjv1)

We can also use the command “ls /home” to view the users created on the system:

7.6 Switch users (su)


Use the su <user> command to log into another user’s account.
Use the exit command to log out of another user’s account.
➢ Logging in from the root user requires no password authentication for the log in.
➢ Logging in from any other user requires password authentication for the log in.

7.6 Changing user passwords (passwd)


The passwd command helps to change any user password, and must be run as root user.
7.7 Modifying existing user access to the system (passwd or usermod)
The passwd or usermod commands can be used to modify an existing user’s access to the
system, without deleting the user account. Both commands must be run as root user.
➢ lock user account in the system - sudo usermod -L <user> or sudo passwd -L <user>
➢ Unlock the user account in the system - sudo usermod -U <user> or sudo passwd -U <user>

7.8 Deleting a user (userdel or deluser)


➢ Two commands can be used, userdel or deluser.
➢ For security purposes, both commands must be executed as root, otherwise any user can
randomly delete other user accounts in 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 confirm new user (kjv2) deleted from /etc/passwd file:


7.9 Creating a group (groupadd or addgroup)
➢ Two commands can be used, groupadd or addgroup.
➢ For security purposes, both commands must be executed as root, otherwise any user can
create groups in the system.

/etc/group file: contains group information for each account.


/etc/gshadow file: contains group information in relation to secure password information.

7.10 More commands about groups (run as root user)


➢ Adding a user to a group - we can use sudo addsuer <user> <group>
➢ To know the groups for a user – we use groups <user1> <user2>
➢ To know the groups for the logged in user – we type groups
➢ To delete a group – we can use delgroup <group>

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).

A process may be in the foreground, in the background, or be suspended. In general, the


shell does not return the prompt until the current process has finished executing. To
examine the list of processes, we can use the command ps (process status).

To background a process, type an & at the end of the command line.


➢ For example, the command sleep 60 returns the prompt after 60 seconds.
➢ To run the command sleep 60 in the background, type sleep 60 &
➢ The & runs the job in the background and returns the prompt straight away, allowing you to
run other programs concurrently.
➢ The user is be notified of a job number (numbered from 1) enclosed in square brackets,
together with a PID (process identifier) and is notified when a background process is
finished. Backgrounding is useful for jobs which will take a long time to complete.

To Suspend and Background a process use the steps below:


1) Suspend the process - use crtl + z
2) Background the process – type bg

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 restart (foreground) a suspended process, type fg % <jobnumber>


➢ For example, to restart sleep 60 above, type fg %1
➢ Typing fg with no job number foregrounds the last suspended process.

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

8.1 Processes Scheduling


➢ Scheduling enables delayed execution or repetitive execution of processes.

We can schedule using three commands or utilities:


1) sleep – to delay execution
2) at – execute a command at a specified system time. This tool is installed and is more
suitable for one-time task scheduling.
3) cron – best for regular jobs (repetitive tasks). For most distros, this is the default built -in
task scheduler.

Using sleep command:


1) sleep 60; cat /home/kjv/work/testall : to delay display of file “testall” by 60 seconds
2) sleep 60s; cat /home/kjv/work/testall : to delay display of file “testall” by 60 seconds
3) sleep 60h; cat /home/kjv/work/testall : to delay display of file “testall” by 60 hours
4) sleep 60m; cat /home/kjv/work/testall : to delay display of file “testall” by 60 minutes

Using the at tool or command:


➢ at is a task scheduling program that runs in the background as a process (daemon). The tool
offers the ability to run a command/script at a specific time or at a fixed interval, but once.

➢ The at tool doesn't come pre-installed in most of the Linux distros.


1) To install, run the appropriate command according to your distro. For Debian/Ubuntu
and derivatives, run the command Sudo apt install -y at.
2) After installation is complete, enable the at tool (daemon) with the command sudo
systemctl enable --now atd.service

➢ 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.

Using the cron tool:


➢ cron is a task scheduling program that runs in the background as a process (daemon). It can
be used to periodically run certain jobs that are scheduled using its predefined set of rules.

➢ cron comes preinstalled on most Linux distributions or available in the repositories to be


installed manually. On Ubuntu distro, cron comes with a set of pre-defined schedulers. To
view these, run the command ls /etc/cron.*
➢ The desired job scripts can be placed in one of the above folders to run automated tasks
hourly, daily, monthly or weekly.

➢ 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.

Illustration for crontab file opened in nano editor:


➢ Below is the breakdown of the crontab file entries:

$ <minute> <hours> <day_of_month> <month> <day_of_week> <command_to_run>

➢ 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)

➢ To list all crontab entries, run the command: crontab -l

➢ 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.

Key notes about creating scripts:


➢ A shell script can be created a cat command, or a text editor.
➢ A shell script should be saved with the extension .sh
➢ The file should begin with the she-bang line (#!) to indicate which shell interpreter to use for
the shell script. For environments that support bash, we use #!/bin/bash.
➢ Shell scripts should have comments to increase readability. We comment such lines by
starting with the character #.
➢ Using user defined Variables in Shell Scripts - Scripts can include user-defined variables that
are clearly defined and that have self-descriptive names. Variables defined are accessed
using $ (dollar) sign symbol and there should be no space in the line where variables are
being assigned a value.
➢ Using user defined functions in Shell Scripts – define user functions by function name
followed by empty parentheses () and followed by curly braces to enclose the statements to
be executed { statements to execute}, that is, function_name () {}
➢ To run the file in read only mode – use the command bash <filename>
➢ To run the file in execution mode – use the command ./<filename> . The script file
permissions must be set to executable. We can use the chmod command to modify the
permissions.

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

Some basic tools and commands to monitor the system include:


➢ System stats - we can use sudo vmstat
➢ Network stats - sudo ip
➢ Network stats (tool to install) - sudo ifconfig

You might also like