Linux Basic

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 28

Linux Basic

Unix: - Unix is an open source operating system. It was released by Dennis Ritchi in AT&T
Bell labs in 1970.
After that HP released HP UX which was supported on Itenium and Pa-rise processor. That
IBM released IBM AIX which was supported on Power series processor and Sun
Microsystems released Solaris which was supported with Spare processor.

Linux: - Linux is an open source operating system. It was released by Linus Torvalds in 1991.
The best part of the Linux is that it supports Intel processor.

Linux Distributions: -
1) Redhat Linux (RHEL 3,4,5,6,7)
2) Centos
3) Suse
4) Fedora
5) Ubuntu
6) Kali
7) Mendrac
8) Debain and other 250+

There are two types of mode in Linux: -


1) GUI (Graphical User Interface)- To switch in GUI Ctrl+Alt+F1
2) CLI (Command Line Interface). To switch in CLI Ctrl+Alt+F2/F3/F4/F5/F6

Kernel: - It hold modules of all devices.


Shell: - It Interpreter between user and Kernel. (/bin/bash (Bourne again shell)).

Basic commands in Linux: -

# tty = To know current working terminal


# w or who= To view login users details.
# pwd = Present Working Directory.
# ls = Listing all files and directory.
# ls -a = Listing all files and directory with hidden files and directory.
# ls -l or ll = Long listing.
# mkdir /abc, abc = To make new directory.
# cd /abc = To change directory.
# touch file name = To make a new file.
# vi file name = To edit a file.
Note: - vi is an editor tool and there are two types of editing mode, first one is Command
mode (Esc) and second one is Insert mode (i).
:set nu (set number) = To set numbering of lines in a file.
:%s/This/That = To replace word.
/word name = To search word in file.
2 (number of lines) yy = To copy lines, number will be accordingly.
Number of lines xx = To cut lines.
Number of lines dd = To delete lines.
Number to lines p = To paste lines.
:wq! = To save and quit file.
:x = To save and quit file.
:q! = Quit without save.

# uname = To check operating system.


# uname -r = To check running Kernel version.
# uname -n or host name = To check host name.
# hostname Server = To change host name temporary.
# vi /etc/sysconfig/network = To change host name permanently.

# Cal = To see calendar.


# date = To check date.
# date monthdayhourminuteyear = To change date.
# rmdir directory name = To remove empty directory.
# rm -r directory name = To remove file directory but it will ask to before delete each file .
# rm -rf (Recursivly Forcrfully) directory name = It will delete directory with all files without
permission.
# rm file name = To remove file.

Copy
# cp file1 file2
# cp file1 dir1
# cp -r dir1 dir2
# cp -rf dir1 dir2

Move
# mv file1 file2
# mv file1 dir1
#mv dir1 dir2

To Display Content of file


# cat file name = To open a file
# More file name = Only scroll down
# Less file name = Scroll down and up
# cat file name |more
# cat file name |less
# head -5 file name = To see top 5 lines, number can be set accordingly.
# head file name
# tail -5 file name = To see bottom 5 lines, number can be set accordingly.
# tail file name
# head -30 file name |tail-5

Filter
# grep word file name
# grep -i word file name (To ignore case sensivity)
# grep -v word file name (To ignore particular word)
# find / -name file name = To find a file
# find /etc -name file name = To find a file in particular directory
# find / -atime +30 (Number of days) = Access time of file .
# find / -mtime +30 (Number of days) = Modified time of file.

To check how many files are there in system:-


# find / -type f (File)
# find / -type d (Directory)
# wc -w (words)
# wc -l (Lines)
# wc -c (Character)

Tar & Compression

Tar is used to make bundle or archive of files and directories.

*Extension of Tar files is .tar

# tar -cvf abc.tar /etc = This is the command to make tar file here cvf stands for create
verbose file, abc.tar is tar file name and etc is a directory which you want a tar file.
# du -sh abc.tar = To check size if Tar file here du stands for disk usage and sh human
readable.

# tar -tvf abc.tar = To display files


# tar -xvf abc.tar -c /test = To extract a tar file here test is directory where you want to
extract content of tar file.
# gzip abc.tar = To compress a Tar file.
Note:- After complete the compression file will be abc.tar.gz (.gz is extension of
compression).

