DIRECTORY STRUCTURE OF LINUX:
/bin/ - Essential user command binaries
/boot/ - Static files of the boot loader
/dev/ - Storage device info
/etc/ - System configuration (secure directories)
/home/ - User home directories
/lib/ - Essential shared libraries and kernel modules
/media/ - Mount point from removable disk
/mnt/ - Mount point for a temporarily mounted file system
/opt/ - Add-on-applications software packages
/sbin/ - System binaries
/srv/ - Data for service provided by this system
/tmp/ - Temporary files
/usr/ - Multi-user utilities and apps
/var/ - Variable files-Home dir for HTTP and FTP
/root/ - Home directory for the root user
/proc/ - Virtual file system documenting kernel and process status as text files.
Basic Commands
To check system configuration
# lscpu ; to get cpu detail
#hostname: to get hostname
# free -m : to check RAM
# fdisk -l | grep sd : to check number of HD
# ifconfig : to check network / IP s cv
1) # pwd : to check present working directory
2)# ls : to list the content
3) #whoami, who, id : to check current user login info
4) #cal : calender --- cal MM YYYY
eg: # cat 11 2019, # cal 2019, #cal 1947, #cal 04 1520
5) #uname -a , uname -r : to check system full info
6) # cat /etc/redhat-release -:to check red hat version
7) #whatis, man, --help : to get the help of any command
8) #du, df - to check disk usage and free space
eg: # du /home/deepak
# df /home/deepak
09) #hostname or # hostnamectl : to get hostname
# hostnamectl set-hostname myserver1 : to change hostname
10) #date : to check date and time
eg: # date , # date +%d
11) #init 0, #halt, #poweroff : to shutdown
12) #init 6, #reboot, press ctrl+alt+del : to restart
13) #eject, eject -t : to in or out optical media
14) # history : to check history of used commands
# history 12 : to show history of last 12 commands only
# history > file1: to store the history result in a new file
# history -c : to delete history
# HISTTIMEFORMAT="%d/%m/%y %T " : to see the history with date and time
15)# ifconfig : to check all network interface
16) # nmtui : to configure IP address
# ifdown eno16777736
# ifup eno16777736
# ifconfig eno16777736
17) # free -m : to check memory status
18)# top : to see cpu and memory usage
19) #kill : to kill PID
20) #sar 1 3 : to check cpu status 3 times within one second
21)# su : to switch to super user or # su deepak : to switch to other user
22)#su - : to switch to super user but getting /root as home directory bydefault
23)# uptime : to check how long server is started
24)#which, whereis command : to check the location of command script
25)# clear : to clear the screen
26)# fdisk -l : to check all connected storage and partition or # df -h
27)#passwd username :changing password of user
28) #ping IP or hostname : to check the communication
29) #nslookup hostname : to get IP
30) ctrl+c or q : to cancel running process
Working with files and Directories
#mkdir dirname : To create a directory/folder.
#cd dirname : To go to inside the directory.
#mkdir dir1 dir2 dir3 : To create multiple dir at a time
# mkdir -p /india/delhi/chandni
#cd / : To come in root level.
#cd ~ : To come to user level.
#cd .. : come one step back.
#touch filename : To create empty file.
#cat >filename : To create a file.
...................
.................... Write ur text normally
Now press “ ctrl+d “ - To save.
#cat filename : To see the file contents.
#more filename : Showing content of file one screen at a time ( Sequentially)
Eg: #more /root/install.log
# head filename : Showing content of top portion only
# tail filename : Showing content of bottom portion only
# echo “ tiger” > file1 : to insert content in a file without opening it but old content will be
removed
# echo “ tiger” >> file1 : to insert content in a file without opening it.
# cat file1 file2 > file3 : To combine content of two file and insert in new file
#rm filename : To remove a file.
#rm –f filename : To remove a file without asking permissions.
#cp : To copy the file and dir.
#cp source-path/file-name dest-path/ : To copy and paste for files
# mv source-path/file-name dest-path/ : To cut and paste for files
# mv old-file-name new-file-name : To rename the file/folder name.
#sort : to show content of file in sorted form ( only for viewing)
Eg:
# sort filename : to show from a-----z
#sort -r filename : to show from z-----a
# sort file1 file 2 : show sorting content from both file
# sort file1 >sort.txt : saving the sorted result in a new file.
SED : To replace the content of a file
To replace test with example in myfile.txt
# sed 's/test/example/g' myfile.txt
s - substitute
g - global ( changes to be done in all lines)
To replace test with example in myfile.txt and print the output in a new file
sed 's/test/example/g' myfile.txt > newfile.txt -- only display
sed -i 's/test/example/g' myfile.txt > newfile.txt -- change in same file
Replacing string on a specific line number
# sed '3 s/unix/linux/' myfile.txt
Deleting lines from a particular file
Syntax:
# sed 'nd' filename.txt
Example:
# sed '5d' filename.txt
Deleting last line from a particular file
# sed '$d' filename.txt
locate
#locate filename : To find something in entire OS
Note: Sometime locate command do not show anything then use # updatedb and use locate
command again
find : to find from specific location
# find /home -name deepak.txt
#rm –r dirname : To remove a directory/folder.
#rm –rf dirname : To remove a directory/folder without asking confirmaton
# rm -rf * : to delete all
# rm -f *.mp3 : to delete all .mp3 files
# rm -rf file* : remove all words related with file text
#cp –r [dir-source-path] [dir-destination-path] : To copy and paste for dir
#mv [dir-source-path] [dir-destination-path] : To cut and paste for dir
#wc : To see how many characters/words/lines are used in file.
# wc -c file-name
# wc -w file-name
# wc -l file-name
# ls /home | wc -l : show number of content in that dir.
ls command examples
#ls –a : To show all
# ls –a /* - to show / contend with subdirectory
#ls –r : To show all but in reverse alphabetic order
#ls -i filename/dirname : to show inode number
# ls -l filename/dirname : to show long format + associated permissions
# ls -lh filename/dirname : to show long format + associated permissions( human readable format)
# ls -t filename/dirname : to show which created first( based on time)
# ls -R : to list in tree format
# ls -s : list file size
# ls - S : sort by file size
# ls -X : to show by extension name
# ls -d */ : display only directories
# ls *.txt : to list only .txt files
# ls [dk]* : to show only dir/file which initial is d and k
# ls [d-k]* : to show all dir/file between d and k
# ls [!dk]* : to show all dir/file except which initial is d and k
# ls > file1 : to store the result of ls in a new file
FILE EDITING IN LINUX:
1) vi 2) vim 3) emacs 4) pico 5) nano 6) lime
For eg:
#vi filename
[press i]
Write your text normally
[press esc]
:x (or) :wq -> to save and exit.
:q! -> Exit without saving.
Compression - Decompression
Using tar command
tar command syntax
tar -cvf [destination-file-name.tar] [source-files or directories]
tar -tvf archive-file.tar
tar -xvf archive-file.tar
The option c creates archive or tarball from the supplied files or directories
The option v displays the progress of archiving process at the command prompt.
The option f specifies the file or device name. it instructs the tar command to store the output data to
the specified device or file.
The option t lists the contents of archive or tarball.
The option x extracts the archive
# touch file1 file2 file3 file4
To compress
# tar -cvf data.tar file1 file2 file3 : to compress
#tar -tvf data.tar : to see the content of compressed tar file
# tar -xvf data.tar : to decompress
# tar -cvf data.tar -C /home/deepak/folder1 : to extract in specified path
File Permission
chmod : to change the permissions of a file or directory. Use ls -l to see the permission settings.
Permission group:
1. Owner
2. Group
3. All Other Users
Permissions representation:
1st digit represent owner
2nd digit represent group
3rd digit represent all other users
Permission types
Read – 4
Write – 2
Execute – 1
Null permission (access denied) – 0
7 -> 4+2+1 -> read+write+execute
6 -> 4+2 ->read+write
5 -> 4+1->read+execute
4 -> 4 -> read
0 -> none (no rights)
Example:
To set permission:
#chmod 763 test
To give full permission to all:
#chmod 777 test
To give permission only for users:
#chmod 700 test
Package Management
How to install any feature ?
Ans: #yum install telnet
How to uninstall any feature ?
Ans: # yum remove telnet
Display list of updated software (security fix)
Type the following command at shell prompt:
# yum list updates
Task: List all installed packages
List all installed packages, enter:
# rpm -qa
# yum list installed
Find out if httpd package installed or not, enter:
# rpm -qa | grep httpd*
# yum list installed httpd
Check for and update specified packages
# yum update {package-name-1}
To check for and update httpd package, enter:
# yum update httpd
Task: Search for packages by name
Search httpd and all matching perl packages, enter:
# yum list {package-name}
# yum list httpd
# yum list perl*
Task: Install the specified packages [ RPM(s) ]
Install package called httpd:
# yum install {package-name-1} {package-name-2}
# yum install httpd
Task: Display the list of available packages
# yum list all
USER ACCOUNT AND GROUP ACCOUNT
USER ACCOUNT:
Command line:
#useradd username : To add a user
#passwd username : To set password
#ls /home/ : To check user created or not
#userdel username : To delete the username
#userdel –r username : To delete the user as well as his saved data or his entire profile
GROUP ACCOUNT:
Command line:
#groupadd groupname : To add a group
#groupdel groupname : To delete the group
# gpasswd –a user2 groupname Adding a user in a group
# gpassword -M ram,ram1,ram2 accounts : adding multiple users to a group
3) With newly user account a group name of same user name is also created and they are linked with
each other.
Q-* How to see all created user/group in a file or Where these all created users and group are stored
?
Ans : # cat /etc/passwd - for users
#cat /etc/group - for group
Q-* How to see encrypted password of all created users ?
Ans : # cat /etc/shadow
Q: How to assign root priviledge to any normal user ?
Ans: 1) log on through root user.
2) # visudo
root ALL=(ALL) ALL
newusername ALL=(ALL) ALL
or
newusername2 ALL=(ALL) NOPASSWD: ALL
For group
% wheel ALL=(ALL) ALL
% sales ALL=(ALL) ALL
add this newuser here then save and exit
Now in other terminal log on with newuser
$ sudo mkdir /india
type password of newuser
$ sudo systemctl stop firewalld
How to lock and Unlock user
# passwd -l deepak ------ to lock
# cat /etc/shadow | grep deepak
deepak : !! now shadow password
two exclamation mark (!!) before the encrypted password which means that the password has been locked
# passwd -u deepak --- to unlock
SSH-SECURE SHELL
It is same like telnet connection but it is secure connection.
Ssh server:
#Systemctl start sshd
# Systemctl enable sshd
Turn off firewall
# Systemctl stop firewalld
# Systemctl enable firewalld
# useradd deepak
# passwd deepak
Ssh client:
#ssh root@serverIP
or
#ssh deepak@192.168.5.1
Run any command on server without login(user password require)
#ssh root@192.168.5.1 mkdir /root/india
# ssh root@192.168.5.1 ls /root/
To configure passwordless login
In client PC
# ssh-keygen
# ssh-copy-id deepak@server-IP
type deepak user password
How to check
# ssh deepak@192.168.5.1
you should be able to log on without asking any password