Linux Notes Selected Pages
Linux Notes Selected Pages
Each block will contain data of only one file, although a file may
reside in many blocks across the file system.
Thus, the smallest disk space that can be allocated to a file is one
block.
File Types
Text Files: Normally contain configuration information.
Directory Files: Files that serve as storage space to organise other
files.
Special Device Files: Important files for systems administrators as
they represent various system devices, e.g., hard drives and serial
ports.
Binary Data Files: Program files are normally associated with
several binary data files, which store information such as functions
and graphics.
Linked Files: Files that are associated with one another, i.e., they
point to another file (shortcut file), or they can represent the same
data.
Executable program files: These exist on the hard drive until they
are executed in memory to become processes.
Named Pipes: Identify the channel that passes information from
one process in memory to another.
Socket Files: A type of named pipe file that allows a process on
another computer to write to a file on the local computer while
another process reads from that file.
File Names
• Linux file names usually consist of alphanumeric characters
underscores ( _ ), dashes or hyphens ( - ) and full stop characters
( . ).
• Files that begin with a dot ( . ) are called hidden files.
• The command-line prompt in a Linux terminal ( $, # ) or to start a
file name with a hyphen.
Directory Files
• Directory Files: a special file that contains a list of the files, paired
with those files’ inode numbers.
• Subdirectory: a directory that is found in another directory.
Linux Files
Terminal Emulators
• When using a graphical user interface, another program is needed.
System Identification
Where Linux is being accessed via a network, the network
administrator assigns each user a unique identification number
associated with the user’s login name. Users are also allocated to a
group (or groups). These groups usually represent a set of users
with similar needs, such as members of a department or a team
working on a particular project.
The User ID (UID) and Group ID (GID) are used to enforce file
ownership, permissions, and other security measures within the
Linux system.
Skill Development
• Builds foundational skills essential for advanced system
administration and programming.
• Opens opportunities for a deeper understanding of the operating
system and its internals.
directory.
• It performs a series of tests on the file(s) specified to
report a classification.
• How the command is used: file <filename>
cat • To view the contents of a file.
• Is a quick command to display the contents of a file
on the screen.
• Does not allow you to move backwards and forwards
in the file and, if the contents cannot fit onto one
screen, the display will scroll to the end of the file.
• How the command is used: cat f<filename>
more • To view the contents of a file in controlled amounts.
• How the command is used: more <filename>
less • To view the contents of the file and be able to move
around in the file.
• The less command is like the more command but
allows greater flexibility in moving backwards and
forwards in a file.
• How the command is used: less <filename>
head • To display the first ten lines of a specified file.
• To display the first n lines of a specified file: head –n
<filename>
• How the command is used: head <filename>
tail • To display the last ten lines of a specified file.
• To display the last n lines of a specified file: tail –n
<filename>
• To display the end of a file from the nth line: tail +n
<filename>
• How the command is used: tail <filename>
wc • To count the number of bytes, lines, words and
characters in a specified file.
• How the command is used: wc [options]
<filename>
sort • To sort the contents of a file into alphabetical or
numerical order:
• Outputs the sorted file to the screen but does not
modify the original file.
• Uses each new line as a new record.
• Will sort according to alphabetical or numerical order,
depending on file content.
• How the command is used: sort <filename>
grep • To search files for a specific pattern or text:
• Is a quick way to search a file for a string pattern.
• Allows you to search all files for a specific text string.
• Requires the search phrase to be placed in single
quotes.
• Displays the line of text containing the specified text
string.
• Will not find text patterns spread over more than one
line.
• How the command is used: grep [options]
<filename>
diff • To display the difference between two text files on a
line-by-line basis.
• Displays output that shows how you would need to
change the first file so that it resembles the second
file.
• Works best when there are few differences between
the two specified files.
• –b: to ignore whitespace characters.
• How the command is used: diff [options]
<filename>
find • The find command is used to locate one or more files
that satisfy a condition in the paths that you specify
and can perform actions on the file that has been
found (such as printing the pathname).
• Options that can be used with the find command:
• How the command is used: find <pathname-
list> <condition-list> <action-list>
chmod • Change the mode bits of a file (permissions).
• The same applies for directories.
• How the command is used: <num code>
<filename>
chown • Changes the ownership of a file.
• How the command is used: <newowner>
<filename>
chgrp • Changes the group ownership of a file.
• How the command is used: <newgroup>
<filename>
umask • To change default file permissions.
• How the command is used: umask <newmask>
nano • Allows you the edit the file.
• How the command is used: nano <filename>
vim •
rmdir • Removing a directory.
• How the command is used: rmdir
<directoryname>
whoami • Shows the username.
• How the command is used: whoami
sudo • A new user is added.
adduser • A new password is required.
su • The person allows the jump between different users.
sudo apt • Install anything.
install • How the command is used: sudo apt install
<itemtobeinstalled>
sudo apt • The user can update their user/terminal.
update • How the command is used: sudo apt update.
wc Command
• -c: bytes count • -w: word count
• -m: character count • -l: line count
grep command
Available Options:
• -c: Displays only the number of matching lines in each file.
• -l: Lists only the name of the file that contains the specified pattern.
• -n: Displays the line number of the line containing the specified
text.
• -i: Does not distinguish between uppercase and lowercase.
• -o: Print only the characters matching the regular expression.
find Command
• Pathname-list specifies the paths to search.
• Condition list could be any of the following:
o -name: the file name of the specific file to be found.
o -perm ###: where ### represents the octal file
permissions.
o -type x: where x is one of the following types: d (directory
file), f (ordinary file), c (character device file), b (block device
file).
o -user <username> – for files with a specific owner or UID.
o -group <group_name> : for files belonging to a specific
group or GID.
o -size n: to find files of size n blocks.
o -links n to find files with n links.
o -atime n: to find files that were accessed n days ago.
o -mtime n: to find files that were modified n days ago.
o -ctime n: to find files that were created n days ago.
• Action list – specifies the action that should be carried out on the
files that meet the criteria (as per the conditions list):
o –print: to display files to the screen.
o -exec command: to execute a command.
o -ok command: like exec, except that you are queried first.
apt update
Updates the local package index with the latest information about
available packages and their versions from the repositories configured in
the system.
Traceroute
• Traceroute: a network diagnostic tool used to track the path that
data packets take from a source computer to a destination host
across an IP network.
• By sending packets with gradually increasing time-to-live (TTL)
values, `traceroute` identifies each hop (router or gateway) along
the path and measures the time it takes for each hop.
• This helps in identifying network bottlenecks, understanding routing
paths, and diagnosing connectivity issues.
• The output includes a list of all the intermediate routers, the round-
trip time to each router, and the IP address or domain name of each
hop, providing a detailed map of the route taken by the packets to
reach the target.
wget
• wget is a command-line utility for downloading files from the web.
• It supports HTTP, HTTPS, and FTP protocols and is particularly useful
for downloading files or websites for offline viewing.
vi Text Editor
vi is a powerful and widely used text editor available in most Unix-like
operating systems.
It has three main modes:
• Command mode: Default mode for navigating and executing
commands.
• Insert mode: Mode for inserting and editing text.
• Visual mode: Mode for selecting and manipulating text visually.
Troubleshooting Scenarios
• Slow Performance: Check system resources using utilities like
htop.
• Network Connectivity Issues: Use tools like ping and traceroute
to diagnose.
File Security
Command Mode File Ownership
• The first level of implementing data security is file ownership.
• Each file in a Linux system has an owner that corresponds with one
of the authorized users.
• When a user creates a file, it is marked as being owned by that
user.
• Only the creator of a file or a superuser (root privileges, i.e.,
sudoers) can change the ownership of a file.
• Once the creator of a file changes ownership of the file, that user no
longer owns it and can regain ownership only if the new owner or
superuser returns ownership.
Permission Categories
• File permissions control access to an ordinary (Text) file or directory
file.
• These permissions specify who can use a file or directory and how.
Classes of the Ownership
• U: user who owns the file.
• G: group to which the user belongs.
• O: other system users.
Permission Categories
Symbol Category Ordinary
r Read Can view the contents
of the file.
w Write Can change the file’s
contents.
x Execute/Search Can run a program file.
Permission Categories
• If you give one user in any group the Write permission (i.e., the
ability to change a file), then you give that permission to everyone
else in the group too.
umask Command
• It essentially controls the permission bits that will be unset for new
files and directories.
• The umask value specifies which permission bits should be masked
(turned off) when new files or directories are created.
• It is a four-digit octal number where each digit represents different
sets of permissions:
o First digit: Special permissions (setuid, setgid, sticky bit).
o Second digit: Owner (User) permissions.
o Third digit: Group permissions.
o Fourth digit: Others' permissions.
New Directories:
- Default permission: 777 (rwxrwxrwx).
- umask 0002 masks the last digit: 777 - 002 = 775.
- Resulting permission: 775 (rwxrwxr-x).
Pipelines
Are used to redirect the flow of data between processes, giving
greater flexibility in the use of the Linux system.
Save time and disk space because intermediate data does not need
to be stored in a temporary file.
The vertical bar ( ¦ or | ) symbol is used to denote a pipe.
To set up a pipe in Linux:
Type command1 ¦ command2
Examples:
$ ls – l , select the $ ls –l | grep file1
filename to use . $ ls –l | grep file1 | wc
$ ls – l | wc $ ls –l | grep file1 | wc –l
$ ls –l | wc –l
Filters
Commands that take their input from the stdin and send their
output to the stdout are called filter programs or filters.
A defining feature of filters is that they are commands or programs
that can be used Examples: cat, head, tail,
anywhere in a wc,sort,cut and grep
pipeline “| ” and
they generally perform useful transformations on the data that
passes through them.
The tr (translate) command is another filter command.
It is used to:
o Convert one character into another character.
o Compress a group of characters into a single character.
o Delete specific characters.
To use the translate command:
Type tr [options] string1 [string2] and press <Enter>.
Example :
o $ ls – l
o $ ls – l | tee testingtee.file
o cat testingtee.file
What you will notice is that although the output was displayed on
the screen, it was simultaneously directed to the file testingtee.file
The data sent to the file is not altered in any way; it is merely sent
in two different directions
To check data after the who command and write it to a file
called sample:
The type ls -l ¦ tee filename.txt ¦ sort and press <Enter>.
Sort the filename.txt file, write the sorted list to the check file
with tee, and use pipelining to append a count of how many
unknown entries there are to the check file.
Get the date using the date command and append the output of
the command to the file check using a tee, then display a count of
the number of words in the data output.
View the check file using the cat command.
Note that the check contains the sorted filename.txt file as well
as the date on the last line.
Applets
Applets are icons that reside in your panels for launching specific
applications and sometimes offering additional functionality.
Applets in Linux is a built-in functionality feature.
There are already some built-in Applets in the taskbar, like a clock,
network manager, and volume, and you can choose
any Applet of your choice from the Applets application.
There are many available not only in the default Ubuntu installation,
but some extras, also.
One of the Applets you can install is Cinnamon on Ubuntu 20.04
o $ sudo apt update && sudo apt update – y
o $sudo apt install cinnamon –y / sudo apt-get install cinnamon
–y
o $sudo reboot
GNOME
What is GNOME?
GNOME, which stands for GNU Network Object Model Environment,
is a free and open-source desktop environment for Unix-like
operating systems. It provides an intuitive and attractive graphical
user interface for users, along with a suite of applications and tools
for everyday tasks.
Applications
GNOME comes with a range of built-in applications, such as the
Nautilus file manager, the GNOME Terminal, the Gedit text editor,
and the GNOME Software app for managing software installations.
These applications are designed to integrate seamlessly with the
GNOME desktop environment.
Customization
While GNOME aims for simplicity, it also allows for extensive
customization through extensions and settings.
2. Install GNOME Shell Integration Add-on for Web Browser. For Firefox
and Chrome install the GNOME Shell Integration.
2. After Installation:
You can launch GNOME Tweaks by searching for "Tweaks" or
"GNOME Tweaks" in your applications menu.
Use GNOME Tweaks to customize various aspects of your
GNOME desktop environment, including appearance,
extensions, fonts, and more.
By following these steps, you should be able to install and use
GNOME Tweaks on your Linux system with GNOME.
System Firewall
What is a Firewall?
A firewall is a facility that prevents unauthorized access to or from a
private network or a computer.
It is a network security device that monitors and controls incoming
and outgoing network traffic based on predetermined security rules.
Acts as a barrier between a trusted network and an untrusted
network (e.g., the Internet).
Types of Firewalls
Hardware Firewalls: Physical devices that provide a barrier at the
network perimeter.
Software Firewalls: Programs installed on individual computers to
protect them.
Network Firewalls: Often a combination of hardware and software
firewalls.
Importance of Firewalls
Protects against unauthorized access.
Helps prevent cyber-attacks.
Controls and monitors network traffic.
Provides logging and reporting on network traffic.
Tracks system events, which you can analyze to set more suitable
rules.
Deny Rules
Block certain types of traffic.
Example: blocking all incoming traffic by default
IP Address Blocking
apt command
Run: apt list –-installed | grep ufw
The first part of the command lists all installed packages.
The second part filters the output to show lines that contain ufw
dpkg command
Run: dpkg –l | grep ufw
The first part of the command lists all installed packages
The second part filters the output to show lines that contain ufw
2. Enable ufw.
sudo ufw enable – to activate ufw once it has been enabled.
Once enabled – ufw starts enforcing the firewall rules.
Port Numbers
Port Numbers: Introduction
They are numerical identifiers, used to differentiate types of
network traffic.
Help direct data to the correct application or service on a
computer.
Each port number corresponds to a specific process or service.
Common Ports
Port 22 - Secure Shell (SSH)
Used for secure remote logins and command execution.
Used to log into a remote machine and execute commands securely.
Provides a secure channel over an unsecured network by using
encryption.
Verbose View
Provides detailed information about the firewall status, including
logging status, default policies, and more detailed rules.
Command: sudo ufw status verbose.
Numbered View
Lists the active firewall rules with numbers. This is useful for
managing the rules, such as deleting specific rules by their number.
Command: sudo ufw status numbered
Process Control
Viewing Processes
The ps command lists currently running processes.
To view all the processes with ps, use ps -ef.
For example, ps -aux provides a detailed view.
Killing Processes
The kill command can terminate processes.
For example, kill -9 PID forcefully kills the process with the specified
PID.
Job Control
Commands like bg, fg, and jobs manage processes within a shell.
Foreground Processes
These run in the foreground and are typically initiated by the user
from the terminal.
The user interacts directly with these processes.
Foreground processes are processes that run directly under the
control of the user.
They are initiated and interact with the user via the terminal or
graphical interface.
The terminal remains occupied by the process until it is completed.
User inputs are directly sent to the process.
Examples:
• Running a Text Editor:
• vi week7.txt
• The terminal is occupied by vi and user input is
directed to editing week7.txt.
• Executing a Command:
• ls -la
• The ls command runs in the foreground and the
terminal displays the output of the directory listing.
Background Processes
These run in the background, without user interaction.
They often perform tasks like system monitoring or handling
network requests.
Types of Processes
Running: current process that is being executed in the operating
system.
Waiting: a process which is waiting for system resources to run.
Stopped: a process that is not running.
Zombie: process whose parent processes have ended, but the child
process is still in the process table.
Terminal Processes
Hangup (SIGHUP) Signal
When a user logs off, the system sends a hangup signal (SIGHUP) to
all processes related to that user, terminating them.
This signal can be redirected or ignored using the nohup command
to prevent unintended termination.
Handling Processes
• Check process status using the process status commands:
• ps -ef or ps aux
• The process status commands can be combined with grep to
filter out the process you want to check
• Terminate a process by its PID using the kill command followed by
the PID.
• Example: kill PID