Or can use bzip2 command for compression.


#bzip2 abc.tar =abc.tar.bz2

To make Tar file back from compressed files


# gunzip abc.tar.gz
# bunzip2 abc.tar.bz2

Aliases

#alias =For temporary


#vi /root/.bashrc =For permanent

Network Configuration

# ifconfig = To check IP address


# ethtool eth0 = To check link connectivity status
# ifdown eth0 = To down Ethernet
# ifup eth0 = To up Ethernet
IP configuration file location: - #/etc/sysconfig/network-scripts/ifcfg-eth0
Host Name configuration file location: - #/etc/sysconfig/network
To restart network services: - service network restart
To know status of network service: - service network status
To start network services: - service network start
To stop network services: - service network stop

Location of hosts file:- # /etc/hosts

DNS server entry file location:- # /etc/resolv.conf


To switch priority of resolution:- # /etc/nsswitch.conf
To change subnet temporary:- # ifconfig netmask 255.255.225.0 eth0
To check gateway:- # netstat -nr
To add gateway:- # route add defaultgw 192.168.0.1
To delete gateway:- # route del defaultgw 192.168.0.1

SSH

SSH is used to take remote of other machines.


SSH package name:- openssh
How to check package whether it is installed or not:- # rpm -qa Package name
Port number of SSH:- 22
Port number of Telnet:- 23
Command to take remote via SSH:- # ssh IP address of that computer which you want to
take on remote (To take remote through root)
To SSH via any other user:- # ssh username@ipaddress
If we have multiple Linux server in organization and don’t want to enter password every
time while SSH then we create a SSH key on client server machine and copy it to SSH server.

To Make key on client= # ssh-keygen


To copy SSH key on server= # ssh-copy-id -i /root/.ssh/id_rsa.pub ip address of server
And then restart SSH service = # service sshd restart
To check host= # .ssh/known_hosts (Remote session history)
Then stop firewall services
#set enforce 0
#service iptables stop
Key history= # .ssh/authorized-keys

SSH configuration file= # /etc/ssh/sshd_config

Partition

To check how many partitions are there in system= # fdisk -l


There are three type of partitions in Linux:-
1)Primary
2)Extended
3)Logical
To create new Partition= # fdisk /dev/sda1 (1 is here partition number)
M=For help
N=For create
P=For Primary
E=For Extended
L=For Logical
Then start size and enter and then end size and hit enter
W and hit enter to write and enter.
Incase Kernel doesn’t read information of new created partition then we have a command
which updates partition table forcefully= # partx -a
File systems in Linux:- 1) Ext4, 2)xfs
To format and create file system:-
# mkfs.ext4 /dev/sda11 (11 is here partition number)
After format file system, it creates these three stuff:-
1) Indore
2) Superblocks
3) Journalling

After format partition we have to map and mount it with directory for that we need to
create a new directory.
# mkdir /test
# mount /dev/sda11 /test
To verify whether it is mounted for not= # df -h (It shows all mounted devices)
To make it boot persistence we have to do entry in configuration file.
# vi /etc/fstab

To check UUID= # blkid /dev/sdb5


To check if it used to someone= # fuser -cu /test
To kill session = # fuser -km /test

Swap

Swap is used as virtual memory.


To check free swap= # free -m
To check on which partition it is created= # swapon -s
To make swap we need to create a new partition.
# mkswap /dev/sda12
# swapon /dev/sda12

For boot persistence: -


# vi /etc/fstab
/dev /sda12 swap swap defaults 0 0
LVM (Logical Volume Manager)

PV= Physical Volume


VG= Volume Group
Need to create Partition first.
# pvcreate /dev/sda13
# pvs or pvdisplay = To verify
# vgcreate vgIB /dev/sda13
# vgs or vgdisplay = To verify
# lvcreate -L +1G -n lvIB vgIB
# lvs = To verify

To format= # mkfs.ext4 /dev/vgIB/lvIB


To mount= # mkdir /test
# mount /dev/vgib/lvib /test
# vgs
# pvcreate /dev/sda14
# vgextend vgib /dev/sda14
# vgs
# lvextend -L +2G /dev/vgib/lvib
# lvs
# resize2fs /dev/vgib/lvib

To active lv
#lvchange -a y lvname

