Unit V
Unit V
Managing Files and Directories : create and edit text files, search for files, perform operations on
files and directories, process text files, manipulate file output, explore the linux kernel, install and
configure kernel modules, monitor kernel modules
Managing the Linux Boot Process : configure linux boot components, configure GRUB2
Managing Kernel Modules : explore the linux kernel, install and configure kernel modules, monitor
kernel modules
Create and Edit Text Files
• TEXT EDITORS: A text editor is an application that enables you
to view, create, or modify the contents of text files.
• Vim
• Nano
vim
• There will also be times when you don't know the exact location
of files created by the system, applications, or other users.
THE locate COMMAND
• The locate command performs a quick search for any
specified string in file names and paths stored in the mlocate
database.
• If locate command is not working, then needs to update the db used
by locate command.
• sudo updatedb
locate COMMAND OPTIONS
THE find COMMAND
• The find command enables you to search a specific location
for files and directories that adhere to some search criteria.
• These commands are useful when you only need to see the beginning
or the end of a file.
head
• displays the first 10 lines
tail
• displays the last 10 lines
THE cp COMMAND
• The cp command enables you to copy and then paste a file or
directory.
• The initial object is left where it is, but an exact duplicate of that
object is created at the destination you specify.
• When you copy directories, you must specify the -R option to
copy the specified directory recursively.
THE mv COMMAND
• The mv command moves files and directories to other
locations.
• The rmdir directory is used to remove directories, but only those that
are empty (i.e., contain no files or subdirectories).
• In order to delete a with actual contents, you must use the rm -R
command.
Process Text Files
• THE echo COMMAND
The echo command is used to display a line of text on the
terminal.
• You can also use the echo command to write text to a file by
providing the string after the echo command and redirecting to
the file.
• The echo command can overwrite the content in the file by
redirecting and not appending.
• You can supply various format characters within the text you
want to output, using a backslash (\) to indicate when they are
being used.
THE tr COMMAND
• The tr command is used to translate a string of characters.
• 1c1 means that line 1 in the first file needs to be changed to match line
number 1 in the second file.
THE diff COMMAND
THE diff COMMAND
THE diff COMMAND
THE diff COMMAND
THE diff COMMAND
THE diff COMMAND
THE diff COMMAND
THE diff COMMAND
• The less-than symbol (<) with a line after it means that line should be removed
from the first file because it doesn’t appear in the second. The greater than
symbol (>) with a line after it means that line should be added from the second
file.
• d stands for deletion, a stands for adding and c stands for changing. the number
on the left of the character is the line number in file1.txt, the number on the
right is the line number in file2.txt.
THE awk COMMAND
• The awk command performs pattern matching on files.
THE sed COMMAND
• The sed or stream editor command is a program that you can
use to modify text files according to various parameters. The
sed command can also be used for global search and replace
actions.
• Hard and symbolic links are a feature of the file system and are common in most file
systems supported by Linux. The ext2, ext3, ext4, and XFS file systems all support hard
and symbolic links.
• A hard link is a reference to another file; it enables the file's data to have more than
one name in different locations in the same file system. Applications treat a hard link
as a real file. If the original file is deleted after a hard link is created, all its contents will
still be available in the linked file. This is because the inode of a hard link is the same as
its target; in other words, it points to the same object on the file system. Hard links
cannot be created between two directories, nor can they be created between two files
in different file systems.
• A symbolic link is a reference to a file or directory that can span
multiple file systems.
If the original file or directory is deleted after a symbolic link is
created, then the original content is lost.
• This is because the inode of a symbolic link is different than its
target; in other words, it points to a different object on the file
system.
• A symbolic link is also known as a soft link.
• A link in UNIX is a pointer to a file.
• Like pointers in any programming languages, links in UNIX are pointers
pointing to a file or a directory.
• Creating links is a kind of shortcuts to access a file.
• Links allow more than one file name to refer to the same file,
elsewhere.
• There are two types of links :
1.Soft Link or Symbolic links
2.Hard Links
• These links behave differently when the source of the link (what is
being linked to) is moved or removed.
• Symbolic links are not updated (they merely contain a string which is the path
name of its target);
• hard links always refer to the source, even if moved or removed.
• If we create a hard link to the file and then delete the file, we can
still access the file using hard link.
• But if we create a soft link of the file and then delete the file, we can’t
access the file through soft link and soft link becomes dangling.
• This naming convention might be confusing for some people, so it’s
probably helpful to remind ourselves that ln is like cp and mv: the
source (A) needs to come first, followed by the destination (B).
• Or we can just say it naturally: We are creating a symbolic link to A,
and we want to call it B.
Soft Link
Manipulate File Output
• TEXT STREAMS: A text stream is a sequence of one or more
lines of text that applications can leverage to read from or write
to a particular device or system component.
• Standard input, or stdin, is a text stream that acts as the source
for command input.
Standard input for the Linux command-line is usually generated
from the keyboard. In
the case of the GUI, the standard input can also come from the
mouse.
• Standard output, or stdout, is a text stream that acts as the
destination for command
output. By default, standard output from a Linux command is
directed to the CLI.
• REDIRECTION OPERATORS
There are several operators that are used to redirect input or
output.
PIPING
• Piping is the process of combining the standard I/O streams of commands. It
uses the
standard output of one command as the standard input for another
command. The
output format of the first command should be compatible with the format that
the
second command works with. The pipe operator (|) can be used with most
commands
in Linux.
• Pipes are a mechanism by which the output of one program can be sent as the input
to another program. Individual programs can be chained together to become
extremely powerful tools.
• Redirection is a Linux feature. Inputs and outputs of a command can be
redirected to another command or a file using redirection operators.
• Linux programmers use:
• 1) Input redirection operator to redirect the input given
• 2) Output redirection operator to redirect the output.
• A less-than sign (<) represents input redirection. On the other hand, a
greater than sign (>) is used for the output redirection. “<” and “>” are
also called angled brackets.
• But what’s the need for using “>” and “>>” both to redirect the output?
Let’s find the reason for using both of these as output redirects.
Input redirection operator to redirect the
input given
• The “>” Operator
• “>” overwrites an already existing file or a new file is created
providing the mentioned file name isn’t there in the directory. This
means that while making changes in a file you need to overwrite
certain any existing data, use the “>” operator.
• This sign is used to redirect the output to something else than stdout.
Example 1:
• echo “Welcome to Linux” > my_file_1.txt
• After executing the above command, you’ll find that a text file “my_file_1.txt” is created in the
directory. It’ll contain the text “Welcome to Linux” in it.
• To check whether the file has been created successfully or not, type:
• ls
• The following command helps read the file type.
• cat my_file_1.txt
• Now, let’s execute the same command with a different text.
• echo “Learn latest tips about Linux” > my_file_1.txt
• And type the command below to read the file.
• cat my_file_1.txt
• You’ll see that the new text has successfully overwritten the earlier text.
Example 2:
• ls > allmyfiles.txt
• This is another example using the “>” operator. The above command
creates the file called “allmyfiles.txt”. And fills it up with the directory
listing given by the “ls” command.
Example 3:
• > newzerobytefile
• Here a zero byte file is created having the name “newzerobytefile”.
• Alternatively, this command can overwrite a file that is already
present with the same name. And it’ll make the file zero bytes in size.
• The “>>” Operator
• “>>” operator appends an already present file or creates a new file if
that file name doesn’t exist in the directory.
Example 1:
• echo “Welcome to Linux” >> my_file_2.txt
• The above command will create a file by the name “my_file_2.txt” in your current directory.
• Once the command is executed, type:
• ls
• This will verify if the file has been created successfully.
• Read the file by:
• cat my_file_2.txt
• Let’s alter the text, now, into:
• echo “Learn latest tips about Linux” >> my_file_2.txt
• Since you’re using a file that was created previously, to check all the modifications made by “>>”, run the
following command:
• cat my_file_2.txt
• And you’ll see instead of overwriting the previously entered text, the “>>” operator has appended the
text.
Example 2:
• echo "End of directory listing" >> allmyfiles.txt
• The above command will add “End of directory listing” at the end of a
file called “allmyfiles.txt”
• file descriptors are 0, 1 and 2. 0 corresponds to STDIN, 1 to STDOUT,
and 2 to STDERR.
• File descriptor 1 is stdout and File descriptor 2 is stderr.
• Using > to redirect output is the same as using 1>. This says to
redirect stdout (file descriptor 1).
• Normally, we redirect to a file. However, we can use >& to redirect
to stdout (file descriptor 1) or stderr (file descriptor 2) instead.
• Therefore, to redirect stdout (file descriptor 1) to stderr (file
descriptor 2), you can use >&2.
• & means both standard output (1>) and standard error(2>).
• snap • f6.txt
• jcameron-key.asc
• packages.microsoft.gpg
• rough.txt
• snap
Another example:
One more example:
Display the top 5 files/directories according to their size
in the current working directory
THE xargs COMMAND (extended
arguments)
• The xargs command reads from standard input and executes
a command for each
argument provided.
• System Call
Interface (SCI): Handles system calls sent from user
applications to the kernel.
• Process
management: Handles different processes by allocating
separate execution
space on the processor and ensuring that the running of one
process does not interfere with other processes.
Memory Management
• Manages the computer's memory, which is one of the complex
tasks performed by the kernel. Like processor sharing, the
system's memory also needs to be shared among different user
space resources.
The kernel maps or allocates the available memory to
applications or programs on request and frees the memory
automatically when the execution of the programs is complete,
so that it can be allocated to other programs.
• File system
management:
Manages the filesystem, which involves storing, organizing, and
tracking files and data on a computer.
• Device
management: Manages devices by controlling device access
and interfacing
between user applications and hardware devices of the
computer.
Install and Configure Kernel Modules
• A kernel module is a system-level object that extends the functionality of the kernel.
It
can be dynamically loaded into the kernel or unloaded from the kernel when required.
It enables the kernel to update or recompile itself without requiring the system to
reboot.
• In this, you'll verify that the modules you installed were actually
loaded into the kernel, and that any configurations you made
were implemented properly.
THE /proc/ DIRECTORY
• The /proc/ directory is a virtual file system (VFS) that
provides significant information about the kernel's running
process.
THE /proc/version FILE
• The /proc/version file specifies several points of
information about the Linux kernel:
10. The user enters a user name and password to log in to the system.
• 11. The system authenticates the user. If the user is valid, then various profile files are
executed.
•
12. The shell is started and the system is ready for the user to work on.
KERNEL PANIC
Kernel panic is a mechanism by which the system detects there
has been a fatal error and responds to it. A fatal error typically
results in the system becoming unstable or
totally unusable. Software that handles kernel panics will display
an error message to the user and dump the current state of
kernel memory to a storage device for later debugging.
Depending on how the system is configured, the panic handler
will either reboot the system automatically, or wait for the user to
do so.
Configure GRUB 2
• GNU GRUB:
The GNU GRand Unified Bootloader (GNU GRUB) is a boot
loader developed by the
GNU Project that became popular on Unix-like systems. It
enables users to choose
which operating system or kernel version to boot in a multi-
platform environment.
GRUB 2 IMPROVEMENTS
• GRUB 2 is more than simply a newer version of GRUB; it is a
complete redesign and
rewrite of the GRUB system. GRUB 2 offers administrators
more control over the boot
process, boot devices, and boot behavior.
GRUB 2 INSTALLATION
• The grub2-install command is used to install the GRUB 2
boot loader on a
storage device. It copies GRUB 2 files into the /boot/grub2
directory and, on some
platforms, installs GRUB 2 into the boot sector. However,
grub2-install applies
to BIOS systems, not UEFI. To install GRUB 2 on a UEFI
system, use a package manager
to install the grub2-efi package. Installing this package will
copy GRUB 2 files onto
the EFI system partition (ESP) in the /boot/efi directory.
THE grub.cfg FILE
• The grub.cfg file is the main configuration file for the GRUB
2 boot loader. On BIOS
systems, it is located in the /boot/grub2/ directory. On UEFI
systems, it is located
in the /boot/efi/EFI/<distro>/ directory.
THE /etc/grub.d/ DIRECTORY
• The /etc/grub.d/ directory contains scripts that are used to
build the main
grub.cfg file.
THE /etc/default/grub FILE
• The /etc/default/grub file contains GRUB 2 display menu
settings that are
read by the /etc/grub.d/ scripts and built into the
grub.cfg file. It enables you to change options such as how
many seconds GRUB 2 will wait before
automatically selecting the default boot option.
THE grub2-mkconfig COMMAND
• The grub2-mkconfig command generates a new grub.cfg
configuration file,
and is used to update an existing grub.cfg file.
Here is a reminder of what these commands
do:
• sudo <command>: executes a command with administrator rights
• ls <directory>: lists the files in a directory
• mv <old_name> <new_name>: moves or renames a file from the old
name to the new name
• tail <file>: shows the last lines of a file
• cat <file>: prints the whole contents of a file
• grep <pattern> <file>: filters the text of a file according to the pattern
• less <file>: lets you browse a file
• We can combine these commands using the | sign. For example:
will first print the output of /var/log/syslog, then keep only the lines that say
"error" and then the last 10 lines of that output.
NOTE: Remember that we can always read the manual page using man
<command_name> to learn more about a command.
Listing system services
• Let's look at the services that are installed in the machine. In order to
do this, we will use the service command.
• If you run service with parameters --status-all, it lists the state of
services controlled by System V.
• If we are interested in seeing only the services that are running, you
can use the following command:
• Alright, now that we've listed the services let's practice stopping and
starting some of them. The first service that we are going to stop is
the rsyslog service. This service is in charge of writing content to the log files,
as in /var/log/syslog, /var/log/kern.log, /var/log/auth.log and others.
Processes that generate output will send that output to the rsyslog service and
the service will write it to the corresponding log files depending on how the
system was configured.
• Let's first start by checking the status of the service. We do this by using
the service command with the status action:
sudo service rsyslog status
• This is showing us a lot of information about the service: it's loaded
(which means that the OS has the information about the service in
memory), it's enabled (which means that it will start automatically on
boot), it's active and running. It also tells us where to find some
documentation about the service and more. Finally, it shows us the
last log lines that this service generated.
• We can see this service in action by using the logger command:
logger This is a test log entry
In the last line, we see that the rsyslog service has exited and is no longer
running.
• We can try sending text with our logger command again:
logger This is another test log entry
And see that it's running again. Let's try our logger command one more
time: logger This is another test log entry
• And then check that the contents of /var/log/syslog:
sudo tail /var/log/syslog
Fixing a failing service
• In order to list the state of services controlled by System V, you can use the
following command:
• sudo service --status-all
• or
• There's no cupsd.conf, but there is cupsd.conf.old. Apparently the
configuration file was deleted. Good thing we kept a copy! Let's move
that file so that cups can find it and start successfully:
• sudo ls -l /etc/cups
• Now that the file was renamed successfully, we can start cups:
• sudo service cups start
• And then check the status:
• sudo service cups status
Restarting services
• Let's go back to the cups service that we just fixed. The logs generated
by cups are written into the /var/log/cups directory. We can see the
contents of the directory using the ls command:
• sudo ls -l /var/log/cups
• The error log file isn't yet created. That's expected because by default
cups will only write warning or error messages into that file. If you
want cups to log debug messages into that file, you'll need to change
the LogLevel parameter in the configuration file. Let's do that.
• Let's edit /etc/cups/cupsd.conf using the nano editor.
• sudo nano /etc/cups/cupsd.conf
• In one of the first lines of the file you'll see there's a line that
says LogLevel warn. We want to replace warn with debug:
• Once you've done this, press "Ctrl-X" to exit the editor. It will ask you
if you want to save your changes, press "Y" for yes and then enter at
the filename prompt.
• If we now restart cups, the service will notice the change to its
configuration file. You could do this by using sudo service cups
stop and sudo service cups start, but there is a shortcut.
• sudo service cups restart
• Restarting the service command is a handy way of stopping a service
and then starting it immediately back up. And once we've done that,
we can see that there's now a lot of content
in /var/log/cups/error_log.
• sudo cat /var/log/cups/error_log
Reloading Services
• Finally, let's look at the reload action. Take this action when you want
a service to re-read its configuration without actually doing a full stop
and start.
• Let's return the cups log level back to its default. One more time, let's
edit /etc/cups/cupsd.conf using the nano editor.
•
sudo nano /etc/cups/cupsd.conf
• Let's change LogLevel debug, replacing debug with warn:
• Once you've done this, press "Ctrl-X" to exit the editor. It will ask you
if you want to save your changes, press "Y" for yes and then enter at
the filename prompt.
• Once we've done this, we can reload cups:
• sudo service cups reload
• If you check the status of the service, you'll see that it was not
restarted (it's been running since we last restarted it).
• sudo service cups status
• LDAP is the most popular and widely used directory access protocol
today.
• Active Directory is the LDAP implementation for ________________.
• Ubuntu
• Linux
• MAC
• Microsoft
• Active Directory is the LDAP implementation for ________________.
• Ubuntu
• Linux
• MAC
• Microsoft