Anand Work Book
Anand Work Book
Class
How To Install RHEL -124
• When people refer to "Linux," they typically mean the Linux kernel, which is the core
component of the Linux operating system.
• However, Linux is often used in conjunction with other open-source software to create
complete operating systems known as Linux distributions (distros).
• These distros typically include the Linux kernel along with various utilities, libraries, and
applications, all of which are open source.
• 1. View the Source Code: Users can access the source code to understand how the software works, how it is structured, and how it accomplishes its
tasks.
• 2.Modify the Software: Users have the freedom to modify the source code to add new features, fix bugs, optimize performance, or customize the
software to better suit their needs.
• 3.Distribute Copies: Users can distribute copies of the software, either in its original form or modified form, to others without restrictions.
• Open-source software is typically developed collaboratively in a transparent manner, often by a community of developers who contribute their time
and expertise voluntarily. This collaborative approach fosters innovation, encourages peer review, and promotes the sharing of knowledge.
• Open-source software is often distributed under licenses that ensure the software remains open and free to use, modify, and distribute. Some
common open-source licenses include the GNU General Public License (GPL), Apache License, MIT License, and BSD License.
• Examples of well-known open-source software include the Linux operating system, the Apache web server, the Mozilla Firefox web browser, the
LibreOffice productivity suite, and the WordPress content management system, among many others.
Red Hat Enterprise Linux
Ecosystem
• Red Hat Enterprise Linux (RHEL) is Red Hat's commercial production-grade Linux distribution.
• Red Hat develops and integrates open-source software into RHEL through a multistage
process.
• Red Hat sponsors and integrates open-source projects into the community-driven Fedora
distribution. Fedora provides a free working environment to serve as a development lab and
proving ground for features to be incorporated into CentOS Stream and RHEL products.
• Red Hat stabilizes the CentOS Stream software to be ready for long-term support and
standardization, and integrates it into RHEL, the production-ready distribution.
Red Hat Enterprise Linux Ecosystem
Red Hat Enterprise Linux (RHEL) is Red Hat's commercial production-grade Linux distribution.
Red Hat develops and integrates open-source software into RHEL through a multistage process.
Chapter 2 Access the Command
Line
• Access the Command Line
Example:
#ls is command
#-l is options
#Anand.txt is arguments
#ls –l Anand.txt
Log in to a Local System
• A terminal is a text-based interface
to enter commands into and print
output from a computer system.
• To run the shell, you must log in to
the computer on a terminal.
What is TTY in Linux?
• CTRL + ALT + F1 – In Red Hat Enterprise Linux 9, when the graphical environment is available, the login
Lockscreen screen typically runs on the first virtual console, which is tty1. This console hosts the
CTRL + ALT + F2 – graphical login manager, such as GDM (GNOME Display Manager) or another
Desktop Environment display manager depending on the desktop environment used.
CTRL + ALT + F3 –
TTY3 Additionally, there are typically five additional text-based login prompts available on
CTRL + ALT + F4 – virtual consoles tty2 through tty6. These consoles allow users to log in and access
TTY4 the system via a text-based interface, even if the graphical environment is not
CTRL + ALT + F5 – functioning or is not desired.
TTY5
CTRL + ALT + F6 – Users can switch between these virtual consoles using keyboard shortcuts such as
TTY6 Ctrl+Alt+F2 for tty2, Ctrl+Alt+F3 for tty3, and so on. This provides flexibility for users
who may prefer to work in a text-based environment or need to troubleshoot issues
with the graphical interface.
Log in to a Remote System
To log in to a remote system in Red Hat Enterprise Linux (RHEL), you typically use the SSH (Secure Shell) protocol.
Here's a basic guide on how to do it:
1. Open a Terminal: Open a terminal on your local machine. You can do this by searching for "Terminal" in the
applications menu or using the keyboard shortcut (often Ctrl+Alt+T).
2. SSH Command Syntax: The syntax for SSH is:
• ssh Anand@192.168.1.12
1. Replace username with your username on the remote system, and remote_host with the IP address or hostname
of the remote system.
2. Example: If your username on the remote system is user and the IP address of the remote system is
192.168.1.12 you would use the following command:
• ssh Aannd@192.168.1.12
1. Enter Password: After executing the SSH command, you'll be prompted to enter the password for the specified
user on the remote system. Type the password and press Enter. Note that as you type the password, no
characters will be displayed on the screen for security reasons.
2. Successful Login: If the username and password are correct, you'll be logged in to the remote system, and you'll
see a command prompt indicating that you're now working on the remote system.
3. Exiting: To exit the SSH session and return to your local system, simply type exit at the command prompt and
press Enter.
Log Out from a Remote System
• When you are finished with the shell and want to quit, you can choose one of several
ways to end the session. You can enter the exit command to terminate the current shell
session.
• Alternatively, finish a session by pressing Ctrl+D.
• Logout
• Exit
Access the Command Line with the Desktop
Introduction to the GNOME Desktop
Environment
• The desktop environment is the graphical user interface on a Linux system.
• GNOME 40 is the default desktop environment in Red Hat Enterprise Linux 9.
• It provides an integrated desktop for users and a unified development platform on top of a
graphical framework provided by either Wayland (by default) or the legacy X Window System.
Parts of the GNOME Shell
The elements of the GNOME Shell include the following parts, as shown in this
screen capture of the GNOME Shell in Activities overview mode:
How to Start a Terminal
• To get a shell prompt in GNOME, start a graphical terminal application such as GNOME
Terminal.
• Use one of the following methods to start a terminal:
• From the Activities overview, select Terminal from the dash, either in Favorites or with the
Show Applications button.
• Search for terminal in the search field at the top of the windows overview).
• Press the Alt+F2 key combination to open the Enter a Command and enter gnome-
• terminal.
1.Lock the Screen and Log Out
2.Lock the screen, or log out entirely, from the system menu on the far right of the top bar.
3.Power Off or Reboot the System
4.To shut down the system, from the system menu in the upper-right corner, select Power
Off/Log out > Power Off or press Ctrl+Alt+Del.
5.A window is displayed that offers the option to Cancel or confirm the Power Off action.
6. If you do not make a choice, then the system automatically shuts down after 60 seconds.
Execute Commands with the Bash
Shell
Basic Command Syntax
• The GNU Bourne-Again Shell (bash) is a program that interprets commands that the user types.
• Each string that is typed into the shell can have up to three parts:
• When you are ready to execute a command, press the Enter key.
• Type each command on a separate line.
• The command output is displayed before the following shell prompt appears.
Example:
Use the semicolon (;) as a command separator if you want to write many commands
on a single line. Semicolons belong to a group of letters known as metacharacters,
which have a unique meaning for the letter "bash." In this instance, the output of both
commands is shown prior to the subsequent shell prompt.
Combining two commands (command1 and command2) on the command line is
demonstrated in the following example.
Write Simple Commands
• The date command displays the current date and time.
• The superuser or a privileged user can also use the date command to set the system clock.
• Use the plus sign (+) as an argument to specify a format string for the date command.
Example:
View the Contents of Files
• The cat command is one of the most simple and frequently used commands in Linux.
• Use this command to create single or multiple files, view the contents of files,
concatenate the contents from various files, and redirect contents of the file to a
terminal or to files.
• Example:
The head and tail commands display the beginning and the end of a file, respectively.
By default, these commands display 10 lines of the file, but they both have a -n option to
specify a different number of lines.
• Example:
The head and tail commands display the beginning and the end of a file,
respectively. By default, these commands display 10 lines of the file, but they
both have a -n option to specify a different number of lines.
• Example:
• [root@node1 ~]# tail /etc/passwd
• dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
• dovenull:x:971:971:Dovecot - unauthorized user:/usr/libexec/dovecot:/sbin/nologin
• tcpdump:x:72:72::/:/sbin/nologin
• admin:x:1000:1000:Admin:/home/admin:/bin/bash
• U1:x:1001:1001::/home/U1:/bin/bash
• U2:x:1002:1004::/home/U2:/bin/bash
• U3:x:1003:1005::/home/U3:/bin/bash
• U4:x:1004:1006::/home/U4:/bin/bash
• U5:x:1005:1007::/home/U5:/bin/bash
• hello:x:1006:1008::/home/hello:/bin/bash
• [root@node1 ~]#
The wc command counts lines, words, and characters in a file. Use the -l, -w, or -c options to display only the
given number of lines, words, or characters, respectively.
• Example:
• [root@node1 ~]# wc /etc/passwd
• 58 136 3288 /etc/passwd
• [root@node1 ~]# wc -l /etc/passwd
• 58 /etc/passwd
• [root@node1 ~]# wc -w /etc/passwd
• 136 /etc/passwd
• [root@node1 ~]# wc -c /etc/passwd
• 3288 /etc/passwd
• [root@node1 ~]#
Understand Tab Completion
• With tab completion, users can quickly complete commands or file names after typing enough at
the prompt to make it unique. If the typed characters are not unique, then pressing the Tab key
twice displays all commands that begin with the typed characters.
• Example:
• Press Tab twice.
• Press Tab once.
Tab completion helps to complete file names when typing them as arguments to commands. Press
• Tab to complete as much of the file name as possible. Pressing Tab a second time causes the shell
• [user@host ~]$ pasTab+Tab
passwd paste pasuspender
• [user@host ~]$ passTab
[user@host ~]$ passwd
Changing password for user user. Current password:
Write a Long Command on Multiple Lines
• Commands with many options and arguments can quickly become long and are
automatically wrapped by the command window when the cursor reaches the right
margin. Instead, type a long command by using more than one line for easier reading.
• Example :
• head -n 3 \
• /usr/share/dict/words \
• /usr/share/dict/linux.words
Display the Command History
• The history command displays a list of previously executed commands prefixed with a command number.
• The exclamation point character (!) is a metacharacter to expand previous commands without retyping them.
• The !number command expands to the command that matches the specified number.
• The !string command expands to the most recent command that begins with the specified string.
• #history
• Example :
• [root@node1 ~]# history
• 1 systemctl reboot
• 2 ls
• 3 ip a
• 4 touch file
• 5 ls
• 6 scp file root@192.168.1.128:
• 7 ping 192.168.1.128
• 8 reboot
• 9 ls
• 10 scp file root@192.168.1.112:
• 11 sftp root@192.168.1.112:
• 12 ip a
• 13 ping 192.168.1.112
•
Chapter 3 Manage Files from the Command Line
• Describe Linux File System Hierarchy Concepts
• The File-system Hierarchy
• The Linux system stores all files on file systems, which are organized into a single
inverted tree known as a file-system hierarchy. This hierarchy is an inverted tree
because the tree root is at the top, and the branches of directories and subdirectories
stretch below the root.
Red Hat Enterprise Linux
Directories
Absolute Paths and Relative Paths
•The path of a file or directory specifies its unique file system location.
•Absolute Paths
•It is a fully qualified name, specifying the files exact location in the file system hierarchy.
•It begins at the root (/) directory and specifies each subdirectory that must be traversed to reach the specific file.
•A path name with a forward slash (/) as the first character is an absolute path name.
•Relative Paths
•The working directory or current working directory refer to their current location.
•A relative path identifies a unique file, specifying only the path necessary to reach the file from
the working directory.
•A path name with anything other than a forward slash (/) as the first character is a relative path
name.
Relative path
•mkdir d1/d2/d3/d4 –pv (Relative path)
•#pwd
•/home/anand/d1/d2/d3/d4
Use the cd command to change your shell's current working directory. If you do
not specify any arguments to the command, then it changes to your home
directory.
Touch command used
• The touch command updates the time stamp of a file to the current date and time without otherwise modifying it.
• This command is useful for creating empty files, which can be used for practice, because when
you use the touch command with a file name that does not exist, the file is created.
• [root@node1 ~]# touch Anand.txt
• [root@node1 ~]# touch Linux
• [root@node1 ~]# touch Linux.txt
• [root@node1 ~]# ls
• [root@node1 ~]# ls
• Anand.txt Dir file Linux Linux.txt
• [root@node1 ~]# ll
• total 0
• -rw-r--r--. 1 root root 0 Feb 21 15:33 Anand.txt
• drwxr-xr-x. 2 root root 6 Feb 21 11:20 Dir
• -rwxrwxrwx. 1 root root 0 Feb 21 11:03 file
• -rw-r--r--. 1 root root 0 Feb 21 15:34 Linux
• -rw-r--r--. 1 root root 0 Feb 21 15:34 Linux.txt
Ls command used
• The ls command has multiple options for displaying attributes on files. The most common options are -l (long
listing format), -a (all files, including hidden files), and -R (recursive, to include the contents of all subdirectories).
• -rw-r--r--. 1 root root 0 Feb 21 15:34 Linux.txt
• [root@node1 ~]# ls
• Anand.txt Dir file Linux Linux.txt
• [root@node1 ~]# ls -l
• total 0
• -rw-r--r--. 1 root root 0 Feb 21 15:33 Anand.txt
• drwxr-xr-x. 2 root root 6 Feb 21 11:20 Dir
• -rwxrwxrwx. 1 root root 0 Feb 21 11:03 file
• -rw-r--r--. 1 root root 0 Feb 21 15:34 Linux
• -rw-r--r--. 1 root root 0 Feb 21 15:34 Linux.txt
• [root@node1 ~]# ls -la
• total 60
• dr-xr-x---. 11 root root 4096 Feb 21 15:34 .
• dr-xr-xr-x. 20 root root 274 Feb 20 17:32 ..
• -rw-r--r--. 1 root root 0 Feb 21 15:33 Anand.txt
• -rw-------. 1 root root 14968 Feb 21 11:58 .bash_history
• -rw-r--r--. 1 root root 18 Aug 10 2021 .bash_logout
Used tilde command
• You can also use the tilde (~) special character in combination with other commands to
facilitate the interaction with the home directory.
• [root@node1 ~]# cd /var/log/
• [root@node1 log]# ls ~
• Anand.txt Dir file Linux Linux.txt
• [root@node1 log]# ls -l ~
• total 0
• -rw-r--r--. 1 root root 0 Feb 21 15:33 Anand.txt
• drwxr-xr-x. 2 root root 6 Feb 21 11:20 Dir
• -rwxrwxrwx. 1 root root 0 Feb 21 11:03 file
• -rw-r--r--. 1 root root 0 Feb 21 15:34 Linux
• -rw-r--r--. 1 root root 0 Feb 21 15:34 Linux.txt
Manage Files with Command-line Tools
• Command-line File Management
• Creating, removing, copying, and moving files and directories are common operations for a system administrator.
• Without options, some commands are used to interact with files, or they can manipulate directories with the appropriate set of
options.
• Create Directories
• The mkdir command creates one or more directories or subdirectories. It takes as an argument a list of paths to the directories that
you want to create.
• The mkdir command fails because the Video directory does not exist. If you had used the mkdir command with the -p option, then the
Video directory would be created unintentionally. The Watched subdirectory would be created in that incorrect directory.
• [root@node1 ~]# tree
• [root@node1 ~]# mkdir Anand
• [root@node1 ~]# mkdir Anand/Anand1
• [root@node1 ~]# mkdir -p Admin/Admin1
• │ └── Admin1
• ├── Anand
• │ └── Anand1
• ├── Anand.txt
• ├── Dir
• ├── file
• ├── Linux
• └── Linux.txt
Copy Files and Directories
• The cp command copies a file, and creates a file either in the current directory or in a
different specified directory.
• [root@node1 ~]# cp Anand.txt /home/admin/
• [root@node1 ~]# ls -R /home/admin/
• /home/admin/:
• Anand.txt hello myetc
• [user@host glob]$ ls a*
able alpha
[user@host glob]$ ls *a*[root@node1 ~]# A=$(date)
• [root@node1 ~]# echo $A
• Wednesday 21 February 2024 04:11:41 PM IST
• [root@node1 ~]#
• Variable Expansion
able alpha baker bravo cast charlie delta easy
• [user@host glob]$ ls [ac]*
• able alpha cast charlie
• [root@node1 ~]# echo {Sunday,Monday,Tuesday,Wednesday}
• Sunday Monday Tuesday Wednesday
Chapter 4 Get Help in Red Hat Enterprise Linux
• Introduction to the Linux Manual Pages
• Search for man Pages by Keyword
• Use the man command -k option (equivalent to the apropos command) to search for a keyword in man
pages' titles and descriptions.
• The keyword search displays as a result a list of keyword- matching man page topics with section
numbers. For example, the following command searches for man pages with the word passwd.
• [root@node1 ~]# man -k passwd
• chgpasswd (8) - update group passwords in batch mode
• chpasswd (8) - update passwords in batch mode
• fgetpwent_r (3) - get passwd file entry reentrantly
• [student@workstation ~]$ man -k boot
• binfmt.d (5)
• bootparam (7) bootup (7)
• ...output omitted...
• - Configure additional binary formats for executables at boot - introduction to boot time parameters of the Linux
kernel - System bootup process
Examples for Output Redirection
• Simplify many routine administration tasks by using redirection. Use the previous table to
assist while considering the following examples:
Example:
[root@node1 ~]# date
Thursday 22 February 2024 03:39:09 AM IST
[root@node1 ~]# date > date.txt
[root@node1 ~]# cat date.txt
Thursday 22 February 2024 03:39:22 AM IST
[root@node1 ~]#
[root@node1 ~]# tail -n 2 /var/log/messages
>/tmp/last2lines_output.txt
[root@node1 ~]# cat /tmp/last2lines_output.txt
• Example:
• [root@node1 ~]# echo "hello" >echo.txt
• [root@node1 ~]# cat echo.txt
• hello
• [root@node1 ~]# echo "hell1" >> echo.txt
• [root@node1 ~]# echo "hell2" >> echo.txt
• [root@node1 ~]# echo "hell3" >> echo.txt
• [root@node1 ~]# echo "hell4" >> echo.txt
• [root@node1 ~]# cat echo.txt
• hello
• hell1
• hell2
• hell3
• hell4
• [root@node1 ~]#
• Example3:
• [admin@node1 ~]$ find /etc -name passwd > /tmp/outpt.txt 2> /dev/null
• [admin@node1 ~]$ cat /tmp/outpt.txt
• /etc/pam.d/passwd
• /etc/passwd
• [admin@node1 ~]$
• Example5:
Example6:
A pipeline connects the standard output of the first command to the standard input of
the next command.
Pipeline Examples
The following list shows some pipeline examples:
Redirect the output of the ls command to the less command to display it on the terminal
one screen at a time.
Example1
• [admin@node1 ~]$ ls -l /usr/bin/ | less
Redirect the output of the ls command to the wc -l command, which counts the number of
received lines from ls and prints that value to the terminal.
Example:
[admin@node1 ~]$ ls -l /usr/bin/ | less
Example:
• [admin@node1 ~]$ ls -t | head -n 10 > /tmp/first-ten-changed-files
• [admin@node1 ~]$ cat /tmp/first-ten-changed-files
• find
• find.txt
• find_output.txt
• Anand.txt1
• Anand.txt
• Linux.txt
• hello
• myetc
• [admin@node1 ~]$
Pipelines, Redirection, and Appending to a File
• When you combine redirection with a pipeline, the shell sets up the entire pipeline first,
and then it redirects the input/output. If you use output redirection in the middle of a
pipeline, then the output goes to the file and not to the next command in the pipeline.
• In the next example, the output of the ls command goes to the /tmp/saved-output file, and
the less command displays nothing on the terminal.
Example:
[admin@node1 ~]$ ls > /tmp/saved-output | less
[admin@node1 ~]$ cat /tmp/saved-output
Anand.txt
Anand.txt1
find
find_output.txt
find.txt
hello
Linux.txt
myetc
[admin@node1 ~]$
Pipeline Examples with the tee Command
• The tee command overcomes this limitation.
• In a pipeline, tee copies its standard input to its standard output and also redirects its
standard output to the files that are given as arguments to the command.
• If you imagine data as water that flows through a pipeline, then you can visualize tee as
a "T" joint in the pipe that directs output in two directions.
• The next example redirects the output of the ls command to the /tmp/saved-output file and
passes it to the less command, so it is displayed on the terminal one screen at a time.
If you use the tee command at the end of a pipeline, then the terminal shows the output of the
commands in the pipeline and saves it to a file at the same time.
Example
[admin@node1 ~]$ ls -l | tee /tmp/saved-output | less
[admin@node1 ~]$ cat /tmp/saved-output
total 132
-rw-r--r--. 1 root root 0 Feb 21 15:48 Anand.txt
lrwxrwxrwx. 1 root root 15 Feb 21 16:05 Anand.txt1 -> /root/Linux.txt
-rw-r--r--. 1 admin admin 1160 Feb 21 22:28 find
-rw-r--r--. 1 admin admin 0 Feb 21 22:26 find_output.txt
-rw-r--r--. 1 admin admin 116332 Feb 21 22:26 find.txt
drwxr-xr-x. 2 root root 6 Feb 20 11:46 hello
-rw-r--r--. 2 root root 0 Feb 21 15:34 Linux.txt
drwxr-xr-x. 173 root root 8192 Feb 18 21:54 myetc
If you use the tee command at the end of a pipeline, then the terminal shows the output of
the
commands in the pipeline and saves it to a file at the same time.
• Example
• [admin@node1 ~]$ ls -t | head -n 10 | tee /tmp/ten-last-changed-files
• find
• find.txt
• find_output.txt Use the tee command -a option to append the
content to a file instead of overwriting it.
• Anand.txt1 Example:
• Anand.txt
[admin@node1 ~]$ ls -l | tee -a /tmp/append-files
• Linux.txt total 132
• hello -rw-r--r--. 1 root root 0 Feb 21 15:48 Anand.txt
lrwxrwxrwx. 1 root root 15 Feb 21 16:05 Anand.txt1 ->
/root/Linux.txt
-rw-r--r--. 1 admin admin 1160 Feb 21 22:28 find
-rw-r--r--. 1 admin admin 0 Feb 21 22:26 find_output.txt
Edit Text Files from the Shell Prompt
• Vim is an improved version of the vi editor, which is distributed with Linux and UNIX
systems. Vim is highly configurable and efficient for practiced users, including split-
screen editing, color formatting, and highlighting for editing text.
• With the Bash shell, you can set shell variables to help to run commands or to modify the behavior of the shell.
You can also export shell variables as environment variables, which are automatically copied to programs that
are run from that shell. You can use variables for ease of running a command with a long argument, or to apply a
common setting to commands that are run from that shell.
• Example:
• [root@node1 ~]# id
• uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-
s0:c0.c1023
• [root@node1 ~]# id admin
• uid=1000(admin) gid=1000(admin) groups=1000(admin),10(wheel)
What Is a Group?
1.A group is a collection of users that need to share access to files and other system
resources. Groups can be used to grant access to files to a set of users instead of to a single
user.
2.Each line in the /etc/group file contains information about one group.
Example
• [root@node1 ~]# cat /etc/group
• root:x:0:
• bin:x:1:
• daemon:x:2:
• sys:x:3:
• adm:x:4:
• tty:x:5:
• disk:x:6:
Primary Groups and Secondary Groups
• Every user has exactly one primary group. For local users, this group is listed by GID in the
/etc/passwd file. The primary group owns files that the user creates.
Gain Superuser Access
The Superuser
• Most operating systems have a superuser that has all power over the system.
• In Red Hat Enterprise Linux, it is the root user.
• This user has the power to override normal privileges on the file system, and you can use it to
manage and administer the system.
• For tasks such as installing or removing software, and to manage system files and directories,
users must escalate their privileges to the root user.
Switch User Accounts
• With the su command, users can switch to a different user account. If you run the su command
from a regular user account with another user account as a parameter, then you must provide
the password of the account to switch to. When the root user runs the su command, you do not
need to enter the user's password.
• Example
• [root@node1 ~]# su - admin
• Last login: Wed Feb 21 22:23:01 IST 2024 on pts/0
• [admin@node1 ~]$
Run Commands with Sudo
• For security reasons, in some cases system administrators configure the root user not to
have a valid password.
• Thus, users cannot log in to the system as root directly with a password. Moreover, you
cannot use su to get an interactive shell. In this case, you can use the sudo command to
get root access.
• dditionally, you can configure the sudo command to allow specific users to run any
command as some other user, or only some commands as that user. For example, if you
configure the sudo command to allow the user01 user to run the usermod command as
root, then you can run the following command to lock or unlock a user account:
• Example
• [root@node1 ~]# su - admin
• Last login: Wed Feb 21 22:23:01 IST 2024 on pts/0
• [admin@node1 ~]$ sudo usermod -L Admin1
• [sudo] password for admin:
• [admin@node1 ~]$ su - Admin1
• Password:
• su: Authentication failure
If a user tries to run a command as another user, and the sudo configuration does not
permit it, then bash blocks the command, logs the attempt, and sends by default an email
to the root user.
Example
• [admin@node1 ~]$ sudo tail -n 2 /var/log/secure
• Feb 22 13:13:59 node1 sudo[15160]: pam_unix(sudo:session): session opened for user root(uid=0) by
(uid=1000)
• Feb 22 13:13:59 node1 sudo[15160]: pam_unix(sudo:session): session closed for user root
• [admin@node1 ~]$
• Another benefit of sudo is to log by default all the executed commands to /var/log/secure.
• Example
• [admin@node1 ~]$ sudo tail /var/log/secure
• Feb 22 13:11:56 node1 usermod[15138]: lock user 'Admin1' password
• Feb 22 13:11:56 node1 sudo[15134]: pam_unix(sudo:session): session closed for user root
• Feb 22 13:12:17 node1 unix_chkpwd[15152]: password check failed for user (Admin1)
• Feb 22 13:12:17 node1 su[15150]: pam_unix(su-l:auth): authentication failure; logname= uid=1000 euid=0
tty=/dev/pts/0 ruser=admin rhost= user=Admin1
Configure sudo file
• The /etc/sudoers file is the main configuration file for the sudo command. To avoid problems if multiple
administrators try to edit the file at the same time, you can edit it only with the special visudo
command. The visudo editor also validates the file, to ensure no syntax errors.
• For example, the following line from the /etc/sudoers file enables sudo access for wheel group members:
Example
• # Allow root to run any commands anywhere
• root ALL=(ALL) ALL
• Hello ALL=(ALL) ALL
• ## Allows members of the 'sys' group to run networking, software,
• ## service management apps and more.
• # %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS
• ## Allows people in group wheel to run all commands
• %wheel ALL=(ALL) ALL
• %Hello ALL=(ALL) ALL
• ## Same thing without a password
• # %wheel ALL=(ALL) NOPASSWD: ALL
• # %Hello ALL=(ALL) NOPASSWD: ALL
Manage Local User Accounts
• In Red Hat Enterprise Linux 9, the useradd command assigns new users the first free UID that
is greater than or equal to 1000, unless you explicitly specify one by using the -u option.
• The /etc/login.defs file sets some of the default options for user accounts, such as the range of
valid UID numbers and default password aging rules. The values in this file affect only newly
created user accounts. A change to this file does not affect existing users.
Modify Existing Users from the Command
Line
• he usermod --help command displays the basic options to modify an account. Some
common options are as follows:
-a –append Use it with the -G option to add the secondary groups to the user's current set of
group memberships instead of replacing the set of secondary groups with a new set.
-c, --comment COMMENT Add the COMMENT text to the comment field.
-d, --home HOME_DIR Specify a home directory for the user account.
-g, --gid GROUP Specify the primary group for the user account.
Delete Users from the Command Line
• The userdel username command removes the username user from /etc/passwd, but leaves
the user's home directory intact.
• The userdel -r username command removes the user from /etc/passwd and deletes the
user's home directory.
[root@node1 home]# ls
admin Admin1 hello myetc U1 U2 U3 U4 U5
[root@node1 home]# userdel Admin1
[root@node1 home]# ls
admin Admin1 hello myetc U1 U2 U3 U4 U5
[root@node1 home]#
Manage Local Group Accounts
Manage Local Groups
• Several command-line tools facilitate group management. While you can use the Users
GUI utility to manage groups, Red Hat recommends to use command-line tools.
Create Groups from the Command Line
• The groupadd command creates groups. Without options, the groupadd command uses the
next available GID from the range specified by the GID_MIN and GID_MAX variables in
the /etc/login.defs file. By default, the command assigns a GID value greater than any
other existing GIDs, even if a lower value becomes available.
Example
[root@host ~]# groupadd -r group02
[root@host ~]# tail /etc/group ...output omitted... group01:x:10000:
The groupadd command -r option creates system groups. As with normal groups, system groups
group02:x:988:
use a GID from the range of listed valid system GIDs in the /etc/login.defs file.
[root@host ~]# groupadd -r group02
[root@host ~]# tail /etc/group ...output omitted... group01:x:10000:
group02:x:988:
Modify Existing Groups from the Command Line
• The groupmod command changes the properties of an existing group. The groupmod
command -n option specifies a new name for the group.
[root@host ~]# groupmod -n group0022 group02
[root@host ~]# tail /etc/group
Notice that the group name updates to group0022 from group02. The groupmod command -g
option specifies a new GID.
[root@host ~]# groupmod -g 20000 group0022
[root@host ~]# tail /etc/group
...output omitted...
group0022:x:20000:
• When a user tries to log in, the system looks up the entry for the user in the /etc/shadow
file, combines the salt for the user with the unencrypted typed password, and encrypts
the combination of the salt and unencrypted password with the specified hashing
algorithm.
Configure Password Aging
• The following diagram shows the relevant password aging parameters, which
can be adjusted by using the chage command to implement a password aging
policy.
• Notice that the command name is chage which stands for "change age", and it
should not be confused with the word "change".
Example demonstrates the chage command to change the password policy of the sysadmin05
user. The command defines a minimum age (-m) of zero days, a maximum age (-M) of 90
days, a warning period (-W) of 7 days, and an inactivity period (-I) of 14 days.
Example
[root@Anand ~]# usermod -L Anand
[root@Anand ~]# passwd -S Anand
Anand LK 2024-03-02 0 90 7 14 (Password locked.)
[root@Anand ~]#
r (read)
File contents can be Contents of the directory (the file names) can be
read. listed.
File contents can be
w (write)
changed.
Any file in the directory can be created or deleted.
Files can be executed as The directory can become the current working directory. You can run the
x (execute)
commands. cd command to it, but it also requires read permission to list files there.
The ls command -l option shows detailed information about permissions and ownership:
• - is a regular file.
• d is a directory.
• l is a symbolic link.
• c is a character device file.
• b is a block device file.
• p is a named pipe file.
• s is a local socket file.
Example:
• The chmod command changes file and directory permissions from the command line.
• The chmod command can be interpreted as "change mode", because the mode of a file is
another name for file permissions.
• The chmod command takes a permission instruction followed by a list of files
or directories to change. You can set the permission instruction either symbolically or in
octal (numeric) notation.
• Change Permissions with the Symbolic Method
• Use the chmod command to modify file and directory permissions. The following example
can help you to understand the chmod command usage:
• chmod Who/What/Which file|directory
• Change Permissions with the Symbolic
Method
1.Identify the File/Directory: Determine the file or directory for which you want to change
permissions.
2.Understand Permission Symbols:
• u: User/Owner
• g: Group
• o: Other
• a: All (equivalent to ugo)
• +: Add permissions
• -: Remove permissions
• =: Set permissions explicitly, overriding existing ones
• r: Read
• w: Write
• x: Execute (for files: execute permission means the file can be run as a program; for directories:
execute permission allows access to the directory's contents)
• s: SetUID or SetGID (for files/directories)
• t: Sticky bit (for directories)
Change Permissions with the Octal Method
• You can use the chmod command to change file permissions with the octal
method instead of the symbolic method. In the following example, the #
character represents a digit.
Change Permissions with the Octal Method
• With the octal method, you can represent permissions as a 3-digit (or 4-digit, when
setting
• advanced permissions) octal number. A single octal digit can represent any single value
from 0-7. In the 3-digit octal representation of permissions, each digit stands for one
access level, from left
• to right: user, group, and other. To determine each digit:
• Start with 0.
• If you want to add read permissions for this access level, then add 4.
• If you want to add write permissions, then add 2.
• If you want to add execute permissions, then add 1.
1. Change Permissions:
1. Adding Permissions: Use + followed by the permissions you want to add.
2. Removing Permissions: Use - followed by the permissions you want to remove.
3. Setting Permissions Explicitly: Use = followed by the desired permissions.
2. Apply Changes to User/Group/Other: Specify whether you're changing permissions for the user
(owner), group, or others.
Command Syntax:
[who]: Represents the users whose permissions you want to change (u, g, o, a).
• [operator]: Specifies whether to add (+), remove (-), or set (=) permissions.
• [permissions]: Denotes the permissions to be added, removed, or set (r, w, x, s, t).
• file(s): The file or files for which you're changing permissions.
Remove the setuid bit on the example directory by using the symbolic method:
[user@host ~]# chmod u-s example
Set the setgid bit and add read, write, and execute permissions for user and group,
with no
access for others, on the example directory by using the octal method:
[user@host ~]# chmod 2770 example
Remove the setgid bit and add read, write, and execute permissions for user and
group, with no access for others, on the example directory by using the octal method.
Note that you need to add an extra 0 at the beginning of the permissions value when
Default File Permissions
• On creation, a file is assigned initial permissions.
• Two things affect these initial permissions.
• The first is whether you are creating a regular file or a directory.
• The second is the current umask, which stands for user file-
creation mask.
• The umask command without arguments displays the current value of the shell's umask:
• Example:
• [root@Anand ~]# umask
• 0002
• [root@Anand ~]#
• [operator1@servera ~]$ echo "umask 007" >> ~/.bashrc
• [operator1@servera ~]$ cat ~/.bashrc
# .bashrc
Chapter 8 | Monitor and Manage Linux Processes
• Example:1
• [root@localhost ~]# ps
• PID TTY TIME CMD
• 5060 pts/0 00:00:00 bash
• 6386 pts/0 00:00:00 ps
• [root@localhost ~]# jobs
• [root@localhost ~]# ps a
• PID TTY STAT TIME COMMAND
• 3612 tty2 Ssl+ 0:00 /usr/libexec/gdm-wayland-session --register-session gnome-ses
• 3618 tty2 Sl+ 0:00 /usr/libexec/gnome-session-binary
• 5060 pts/0 Ss 0:00 bash
• 6398 pts/0 R+ 0:00 ps a
• [root@localhost ~]#
Run Jobs in the Background
• Any command or pipeline can be started in the background by appending an
ampersand (&) character to the command.
• The Bash shell displays a job number (unique to the session) and the PID of the new
child process. Example:
• Example: [4]+ Stopped top
[root@redhatlinux1010gmail ~]# jobs
• [root@redhatlinux1010gmail ~]# sleep 1000 &
[1] Running sleep 1000 &
• [1] 8302 [2] Running sleep 1000 &
• [root@redhatlinux1010gmail ~]# sleep 1000 & [3]- Running sleep 1000 &
[4]+ Stopped top
• [2] 8307 [root@redhatlinux1010gmail ~]# ps
• [root@redhatlinux1010gmail ~]# sleep 1000 & PID TTY TIME CMD
8265 pts/0 00:00:00 bash
• [3] 8312 8302 pts/0 00:00:00 sleep
• [root@redhatlinux1010gmail ~]# top 8307 pts/0 00:00:00 sleep
8312 pts/0 00:00:00 sleep
8318 pts/0 00:00:00 top
8327 pts/0 00:00:00 ps
[root@redhatlinux1010gmail ~]#
• Example:2
• [root@anand ~]# ps
• PID TTY TIME CMD
• 6490 pts/3 00:00:00 bash
• 6529 pts/3 00:00:00 ps
• [root@localhost ~]# ps a
• PID TTY STAT TIME COMMAND
• 3612 tty2 Ssl+ 0:00 /usr/libexec/gdm-wayland-session --register-session gnome-ses
• 3618 tty2 Sl+ 0:00 /usr/libexec/gnome-session-binary
• 5060 pts/0 Ss 0:00 bash
• 6406 pts/0 S+ 0:00 vim jet
• 6407 pts/1 Ss 0:00 bash
• 6446 pts/1 S+ 0:00 vim jet1
• 6448 pts/2 Ss 0:00 bash
• 6488 pts/2 S+ 0:00 vim jet2
• 6490 pts/3 Ss 0:00 bash
• 6534 pts/3 R+ 0:00 ps a
Use the jobs command to display the list of jobs for the
shell's session.
• Example:
• Use the lscpu command to determine the number of CPUs that are present on a system.
Use the lscpu command to determine the number of CPUs that are present
on a system.
• Example:
• Example:
• [root@localhost ~]# systemctl list-units --type=service
• UNIT LOAD ACTIVE SUB DESCRIPTION
• UNIT
• The service unit name.
• LOAD
Whether the systemd daemon properly parsed the unit's configuration and loaded the
unit into memory.
• ACTIVE
• The high-level activation state of the unit. This information indicates whether the unit
started successfully.
• SUB
• DESCRIPTION
• The short description of the unit.
Example:
[root@localhost ~]# systemctl list-units --type=service --all
[root@localhost ~]# systemctl
The systemctl command list-units option displays units that the systemd
service attempts to parse and load into memory. This option does not
display services that are installed but not enabled. You can use the systemctl
command list-unit-files option to see the state of all the installed unit files:
• Example:
• [root@localhost ~]# systemctl list-unit-files --type=service
• Example:
• [root@localhost ~]# systemctl status sshd.service
• ● sshd.service - OpenSSH server daemon
• Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; preset: enabled)
• Active: active (running) since Wed 2024-03-13 07:30:50 EST; 1h 32min left
Service Unit Information
Service States in the Output of
systemctl
Verify the Status of a Service
• The systemctl command verifies the specific states of a service.
• For example, use the systemctl command is-active option to verify whether a service unit
is active (running):
• Example:
• [root@localhost ~]# systemctl is-active sshd.service active
• active
• #systemctl is-enabled sshd.service enabled
• #systemctl is-failed sshd.service active
List Unit Dependencies
• Some services require other services to be running first, which creates dependencies on
the other services. Other services start only on demand, rather than at boot time.
• In both cases, systemd and systemctl start services as needed, whether to resolve the
dependency or to start an infrequently used service.
• Example:
• #systemctl list-dependencies sshd.service
Mask and Unmask Services
• At times, different installed services on your system might conflict with each other.
• For example, multiple methods are available to manage mail servers (the postfix and
sendmail services). Masking a service prevents an administrator from accidentally starting
a service that conflicts
with others.
• Masking creates a link in the configuration directories to the /dev/null file which prevents
the service from starting. To mask a service, use the systemctl command mask option.
• Example
• [root@localhost ~]# systemctl mask sshd
• Created symlink /etc/systemd/system/sshd.service → /dev/null.
• [root@localhost ~]#
Then, check the state of the service by using the systemctl list-unit-
files command:
• Example
Attempting to start a masked service unit fails with the following output:
Use the systemctl unmask command to unmask the
service unit.
Enable Services to Start or Stop at Boot
• Starting a service on a running system does not guarantee that the service automatically starts when
the system reboots.
• Similarly, stopping a service on a running system does not keep it from starting again when the
system reboots. Creating links in the systemd configuration directories enables the service to start at
boot.
• You can create or remove these links by using the systemctl command with the enable or disable option.
• Example:
Disable the service from starting automatically,
systemctl disable command,
which removes the symbolic link that was created while enabling a service.
To disable and stop a service, you can execute both the systemctl stop and systemctl
disable commands or use the equivalent systemctl disable --nowcommand.