Package Management
Extension of Linux installation files is .rpm(Red hat Package Management).

#arch = To check bit whether it’s 32 or 64 bit.


We can get all Linux Packages from Linux installation Disc. So first we need to insert Linux
installation disc in cd rom and mount it.
#mount /dev/cdrom /mnt
Then we shall copy Packages directory to /var/ftp
#scp -rv /mnt/Packages /var/ftp
To install a Package:- #rpm -ivh Full Package name
#rpm -q Service name = To check package if that is installed or not
To remove Package: - #rpm -e Package name
#rpm -qa = To check all installed Packages
#rpm -qa:more = To check page wise
#rpm -qa service name = To check particular package
#cat /etc/redhat-relaese = To check Linux os version.
#rpm -qa|wc -l = To know count of installed Packages
To know Package name:- #rpm -qf /etc/passwd (Any service name whose package name you
want to know)
To know files which come after install Package: - #rpm -ql Package name
There two commands to update Package: -
#rpm -Uvh (It will install without check previous version)
#rpm Fvh (It checks previous version first if it doesn’t find it doesn’t install)

How to create Repository: - Basically we create repository to create yum server


If repository package is not installed then it need to be install first, package name is
createrepo

To create repository: - #createrepo -v. (Dot is here for current location, it can be change
accordingly).
Index of repository= repodata/repomd.xml
Configuration file of repository: -
#vi /etc/yum.repos.d/server.repo
[Server]
Name=server
Baseurl=file:///var/ftp/Packages
Gpgcheck=0
Enabled=1
:wq!

#yum clean all (To clean all)


#yum repolist (To see repolist)
To install any Package through yum: - #yum install Package name
To uninstall Package: - #yum erase Package name
To update Package: - #yum update Package name
To see list: - #yum list Package name
If we shall map this yum server to client machines to install Packages, then we shall have to
stop firewall services.
To stop firewall services: -
#service iptables stop
#chkconfig iptables off
#setenforce 0
#vi /etc/sysconfig/selinux
Disabled

Then we need to start ftp service on server:-


#service vsftpd start
#ckhconfig vsftpd on
Then we need to create a new configuration file on client machine.
#vi /etc/yum.repos.d/client.repo
[Client]
Name=client
Baseurl=ftp://192.168.X.X/Packages
Gpgcheck=0
Enabled=1
:wq!

User & Group Management

Users are categorized in three categories in Linux.


1) Privileges (Root/Nonroot).
2) Local/Remote
3) Login/ Non login

#useradd user name :- To create a new user.


#cat /etc/passwd : - To see all existing users.
#cat /etc/default/useradd :- To check or modify default values of user.
To change default values of user or create a new user without default values: -
#useradd -4100, -d,
To check UID or GID of user: - id user name
#groupadd Group name :- To add a new group
#groupadd -g GID group name :-To change GID
#cat /etc/group :- To check all groups
To add any user in group:- #usermod -aG group name username
To remove any user from a group: - #gpasswd -d user name group Name
There are two types of group in Linux:- (1) Primary (2) Secondory
To change Primary Group: - g (small g)
To change secondary Group: - G (Capital G)
To set password for user:- #passwd user name
User configuration file:- #cat /etc/login.defs
To see password:- #cat /etc/shadow
To check password policy:- #chage -l user name
To change password policy:- #chage -m or M or L
To switch user:- #su user name
To check skel files:- #ls -a /etc/skel (Default files)

Permissions

To check permission on file= #ls -l file name


To check permission on directory= #ls -ld directory name

Permissions can be applying on files and Direction


Files: -
R – Cat, more, less, tail, head (Example commands) value of r is 4
W- Touch (Example) value of r is 2
X- Scripts, command (Example) value of r is 1
Direction: -
R – ls
W - rm, touch
X – ls -d, cd
To check logged in user #w
To check umask= #umask
Default root umask is 022
Normal user umask is 002
To change umask= #umask new value
To change permissions= #chmod 666 /root/file name (666 is here value of appropriate
permission)
To change owner= #chown new owner name /root/file name
To change group owner= #chgrp new group ower name /root/file name
To give any permission to particular user then we use ACL (Access Control List)
#setfacl -m u:ITB:rw filename (ITB is user here)
To check= #getfacl user name

