Sunday, January 24, 2021

Quick Review in linux

 

 

How to Creating, Removing, Copying, Moving files & Directories in Linux

Using cat command:

[root@dev ~]# cat > filename1
This is world file                                Ctrl+d (To save the file)

To display the content of the file

[root@dev ~]# cat filename1
This is world file

To display the content of the file

[root@dev ~]# cat >> filename1
This second line
[root@dev ~]# cat filename1
This is world file This second line

Creating multiple files at same time using touch command

[root@dev db_bak]# touch file{1..5}
[root@dev db_bak]# ls
file1  file2  file3  file4  file5

Creating a Directory:

[root@dev db_bak]# mkdir dir1
[root@dev db_bak]# ls
dir1

Making multiple directories inside a directory

 [root@dev ~]# mkdir -p technology/{linux/{shell_script,linux},java/{basic,adv_java},mysql/{DBA,Dev}}

[root@dev ~]# tree technology/
technology/
|-- java
|   |-- adv_java
|   `-- basic
|-- linux
|   |-- linux
|   `-- shell_script
`-- mysql
    |-- DBA
    `-- Dev

9 directories, 0 files
 

Copying files into directory

[root@dev ~]# ls
filename    

[root@dev ~]# cp filename technology/
[root@dev ~]# ls
filename  

[root@dev ~]# cd technology/
[root@dev technology]# ls
filename  java  linux  mysql

Copying directories from one location to other

[root@dev technology]# cp -rvfp java  linux/
‘java’ -> ‘linux/java’
‘java/basic’ -> ‘linux/java/basic’
‘java/adv_java’ -> ‘linux/java/adv_java’
[root@dev technology]# cd linux/
[root@dev linux]# ls
java  linux  shell_script

Moving files from one location to other (cut and Paste)
[root@dev technology]# ls
filename  java  linux  mysql
[root@dev technology]# mv filename  java/

[root@dev technology]# cd java/
[root@dev java]# ls
adv_java  basic  filename

Moving a Directory from one location to other

[root@dev technology]# ls
java  linux  mysql
[root@dev technology]# mv java/ linux/
[root@dev technology]# ls
linux  mysql
[root@dev technology]# cd linux/
[root@dev linux]# ls
java  linux  shell_script

Renaming a File

[root@dev linux]# ls
java  linux  shell_script
[root@dev linux]# mv linux/ linux_adv
[root@dev linux]# ls
java  linux_adv  shell_script

[root@dev linux]# rm file

rm: remove regular empty file ‘file’? y

Vim editor command

Command Mode:
gg     To go to the beginning of the page
G       To go to end of the pagew
b       To move the cursor forward, word by word
nb     To move the cursor backward, word by word
nw    To move the cursor forward to n words (5W)
nb     To move the cursor backward to n words (5B)
u       To undo last change (word)  

Ctrl+R   To redo the changes

yy    To copy line 

nyy   to copy no of line

dd    to delete line 

p     to past line

Symbolic Link :-  There are two types of Links

 Soft Link :                                                       

1. Size of link file is equal to no. of characters in the name of original file
2. Can be created across the Partition
3.Inode no. of source and link file is different
4.if original file is deleted, link is broken and data is lost
5.SHORTCUT FILE

[root@dev ~]# ln -s /aws/  aws.slink 

Hard link :

1. Size of both file is same
2 .Can't be created across the partition
3.Inode no. of both file is same
4.If original file is deleted then also link will contain data
5.BACKUP FILE

[root@dev ~]# ln  /aws/  aws.hlink  

File Permissions:

Permissions are applied on three levels:-
 Owner or User level
 Group level
 Others level
Access modes are of three types:-
 r  - read only
 w - write/edit/delete/append
 x  - execute/run a command

[root@dev ~]# chmod u=rwx,g=rw,o=r filename
[root@dev ~]# ls -l filename
-rwxrw-r--. 1 root root 13 Nov 18 15:35 filename

2 Absolute Method (numbers) In Absolute method we use numbers instead of using symbols i.e.
Read=4
Write=2
Execute=1

[root@dev ~]# chmod 764 filename
[root@dev ~]# ls -l filename
-rwxrw-r--. 1 root root 13 Nov 18 15:35 filename

Removing all permissions from others
[root@dev ~]# chmod 760 filename (where 0 indicates no permissions)

Umask:
When we create any file using touch, cat or vi commands they get created with default file permissions as stored in umask (User file creation mask).

[root@dev ~]# umask
0022

Calculation of default permissions for file and directory, basing upon the umask value
Note: For a file by default it cannot have the execute permission, so the maximum full permission for a file at the time of creation can be 666 (i.e. 777 -111 = 666), whereas a directory can have full permissions i.e. 777

