GitHub - Pushkar100 - notes-Linux-Admin - Linux Administration Notes & Quick Reference
GitHub - Pushkar100 - notes-Linux-Admin - Linux Administration Notes & Quick Reference
14 stars 6 forks
Star Notifications
master Go to file
View code
Translating Characters
Formatting output into columns
more and less commands
Copying files over the network
SCP
SFTP:
Customizing the Shell Prompt
Shell aliases
Environment variables
Viewing all the environment variables
Creating or modifying environment variables
Removing environment variables
Persisting the environment variables settings
Processes and job control
Displaying process information
Options for ps
Killing a currently running foreground process
Suspend a foreground process
Background processes
Starting a background process
Listing jobs
Forcing processes into the background
Killing Processes
Killing any process using PID
Killing any process using job number:
Signals have numbers associated with them
Summary
Scheduling repeated jobs with cron
Redirecting the output of the cron jobs
Using multiple values
crontab command
Switching users and running commands as others
Options for su
User identification commands
The sudo command
Executing commands with sudo
Switching users by using sudo su
Modifying the sudo configuration
Shell history
Viewing history
Setting the size of the history
Repeating commands from history
Reuse or pull out the arguments from the previous command
Searching for commands
Autocompletion
Installing & managing software
Package manager
The RPM format
Installing packages not included in the package manager
General package info commands
Installing on Debian Distros with the APT package format
The dpkg command used in addition to the apt utility
The Linux boot process
BIOS
Initial RAM Disk
The boot directory
Viewing the boot directory
The kernel ring buffer
Location of the kernel messages
Linux uses run Levels
Run levels
Setting the run level
systemd
Rebooting
The system log
Facilities
Severities
rsyslog
Caching vs non-caching
Disk management
Advantages of partitioning
Master Boot Record
GUID Partition Table
Mount points
Mount partitions over existing data
Mount points over other mount points
fdisk to create and modify partitions on a disk
CREATE an MBR partition
View all the existing partitions
DELETE a partition
SAVING all the partitions added or deleted in the fdisk utility
QUITTING without Saving
CREATING a GPT Partition
File systems
Create a file system
Mounting a device partition
Viewing the currently mounted file systems
Unmount a file System
Preparing a swap space
The file system table
Viewing labels and UUIDs of file systems
Labelling a file system
Managing users and groups
The root account
Passwords are stored in a shadow file
UIDs
GIDs
Comment field
Home directory
Shell
The /etc/shadow file
Creating a user account
Create a password for the created user
System or application accounts
The -m option
Deleting an account
Mpdify an existing account
Group details and creation
The /etc/gshadow file
Create groups
Delete a group
Modify a group:
Special permission modes
The setuid bit
Security measures
Octal permissions
Adding the setuid attribute to a file
Removing the setuid attribute from a file
Find all the files on the system that have setuid set
The setgid bit
Examples of commands using this setgid bit
Finding setgid files
Adding setgid permission
Removing the setgid attribute from a file
Adding both setuid and setgid
The sticky bit
Adding the sticky bit
Removing the sticky bit
Reading the ls command output
Networking
TCP/IP:
IPv4 Classes
Classless Inter-Domain Routing
Reserved private address space
Knowing the host computer IP address
Another way to determine the host IP address
DNS Hostnames
Domains
Viewing the hostname
Setting the hostname
Resolving DNS names
The hosts file
DHCP static and dynamic addressing
Ports
DHCP
Configuring a DHCP Client
Configuring an Ubuntu based System
GUI or TUI Tools for networking
Network troubleshooting
Test connectivity to a host with ping
Testing connectivity over Hops
Output of traceroute
Alternative to traceroute
The netstat command
Packet sniffing with tcpdump
The obsolete telnet command
Connecting via SSH to a Linux Virtual Machine
What is LINUX?:
A distribution differs from others in the type of software that it contains for a particular
application. Ex: Different Linux distributions might have different default browsers, but all
have a browser.
Distros/Flavors = Distributions. Ex: Red Hat Enterprise Linux, Ubuntu (Most Popular).
Red Hat = Popular in Banks, Airlines, Telecom and Healthcare sectors. (Red Hat - Need to
pay for license.)
Free Version of Red Hat for personal use = CentOS - A free brand of Red Hat Linux
(Other Distros: Linux Mint, Debian, Mageia, openSUSE, Fedora, ArchLinux, Slackware.)
NOTE: Distros are only slightly different from each other. Linux kernel is at the core of
every distro, so the main concepts are the same for all and learning to do something in one
distro is not very different from trying to do the same thing in another distro.
Linux Directories === Windows Folders (Folders and Directories are used interchangeably.)
Common Directories
/ => "root" (or, just slash ) is the top level of the file system hierarchy.
/opt => Optional or Third Party Software (Ex: Google Earth s files and executables)
/tmp => Temporary space, usually cleared on reboot(DONT have important stuff that
you want to SAVE)
/usr => User related programs.
/var => Variable Data, most notable being the log files (system log files.)
/usr :=
/home :=
/dev => Device Files, typically controlled by Operating Sytem and System
Administrators.
/export => Shared file systems.
/lost+found => Used by OS to recover files after a file system check has been
performed.
/proc => Provides information about running processes.
/sys => Used to display and sometimes configure the devices known to the Linux
Kernel.
Third Party Applications can be in /usr/local Directory: Some applications that are
NOT BUNDLED(Third Party) with the Linux OS by default are stored in the:
/usr/local directory. These application directories have their own Linux-Likes sub-
directory structure. Ex:
/opt/<application-name>/bin
/opt/<application-name>/etc
/opt/<application-name>/lib
NOTE:
/etc/opt/<application-name>
/var/opt/<application-name>
Sometimes when third-party applications are installed, they are not given their own
directory structure, but instead they are installed in a 'Shared' Manner. Ex:
/usr/local/bin/<application-name>
/usr/local/etc/<application-name>.conf
/usr/local/lib/<application-name>.so
We could use organisation/company name and store all the application(s)' files
belonging to a particular organization under one folder. Ex:
The Shell
It's the Default Interface to Linux. Programs that accepts commands and executes them.
Also called a Command Line Interpreter.
Command Line is more powerful that GUI. There will always bea command line. Server
Distributions do NOT include GUIs. Desktop Distributions have both GUI and CLI.
The Prompt
Waits for user to do something(Execute some command). Typically, for normal users the
prompt ends with '$' and for a superuser with a '#' (pound). Ex:
[pushkar@linuxsvr ~]$
[pushkar@linuxsvr ~]#
The '~' (tilde) represents your (the user's) HOME directory (Ex: /home/pushkar )
root account
superuser/root account is ALL POWERFUL. All other NORMAL accounts can only do a
SUBSET of the things that a superuser can do. (Note: root/superuser account is NOT to be
confused with the root(/) directory).
Note: sometimes you may have a root access and a normal account as well. ROOT
ACCOUNT'S HOME FOLDER EXPANDS TO: /root (= ~root )
Note: Some services have THEIR OWN ACCOUNTS (like ftp ): And hence, their own
HOME folder. Ex: /srv/ftp (= ~ftp )
Environment Variables
They are storage locations containing name and value pairs. They are typically in
UPPERCASE.
It controls the 'command search path' = Means that whenever we enter a command at the
command line, the system searches for that command in the command paths existing
inside the $PATH variable. If it finds it, executes the command, else moves onto the next
path and searches for it there and so on. If command is not found in any of these
directories, it returns a command-not-found error.
Ex:
(Searches for commands in every directory separated by a colon starting from the first one
that is listed).
Note: If the same command exists inside multiple paths, then the one found in an earlier
path is executed for that command.
which command
which
Searches for the path of a command. (Location of the file which runs on the command
execution). Ex:
help option/flag:
--help
ls --help
Linux Directories
Containers for other files and directories. They give a tree like structure to the file system.
Can be accessed by 'name' or a 'shortcut'(symbolic link or soft link)
$OLDPATH is an environment variable holding the path of the previous wroking directory.
Ex:
We can use the FULL(ABSOLUTE) PATH of the command (location of the command)
to execute it.
Syntax: /full/path/to/command
Ex:
/bin/cat datafile.txt => '/bin/cat' is the full path of the cat command.
(datafile.txt is in CWD)
Use ./command to execute the command residing in/relative to the current working
directory.
Ex:
rm -rf directory => Forcibly delete a directory(empty or not, all contents deleted -
recursively)
mkdir -p directory => Create Empty directories along with Parent directories(if
not existing).
Ex:
rmdir -p directory => Deletes empty directories including the specified empty
parent directories.
Ex:
rmdir -p one/two/three => Deletes the nested empty directories one , two and
three
NOTE: When you delete something from the CLI, it's gone forever. NO TRASH! from which
we can retrieve
ls -l => Long Listing (Permissions, number of links, owner, group, file size in bytes,
last modification time, file name)
ls -a => List Hidden files as well (files that begin with a '.' / period)
If name "ends" in: / => Directory, @ or -> => Link, * => Executable.
Others:
ls -d => List Directory names but not contents (of the listed directories).
tree command
tree
Similar to ls -R . But, it's more of a visual output only. (Using tree-like lines) Ex:
( tree command may not be available as a command by default. Will need to add it.)
Spaces in names
(or)
2. Use quotes('' or "") (Ex: cd 'My Notes.txt' )
Instead, try using: '-' (dashes), '_' (underscores), or camelCase. (Try to avoid spaces while
naming!)
File Permissions
First character
Permission Categories
u => user
g => group
o => other
a => all
Groups(g): Every user -> Belongs to at least one group. A user maybe part of multiple
groups. Groups are used to organize users.
Check all the groups which a user belongs to: groups (or) id -Gn (Same output for
both)
Changing Permissions
Modifying permissions
chmod
Ex:
chmod 761 data-file.txt , (rwx for user, only rw for group, only x for others)
chmod 400 data-file.txt , (read for user, no permissions for group or others)
chgrp
Changes the group that the file belongs to: chgrp <groupname> fileOrDirectory
Ex:
chgrp sales sales-data.txt (For example, we can even move the file to the
shared folder '/usr/local/sales' so that people belonging to the sales group can edit
the file there.)
NOTE: IF FILE PERMISSIONS SEEM CORRECT BUT YOU STILL CAN'T DO WHAT YOU WANT
TO DO, CHECK THE DIRECTORY PERMISSIONS, THEN THE PARENT DIRECTORY
PERMISSIONS.. AND SO ON UNTIL YOU FIND THE PERMISSION THAT NEEDS TO BE
UNBLOCKED OR UNTIL YOU REACH THE ROOT(/) DIRECTORY.
umask
The file creation mask decides what permissions must a file or directory have (by default)
when it is created!.
If no mask is set:
umask command
umask
umask "subtracts" permissions (opposite of chmod ): Ex: If base is '777' and mask is
'022', it would subtract 022 from 777. So, new permission = 755. (umask of 002 is ideal for
working with groups since it gives your group permission to work with files)
umask sometimes needs to MAKE APPROXIMATIONS: Ex: Base = 666 and umask = 007
then final file permission is '660' (and not 66-1)
Usage Examples:
umask -S => View the current umask setting in Symbolic Notation (Ex:
u=rwx,g=rx,o=rx [Displays the allowed permissions])
umask 002 => Changes the umask to 002 (popular way of changing permissions)
umask -S u=rwx,g=rx,o=rx
Note: Usually in umask and chmod the 4th MSbit is ignored. Ex: 0644 = 644, 0022 = 022.
But, The 4th MSBit can sometimes denote Special Modes: Ex:
1. setuid,
2. setgid,
3. sticky [Covered Later]
find command
find
Recursively finds files in the path that match the expression. If no arguments are supplied,
it finds all files in the current directory. (Ex: find )
Options:
-name pattern => Finds files and Directories that match that pattern.
-mtime days => Finds files that are 'days' old. ('+' => More than, '-' Less than )
-size nums => Find files that are size of 'num'. ('+' => More than, '-' Less than )
-newer file => Find files that are newer than 'file'
-type d => Find files that are of type d (directory) [@ => links, * => executable]
exec <command> {} \; => Run 'command' against all the files that are found.
Examples:
find => Recursively lists all files under the current directory.
find /sbin -name makedev = > Searches for files named 'makedev' inside '/sbin'
directory.
find /sbin -iname makedev = > Searches for files named 'makedev' inside '/sbin'
directory(IGNORE CASE)
find /sbin -name makedev = > Searches for files named 'makedev' inside '/sbin'
directory.
find /sbin -name *v = > Searches for files ending 'v' inside '/sbin' directory.
find /sbin -name makedev = > Searches for files named 'makedev' inside '/sbin'
directory.
find . -mtime +10 -mtime +13 = > Searches for files more than 10 days old but
less than 13 days old inside current(.) directory.
find . s* -ls => Recursively find anything that starts with 's' in CWD(.) and
perform ls on it.
find . -size +1M => Recursively find files in CWD(.) that are 1 MegaByte or larger.
(K = kilo, G = giga)
find . -newer file.txt => Searches for files that are newer than the file.txt
file(modif. time-wise)
find . exec file {} \; => Finds all files in the CWD and executes command 'file'
against all of them.
locate
Faster than find. Queries an index(adv.), but results are NOT in real-time.(disadv.) May NOT
be enabled on all systems.
Viewing & Editing Files
Basic commands:
less file => Display the more than the 'more' command (less is actually more!) ->
'q' to exit.
head [-x] file => Output the top portion(x lines) of the file (Default: 10 lines)
tail [-x] file => Output the bottom portion(x lines) of the file (Default: 10 lines)
To view the changes to a file in real-time, use tail -f but not 'cat'(not real-time).
Ex: log files being written to -> use 'tail'on that log file : tail gets updated as file grows (to
exit press 'CTRL-C')
Browsing through a 'more' or 'less' command screen => Same controls as in 'man' pages
(Refer 'man')
nano editor
nano
Small text editor. Easy to learn and use. Control commands appear on the screen itself.
Not very powerful.
vi editor
vi
More powerful than 'nano'. Requires a learning curve. Commands are Not intuitive.
vi commands:
Movement:
k - up one line
Inserting Text:
vi Line Mode:
:w - Writes(Saves) file
:x - same as ':wq'
Deleting Text:
D - Delete from the current position to end of line (Delete remaining text on line)
Replacing/Changing text:
c$ (or) C - change text from current position to the end of the line($)
u - Undo
<CTRL-R> - Redo
8. Searching:
n - go to Next match
N - go to Previous match
: - Line Mode
Need vim help? Type vimtutor and hit enter at the command prompt.
emacs editor
emacs
Also a powerful editor. Some people use vi, some use emacs. => Choose whatever you're
comfortable with.
M-<char> : means hold down either ALT while pressing character (or) means press
ESC key, release it, and then type a character.
C-h : Help
C-k : Kill(cut)
C-y : Yank(paste)
C-x u : undo
Graphical Editors
rm command: (remove)
cp command: (copy)
cp source_file destination_file => Copy source file to destination file
If destination directory does NOT exist, it gets created with the contents of the source
directory.
mv file1 file2 => Rename file1 to file2 (Overwrites file2 if it exists) [file1, file2 in same
folder] mv -i file1 file2 => Rename file1 to file2 (Asks to overwrite file2 if it exists)
[file1, file2 in same folder]
sort command
sort
sort sorts the text in a text file Alphabetically (by default) line by line.
Syntax: sort file (THE ORIGINAL FILE IS UNAFFECTED - THIS IS ONLY FOR PRINTING
TO SCREEN/STDOUT)
Options:
-kF => Sort by key supplied. F is the field number(column number) (Ex: sort -k2
file.txt => Sorts lines alphabetically according to the 2nd column on each line)
(bundle/archive)
tar does NOT need the hyphen (-) for options, but including it is optional (no harm!).
Create, extract or list contents of a tar archive using pattern, if supplied.
tar options:
v => Be Verbose
Usage examples:
tar cf tps.tar tpsreports => create(c) an archive (tps.tar) for this file(f) called
'tpsreports'
tar xf tps.tar => Extract(x) this file(f) 'tps.tar'
tar xfv taps.tar => Be verbose(give a listing of all the extracted files)
Compress Files
gzip file => Compress files and adds extension '.gz' to it (original file AFFECTED!)
du -k data.txt.gz => gives how much space data.txt.gz (the compressed file) is
using.
gunzip data.txt.gz => uncompresses data.txt.gz to data.txt (original state)
tar gzip
-z option of tar uses gzip for compression while archiving. (Uses '.tgz' or 'tar.gz'
extension)
Ex:
tar zcf tps.tgz tpsreports => Compresses(z) and archives(c) this file(f)
tpsreports into tps.tgz.
tar ztvf tps.tgz => Displays contents(t) of compressed(z) archive file(f) tps.tgz in
a verbose(v) way.
WildCards
(Already done in other courses, just SKIPPING them here) (Learn from notes of other,
previous courses)
File Descriptor number is like the number/id of the inputs or outputs. The machine uses
the numbers instead of 'standard input' (human readable form) to recognize input and
output
Redirection
>> => Redirects standard output to a file. (Appends to any existing contents)
Ex:
echo new line > file.txt => 'file.txt' contains the output of echo command
('new line').
ls -l > file.txt => 'file.txt' contains the output of ls -l command (nothing
printed on screen).
ls -l >> file.txt => Appends the output of ls -l command to 'file.txt' (nothing
printed on screen).
Ex:
sort < files.txt => sort works on input which is the content of 'file.txt' (In sort's
case it is the same as 'sort file.txt')
& => Used with redirection to signal that a file descriptor is being used.
Ex:
2>&1 : Redirecting standard error to standard output (Combines standard error and
standard output)
2>file : Redirect standard error to a file.
If you want to 'IGNORE/DISCARD' the output, you can send it to the Null Device('/dev/null'):
Ex:
ls here not-here 2> /dev/null [Don't want to see errors on screen nor save them
to a file]
ls -l 2> file.txt => Redirects std error to 'file.txt'(No space between 2 and >)[&
stdout to screen]
ls -l 1> file.txt => Redirects std output to 'file.txt'(No space between 1 and >)[&
stderr to screen]
Sending standard output to one file and standard error to another(or to the same). Ex:
ls existingFile not-here-file 1> out.txt 2> err.txt => Std. output (for
existingFile) goes to out.txt and Std error (for not-here-file) goes to err.txt
Combining standard output and standard error (redirect to the same file): Ex:
The above appends standard error to standard output, so both are saved into out.txt only
(not screen)
Ex:
ls here not-here > /dev/null 2>&1 => Appends standard error to standard
output, so both are sent to /dev/null (ignored)
Important: When NO file descriptor is used in redirection, Only the standard output is
redirected but the standard input is printed on the screen. Ex:
Sample Output:
The command is run with 'ipFileName' as input and the output of the command is saved to
'opFileName'
sdiff file1 file2 = Compare two files Side-by-Side (file1 : left, file2 : right).
vimdiff file1 file2 = Highlight differences in vim editor.
diff Example
diff
Output:
3c3
< this is a line in a file
---
> this is a line in a file
sdiff Example
sdiff
< => @beginning of a line indicates it is a line from file1 (line only exists in file1)
> => @beginning of a line indicates it is a line from file2 (line only exists in file1)
vimdiff Example
vimdiff
:qa! : Force Quit All (Force close both files!) - changes that you don't want to save.
Use the grep command to search inside files. grep displays Lines of a file matching a
pattern. (If we DON'T supply a file name grep uses the STANDARD INPUT to search
against.)
Options:
Ex:
grep o secret.txt => searches for 'o' in 'secret.txt' and prints the matching lines
(lines from the file that contain 'o')
grep -v user secret.txt => Matches all lines that do NOT contain 'user' in
'secret.txt' file.
grep -i User secret.txt => Matches all lines that contain 'user' in 'secret.txt' file.
(IGNORES CASE)
grep -n blah secret.txt => Matches all lines that contain 'blah' in 'secret.txt' file.
(PRINTS LINE NUM)
Ex:
To display printable strings contained in a binary file, use the string command.
Pipes or Pipelining
(The pipe takes the std. output of one command(left) and feeds it as std. input to other
command(right))
Only the standard output is sent as standard input to the next command. (use 2>&1 to
send standard error as well - check redirection topic)
cut command
cut
cut [file] => Cuts out selected portions of the file. (If file is omitted, uses
STANDARD INPUT). (Cut does NOT affect the original file.)
Ex:
cut -d' ' -f2 file1.txt => Selects column 2 from file1.txt using space as
delimiter between fields
grep bob /etc/passwd | cut -d: -f1,5 => cuts 1 and 5 ':' separated columns of
/etc/passwd.
Translating Characters
tr
Ex:
tr ":" " " file.txt => Translate all the ':' with spaces(' ') in 'file.txt'.
column
Ex:
column -t : Determine the number of columns the input contains and create a table.
(Space is the delimiter between columns by default)
Already learnt. (Refer earlier or previous notes) Keep in mind that these two commands
can also take take redirected inputs as well (STANDARD INPUT). Ex:
(THERE ARE MANY SMALL COMMANDS THAT DO ONE THING VERY WELL. WE CAN
CHAIN MANY OF THESE COMMANDS TOGETHER TO EXECUTE SOMETHING COMPLEX
AND POWERFUL)
To copy files between 'remote server and local host' (or) 'between two remote servers'.
Both SCP and SFTP are extensions of the 'SSH' (Secure Shell) Protocol.
(In SCP, we need to know what files are to be transferred while writing the
command/connecting.)
(In SFTP, we need NOT know before connecting, what files are going to be
transferred.)
Using SCP/SFTP:
Mac & Linux come with scp and sftp command line utilities (openSSH in the case of
MAC)
For Windows systems, we need to install a tool called 'putty' ['pscp.exe' and
'psftp.exe'].
SCP
Ex:
(We can use SSH or SFTP to check if the copied local files exist on the remote server now.)
SCP Options:
-v => We can use the -v parameter to print debug information into the screen.
-p => An estimated time and the connection speed will appear on the screen.
-C => The -C parameter will compress your files on the go, making the transfer
faster. (No further compression if file is already compressed. Ex: .zip, .rar, .iso, ... etc)
-p => Specify the Specific port to use. Ex: scp -P 2249 Label.pdf
[email protected]:. (we are using port 2249)
By default SCP using AES-128 to encrypt files. If you want to change to another cipher to
encrypt it, you can use -c parameter. Take a look of this command. Ex:
Limiting Bandwidth:
-l => limit the bandwidth to use. (It will be useful if you do an automation script to
copy a lot of file, but you don’t want the bandwidth is drained by the SCP process.) Ex:
scp -l 400 Label.pdf [email protected]:.
The 400 value behind “-l” parameter is mean that we limit the bandwidth for SCP process
only 50 KB/sec. One thing to remember that bandwidth is specified in Kilobits/sec (kbps).
It is mean that 8 bits equal with 1 byte. BUT, While SCP counts in KiloByte/sec (KB/s). So if
you want to limit your bandwidth for SCP maximum only 50 KB/s, you need to set it into 50
x 8 = 400.
SFTP:
sftp user@host => Start a secure file transfer session with host (host can be an IP
address as well) Ex:
SFTP Note/Points:
Once you successfully connect, you are at the command prompt of the remote server.
For example, pwd returns CWD on the server, ls returns ls of CWD on the server.
To use the commands for your local host computer while connected, precede
commands with an 'l',(stands for 'local').
So, to view the CWD on your local system, type lpwd , to list the files in the CWD of
your local system, type 'lls', .. and so on.
[NOTE: To transfer directories in put , mput , get or mget , user -r option (recursive)]
9. ? (or) help => SFTP help command screen shows the commands we can use to
accomplish various tasks.
10. cd => Changes directory on the Remote Server.
11. lcd => Changes directory on the Local System.
12. mkdir => Make a directory on the Remote Server.
13. lmkdir => Make a directory on the Local System.
14. rm => Remove files and Directories on the Remote Server.
15. rmdir => Remove empty Directories from the Remote Server.
16. exit or bye => Close/Terminate the SFTP the session.
17. chown , chgrp , chmod => All on the Remote Server.(NO command from SFTP to
change local permissions!)
18. lumask => This is the only permission related command in SFTP for the Local
System.
(Other utilities):
Using ftp means that your Login credentials are sent in plain text over the network. The
files that you download/upload are NOT encrypted either.
Bash Prompt:
Format Strings: (That can be placed within the prompt environment variable):
\d => Date in 'Weekday Month Day' format (Ex: Tue May 26)
\H => Hostname
\n => Newline
PS1 changes created on the prompt are gone after we quit the session.(Not available
for the next session).
So, We must add the PS1 changes as a line to the '~/.bash_profile' file.
(NOTE: personal initialization files, like .bash_profile, are also known as 'Dot Files' since
they begin with a '.')
Shell aliases
alias => List all of the current aliases that are set.
Aliases can also be used for adjusting commmon typing errors (ex: 'grpe' alias for
'grep')
Aliases can also be used to make Linux behave like another OS (ex: 'cls' alias for
'clear')
Removing Aliases:
Aliases create on the prompt are gone after we quit the session.(Not available for the
next session).
So, We need to add the alias command as a line to the '~/.bash_profile' file (just like
for shell prompt).
1. Manually: Insert something like alias cls="clear" into '~/.bash_profile' file. (OR)
2. Append to file: echo 'alias cls="clear"' >> ~/.bash_profile .
(NOTE: Keep your alias usage to a minimum. Because, working on a different system
where your aliases don't work might cripple you/slow you down/Need to copy your
configuration file to each system you work on)
Environment variables
(Use the man bash pages for more info on environment variables)
printenv ENV_VAR => Prints the value of the specified Environment Variable. (Case-
Sensitive!)
echo $ENV_VAR => Prints the value of specified Environment Variable. (prepend
name with a $).
Ex:
Ex:
The act of setting/unsetting the Environment Variables on the command line is NOT
persistent. (That is, the changes made to them won't be available for the subsequent
sessions). So, we must save the environment variables into the '~/.bash_profile' file.
Important NOTE:
Whenever the output of a command is too much/too long, we can pipe the output of
that command to a pager utility like 'less' or 'more': Ex:
Refreshing the terminal to include the changes made to '~/.bash_profile': To see the
changes take effect, run:
(This is a handy command that can be used instead of exiting and restarting the terminal)
who
whoami
Options for ps
ps
One of the main reasons for running ps is to get the Process ID (PID)
Common ps commands:
top => Interactive process viewer. (Press 'q' to exit, '?' for help)
htop => Interactive process viewer. (Less popular, may not be available by default on
the system)
The top command places the processes using most of the CPU and Memory resources
at the TOP of the list. It also displays the CPU and Memory usage columns.
Press <CTRL-C> on the CLI while the process is running. (Pressing this kills the
foreground process and return the shell prompt to the user)
Background processes
<command> & => Start the command in the background. (It Displays two numbers as
output => 'Job No.' in brackets[] and 'PID' (Ex: [1] 2373) ) Ex:
./some-long-running-script & => Starts the script in the background (Ex.
O/P: [1] [4232]) (We can view the status of the process by running ps -p 4232 )
Listing jobs
(Helps list all the currently active jobs => Usually lists the Background Processes)
jobs [%num] => List jobs. (No arguments? List all the active jobs)
(The '+' sign in the jobs output represents the current job. The '-' sign in the jobs output
represents the previous job.)
Killing Processes
(Job-wise/Process-wise)
Killing any process using PID
kill pid => Kill a process with Process ID 'pid'. (Default signal used by kill = TERM
(termination) | 15)
kill -sig pid => Send a signal 'sig' to a process (to kill a process).
kill 123 => kill process with ID 123. (Default signal used by kill = TERM
(termination) | 15)
- kill %jobnumber => Kills a process with job number 'jobnumber'. Ex: - kill %1 => Kills
process with job number 1.
kill -l => Displays a list of signals that can be sent to a process and the numbers
associated with them.
For Example:
Ex:
kill -15 123 => Same as kill process with ID 123 since default signal is -TERM (or
-15)
kill -TERM 123 => Same as kill process with ID 123 since default signal is -TERM
(or -15)
NOTE:: kill -9 123 => If a process does NOT terminate with default signal '15' - then
USE kill signal '9'.
Summary
cron
We can use cron to SCHEDULE and AUTOMATE tasks. The cron service STARTS when the
SYSTEM BOOTS and checks for SCHEDULED JOBS to be RUN EVERY MINUTE.
crontab => A program to create, read, update and delete your job schedules
crontab format: (config file that contains information about scheduled jobs)
1. When to run
2. What to run.
Format:
* * * * * command
| | | | |
| | | | + -- Day of the week (0-6) - Starting with 'sunday'(0)
| | | + ---- Month of the year (1-12)
| | + ------ Day of the month (1-31)
| + -------- Hour (0-23)
+ ---------- Minute (0-59)
0,30 * * * * /opt/acme/bin/half-hour-check
Another way to do the same thing: (Dividing the total minutes(60) by 2 => half hour)
*/2 * * * * /opt/acme/bin/half-hour-check
0-4 * * * * /opt/acme/bin/first-five-mins
@yearly => 0 0 1 1 * (Run once a year on the 1st day of the 1st month) (day can
be any weekday)
@annually => 0 0 1 1 * (Run once a year on the 1st day of the 1st month) (day
can e any weekday)
@monthly => 0 0 1 * * (Run once every month on the 1st day of the month) (day
can be any weekday)
@weekly => 0 0 * * 0 (Run once every sunday on every month) (sunday can be
any day)
@daily => 0 0 * * * (Run once every day at 00:00 (12AM), every month, any
weekday)
@midnight => 0 0 * * * (Run once every day at 00:00 (12AM), every month, any
weekday) (12AM = midnight)
@hourly => 0 * * * * (Run once every hour at start of hour (0th minute), every
day, month, any wkday)
Not all of these shortcuts might work on your linux distribution. (Use man cron to check)
crontab command
crontab
crontab file => INSTALL a new crontab from the contents of the file specified.
crontab -l => LIST your cron jobs. (No cron jobs? -> Ex. o/p = 'no crontab for
adminuser')
crontab -e => EDIT your cron jobs. (Invokes the editor specified in the '$EDITOR'
environment variable)
crontab -r => REMOVE all of your cron jobs.
Ex:
(Ex. contents: 0 7 * * 1 /opt/bin/weekly -> runs the weekly file every monday)
Once a cron job has been added to the crontab it is run at scheduled times and specified
file.
crontab -r => Deletes/Removes all the cron jobs(In this case, 'my-cron-file')
Therefore, cron service runs scheduled jobs and these jobs can be manipulated using the
crontab command.
su
su (or) su root => Become Superuser (The admin is usually the superuser of the
system)
su username => Switch to account having user name as 'username'.
Ex:
Options for su
su
1. - => A hyphen is used to provide an environment similar to what the user would
expect had he/she logged in directly.
For example, we end up in the HOME directory of the switched-in user. We can check the
environment variables set for the currently-switched-to user using '-'. Otherwise, we can
ONLY see environment variables associated with the previous user's account
export TEST=1
su oracle
echo $TEST # Returns 1 even if TEST was the environment variable of the
previous user's session.
export TEST=1
su - oracle # (Using '-' to set the environment similar to direct
login to terminal of the switched user)
echo $TEST # Returns nothing (Because $TEST was not set in this
(switched) user's session)
If command is more than one word in length, surround it with quotes('' or ""). Ex: su -c
"ls -l" . Ex: - su -c COMMAND anotherusername : Runs COMMAND as anotherusername
[DOES NOT SWITCH USER] - su -c COMMAND - anotherusername : Runs COMMAND as
anotherusername (& has access to his/her environment variables). DOES NOT SWITCH
USER.
NOTE:: Alternate way to execute commands as another user account is to use the sudo
command (later)
whoami => To know the effective username. To know what user you are logged in as.
Ex:
su oracle
who => Lists all the users currently logged onto the system.
sudo
It is commonly used to install, start and stop applications that require the 'root' user
privileges.
Note: On running sudo , you might be prompted for a password(once in a session), and
you have to give the USER'S PASSWORD (& NOT the root password) - And if the user has
root permissions, the sudo command is executed.
sudo -u user <command> => Run the command as user 'user' ( -u is used to
specify the user.)
Alternatively:
sudo -u user -s => Starts a shell as 'user' (from the specified account).
Examples:
visudo => Edit the '/etc/sudoers' file. (We need root access to execute visudo )
Therefore, switch to root and run visudo (OR) run sudo visudo from current account
The visudo file format: There are many lines of code in the visudo file but one common
type of line encountered is - Syntax:
user host=(users)[NOPASSWD:]commands
user : username of an account,
host : system name/ host name,
NOPASSWD:: 'Optional' parameter suggesting that this user does NOT need a
passwrod to run these commands.,
commands : Contains all the commands the user can run(Multiple commands
are comma(,) separated)
Ex:
adminuser ALL=(ALL)NOPASSWD:ALL ,
Note: If you ever forget to run a command with sudo , you can run the command again
with:
!! refers to the last command in the command history and runs that command with
superuser/root permission.
Running a previous command with sudo provided the command starts with a particular
string:
sudo !u => Runs the most recently executed command in history that starts with 'u'
from the root/superuser account.
Summary:
Shell history
All executed commands are added to the shell history, which can be displayed and
recalled. Shell history is stored in Memory and on Disk. Commands can be stored in one
these files (depends on shell):
~/.bash_history , (BASH SHELL)
~/.history ,
~/.histfile
Viewing history
history
history => Displays the shell history (each line contains a command along with a
serial/command number).
The HISTSIZE environment variable controls the number of commands in the history.(500
by default)
export HISTSIZE=1000 => Sets the history size to 1000. (Can place this line in
'~/.bash_profile' to persist changes)
!string => Repeat the MOST RECENT comman STARTING with 'string'. (Ex: !gre
=> maybe executes 'grep')
!:N => Pulls out Nth argument from the previous command. :N - Represents a
word on the command line . 0 - command, 1 - first argument, ... etc. Ex:
head file1 file2 file3
vi !:2 => Pulls out argument 2 from previous command (file2) and opens it in the
vi Editor
Another example:
echo !:2 !ch:2 => Pulls out 2nd argument to previous command and 2d argument
to the most recently used command that started with ch (Maybe chown, chgrp,
chmod, .. etc).
Shortcuts:
!^ => pulls out FIRST argument to the previous command. ( !^ <=> !:1 )
!$ => pulls out LAST argument to the previous command. ( !^ <=> !:N where N is
the Nth among N args)
!* => Reuse ALL the arguments to the previous command in the current command.
(Ex: grep !* )
Ex:
<CTRL-R> => Reverse search the shell history. (Matches typed pattern with
commands in history)
<ENTER> - Execute the command
Autocompletion
1. Data, and
2. Metadata: Package Description, Version, & Dependencies.
Package manager
RPM
For installing Software on RPM Distros: RedHat, CentOS, Fedora, Oracle Linux, Scientific
Linux. The yum command is a package manager utility for the distros supporting RPM
format:
yum search string => Search for packages (online, included in the pkg mgr)
matching the 'string'.
yum info [package] => Display information.
Options:
-i : Install package,
-v : Verbose,
Examples(yum):
yum search inkscape => searches online for matching 'inkscape' packages (from
mirrors)
yum info inkscape-docs.x84_64 => Gets info on a particular package (Ex: one of
matched packages in search) (info gives a brief descrption and specs to help
understand what the package is)
If installation/removal requires superuser access: (run su -s and switch or sudo the
command)
sudo yum install inkscape => Installs the 'inkscape' package (With prompt asking
for yes(y)/no(n)) (OR)
sudo yum install -y inkscape => Installs the 'inkscape' package (Without prompt)
sudo yum remove inkscape => Removes the 'inkscape' package (With prompt
asking for yes(y)/no(n)) (OR)
sudo yum remove inkscape => Removes the 'inkscape' package (Without prompt)
These are the applications/software that are not yum search able. In that case, we must:
1. Goto the website and directly download the .rpm package file for the
application(GOES TO '~/Downloads'),
2. Run the rpm commands to install (NOT the yum commands) Ex:
rpm -qa | sort | less => Displays all the installed package in alphabetical order
on the less pager.
rpm -qf /usr/bin/which => Displays to what package a file belongs to(Ex.o/p:
which-2.20-7.el7.x86_64 )
rpm -ql which => Lists all the files that are part of the 'which' package
Note:
1. While installing a package, it also installs All the Other Packages that this Package
depends on.
2. Use the which package-name command to check if the package was
installed(returns location of it)]
apt
The 'Debian' distros do NOT use '.rpm' packages but uses .apt instead. (Debian distros
also includes 'Linux Mint' and 'Ubuntu')
apt is composed of a few smaller utilities, two of the most famous of them being
apt-cache and apt-get .
Commands:
apt-cache search string => Searches for a package (online, included in the pkg
mgr) matching the 'string'.
apt-get install [-y] package => Installs the package. If -y is supplied, it does
NOT prompt for a y/n.
apt-get remove package => Remove a package from the system. (Leave any
configuration files undeleted)
apt-get purge package => Remove a package from the system and delete the
configuration files also.
dpkg
Installing from '.deb' that was downloaded to the system - similar to directly installing
.rpm pkgs.
dpkg -L package => List all the files in the package. (Capital L option)
Note:
1. While installing a package, it also installs All the Other Packages that this Package
depends on.
2. Use the which package-name command to check if the package was
installed(returns location of it)]
BIOS
It is Operating System Independent (Applies to all OSes and not just Linux)
BIOS - performs the POST (Power-On Self Test) which basically tests CPU, MEMORY,
etc.
Only if the POST succeeds does the BIOS load the 'Boot Loader'.
BIOS - knows about different 'Boot devices' : Like the Hard Drives, USB Drives, DVD
Drives, etc.
The BIOS searches the above list for a 'bootable device' in the order specified.
The boot device search order can be changed (Interrupt the boot sequence and enter
into an interactive mode).
The key sequence to do this (change boot device search order) varies from one
hardware manufacturer to another.
(Ex: F2 opens the setup in some systems. You might have to press F12 and then enable
F2 key from the options.)
Once the 'Bootable Device' is found, the BIOS will run the 'Boot Loader'. This is typically the
'GRUB' (Grand Unified Bootloader) is used. But, on older Linux systems you may find 'LILO'
(Linux Loader). The primary purpose of the 'Boot Loader' is to START the OPERATING
SYSTEM. Boot loaders could start the Operating System with Different Options. (If there
are multiple OSes installed, we can tell the Boot loader which OS to load/run.)
Once the real OS filesystem has been mounted by initrd , its job is done and the loading
process continues from the real Operating System File System.
/boot
1. initrd
2. kernel (The Linux Kernel)
3. Boot Loader Configuration
NOTE: You can use -F (in ls command) which classifies the file with different special
character for different kind of files: - / – directory. - nothing – normal file. - @ – link file. - *
– Executable file
The ring buffer is a data structure maintained by the kernel to store messages from the
kernel. It is of fixed size and older messages get deleted when new ones are added. Get
kernel messages by executing the command:
dmesg (or)
dmesg -T (Also displays the time of the message in human-readable format). The
message log contains even the earliest messages that fly away quickly during boot
process
var/log/dmesg => (Viewing this file is equivalent to running the dmesg command)
This files contains all kernel messages from start to now, unlike the ring buffer's
dmesg command.
Run levels
Traditionally run levels were controlled by the 'init' program. The File containing 'init'
configurations is: /etc/inittab .
NOTE: 'init' is slowly being phased out by other utilities like 'systemd'.
systemd
systemd
Uses 'targets' instead of run levels. (targets are roughly equivalent to run levels). To get the
list of available 'targets', look inside: '/lib/systemd/system' (Ex: ls -l
/lib/systemd/system , ls -l /lib/systemd/system/runlevel5.target )
Run level targets are actually 'symlinks' to the real targets being used.
To get/view the System default run-level target:
To change the target/run level target: (NOT the default run level target)
Rebooting
telinit 6 (or)
systemctl isolate reboot.target to reboot the system, We can also use system
command:
Even though we can use: telinit 0 to shutdown, there exists commands to shutdown
the system:
'time' formats:
'message': This is a broadcast message sent to all users on the system that it is being
shutdown/rebooted. (All logged-in users are notified that the system is going down, and
login operations are blocked.)
Ex:
shutdown -r now
1. telinit 0
2. systemctl isolate poweroff.target (Selects the 'poweroff' target)
3. poweroff (Simple command that can be executed at the CLI to power off the
system)
Aids in the process of messages. (Each process need not have to create its own log files).
Allows logging to be centrally controlled. Uses facilities and severities to categorize
messages.
Facilities
What type of program / what place in the system the message originated from.
Severities
0 Emergency emerg(panic) System is unusable
1 Alert alert Take action immediately
2 Critical crit Critical Conditions
3 Error err (error) Error conditions
4 Warning warning (warn) Warning conditions
5 Notice notice Normal but significant
condition
6 Info info Informational messages
7 Debug debug Debug-level messages
rsyslog
rsyslog
Logging rules:
'' severity for all[Ex: 'mail.' <=> 'mail'] (Wildcards supported for both facilities and
severities),
'none' severity for none[Ex: mail.none],
'mail.emerg;ftp.err;cron.info' => Match multiple severities with semicolon(;)
Caching vs non-caching
Caching is used if the path starts with a hyphen(-) Ex: 'mail.*' logs saved to
'-/var/log/mail.info'
You may lose some messages during a system crash if you are using the crash mode.
Using caching mode can improve I/O performance.
Lower severities are cached while higher severities are not cached.
Ex:
sudo tail -1 /var/log/mail.log => (Sample o/p: 'Apr 4 14:33:16 linuxsvr mailtest:
Test.')
NOTE: logrotate command => Did not learn (go back to videos if you wish to learn)
Disk management
Disks can be divided into parts - called Partitions. Partitions allow you to separate data.
Participation Schemes: Ex:
Advantages of partitioning
Can protect the overall system. Keep users from creating outages by using a home
directory partition. (Ex: If the system runs a web server, we can partition OS and the server
on the disk, so damage/outage in one won't affect the other, esp. the OS will still keep
running)
(MBR)
MBR - It's a 'boot sector' that exists at the beginning of partitioned computer mass
storage devices like fixed disks or removable drives.
MBR = Boot Sector (sectors, tracks, cylinders ...) at the beginning of a storage device
MBR contains information about how the 'logical partitions' are 'organized' on the
disk. The information is contained in a Partition Table.
MBR allows UPTO '4' PRIMARY partitions.
If you want to use more than 4, we need to use an 'Extended Partition'. An Extended
Partition is a special kind of primary partition that is used as a 'container' for OTHER
partitions. (Hence, create unlimited number of partitions inside the extended partition)
(GPT)
GPT Supports:
(GPT NOT supported by older OSes and May require Newer or Special Tools)
Mount points
A mount point is simply a DIRECTORY that is USED to ACCESS THE DATA on a Partition.
'/' (slash) => It is always a Mount Point.(At least 1 Partition is mounted on the '/'
directory). Any other additional partitions are mounted Inside the '/' Directory Tree.
Ex: If we allocated a partition to the '/home' directory (mounted), then all the files and
directories inside it can be found under that partition. (Ex: '/home/jason' is on the partition
mounted on '/home')
If we, say, umounted (remove) the /home partition and instead allocate it to the
'/export/home' direcory (mount) then all the files inside '/home' will be available under the
mounted partition of '/export/home'. (Ex: '/export/home/jason' available under the
mounted partition '/export/home')
We can mount partitions over existing data. For example, if files(or directories) were create
inside '/home' before the '/home' partition was mounted/create, those files will NOT be
accessible after '/home' is mounted as a partition. They will exist but you not be able to
access them.
Ex: Assume '/home' is not mounted and '/' is the existing mount:
mkdir /home/sarah
You will not be able to access '/home/sarah' now. Data for that folder exists on '/' partition.
Therefore, We cannot access the '/home/sarah' folder from the '/home' partition.
unmount /home (Removing the '/home' partition, so files inside belong once again to
'/' mount). You can now access '/home/sarah' once again since mount was '/' when
'sarah' directory was created
This is possible. For Example: If '/home' is a mount point, we can create another mount
point '/home/jason' over the existing '/home' mount point. (The important thing to note is
that '/home' must be mounted BEFORE mounting '/home/jason'!).
fdisk
fdisk is a standard linux tool or a utility that has been traditionally used to CREATE and
MODIFY PARTITONS on a Disk. (Alternatives: gdisk or parted )
To manage the partitions on a disk using the fdisk utility, simply provide the 'path' to the
'device' you wish to manage as an argument to the command, Ex:
fdisk /path/to/device
fdisk -l => Displays a list of available devices('disks') and all the 'partitions' they
contain. You may like to use fdisk -l | less . (The above will list the disks and the
partitions that they have, if any.)
fdisk -l /dev/sda => Displays a specific disk device's partitions (and its nested
partitions).
fdisk /dev/sdb => Opens the command utility for '/dev/sdb' disk device (Use 'm' for
commands help) (Once you run this command, the fdisk utility opens up, with its
own commands:)
Prompted to press 'p' for primary partition (or) 'e' for extended. (You chose 'p' -
primary, say)
It will ask you to choose partition number, 1 to 4. (Default is partition 1) (You chose 1,
say)
It will prompt you to select a start address from X-to-Y. (Default is X) (You chose X,
say)
It will ask you for the size of the partition. Format to enter: +NS (Ex: You typed '+1G',
say)
(S is size = K for KiloBytes, M for MegaBytes, G for GigaBytes) (N is the quanity. Ex: '+1G'
means you chose 1 GigaByte of data from start address(X) for that partition)
Once you create a partition 'x' (1 <= x <= 4), the next partition will ask you to select a
partition number from '1-4 excluding x'. Suppose you selected 1 initially then next time it
will ask you to select a partition number from 2-4.
The default size for any partition is the full remaining size that you have left. For example
you may create 3 partitions of sizes 1GB, 2GB, and for the third one just hit <enter> and
the default/remaining size is selected for it.
Note: Default partition type created is 'Linux' and represented by the number '83'.
Type 't'
Output is 'selected partition is 1'
You are prompted for the Partition number(A hex number) [Says type 'L' for help with
partition types]
Type 'l' to see the list of partitions and their numbers.
(Say, Linux is 83 and you want to change to 'linux Swap' type, which is '82')
You are prompted again for the Partition type Hex number. You Type '82' (say)
Sample Output: Changed type of partition 'Linux' to 'Linux Swap / Solaris'
Note: You may repeat step (A) and, optionally step (B), for subsequent partitions you may
want to create (and change the type of).
(Partition Table)
Type p .
DELETE a partition
(Inside the chosen disk)
Type 'd'
As we enter ‘d‘, it will prompt me to enter partition number that we want to delete from
disk.(Ex: '4')
It will delete that partition number (Ex: '4') on disk and shows free space in partition
table.
(Also EXITING/QUITTING)
Type w : It saves all the partitions it showed in the partition table (whatever we
added/deleted) and quits the utility back to the command prompt.
Type q .
1. Type 'g': (Prints message that you 'building a new GPT disklabel')
File systems
ext : Extendeded file system was create specifically for linux and is the default(ext2,
ext3, ext4 are later releases)
mkfs -t TYPE DEVICE => Creates a file system of specified TYPE on the mentioned
disk DEVICE. (DEVICE: path to the partition where you want the file system to reside).
(Ex: mkfs -t ext3 /dev/sdb2 )
Note: We may also use dot(.) notation instead of -t : Ex. mkfs.ext4 /dev/sdb3
'mkfs' help: man mkfs.ext2 to find more info about the ext2 file system creation
commands.
mount DEVICE MOUNT_POINT => Mounts a device partition to the directory specified.
Ex:
mount => No Args - Therefore, mount displays all the filesystems (physical as well as
virtual file systems.)
In order to makes mounts persist between reboots, add an entry in the '/etc/fstab' file.
( umount command)
umount DEVICE_OR_MOUNT_POINT
Ex:
Instead of creating a file system and mounting it, we can create a 'Swap Area' and 'Enable'
it.
swapon DEVICE => Enables the created swap space.(Ex: swapon /dev/sdb1 )
swapon -s => Displays the swap devices in use.
/etc/fstab
Controls what devices get mounted and where on boot. Each entry(one line) has 6 fields:
Example Entries:
UUID=dbae4fe7-b06f-4319-85dc-b93ba4a16b17 /
xfs defaults 0 1
You may ignore the dump utility column (leave it at 0) if you do not use it to backup
filesystems.Good practice to set fsck of '/' to 1 and remaining FSes to '2'
Linux is a multi-user OS. The multi-users can also use the system at the Same Time! Each
user account has the follwoing fields associated:
username:password:UID:GID:comments:home_dir:shell
joe:x:1000:1000:Joe Henderson:/home/joe:/bin/bash
NOTE!: password is 'x' - Exncrypted password is actually stored in the '/etx/shadow' file.
Note:
'/etc/shadow'
UIDs
The root/superuser account always has UID = 0 . UIDs are unique numbers. System
accounts typically have UIDs less than 1000 (< 1000). (Configured in '/etc/login.defs')
GIDs
The GID listed in the '/etc/passwd' file is the default group for an account. New files
belong to a user's Default group. Users can switch groups using the newgrp command.
(This can be done before creating new files for the new group)
Note: Systems or applications also have accounts - viewable inside the '/etc/passwd' file.
Comment field
Home directory
Upon login, the user is placed inside his HOME directory (Ex: '/home/jason' for user
'jason'). If this directory does NOT exist then he is placed in the root directory('/').
Shell
The shell will be executed when a user logs in. List of available shells are in '/etc/shells'.
The shell does NOT have to be a shell:
Example: To prevent the interactive use of an account, use : '/usr/sbin/nologin' (or)
'/bin/false' as the shell. (In the above, No one can execute the shell interactively, but only
execute a menu-driven application that only gives them access to certain actions). Shells
can be command line applications.
Format:
username:encryptedpass:dayssincepasswordchanged:numdaysbeforewhichpasswordmust
bechanged:daystochangepass(99999-
neverchange):daystowarnusertochangepass:numdaysafterpasswordexpiredtodisableac
ct:numdayssinceacctdisabled:futureuse
Ex: root:$@234524#242Dde#$3:16502:0:99999:7:::
Options:
Note:: Specify option -u UID to explicitly set the UID of the account being created. Ex: -
u 97 .
Ex:
passwd grant => System asks for password for the user 'grant' (and a retype to
confirm).
Note: The created user entry and his password are "Appended" to the '/etc/passwd' and
'/etc/shadow' files respectively.
Not every account is meant to be for a user. Some accounts exist to run applications or
perform system functions. Examples of these accounts include those that run web server
processes, database server processes, etc.
Extra Options:
Ex:
The -m option
-m
When using the -m option, the Home directory for the user is created. The contents of
'/etc/skel' (stands for 'skeleton') are copied into the User's Home directory. This '/etc/skel'
contains shell "configuration files" ('.profile', '.bashrc', etc)
Deleting an account
Ex:
userdel grant => Deletes user 'grant' from system but keeps his home folder un-
deleted.
userdel -r grant => Deletes user 'grant' from system and also deletes his home
folder. (The -r also removes the user's mailspool file if it exists.)
Ex:
usermod -c "MYSQL User" mysql => Updates comment associated with a MySQL
account.
FIRST entry in the '/etc/group' file is the 'Root Group'. Ex: root:x:0:
IMPORTANT NOTE: Users whose default is a certain group are NOT shown in the entry for
that group in '/etc/group' file. BUT, we can check the '/etc/passwd' file to find the user's
default group (or) run groups user-name .
/etc/gshadow
groups root => Displays all groups that root belongs to.
groups => Displays your(currently logged in user) groups (groups that you as the
user belong to)
Create groups
Ex:
groupadd -g 2500 db => Adss the 'db' group and also explicitly sets the GID to 2500.
Delete a group
groupdel GROUP_NAME
Ex:
Modify a group:
Options are:
When we start a process(execution), it runs using the User's UID and GID (we may have run
it as others used 'su'/'sudo' etc for root, doesn't matter.)
setuid
ls -l => -rwsrw-r-x .. The 's' in the Owner's execution field(x) tells that setuid is
enabled.
Examples of commands and files that run with setuid/as owner of the file:
Security measures
Octal permissions
Good: 4755 or below, Bad: 4775, Really bad: 4777 (anyone can edit the file!)
(Ex: 4775 or 4777 is what an attacker hopes to find in your system if they break in! - they
can do anything they want to that file and maybe get root permissions.)
Find all the files on the system that have setuid set
setgid
setgid => Set Group ID upon execution. (Ex: -rwxr-sr-x .. => The execution bit(x) of
the 'group' is set to 's' - setgid enabled)
/usr/bin/wall : anybody who can edit this file can write whatever they want to the
terminal(check).
NOTE:
1. Setting the 'setgid' on a Directory causes: 'New' Files & Directories inside the directory
to "inherit" the group of the directory. (Pre-existing files/directories within the directory
are NOT affected by the setgid.)
2. Because of the above point, 'setgid' is "great for working with 'groups'". We can create
a folder with a group's GID and appropriate/desired group permissions to the
directory. So, whatever is added/deleted/modified inside the directory can be
accessed by everyone belonging to the group (Shared folder).
** THIRD PARTY TOOLS TO CHECK FOR SETUID AND SETGID ON FILES (alternatives to
'find'): **
Used on a directory to ONLY allow the OWNER of the file/directory to RENAME (or) DELETE
the file. Without the sticky bit set, another user to delete a user's files IF the
permissions(777, say) allowed for it. Sticky Bit reperesented by 't' on others(o). (Ex: -
rwxr-xr-t ... )
(You would typically set sticky bit on 777 permissions because that is where it makes
sense to use the sticky bit to only allow user to rename/delete the files/directories even
when everyone else has permissions for it.)
Capitalized special permission bit => Means underlying normal permissions are NOT set.
Ex: -rwSr-xr-- ..
Ex: -rwxr-xr-T ..
Lowercase special permission bit => Means underlying normal permissions are SET.
README.md
Ex: -rwsr-xr-- ..
Ex: -rwxr-xr-t ..
Networking
TCP/IP:
IPv4 Classes
CIDR
Dividing networks irrespective of their classes. Division depends on subnet mask. Ex:
Any of these IP address entries in the hosts file (/etc/hosts) is considered private and non-
routable publicly.
1. lo: inet:127.0.0.1 => Your loopback address. (lo stands for 'loopback')
2. eth0: inet:192.168.1.122/24 => Actually hardware NIC device - has an IP address
associated.
ifconfig
Another way to determine host's IP addresses. (DEPRECATED, but still very useful-maybe
around for sometime)
Command: ifconfig => Displays all the IP addresses associated with the computer.
Terms:
Domains
(To the LEFT of the TLDs (Below the TLDs in the tree) (Ex: 'mycompany' in
mycompany.com)
Domains can be further sub-divided into: Sub-Domain => To the LEFT of the Domains
(Below the Domains in the tree) (Ex: 'webprod1' in webprod1.mycompany.com)
hostname (or)
uname -n (or)
hostname -f
hostname HOST_NAME => sets the host name to specified argument(ex: hostname
webprod02 )
1. UBUNTU AND REDHAT SYSTEMS: echo 'webprod02' > /etc/hostname (or, edit the
file and put the hostname as a line) (or)
2. FOR EARLIER VERSIONS OF REDHAT: Save the line 'HOSTNAME=webprod02' in
'/etc/sysconfig/network' file
host HOSTNAME => Displays the IP for the hostname (Ex: for the hostname
'www.mycompany.com')
host IPADDRESS => Displays the Hostname for the IP (Ex: for the IP '11.2.255.143')
Contains a list of IP addresses and Hostnames. We can add hosts as an entry to the file:
Format: ipaddress FQDN alias(es) => Maps IP address to hostname (or hostnames)
Ex:
Points:
(THIS CAN BE USEFUL IF YOU WANT TO ACCESS COMPUTERS THAT DON'T HAVE
DNS HOSTNAMES(for ex))
(HOSTNAMES IN THE '/etc/hosts' IS USED TO OVERRIDE THE DNS HOSTNAMES FOR
THE SYSTEM - Ex. you can have a private network for a cluster of web servers that
you own that only they and no one else can access - Create private IP addresses for
each of the servers in the '/etc/hosts' file thus forcing each of the servers to go
through the private network to communicate with each other.)
Note: '/etc/hosts' file is LOCAL to your Linux System. It does NOT propagate to the Rest of
the Network.
NOTE:: The '/etc/hosts' file is checked first before the DNS is queried.(for search
resolutions). We can change this lookup/search resolution order in the
'/etc/nsswitch.conf' file.(controls the search order for resolutions)
hosts: files dns => (If IP address is found in /etc/hosts, it is used. Search stops.
Else, check DNS)
hosts: files nis dns => (First check in files, then NIS, then DNS)
Ports
Ports above 1023(1024+) can be opened and used by normal users on the system(need
not be root/superuser) (1024+ => Unprivileged Ports)
Port Names: '/etc/services'. Maps port names to port numbers (Human readable port
names) Ex:
Sometimes, when a third party service is installed, we can ADD a port number and name
for the service it provides in the '/etc/services' file. (Therefore, we can also set port
numbers for the custom applications/services that we write)
DHCP
Dynamic Host Control Protocol. When a DHCP (host) client wants an IP address to itself, it
sends out a B/C msg looking for DHCP Servers to assign it an IP address. 'DHCP Servers'
assign IP address to DHCP Clients.
1. IP address
2. netmask
3. gateway
4. DNS servers
The DHCP client then configures itself with this information and communicates with
others using the given IP.
Each IP is 'leased' from the pool of IP addresses that the DHCP server manages.(The lease
expiration time is configurable on the DHCP server. 1hr, 1day, 1Weeks. The client must
renew the Ip address if it wantsto continue using it. Otherwise, the IP address is available
to other DHCP clients for use.)
To Edit a Red Hat based system as a DHCP Client, edit the configuration file located in:
/etc/sysconfig/network-scripts/ifcfg-DEVICE . Ex:
/etc/sysconfig/network-scripts/ifcfg-eth0 ,
/etc/sysconfig/network-scripts/ifcfg-enp5s2
ifconfig -a (or)
ip link
Once you have identified the configuration file for the network device: Set the
'BOOTPROTO' variable to 'dhcp':
BOOTPROTO=dhcp
Edit the '/etc/network/interfaces' file. Set a network device as a DHCP Client: Add line
iface NETWORK_DEVICE inet dhcp Ex:
DEVICE=eth0
BOOTPROTO=static (This is a MUST!!)
IPADDR=10.109.155.174 (Assign the IP, NW and BC)
NETMASK=255.255.255.0
NETWORK=10.109.155.0
BROADCAST=10.109.155.255
GATEWAY=10.109.155.1
ONBOOT=yes (To set the Ip address on
boot? yes)
2. Setting a STATIC IP address on UBUNTU Based system(RHEL):
(OR)
NOTE: Bring the interface up(enabled with the given static ip): ip link set
NETWORK_DEVICE up => Enables/sets up N/w Device with given IP (Ex: ip link set
eth0 up )
Ex:
NOTE: Bring the interface up(enabled with the given static ip): ifonfig NETWORK_DEVICE
up => Enables/sets up N/w Device with given IP (Ex: ifonfig eth0 up )
ifup and ifdown => Quick way to bring a NW device up or down. It takes the
network specs(IP, mask, etc) for the NW Device from the "configuration files" and
enables/disables it. (/etc/sysconfig/... etc)
Ex:
ifup NW_DEVICE => brings up the network device (Ex: ifup eth0 )
ifdown NW_DEVICE => brings down the network device (Ex: ifup enp5s02 )
GUI or TUI Tools for networking
Network troubleshooting
Some of the common tools for network diagnostics. Cannot rely on only one tool/ use
many tool.
ping
Sends one or more ICMP packets to a host (Hostname (or) IP-ADDRESS) and waits for a
reply
ping HOST => Continuously pings the host until you stop program with <CTRL-C>
(ex: ping google.com )
ping -c COUNT HOST => Specifies the number of packets to send with ping (stops
after sending these) (Ex: ping -c 3 google.com , ping -c 3 10.1.244.101 , .. etc.)
Ping returns the no of packets sent and Round Trip time(RTT) for each packet( '/'
separated ) - In case of no replies from host: 100% packet loss is displayed in output.
Note: Ping also resolves the Hostname to IP address (If it cant => Unknown host error
displayed - In that use IP address of system that you are trying to connect to.)
1. Check if ping works to a local host in the network. If that also does NOT work then
maybe there is a problem with OUR SYSTEM(OUR HOST) itself. Ex: Network cables
got disconnected, NW drivers didn't get upgraded when Server System was upgraded,
... etc.
2. If we can successfully ping a host within our local network: Then the problem lies
outside of our network and definitely not on our host(our computer). If we can
successfully ping other external hosts, the problem might be with one particular host
that w pinged initially. (Ex: google.com ping fails but youtube.com and facebook.com
pings are successful). POSSIBLE REASON: The destination host has a 'firewall' that
has blocked/discarded icmp requests and responses. In this case, it will require other
diagnostic tools other than 'ping'.
Testing connectivity over Hops
Use the traceroute command. ( ping only gives you the end to end connectivity info)
traceroute -n HOST_NAME => Skips the DNS server and directly to IP of host
(Ex: traceroute -n google.com )
Advantages:
skips DNS - If issue was with DNS server then we will know.
Faster.
Output of traceroute
Lists all the router IPs along the way(route) along witht the milliseconds it took for the
packets to cross that network. Too much time? => Maybe problem is in that network. '*' for
time => Either n/w not responding (or, router configured to not show traceroute - use other
diagnostic tool)
Alternative to traceroute
tracepath
Ex:
tracepath -n google.com => Produces one line of output for Each Response it
receives.(unlike traceroute)
netstat
Options:
-n => Display numerical addresses and ports
Ex:
netstat -i
tcpdump
tcpdump => Inspect contents of network packets to ensure payloads(data) are actually
being delivered. (Requires root/superuser privileges)
Options:
-n => Display numerical addresses and ports (suppresses DNS queries as well)
( tcpdump output: timestamp, nw id, source id, portnos, pkt spec info. etc)
Ex:
sudo tcpdump => Produces output for all the packets from/to the network devices
assoc. with the system.
telnet
It was originally intended to log onto 'remote systems' but is replaced with with better
protocols such as SSH.
'telnet' can still be used in N/W TROUBLE SHOOTING. (May or may not be installed by
default on linux systems - bcoz it is obsolete for connectin to systems)
Usage of telnet: Initiate a TCP Connection to a host (or ip) by specifying the port.
About telnet :
Quit telnet: Press quit at the telnet prompt. (Output is: "closed")
(Running on VirtualBox)
THE END
Releases
No releases published
Packages
No packages published