Special Permissions: - (1) setuid or suid and value is 4.


(2) sgid and value is 2
(3) Sticky bit and value is 1

(1) To set special permission= #rwsr_xr -x


To remove= #chmod 4755 /user/bin/passwd

(2) sgid
Drwxrws r-w root itbrains /data

To chang sgid= #chmod 2775 /data


To add users= #g+s

(3) Sticky bit- Can read and write by any group user but can’t delete any file
#chmod 1777
By default example is /tmp

NFS

NFS stands for Network file system. Port number of NFS is 2049
First of all, we need to install Package of NFS.

#yum install nfs-utils


Then we need to start nfs services: -
#service rpcbind start
#service nfs start
Configuration file of nfs is= #vi /etc/exports
/Folder name which want to share *(rw,sync,no_root_sqash)
:wq!
Note- Here * is used for everyone but you want to share the folder with any particular
system then need to put IP address of that computer.
To export = #exportfs -a
To check shared folder= #exportfs
Or #showmount -e localhost

Then we need mount this shared folder on client machine: -


#mount -t nfs 192.168.0.113:/share /abc
Here share is shared folder name and abc is local directory of computer on which you want
to mount.
To check= #showmount -e IP address(Server)
To mount it boot persistence you need to do entry in below configuration file: -
#vi /etc/fstab
192.168.0.113:/share /abc nfs defaults 0 0

Autofs

In the case of NFS it always keeps synchronize with server which consume server resources
that’s why we use Autofs, it connects share file when it is required.

 Autofs always configures on client


 Autofs package need to be install
There are two types of mapping in Autofs first is Indirect mapping and second one is direct
mapping.
Indirect Mapping: -
#vi /etc/auto.master
/abc /etc/auto.abc
#vi /etc/auto.abc
Abc -rw,sync 192.168.0.113:/share
:wq!

Direct Mapping: -
#cd /net/192.168.0.113
Configuration file of autofs:-
#vi /etc/sysconfig/autofs

Apache

Apache is a web server in Linux.


We need to install Package of http and Package name is httpd.
Configuration file of Apache:- #vi /etc/httpd/conf/httpd.conf
Then start httpd service:- service httpd start
Or
#/etc/init.d/httpd start
Web sites contents are stored in:- #/var/www/html
#vi index.html

Note:- elinks command is used as browser in Terminal.

Name based virtual hosting: - To create virtual hosting, need to open configuration file.
#vi /etc/httpd/conf/httpd.conf
Then go to bottom of that file by using short key Shift+G
Then copy all virtual host files (7 files) and paste and then need to remove # and put new
host entry.
Need to create new directory in #/var/www/html2
#vi index.html
Then need to do entry in host file and restart httpd service and check it should work.

IP based virtual hosting: - Need to create virtual eth cards and change entry in configuration
file of * as IP address.

Logs

Default location of logs in Linux is: - #/var/log/messages


Kernel logs: - #/var/log/dmesg
Type of logs: - There are two type of logs.
1) Facility: - Which service created the logs.
2) Severity: - How critical are those logs.
File location: - #vi /etc/rsyslog.conf
If you want to save logs on a centralized server- On server need to make changes in conf file
as below and enable reception service.

# provides UDP syslog reception


$ModLoad imudp
$UDPServerRun 514

# provides TCP syslog reception


$ModLoad imtcp
$InputTCPServerRun 514
On client need to give IP address of server in conf file-
Remote host (In bottom of conf file)
*.*@@192.168.0.102:514
How to rotate log files:- #vi /etc/logrotate.conf

SE Linux

#vi /etc/sysconfig/selinux
#chcon -t = command to change security context

To change bool: -
To see list: - #getsebool -a
To change: - #setsebool value which you want to change

Services

To check services in system: - #chkconfig --list


0 = Halt/Shutdown
1 = Single user mode
2 = Multi user without nfs
3 = Multi user without GUI
4 = Unused
5 = Multi user with GUI
6 = Reboot
Note:- These all above numbers are used with init.
To switch to any other level = init 3
To off any particular level= #chkconfig –level=3 iptables off
To check status of all services= #service –status-all
To check all running processes= #ps -ef

There are two type of services: -


