Linux RHSCA
Introduction
UNIX is the first Operating system in the world, developed by Kem Thompson and Dennis Ritchie in 1969 at Bell
Lab by AT&T Company
IBM : AIX
SGI : IRIX
Sun : Solaris
Free software foundation organization, they start a project by name GNU. The main aim of this project is to
develop such an operating system that can run on any platform.
In 1991, a student Linuz Torvalds developed a kernel named Linux’s kernel plus GNU application called Linux
operating system.
Linux is an open-source technology.
Different companies that provide Linux in Market are Redhat, SuSe, Scientific, Centos, and Knoppix etc.
Features:
Linux is the fastest Operating system in the world. It runs 2 to 3 times fast than windows OS.
Linux is the much-secured OS because there is no any problem of virus.
Linux file format is text format and windows file format is binary format.
Linux is very reliable OS because kernel of Linux is very stable as compared to windows kernel not crashed easily.
Kernel of Linux is very small in size it can be stored in floppy.
Linux uses the x-Window system which is advanced network windowing system. Using this system, we can
display output of any workstation monitor attached in the network.
Advantages:
Virus Proof
Crash Proof
Economical
Multiuser, Multi-Tasking and Multiprocessing capacity
Login Modes:
Two modes: 1. Text mode (CLI) 2.Graphical Mode (GUI)
Login to Text mode we have use (Ctrl+Alt+F1…..F3,F4,F5,F6) (Ctrl+Alt+F2) for Graphical Mode
2. Windows Vs Linux
Windows Linux
It is a proprietary software everything need to It is an open source software everything is
buy free
Less Secure More Secure
More costly Less Cost compare to windows
Chance to get a carpal tunnel syndrome There is no chance to get carpal tunnel
syndrome
Developed by Microsoft corporation Developed by Linus Torvalds
Options need to select You can develop anything as per your
requirement
Kernel is not editable Kernel is editable
Linux Architecture:
Kernel: It is a program. Kernel is a Core component of operating system, interacts directly with hardware and provides low level
services to upper layer components.
Shell: An interface to kernel, hiding complexity of kernel’s functions from users. Takes commands from user and executes kernel’s
functions.
User Interface: In information technology, the user interface (UI) is everything designed into an information device with which a
human being may interact. Two types CLI (Command line interface) and GUI (Graphical User Interface).
System Library - System libraries are special functions or programs using which application programs or system utilities accesses
Kernel's features. These libraries implements most of the functionalities of the operating system and do not requires kernel
module's code access rights.
System Utility - System Utility programs are responsible to do specialized, individual level tasks.
Before you turn over to the next chapter where you are going to meet a plethora of commands, remember a few things that apply
to all UNIX commands.
All UNIX commands must always be entered in small case letters
Between the command name and the options that may be available with the command there must always be a space or a
tab, for example, ls –l. Here is the command whereas –l is the option and the two have been separated by space. The
option is usually preceded by a minus (-) sign. The option available with a command are often known as switches.
Two or more options available with command can usually be combined, for example, the command ls –l –a is same as ls –
la.
If you make a typing mistake, press backspace to erase characters Don’t try back using arrow keys and then attempt
deleting using the del key.
To cancel the entire command before you press Enter, press ctrl+c Or del key
All Basic Commands
Date
Some commonly used formats are:
%A – locale’s full weekday name (for example, Sunday)
%D – date; same as %m/%d/%y
%F – full date; like %+4Y-%m-%d
%s – seconds since 1970-01-01 00:00:00 UTC
%y – last two digits of the year (00..99)
%Y – year (for example, 2023)
%Z – alphabetic time zone abbreviation (for example, EST)
%N – nanoseconds
Cal
The cal command displays a calendar of the specified year or month.
Options Description
default The current month is displayed.
-y The same default.
-3 Shows the calendar of previous, current, and next month.
-j Show Julian the calendar current month.
Options Description
-A n Show the current month and next n months.
-B n Show the current month and the previous n months.
The "w" command displays information about all users logged into the current system.
who command is used to determine when the system has booted last time, a list of logged-in users, and the system’s current run level.
whoami command is used to print the user’s name from which they are currently logged-in
PWD command in Linux stands for 'print working directory.' It is used to display the current working directory on the screen
Exp: /home/nischal
The Ls command is used to get a list of files and directories.
The ls command supports the following options:
ls -a: list all files including hidden files. These are files that start with “.”.
ls -A: list all files including hidden files except for “.” and “..” – these refer to the entries for the current directory, and for the parent directory.
ls -R: list all files recursively, descending down the directory tree from the given path.
ls -l: list the files in long format i.e. with an index number, owner name, group name, size, and permissions.
ls – o: list the files in long format but without the group name.
ls -g: list the files in long format but without the owner name.
ls -i: list the files along with their index number.
ls -s: list the files along with their size.
ls -t: sort the list by time of modification, with the newest at the top.
ls -S: sort the list by size, with the largest at the top.
ls -r: reverse the sorting order.
Cat in Linux stands for concatenation (to merge things together) and is one of the most useful and versatile Linux commands.
The cat command has three primary purposes involving text files:
Create
Read/Display
Update/Modify
Exp: cat >new_file_name>
Press “ENTER” and then use “CTRL + d" to exit the prompt.
Some of the common options to use with the cat command are:
Options Description
-n Show line numbers for all lines
-T Show every tab character in the file
-e Show the end of every line in the file
-s Merge successive empty lines at the end of the file as one
-b Show only non-empty lines
Merge Two Files into a New File
cat <option> <file_1> >> <file_2>
The mkdir command in Linux/Unix allows users to create or make new directories. mkdir stands for “make directory.
Exp: mkdir directory
mkdir -p new{1..5}
mkdir -p /linux/redhat/centos
#to create parent
directories.
touch Create an empty file / Multiple empty files yet a time
Exp: touch NischalTechSupport
CP
command for copying files from one location to another.
Exp: cp<Source path> <Destination path>
MV command is used to rename file directories and move files from one location to another
mv <source> <destination> ………………………….. Move files/directories.
mv <old name> <new name> ………………………… Rename the file and directory.
mv command main options:
option description
mv -f force move by overwriting destination file without
prompt
mv -i interactive prompt before overwrite
mv -u update - move when source is newer than destination
mv -v verbose - print source and destination files
man mv help manual
rm Command:
rm stands for remove, and it is used to remove files, and directories.
rmdir <directory Name> ………………………………….#Delete directories only
rm <file Name> ------------------------------------------#Delete files
rm -rfv ………………………………………………………………#Delete files and directories forcefully
The different rm command options include:
-f: Forces the removal of all files or directories.
-i: Prompts for confirmation before removing.
-I: Prompts once before removing more than three files or when removing recursively.
-r: Removes directories and their content recursively.
-d: Removes empty directories.
-v: Provides a verbose output.
--help: Displays the help text.
--version: Displays the command version
More Less
1) Displays file contents 1) Displays file contents
2) Syntax 2) Syntax
$ more filename $ less filename
3) The output displays file line-by-line. Next 3) The next lines are viewed by using arrow
lines are viewed by pressing ENTER key. keys or by ENTER key
Arrow keys will not work 4) The file contents can be navigated forward
4) No backward navigation & backward
less - read and display contents of text file one page per time.
Exp: less /etc/passwd
more command is used to view the text files in the command prompt, displaying one screen at a time in case the file is large
Exp: more /etc/passwd
Command Description
cd / Switch to the root directory (/)
cd dir_name Switch to the dir (with absolute or relative path)
cd .. To navigate to the parent directory
cd - Switch back to the previous directory
cd ~ Switch back to the home directory
Cd ../../..
HEAD
xTAIL
1) Displays topmost lines 1) Displays bottom-most lines
2) Syntax 2) Syntax
$ head –n filename $ tail –n filename
n > = 1 (default ‘n’ value is 10) n >= 1 (default ‘n’ value is 10)
The output displays ‘n’ top-most lines The output displays ‘n’ bottommost lines
3) Ex, 3) Ex,
$ head -15 test1.txt $ tail -15 test1.txt
Displays top 15 lines Displays bottom 15 lines
By Nischal Tech Support
tail command:
Tail command Display the last 10 lines of a file
Exp: tail /etc/passwd
For example, to display the last 5 lines. To do so, invoke the command
Exp: tail -n 5 /etc/passwd
man tail or tail --help
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 15
By Nischal Tech Support
head command:
head command display first 10 lines of the file
Exp: head /etc/passwd
For example, to display the first 5 lines. To do so, invoke the command
Exp: head -n 5 /etc/passwd
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 16
By Nischal Tech Support
Piping and Redirecting:
Redirecting Redirecting command output to a file
There are three redirectors to work with: >, >>, and <. The following
information describes each one:
Redirection with >
command > file: Sends standard output to <file>
command 2> file: Sends error output to <file>
command 2>&1: Sends error output to standard output
command > file 2>&1: Sends standard output and the error output to a file
command &> file: Sends standard output and the error output to a file
command 2>&1 > file: Sends error output to standard input and the
standard input to a file
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 17
By Nischal Tech Support
Append with >>
command >> file: Appends standard output to a file
command 2>> file: Appends error output to a file
command >> file 2>&1: Appends standard output and error output to a file
command &>> file: Appends standard output and error output to a file
command 2>&1 >> file: Sends error output to standard input and appends
standard input to a file
Redirect with <
command < input: Feeds a command input from <input>
command << input: Feeds a command or interactive program with a list
defined by a delimiter; this is known as a here-document (heredoc)
command <<< input: Feeds a command with <input>; this is known as a
here-string
[ Download the free Bash shell scripting cheat sheet. ]
Shell I/O examples
Here are some examples of using each operator in the order
presented above.
Redirect the standard output for a given command to a file:
$ echo "Enable Sysadmin" > myfile
$ cat myfile
Enable Sysadmin
Redirect error output for a given command to a file:
$ ls /root 2> myfile
$ cat myfile
ls: cannot open directory '/root': permission denied
Redirect error output for a given command to the standard output,
the terminal:
$ ls /root 2>&1
ls: cannot open directory 'root/': Permission denied
Redirect both standard output and error output for a given command
to a file:
$ find /usr -name ls > myfile 2>&1
$ cat myfile
/usr/bin/ls
find: '/usr/share/polkit-1/rules.d': Permission denied
find: '/usr/share/selinux/targeted/default/active': Permission denied
find: '/usr/libexec/initscripts/legacy-actions/auditd': Permission denied
$
$ find /usr -name ls &> myfile
$ cat myfile
/usr/bin/ls
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 18
By Nischal Tech Support
find: '/usr/share/polkit-1/rules.d': Permission denied
find: '/usr/share/selinux/targeted/default/active': Permission denied
find: '/usr/libexec/initscripts/legacy-actions/auditd': Permission denied
Redirect error output for a given command to the standard output,
the terminal, and the standard output for the same command to a
file:
$ find /usr -name ls 2>&1 > myfile
find: '/usr/share/polkit-1/rules.d': Permission denied
find: '/usr/share/selinux/targeted/default/active': Permission denied
find: '/usr/libexec/initscripts/legacy-actions/auditd': Permission denied
$ cat myfile
/usr/bin/ls
Append the standard output for a given command to an existing file
(if the file doesn’t exist, it creates it, just as the > operator does):
$ cat myfile
/usr/bin/ls
$ echo "Enable Sysadmin" >> myfile
$ cat myfile
/usr/bin/ls
Enable Sysadmin
Append the error output for a given command to an existing file:
$ ls /root 2>> myfile
$ cat myfile
/usr/bin/ls
Enable Sysadmin
ls: cannot open directory '/root': Permission denied
Append both the standard output and the error output for a given
command to an existing file:
$ find /usr -name cd >> myfile 2>&1
$ cat myfile
/usr/bin/ls
Enable Sysadmin
ls: cannot open directory '/root': Permission denied
/usr/bin/cd
/usr/lib/.build-id/cd
/usr/share/X11/xkb/symbols/cd
find: '/usr/share/polkit-1/rules.d': Permission denied
find: '/usr/share/selinux/targeted/default/active': Permission denied
find: '/usr/libexec/initscripts/legacy-actions/auditd': Permission denied
$
$ find /usr -name cd &>> myfile
$ cat myfile
/usr/bin/ls
Enable Sysadmin
ls: cannot open directory '/root': Permission denied
/usr/bin/cd
/usr/lib/.build-id/cd
/usr/share/X11/xkb/symbols/cd
find: '/usr/share/polkit-1/rules.d': Permission denied
find: '/usr/share/selinux/targeted/default/active': Permission denied
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 19
By Nischal Tech Support
Redirect the error output for a given command to the standard
output, the terminal, and append the standard output for the same
command to an existing file:
$ find /usr -name cd 2>&1 >> myfile
find: '/usr/share/polkit-1/rules.d': Permission denied
find: '/usr/share/selinux/targeted/default/active': Permission denied
find: '/usr/libexec/initscripts/legacy-actions/auditd': Permission denied
$ cat myfile
/usr/bin/ls
Enable Sysadmin
ls: cannot open directory '/root': Permission denied
/usr/bin/cd
/usr/lib/.build-id/cd
/usr/share/X11/xkb/symbols/cd
find: '/usr/share/polkit-1/rules.d': Permission denied
find: '/usr/share/selinux/targeted/default/active': Permission denied
find: '/usr/libexec/initscripts/legacy-actions/auditd': Permission denied
/usr/bin/cd
/usr/lib/.build-id/cd
/usr/share/X11/xkb/symbols/cd
find: '/usr/share/polkit-1/rules.d': Permission denied
find: '/usr/share/selinux/targeted/default/active': Permission denied
Feed a command with a nonstandard input, in this case, a file:
$ sort < myfile
/usr/bin/cd
/usr/bin/cd
/usr/bin/cd
/usr/bin/ls
/usr/bin/.build-id/cd
/usr/bin/.build-id/cd
/usr/bin/.build-id/cd
/usr/share/X11/xkb/symbols/cd
/usr/share/X11/xkb/symbols/cd
/usr/share/X11/xkb/symbols/cd
Enable Sysadmin
find: '/usr/libexec/initscripts/legacy-actions/auditd': Permission denied
find: '/usr/libexec/initscripts/legacy-actions/auditd': Permission denied
find: '/usr/share/polkit-1/rules.d': Permission denied
find: '/usr/share/polkit-1/rules.d': Permission denied
find: '/usr/share/selinux/targeted/default/active': Permission denied
find: '/usr/share/selinux/targeted/default/active': Permission denied
ls: cannot open directory '/root': Permission denied
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 20
By Nischal Tech Support
Piping sending output or one command as a input to the another command
| Pipe symbol.
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 21
By Nischal Tech Support
The semicolon ( ; ) is used as a command separator.
You can run more than one command on a single line by using the command separator,
placing the semicolon between each command.
Command1; command2
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 22
By Nischal Tech Support
wc command
for word count, character count, and line count
Options:
-l #Check line count
-c #Character Count
-w #Word Count
Absolute path & relative path : Linux file system
1. Absolute Path
You use the absolute path when you specify the path to a file or folder,
starting with the root directory. This path begins with the root (/)
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 23
By Nischal Tech Support
/home
/home/nischal
/home/nischal/test
2. Relative Path
Unlike the absolute path that starts from the root directory, the relative
paths start from the present working directory. Thus, the relative path
changes depending on your current directory
../../…
~/Downloads
destination. In link updating is possible for both the files.
HARD Link SOFT Link
File Types
-b - block device file Example: HDD and pen drive
-d - directory file
- - common file
c - Character device file Example: terminal
l - Linked file
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 24
By Nischal Tech Support
Linking means reflecting to the original file, In case of copy command updating is not possible
after copying the file from the source to destination. In link updating is possible for both the files.
HARD Link SOFT Link
1. The destination file is exact image of the 1. The destination file size is length of the
source file. source file name
2. If source got deleted also even we can 2. if source got deleted we can’t access
access the destination file destination file
3. inode numbers of source and destination 3. inode numbers of source and destination are
are same different
4. We can’t put the hard link to different file 4. we can put a link between different file systems
system (partitions) because it will
different.
5. Ex: ln <source> <destination> Ex: ln –s <source> <destination>
Soft Link
Hard Link
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 25
By Nischal Tech Support
Linux file system and directory structure
Overview
Everything in Linux can be reduced to a file. Partitions are associated with files such as
/dev/hda1. Hardware components are associated with files such as /dev/modem. The Filesystem
Hierarchy Standard (FHS) is the official way to organize files in Unix and Linux directories.
Linux file system and directory structure
Several major directories are associated with all modern Unix/Linux operating systems.
These directories organize user files, drivers, kernels, logs, programs, utilities, and more into
different categories. The standardization of the FHS makes it easier for users of other Unix-based
operating systems to understand the basics of Linux. All of the other directories shown in Table are
subdirectories of the root directory, unless they are mounted separately.
Directory Description
The root directory, the top-level directory in the FHS. All other directories are subdirectories of
/ root, which is always mounted on some partition. All directories that are not mounted on a
separate partition are included in the root directory’s partition.
Essential command line utilities. Should not be mounted separately; otherwise, it could be
/bin
difficult to get to these utilities when using a rescue disk.
Includes Linux startup files, including the Linux kernel. Can be small; 16MB is usually adequate
/boot for a typical modular kernel. If you use multiple kernels, such as for testing a kernel upgrade,
increase the size of this partition accordingly.
/etc Most basic configuration files.
Hardware and software device drivers for everything from floppy drives to terminals. Do not
/dev
mount this directory on a separate partition.
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 26
By Nischal Tech Support
/home Home directories for almost every user.
Program libraries for the kernel and various command line utilities. Do not mount this directory
/lib
on a separate partition.
/mnt The mount point for removable media, including floppy drives, CD-ROMs, and Zip disks.
/opt Applications such as WordPerfect or Star Office.
Currently running kernel-related processes, including device assignments such as IRQ ports, I/O
/proc
addresses, and DMA channels.
/root The home directory of the root user.
/sbin System administration commands. Don't mount this directory separately.
/tmp Temporary files. By default, Red Hat Linux deletes all files in this directory periodically.
Small programs accessible to all users. Includes many system administration commands and
/usr
utilities.
/var Variable data, including log files and printer spools.
Booting Process
POST: Power on self-test - Hardware will check it self
BIOS: Basic input and output system will load
MBR: Master boot record – which will have 446Bytes in size first sector of the HDD.
446 MBR 64 Partition Table 2 Active Sign
GRUB: it will boot in three steps
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 27
By Nischal Tech Support
Stage 1: The duty of the stage 1 is to identify where the stage is located to
Stage 1.5: The duty of the stage 1.5 is to synchronize with the deferent file system. LBA – logical block
addressing. Once executed, core.img will load its configuration file and any other modules needed,
particularly file system drivers; at installation time, it is generated from diskboot.img and configured to
load the stage 2 by its file path.
Stage 2: it will list out all the available operating systems and it will boot with the default operating
system.
Kernel: Kernel version and its installed modules will load
Init: init is the root daemon process after this process all the process will start under init
Run levels:
Run levels are 7
0 - Shutdown/Halt/Power off
1 - Single User Mode
2 - Multiple user without Network
3 - Multiple users with Network (CLI)
4 - Unused
5 - GUI mode
6 - Reboot
After completion of run level it will enter into the User interface will ask you for the credential to login.
1 sylinder = 7.8 MB
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 28
By Nischal Tech Support
VIM Command : Text Editors
Console Based Text Editors:
3. Vim/vi
Graphical (GUI) Editors:
1. Gedit
2. Gvim
3. Sublime (have to install separately)
Now we are going to discuss about vi/vim editor.
Vim is a highly configurable text editor built to enable efficient text editing. It is an improved
version of the VI editor distributed with most UNIX systems. It is a tool, the use of which you must be
learned. Program is written by Bram Moolenaar et al.
Usage of vi/vim, it has three modes
1. Command mode
2. Insert Mode
3. Ex-mode
Command mode: this mode is the default mode following things we can do
Editing the Existing File
Step 1 type vim filename (edit the existing file named filename)
Step 2 move around the file using h/j/k/l key or any appropriate command
h Moves the cursor one character to the left
l Moves the cursor one character to the right
k Moves the cursor up one line
j Moves the cursor down one line
nG or :n Cursor goes to the specified (n) line
(ex. 10G goes to line 10)
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 29
By Nischal Tech Support
Step 3 edit required text (replace or delete or insert)
Step 4 hit Esc key (exit from insert mode if you insert or replace text)
Step 5 type :wq
Some useful commands for VIM
Text Entry Commands (Used to start text entry)
a Append text following current cursor position
A Append text to the end of current line
i Insert text before the current cursor position
I Insert text at the beginning of the cursor line
o Open up a new line following the current line and add text there
O Open up a new line in front of the current line and add text there
The following commands are used only in the commands mode.
Cursor Movement Commands
h Moves the cursor one character to the left
l Moves the cursor one character to the right
k Moves the cursor up one line
j Moves the cursor down one line
nG or :n Cursor goes to the specified (n) line
(ex. 10G goes to line 10)
^F (CTRl F) Forward screenful
^B Backward screenful
^f One page forward
^b One page backward
^U Up half screenful
^D Down half screenful
$ Move cursor to the end of current line
0 (zero) Move cursor to the beginning of current line
w Forward one word
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 30
By Nischal Tech Support
b Backward one word
Exit Commands
:wq Write file to disk and quit the editor
:q! Quit (no warning)
:q Quit (a warning is printed if a modified file has not been saved)
ZZ Save workspace and quit the editor (same as :wq)
: 10,25 w temp
write lines 10 through 25 into file named temp. Of course, other line
numbers can be used. (Use :f to find out the line numbers you want.
Text Deletion Commands
x Delete character
dw Delete word from cursor on
db Delete word backward
dd Delete line
d$ Delete to end of line
d^ (d caret, not CTRL d) Delete to beginning of line
Yank (has most of the options of delete)-- VI's copy commmand
yy yank current line
y$ yank to end of current line from cursor
yw yank from cursor to end of current word
5yy yank, for example, 5 lines
Paste (used after delete or yank to recover lines.)
p paste below cursor
P paste above cursor
"2p paste from buffer 2 (there are 9)
u Undo last change
U Restore line
J Join next line down to the end of the current line
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 31
By Nischal Tech Support
File Manipulation Commands
:w Write workspace to original file
:w file Write workspace to named file
:e file Start editing a new file
:r file Read contents of a file to the workspace
To create a page break, while in the insert mode, press the CTRL key
And l. ^L will appear in your text and will cause the printer to start
A new page.
Other Useful Commands
Most commands can be repeated n times by typing a number, n, before
the command. For example 10dd means delete 10 lines.
. Repeat last command
cw Change current word to a new word
r Replace one character at the cursor position
R Begin overstrike or replace mode � use ESC key to exit
:/ pattern Search forward for the pattern
:? pattern Search backward for the pattern
n (used after either of the 2 search commands above to
continue to find next occurrence of the pattern.
:g/pat1/s//pat2/g replace every occurrence of pattern1 (pat1) with
pat2
Example :g/tIO/s//Ada.Text_IO/g
This will find and replace tIO by Ada.text_IO everywhere in the file.
:g/a/s// /g replace the letter a, by blank
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 32
By Nischal Tech Support
:g/a/s///g replace a by nothing
note: Even this command be undone by u
User management
Type of Users:
Root user - Default user highly privileged UID is 0. This will create while installing the
operating system
System users - is nothing but services, at the time of installing particular package. UID starts
from 1 to 999.
Local users - after installing of the operating system admin user will create these users. UID
starts from 1000 to 60000.
After creating a user, user home directory will be created in default path /home.
One group is will be created with same user name (primary group)
Files from /etc/skel will be copied automatically to user home directory
/etc/passwd file is updated with user information
/etc/group file is update with primary group information
Important files
/etc/passwd User Information
/etc/shadow User Passwords
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 33
By Nischal Tech Support
/etc/group Group Information
/etc/gshadow Group Passwords
User Administration Commands:
# useradd <user name> - To create specified local user
#echo "userpasswd1"|passwd --stdin user1 to set user password
# useradd –d <home directory> <user name> - create a user with specified home path
# useradd –u <UID> <user name> - create user with specific UID.
# passwd <user name> - change the user password
# userdel <user name> - delete user
# userdel - r <user name> - delete user including home directory
# finger <user name> - See user properties
# chfn <user name> - Change user information
# chage –l <user name> - to check user password expiry and account expiry information
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 34
By Nischal Tech Support
# su - <user name> - Switch to other user account
# id <user name> - it will show the user id
# system-config-users - create and manage user account in GUI
Usermod command options:
Each line in /etc/passwd file represents an individual user account and contains following seven fields separated
by colons (:).
1. Username or login name
2. Encrypted password
3. User ID
4. Group ID
5. User description
6. User’s home directory
7. User’s login shell
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 35
By Nischal Tech Support
-c = We can add comment field for the user account.
-d = To modify the directory for any existing user account.
-e = Using this option we can make the account expiry in specific period.
-g = Change the primary group for a User.
-G = To add a supplementary groups.
-a = To add anyone of the group to a secondary group.
-l = To change the login name from arkit to arkit_admin.
-L = To lock the user account. This will lock the password so we can’t use the account.
-m = moving the contents of the home directory from existing home dir to new dir.
-p = To Use un-encrypted password for the new password. (NOT Secured).
-s = Create a Specified shell for new accounts.
-u = Used to Assigned UID for the user account between 0 to 999.
-U = To unlock the user accounts. This will remove the password lock and allow us to use the user
account.
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 36
By Nischal Tech Support
Creating Groups:
Group information is located/stored on /etc/group file.
# groupadd <group name> - Create a group with specified name
# usermod –G <group name> <user name> - Add user to group
# gpasswd –a nischal Administrators – Adds the user nischal to the group Administrators
# gpasswd –A nischal Administrators – give user nischal administrative rights to the group
# gpasswd –d nischal Administrators – remove user nischal from the group Administrators
# groupdel <group name> - Delete group name
# groupmod –n <new group name> <old group name> - change group name
# newgrp - <group name> - Login into the group if successful, re-initializes the user environment
Permissions
chmod +rwx filename to add permissions
chmod -rwx directoryname to remove permissions.
chmod +x filename to allow executable permissions.
chmod -wx filename to take out write and executable permissions.
Note that “r” is for read, “w” is for write, and “x” is for execute.
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 37
By Nischal Tech Support
permission
There are three options for permission groups available to you in Linux. These are
owners: these permissions will only apply to owners and will not affect other
groups.
groups: you can assign a group of users specific permissions, which will
only impact users within the group.
all users: these permissions will apply to all users, and as a result, they
present the greatest security risk and should be assigned with caution
There are three kinds of file permissions in Linux:
Read (r): Allows a user or group to view a file.
Write (w): Permits the user to write or modify a file or directory.
Execute (x): A user or grup with execute permissions can execute a file or
view a directory.
Permissions:
There are two ways to use the commands. They are as follows:
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 38
By Nischal Tech Support
1. Numeric mode
In a numeric mode, file permissions do not denote as characters but as a three-digit
octal number. The following table provides the numbers for all permission types.
Number Character of Symbol Permission Type
0 --- No permission
1 --x Execute
2 -w- Write
3 -wx Write+Execute
4 r-- Read
5 r-x Read+Execute
6 rw- Read+Write
7 rwx Read+Write+Execute
2. Symbolic mode
In this mode, we can change permissions for all three owners. We can modify the
permissions of a specific owner. With the use of mathematical symbols, we can
modify the file permissions.
Operator Description
+ Adds permission to access directory or files
- Removes permissions
Sets permission and overrides the
=
permissions set earlier
Permission Value Number
Read r 4
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 39
By Nischal Tech Support
Write w 2
Execute x 1
Default permissions when you create a file or directory
Without any change in default umask permissions, all files created by user root will get 644 (666 - 022)
permissions and all directories will get 755 (777-022) permissions.
File Permissions
File 644
Directory 755
In above image explained about file permissions
Commands to Change file/directory permissions Symbolic
permissions
u -
user/owner g
- Group
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 40
By Nischal Tech Support
o -
Others w
Write x -
Execute
+ -
Allow
- - deny
# chmod [options] <mode/permissions> <file/directory> - to change permissions file/folder
Example: chmod 744 file1
# chmod u+rwx file or directory : in case of user only
# chmod ug+rwx file or directoty : in case of user and group
# chmod u+w,g+r,o+x directory/file
# chmod u+rw,g+rw directory/file
# chmod u-r, g-w,o-rw directory/file
# chmod ugo+rwx file/directory
# chmod ugo-rwx file/directory
# chown [options] <new owner> <file/directory> - to change ownership of file/folder
Example: chown user2 file1
chown user1:group1 file2
# chgrp [options] <new group> <file/directory> - to change group of file/folder
Example: chgrp gorup2 file2
Process Management
Command Description
bg To send a process to the background
fg To run a stopped process in the foreground
top Details on all Active Processes
ps Give the status of processes running for a user
ps PID Gives the status of a particular process
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 41
By Nischal Tech Support
pidof Gives the Process ID (PID) of a process
kill PID Kills a process
nice Starts a process with a given priority
renice Changes priority of an already running process
df Gives free hard disk space on your system
free Gives free RAM on your system
Killing the processes:
# kill -9 <PID> - kill the process using Process ID
# kill –a <name> - kill the process using process name
#ps - -it will display the process status correct terminal
Example:
ps –a (a = all the processes)
ps –f - full description of the process
ps –af
ps –u - for user processes
ps –x - system processes
ps –ax - all
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 42
By Nischal Tech Support
Service Management
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 43
By Nischal Tech Support
service state is the systemd units’ operational status. The most common ones are:
Enabled – active services running in the background.
Disabled – disabled services that users can enable directly using the start command.
Masked – stopped services that can only be started by removing the masked property.
Static – services that only run when another program or unit requires them.
Failed – inactive services that can’t load or operate properly.
systemctl status sshd to check the staus of service
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 44
By Nischal Tech Support
systemctl stop sshd to stop the service
systemctl disable sshd to disbale the service
systemctl enable sshd to senable the service
systemctl restart sshd to start and stop the service
systemctl start sshd to start the service
#systemctl list-units --type=service --state=running
Search Related Commands
Search commands will help you to search files/directories and file content.
The 'grep' command stands for "global regular expression print". grep command filters the content of a
file which makes our search easy.
Command GREP
Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple
times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,
with each such part on a separate output line.
-A n : Prints searched line and nlines after the result.
-B n : Prints searched line and n line before the result.
-C n : Prints searched line and n lines after before the result.
Grand regular expression, it is used to search the file name and content of the file
Command FIND
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 45
By Nischal Tech Support
It is command to find files with different options.
# find / -iname <file name> - to search files with file name
# find / -perm 770 - to search files with their permissions
# find / -user <user name> - to search files with user ownership
# find / -size 10M - to search files with their size
Command LOCATE
Locate command can be used to searching for the file based on the string name from / onwards.
locate command in Linux is used to find the files by name.
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 46