The full permission for the file                         666
Minus the umask value                                   -022
The default permission for file is                     644  (rw-,r--,r--)


The full permission for the directory                777

Minus the umask value                                    - 022 

The default permission for file is                    755 (rwx, r-x, r-x)

HOW TO CREATE PARTITION AND MOUNT

[root@dev ~]# fdisk /dev/sdb 

Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended

Partition number (1-4, default 1): 1
First sector (2048-16777215, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-16777215, default 16777215): +2048M   
Partition 1 of type Linux and of size 2 GiB is set

Command (m for help): w
The partition table has been altered!

[root@dev ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)

Temporary mount
[root@dev ~]# mount /dev/sdb1 linux
[root@dev ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   60G  0 disk
├─sda1            8:1    0    1G  0 part
└─sda2            8:2    0   59G  0 part
  ├─centos-root 253:0    0   37G  0 lvm  /
  ├─centos-swap 253:1    0  3.9G  0 lvm  [SWAP]
  └─centos-home 253:2    0 18.1G  0 lvm  
sdb               8:16   0    8G  0 disk
└─sdb1            8:17   0    2G  0 part /root/linux

Fuser command used

[root@dev ~]# fuser -cu linux
/root/linux:         23963c(root)

Note :- It is show how many user used this directory  

[root@dev ~]# fuser  -ck linux
/root/linux:         23963c

Note :- forcefully kill user and log out 

To view the usage information of mounted partition:

[root@dev ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G   34M  1.9G   2% /run
tmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root   38G  4.6G   33G  13% /
/dev/sda1               1014M  149M  866M  15% /boot
/dev/mapper/centos-home   19G   33M   19G   1% /home
/dev/sdb1                2.0G  6.0M  1.8G   1% /root/linux

Creating a Swap Partition:

[root@dev ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p

Disk /dev/sdb: 8589 MB, 8589934592 bytes, 16777216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x13fe498e

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352     8390655     2097152   8e  Linux LVM

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (8390656-16777215, default 8390656):
Using default value 8390656
Last sector, +sectors or +size{K,M,G} (8390656-16777215, default 16777215): +2048M
Partition 3 of type Linux and of size 2 GiB is set

Command (m for help): p

Disk /dev/sdb: 8589 MB, 8589934592 bytes, 16777216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x13fe498e

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352     8390655     2097152   8e  Linux LVM
/dev/sdb3         8390656    12584959     2097152   83  Linux

Command (m for help): t
Partition number (1-3, default 3): 3

Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'

Command (m for help): p

Disk /dev/sdb: 8589 MB, 8589934592 bytes, 16777216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x13fe498e

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352     8390655     2097152   8e  Linux LVM
/dev/sdb3         8390656    12584959     2097152   82  Linux swap / Solaris

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@dev ~]# partprobe
[root@dev ~]# mkswap /dev/sdb3
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=d2fde43f-4e96-42a5-9ac8-8cb7fa6d18c5
[root@dev ~]# swapon /dev/sdb3
[root@dev ~]# swapon -s
Filename                Type        Size    Used    Priority
/dev/dm-1                                  partition    4063228    0    -2
/dev/sdb3                                  partition    2097148    0    -3
 

Check Swap memory 

[root@dev ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3789         829         417          33        2542        2648
Swap:          6015           0        6015
 

Reduce Swap memory

[root@dev ~]# swapoff /dev/sdb3
[root@dev ~]# free -m

              total        used        free      shared  buff/cache   available
Mem:           3789         828         417          33        2542        2648
Swap:          3967           0        3967


Apache Configuration

HOW TO CONFIGURE VIRTUAL WEB HOSTING 

 [root@dev ~]# vi  /etc/httpd/conf.d/ip.conf
<VirtualHost 192.168.0.10:80>
ServerAdmin root@dev.example.com
ServerName  www1.example.com
DocumentRoot /var/www/
</virtualhost>
<Directory "/var/www/">
AllowOverride none
Require all Granted
</Directory>

[root@dev ~]# vi /var/www/index.html 

[root@dev ~]# vi /etc/hosts 

192.168.105.62 www1.example.com
192.168.105.62 www2.example.com

[root@dev ~]# firewall-cmd --permanent --add-service=http
success
[root@dev ~]# firewall-cmd --complete-reload
success

[root@dev ~]# elinks www1.example.com  

HOW TO REDIRECT WEBSITE

 [root@dev ~]# vi  /etc/httpd/conf.d/virtual.conf  

<VirtualHost *:80>
ServerAdmin root@dev.example.com
ServerName  www1.example.com
DocumentRoot /var/www/
Redirect  /  "https://www.ecollege.life"
</virtualhost>
<Directory "/var/www/">
AllowOverride none
Require all Granted
</Directory>
 

[root@dev ~]# vi /var/www/index.html

PORT BASED WEB HOSTING

 [root@dev ~]# vi  /etc/httpd/conf.d/port.conf  

<VirtualHost *:8999>
ServerAdmin root@dev.example.com
ServerName  www1.example.com
DocumentRoot /var/www/
</virtualhost>
<Directory "/var/www/">
AllowOverride none
Require all Granted
</Directory>

[root@dev ~]# vi /var/www/index.html  
[root@dev ~]# vi /etc/httpd/conf/httpd.conf  
Listen 8999
[root@dev ~]# semanage port -a -t http_port_t -p tcp 8999
[root@dev ~]# firewall-cmd --permanent --add-port=8999/tcp
success
[root@dev ~]# systemctl restart httpd
[root@dev ~]# firewall-cmd --complete-reload
[root@dev ~]# elinks www3.example.com:8999
[root@dev ~]#firewall-cmd --permanent --add-port=21/tcp 
[root@dev ~]#firewall-cmd --permanent --add-service=ftp
Restart firewall:
[root@dev ~]#firewall-cmd --reload
[root@dev ~]# netstat -tulpn | grep LISTEN

How to create LVM

Step1 :[root@dev ~]# fdisk /dev/sdb                                    ( Create partition)
Welcome to fdisk (util-linux 2.23.2).Changes will remain in memory only, until you decide to write them. Be careful before using the write command. 

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (4196352-16777215, default 4196352):
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-16777215, default 16777215): +2G
Partition 2 of type Linux and of size 2 GiB is set

Command (m for help): p

Disk /dev/sdb: 8589 MB, 8589934592 bytes, 16777216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x13fe498e

 Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352     8390655     2097152   83  Linux

Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris          

Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sdb: 8589 MB, 8589934592 bytes, 16777216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x13fe498e

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352     8390655     2097152   8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@dev ~]# partprobe
 

Step2 :- Phycial Volume Create 

[root@dev ~]# pvcreate /dev/sdb2
Physical volume "/dev/sdb2" successfully created.
[root@dev ~]# vgcreate /dev/sdb2 vg_web
  /dev/sdb2: already exists in filesystem
  Run `vgcreate --help' for more information.
 

Step 3: Create Volume Group

[root@dev ~]# vgcreate vg_web_group /dev/sdb2  

 Volume group "vg_web_group" successfully created
[root@dev ~]# vgs
  VG           #PV #LV #SN Attr   VSize   VFree
  centos         1   3   0 wz--n- <59.00g  4.00m
  vg_web_group   1   0   0 wz--n-  <2.00g <2.00g
Step 4 :- Logical Volume Create 

[root@dev ~]# lvcreate -L +500M -n web_lv vg_web_group
Logical volume "web_lv" created.

[root@dev ~]# lvs
  LV     VG           Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home   centos       -wi-ao----  18.08g                                                    
  root   centos       -wi-ao---- <37.04g                                                    
  swap   centos       -wi-ao----  <3.88g                                                    
  web_lv vg_web_group -wi-a----- 500.00m                                                    

Step 5: Format Logical Volume 

[root@dev ~]# mkfs.ext4 /dev/vg_web_group/web_lv
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
128016 inodes, 512000 blocks
25600 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=34078720
63 block groups
8192 blocks per group, 8192 fragments per group
2032 inodes per group
Superblock backups stored on blocks:
    8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

Step6:- Mount the Logical Volume
[root@dev ~]# mount /dev/vg_web_group/web_lv devops/
[root@dev ~]# cd devops/

[root@dev devops]# ls
lost+found
[root@dev devops]# touch file{1..10}
[root@dev devops]# ls
file1  file10  file2  file3  file4  file5  file6  file7  file8  file9  lost+found  

Final Step 7 : Check Mount point 

[root@dev devops]# lsblk
NAME                    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                       8:0    0   60G  0 disk
├─sda1                    8:1    0    1G  0 part /boot
└─sda2                    8:2    0   59G  0 part
  ├─centos-root         253:0    0   37G  0 lvm  /
  ├─centos-swap         253:1    0  3.9G  0 lvm  [SWAP]
  └─centos-home         253:2    0 18.1G  0 lvm  /home
sdb                       8:16   0    8G  0 disk
├─sdb1                    8:17   0    2G  0 part /root/linux
└─sdb2                    8:18   0    2G  0 part
  └─vg_web_group-web_lv 253:3    0  500M  0 lvm  /root/devops
sr0                      11:0    1 1024M  0 rom




No comments:

Post a Comment