1) Standalone – init.d
2) Transent/ On Demand – xinetd.d
On demand= #service xinetd start
#vi /etc/xinetd.d/telnet
Disabled=no
:wq!

NTP (Network Time Protocol)

NTP works on UDP protocol, port number is 123.


First need to install NTP package.
Configuration file = #vi /etc/ntp.conf
Keep only three server entries rest can be deleted from configuration file but keep backup
of file.
To Manually Sync= #ntpdate server name
To sync time slowly= #service ntpd start
To check sync status= #ntpq -p

To configure server: -
Vi configuration file
Server 127.127.1.0 #Local clock
Fudge 127.127.1.0 #spectrum 10
Need to keep these two lines only and delete rest lines.
Firewall must be off on both machines.
NIC bonding
NIC bonding is basically used to bond two or more than two Ethernet cards because if one
goes faulty then other one provides fault tolerance.
There are two type of modes: -
Mode1: - If one fails then it switches to another one.
Mode2: - Both cards work simultaneously.

Bonding module should be loaded to make boding.


To check bonding module whether it is loaded or not: - #lsmod |grep bonding
If it is not loaded, then we need to load bonding module: - #modprobe bonding
After load the module we need to copy eth0 file as ifcfg-bond0
#vi ifcfg-bond0
Device name=bond0
IPADDR=192.168.0.1
Master: yes
Remove UID
:wq!
And then copy eth0 file as eth1
Vi both files and remove IP addresses.
Device name=eth0
Slave=yes
Master=bond0
:wq!

#modinfo module name

#vi /etc/modprobe.d/bonding.conf
Alias bond0 bonding
Options bond0 mode=1 miimon=100
#service network restart
To see bonding: - #cat /proc/net/bonding/bond0

To open device configuration utility: - #system-config-network

Important: - If eth1 has failed and need to add a new eth2 without restart network service
then we use ifenslave utility.
#ifenslave -d bond0 eth1
#ifenslave bond0 eth2

Cache only DNS

First need to install DNS package, package name is bind.


To check Package name: - #which nslookup
It shows location /usr/bin/nslookup
#rpm -qf /usr/bin/nslookup
File location of file on which we do DNS entries: -
#vi /etc/named.conf
We need to do below entries in Cache only DNS server configuration file: -

Listen on port 53{127.0.0.1;192.168.0.113(IP of own machine);};


Directory “/var/named”;
Allow query {localhost;192.168.0.0/24;any;};
Forward only;
Forwarders{113.193.0.148(DNS IP);};
};

Note: - ; is syntax.
Then restart named service
#service named restart

Note: - To check DNS IP can use nslookup www.google.com

Entries on Client: -
#vi /etc/resolv.conf
Name server 192.168.0.113 (DNS server IP)
:wq!

Samba Server

Samba server is used to access shared files, printers, AD users through windows machine.
First need to install package of Samba.
#yum install samba
Configuration file of Samba: - #vi /etc/samba/smb.conf

Configuration to share a folder for windows machine go to bottom in configuration file and
copy 7 lines and change those lines as given below: -
[Share folder name]
Comment=Public staff
Path=/temp(Which directory you want to share)
Public=yes
Writable=yes
Printable=no
Write list=+staff
Then create a new Samba user:- #useradd smbuser(Whatever user name you can give)
To set password= #smbpasswd -a smbuser
If you want to access windows shared folder in Linux, need to create new directory and
mount in that directory. CIFS (Common internet file system) file system used for that.
#mkdir songs
#mount -t cifs -0 username=raj,password=redhat //192.168.56.1/songs/songs
Then need to do entry in fstabs file.
///192.168.56.1/songs /songs cifs username=raj,password=redhat 0 0

Linux Process Management

To check all running processes: - #ps -ef


Parent process tree:- #pstree -p
#top
To Kill process: - #kill UID
Forcefully: - #kill -9 UID
Kill Signals:- #kill -l
To change nice value: - #renice pid PR NI
To check running jobs:- #jobs
To run service in background:- #sleep 7000&
#fg %3
No terminal dependent:- #nohup sleep 7000&
To schedule any process which task we do repeatedly:-#crontab -e
Mins Hrs Date Month DOW(Day of week)
*
0-59 0-23 1-31 1-12 0-7 “/bin/date>>/tmp/test”

Note: - 0 and 7 is reserved for Sunday


To Schedule job at once: - #at now 02min
At>/sbin/reboot
Press Ctrl d
#atq
Linux System Monitoring

To check free memory: - #free -m


To check uptime: - #uptime
Last logged in user list: - #last
To check performance of disk: - #iostat 3 5
Note: - 3 is internal and 5 is frequency here.
To check performance of virtual memory: - #vmstat 3 5
To check Microprocessor status: - #mpstat 3 5
To capture CPU utilization data in every 10 minutes: - #sar (System Activity Reporter)
To capture all hardware data: - #sar -A
To check date wise: - #sar-f /var/lag/sa
If Sar command doesn’t work then we have to install Package and package name is #sysstat
When we install package it schedules a crone job.
To check: - #cat /etc/cron.d/sysstat
To check active network connections: - #netstat -tlnp
To check open ports in remote server: - #nmap server name
To check configured routes: - #route -n
#traceroute
#tcpdump
#tcpdump -i eth0 –w 192.168.0.5 test.pcap
To read: - #tcpdump -r test.pcap

Kernel tuning

To check kernel version: - #uname -r


To update kernel: - #yum install kernel name
To check host name of server:- #uname -n
To check Architecture/bit of Linux OS : - #uname -m/arch
To check loaded volume:- #lsmod
To load a module: - #modprobe module name (eg:-bonding)
To unload a module: - #modprobe -r module name
Where modules are saved: - #cd /lib/modules/module name
For module info:- #modinfo module name

Boot Process

POST
BIOS
MBR
GRUB
/boot (Grub consider /boot as /root)
/boot/grub/grub.conf
Initrd (To load minimum number of drivers to load kernel)
Kernel
Init
/etc/rc.sysinit
/etc/inittab
/etc/rc.d/rc3.d/
/etc/rc.load

Sudo

Configuration file: - #vi /etc/sudoers


#visudo
There are three ALLs in configuration file, what they stand for: -
User name ALL=(ALL) ALL
Hosts Users Commands
Example: - Sunil Server1=(ALL)nopasswd /sbin/fdisk,/sbin/shutdown
To give Sudo permission to a Group: -
#%group name server1=(ALL)nopasswd /sbin/fdisk,/sbin/shutdown
Note:- We put this nopasswd here because we don’t want that it ask password every time.
If want to give Sudo access to few users of group: -
#user_Alias Alias name=user1,user2,user3
#Alias name ALL=(ALL)ALL
To make commands Alias: - #cmnd_Alias Alias name=/sbin/ifconfig
#Alias Name ALL=(ALL)ALL
If you don’t want to give access to a single user from a group: -
#%groupname,!user name ALL(ALL)ALL
Logs file: - #/var/log/secure
To check which commands access user have: - #sudo -U sunil-l

KickStart

Package name: - #yum install system-config-kickstart


To access Kick start console:- #system-config-kickstart
After make changes need to save file any location and share with NFS.
But it works in GUI mode only.
By default file of Kickstart in Linux is :- /root anaconda-ks.cfg
To insert ks file while installation: - #linux ks=nfs:192.168.56.10:/tmp/ks.cfg

Mail server
Mail server works on two agents in Linux:-
1)MTA (Mail Transfer Agent)
2)MUA (Mail user Agent)
Postfix service works as MTA in Linux it is used to send email and works on SMTP protocol.
We need to install Package of postfix.
#yum install postfix
Configuration file :- #vi /etc/postfix/main.cf
#service postfix start
Then we’ll create a user name Sunil and I want to send email to Sunil:-
#mail -s “Test mail” Sunil (S stands for Subject here)
And then type content whatever message you want to send and hit Ctrl=D.
To check mail queue: - #mailq
Or can check in log file as well: - #/var/log/maillog
*mutt is utility of mail client in Linux.
Need to install Package of mutt.
To view mail:- #mutt

To receive email Dovecot is a service in Linux which works on POP and IMAP protocols.

Configuring file: - vi /etc/dovecot/main.cf

Crontab

/var/spool/cron/

crontab -e to do any entry in crontab


crontab -l to list existing entry
crontab -r to remove crontab file

Minute: Hour: day of month: month: day of week


To compress size:- Truncate -s 3G file5.txt

Fallocate -l filesize filename

You might also like