file permissions
Chalo, ek example lete hain aur usko detail mein samajhte hain. Example file details:
Ab isko part-by-part tod ke dekhte hain:
1. File Type aur Permissions
Yeh part file type aur permissions ko indicate karta hai.
- `-` : File type ko show karta hai. Yahan `-` ka matlab regular file hai. Kuch aur possible values:
- `d` : Directory
- `l` : Symbolic link
- `c` : Character device
- `b` : Block device
- `rw-` : Owner (user) ki permissions. Yahan:
- `r` : Read (padne ki permission)
- `w` : Write (likhne ki permission)
- `-` : Execute (chalane ki permission nahi hai)
- `r--` : Group ki permissions. Yahan:
- `r` : Read (padne ki permission)
- `--` : Write aur Execute (likhne aur chalane ki permission nahi hai)
- `r--` : Others (baaki sab log) ki permissions. Yahan bhi:
- `r` : Read (padne ki permission)
- `--` : Write aur Execute (likhne aur chalane ki permission nahi hai)
2. Link Count
Yeh part batata hai ki kitne hard links hain is file ke. Hard link multiple names ke saath ek hi file ko refer
karte hain. Yahan 1 ka matlab hai sirf ek link hai.
3. Owner (User)
Yeh part batata hai ki file ka owner kaun hai, yahan owner `root` hai.
4. Group Owner
Yeh part batata hai ki file kis group ke andar hai, yahan group owner bhi `root` hai.
5. File Size
Yeh part batata hai ki file ka size kitna hai bytes mein. Yahan file size 0 bytes hai, matlab file empty hai.
6. Time
Yeh part batata hai ki file ko last time kab modify kiya gaya tha. Yahan `May 25` ko last modify kiya
gaya hai.
7. File Name
Yeh part file ka naam batata hai, yahan file ka naam `file1` hai.
Full Example ke sath
Agar hum ek aur example lein:
- `d` : Directory
- `rwx` : Owner (user1) ke liye read, write, execute permissions
- `r-x` : Group (group1) ke liye read aur execute permissions, write nahi
- `r-x` : Others ke liye read aur execute permissions, write nahi
- `2` : Link count
- `user1` : Owner
- `group1` : Group owner
- `4096` : Directory size (usually 4096 bytes for directories)
- `May 25 12:34` : Last modification date and time
- `dir1` : Directory name
types of files in linux
1. Regular Files (Ordinary Files)
Regular files are the most common type of files in Linux. Yeh files text, binary data, ya application
data contain kar sakti hain.
- Text Files: Plain text files containing human-readable text.
- Example: `/etc/passwd` (system file containing user account information)
- Command: `cat /etc/passwd`
- Binary Files: Executable files ya compiled programs jo directly run ho sakte hain.
- Example: `/bin/ls` (binary file for the `ls` command)
- Command: `file /bin/ls`
- Data Files: Application-specific data files, jaise images, videos, database files.
- Example: `/var/lib/mysql/` (MySQL database files)
2. Directory Files
Directories are special files that contain references to other files and directories. They act as folders
organizing files into a hierarchical structure.
- Example: `/home/username/`
- Command: `ls -l /home/username/`
3. Special Files
Special files are used to represent hardware devices or perform inter-process communication.
-Character Device Files
Character device files represent devices that transfer data character by character.
- Example: `/dev/tty` (terminal device)
- Command: `ls -l /dev/tty`
- Block Device Files
Block device files represent devices that transfer data in blocks.
- Example: `/dev/sda` (first hard disk)
- Command: `ls -l /dev/sda`
4. FIFO Files (Named Pipes)
FIFO (First In, First Out) files allow one-way communication between processes. They act as a pipe
where data written by one process can be read by another.
- Example: `mypipe` (created using `mkfifo mypipe`)
- Command:
5. Socket Files
Socket files are used for inter-process communication, especially over a network. They allow
processes to communicate with each other.
- Example: `/var/run/docker.sock` (Docker socket file)
- Command: `ls -l /var/run/docker.sock`
6. Symbolic Links (Soft Links)
Symbolic links are special files that point to another file or directory. They are like shortcuts.
- Example: `ln -s /etc/passwd mypasswd` (creates a symbolic link named `mypasswd` pointing
to `/etc/passwd`)
- Command:
7. Hard Links
Hard links are references to the same inode on the disk. Multiple filenames can refer to the same
data on the disk.
- Example: `ln /etc/passwd myhardlink` (creates a hard link named `myhardlink` pointing to
`/etc/passwd`)
- Command:
File Types ko Identify Karna
Using `ls -l` Command:
- `-` at the beginning: Regular file.
- `d` at the beginning: Directory.
- `c` at the beginning: Character device file.
- `b` at the beginning: Block device file.
- `p` at the beginning: FIFO file.
- `s` at the beginning: Socket file.
- `l` at the beginning: Symbolic link.
Using `file` Command:
The `file` command is used to determine the type of a file.
### Examples of File Types
1. Regular Text File
2. Binary File
3. Character Device File
4. Block Device File
5. FIFO File
6. Socket File
7. Symbolic Link
Is tarah se, Linux file system mein alag-alag types of files ka specific purpose hota hai, aur inko
samajhna zaroori hai for effective system administration and file management.
Hard link and soft link
Hard link aur soft link (symbolic link) dono hi files ke references hain, lekin dono kaam karte hain
alag tarike se. Inka difference samajhne ke liye, pehle samajhte hain ki ye links kya hote hain:
Hard Link
Hard link ek direct reference hota hai kisi file ke data blocks par. Multiple filenames ek hi inode
(file metadata) ko point karte hain.
Features:
1. Same Inode Number: Hard link aur original file ka inode number same hota hai.
2. Direct Reference: Hard link directly file ke data blocks ko point karta hai.
3. Independent: Agar original file delete ho jaye, hard link tab bhi data ko access kar sakta hai kyunki
wo data blocks ko directly refer karta hai.
4. Same Filesystem: Hard link ko same filesystem me create karna hota hai.
5. Cannot Link Directories: Hard links directories ke liye nahi banaye ja sakte.
Example:
Suppose humare paas ek file `file1` hai:
Hard link create karne ke liye:
Ab, `file1` aur `hardlink1` dono hi same data blocks ko refer karenge. Verify karne ke liye:
Output kuch aisa ho sakta hai:
Yahan, dono files ka inode number `123456` same hai, aur link count `2` ho gaya hai, jo indicate karta hai
ki do references hain us file ke liye.
Soft Link (Symbolic Link)
Soft link ya symbolic link ek reference hota hai original file ke path ka. Yeh ek alag file hoti hai jo
original file ke path ko point karti hai.
Features:
1. Different Inode Number: Soft link aur original file ka inode number alag hota hai.
2. Path Reference: Soft link original file ke path ko point karta hai, direct data blocks ko nahi.
3. Dependent: Agar original file delete ho jaye, soft link unusable ho jata hai (dangling link).
4. Cross-Filesystem: Soft link different filesystems me create kiya ja sakta hai.
5. Can Link Directories : Soft links directories ke liye bhi banaye ja sakte hain.
Example:
Suppose humare paas ek file `file1` hai:
Soft link create karne ke liye:
Ab, `softlink1` original file `file1` ke path ko refer karega. Verify karne ke liye:
Output kuch aisa ho sakta hai:
Yahan, `file1` aur `softlink1` ka inode number alag hai, aur `softlink1` me arrow (`->`) indicate karta hai
ki yeh soft link hai jo `file1` ko point kar raha hai.
chown command
`chown` command ka use Linux/Unix systems mein file ya directory ka owner aur group change karne ke
liye hota hai. Yeh command system administrators ke liye kaafi useful hai jab unhe file ownership
manage karni hoti hai. Chalo isko detail mein samajhte hain.
Basic Syntax
- `owner` : Naya owner (user) ka naam ya UID.
- `group` : Naya group ka naam ya GID (optional).
- `file` : Jis file ya directory ka owner/group change karna hai.
Examples
1. Change Owner
Agar aapko sirf file ka owner change karna hai:
Isse `file1` ka owner `newuser` ban jayega, lekin group same rahega.
2. Change Owner and Group
Agar aapko file ka owner aur group dono change karna hai:
Isse `file1` ka owner `newuser` aur group `newgroup` ban jayega.
3. Change Only Group
Agar aapko sirf file ka group change karna hai:
Notice karo ki owner ka naam mention nahi kiya, bas `:` ke baad naya group diya. Isse `file1` ka group
`newgroup` ban jayega aur owner same rahega.
4. Recursive Change
Agar aapko directory ke andar ke sabhi files aur subdirectories ka owner aur group change karna hai
to aap `-R` (recursive) option use kar sakte hain:
Isse `directory1` aur uske andar ke sabhi files aur subdirectories ka owner `newuser` aur group
`newgroup` ban jayega.
Options
- `-R, --recursive` : Recursively change ownership of directories and their contents.
- `-v, --verbose` : Output a diagnostic for every file processed.
- `--dereference` : Affect the referent of each symbolic link (default behavior).
- `-h, --no-dereference` : Affect symbolic links instead of any referenced file.
Examples Breakdown
Suppose humare paas ek file `file1` hai jiska owner `user1` aur group `group1` hai:
Output:
Example 1: Change Owner
Output:
Example 2: Change Owner and Group
Output:
Example 3: Change Only Group
Output:
Example 4: Recursive Change
Agar aap directory `dir1` ke liye recursive change karna chahte hain:
Isse `dir1` aur uske andar ke sabhi files aur subdirectories ka owner `user3` aur group `group3` ban
jayega.
Summary
`chown` command ka use aap easily file ya directory ka owner aur group change karne ke liye kar
sakte hain. Commands ka format aur options yaad rakhne ke liye examples ko regularly practice karna
beneficial hoga. `chown` ka use system administration me ownership manage karne ke liye bohot
important tool hai aur iske different options aur use-cases ko samajhna helpful hota hai.
chgrp command
`chgrp` command ka use Linux/Unix systems mein file ya directory ka group change karne ke liye hota
hai. Yeh command kaafi useful hai jab aapko kisi file ya directory ko ek naya group assign karna ho.
Chalo isko detail mein samajhte hain.
Basic Syntax
- `group` : Naya group ka naam ya GID.
- `file` : Jis file ya directory ka group change karna hai.
Examples
1. Change Group
Agar aapko sirf file ka group change karna hai:
Isse `file1` ka group `newgroup` ban jayega.
2. Recursive Change
Agar aapko directory ke andar ke sabhi files aur subdirectories ka group change karna hai to aap `-
R` (recursive) option use kar sakte hain:
Isse `directory1` aur uske andar ke sabhi files aur subdirectories ka group `newgroup` ban jayega.
Options
- `-R, --recursive` : Recursively change group ownership of directories and their contents.
- `-v, --verbose` : Output a diagnostic for every file processed.
- `-c, --changes` : Like verbose but report only when a change is made.
- `-f, --silent, --quiet` : Suppress most error messages.
Examples Breakdown
Suppose humare paas ek file `file1` hai jiska owner `user1` aur group `group1` hai:
Output:
Example 1: Change Group
Output:
Example 2: Recursive Change
Agar aap directory `dir1` ke liye recursive change karna chahte hain:
Isse `dir1` aur uske andar ke sabhi files aur subdirectories ka group `group3` ban jayega.
Output:
Aur agar directory ke andar files hain:
Output:
Summary
`chgrp` command ka use aap easily file ya directory ka group change karne ke liye kar sakte hain.
Commands ka format aur options yaad rakhne ke liye examples ko regularly practice karna beneficial
hoga. `chgrp` ka use system administration me group ownership manage karne ke liye bohot important
tool hai aur iske different options aur use-cases ko samajhna helpful hota hai.
permissions ( read, write and execute)
Linux/Unix systems mein files aur directories ke liye read, write, aur execute permissions define kiye
jaate hain. Yeh permissions specify karte hain ki ek user ya group ke members kya actions perform
kar sakte hain. In permissions ko detail mein samajhne ke liye chalo examples ke sath dekhen:
Types of Permissions
1. Read (r): File ko read karne ki permission.
- File ke liye: Content ko dekh sakte hain.
- Directory ke liye: Directory listing dekh sakte hain (i.e., `ls` command use kar sakte hain).
2. Write (w): File ko modify karne ki permission.
- File ke liye: Content ko change ya edit kar sakte hain.
- Directory ke liye: Directory ke andar files create, delete, rename kar sakte hain.
3. Execute (x): File ko execute/run karne ki permission.
- File ke liye: File ko program ya script ke roop mein run kar sakte hain.
- Directory ke liye: Directory ke andar navigate kar sakte hain (i.e., `cd` command use kar sakte hain).
Permission Notation
Permissions ko three types of users ke liye define kiya jata hai:
- Owner (u): File ka owner.
-Group (g): Group jise file assign ki gayi hai.
- Others (o): Baaki sabhi users.
Permissions ko alphanumeric ya numeric notation mein represent kiya jata hai.
Alphanumeric Notation
- Pehle 3 characters: Owner ke liye permissions.
- Next 3 characters: Group ke liye permissions.
- Last 3 characters: Others ke liye permissions.
Numeric Notation
Numeric notation (octal representation) ka use permissions ko aur compact tarike se represent karne
ke liye hota hai:
- `r` = 4
- `w` = 2
- `x` = 1
Example:
ko numeric notation mein convert karne ke liye:
- Owner: `rwx` = 4+2+1 = 7
- Group: `r-x` = 4+0+1 = 5
- Others: `r--` = 4+0+0 = 4
Toh yeh permission 755 ban jayega.
Examples
Suppose humare paas ek file `file1` hai:
Output:
Example 1: Change Permissions using `chmod`
1. Give execute permission to owner
Output:
2. Give write permission to group
Output:
3. Remove read permission from others
Output:
4. Set permissions using numeric notation
Output:
Directory Permissions
Suppose humare paas ek directory `dir1` hai:
Output:
1. Give write permission to group
Output:
2. Remove execute permission from others
Output:
Understanding `umask` Number in File Permissions
`umask` (User file creation mask) is a Linux command used to set default file permissions for newly
created files and directories. It determines the default permission sets by subtracting the umask value
from the maximum possible permissions.
Key Concepts:
1. File Permissions:
- Read (r): 4
- Write (w): 2
- Execute (x): 1
2. Maximum Permissions:
- Files: 666 (rw-rw-rw-)
- Directories: 777 (rwxrwxrwx)
3. umask Value: Subtracted from the maximum permissions to set default permissions.
How `umask` Works:
- Default umask Value: Typically `022`.
- Calculation
- Subtract the `umask` value from the maximum permissions.
- For example, if the `umask` is `022`:
- For files: 666 - 022 = 644 (rw-r--r--)
- For directories: 777 - 022 = 755 (rwxr-xr-x)
Examples:
1. Setting umask
- To set a `umask` value, use the command: `umask [value]`.
- Example: `umask 027`
2. Calculating Permissions
- umask : 027
- Files:
- Maximum: 666
- umask: - 027
- Result: 640 (rw-r-----)
- Directories
- Maximum: 777
- umask: - 027
- Result: 750 (rwxr-x---)
Summary:
- umask sets default file and directory permissions.
- umask Value is subtracted from maximum permissions.
- umask ensures secure default permissions.
- It is crucial for maintaining security and proper access control in a multi-user environment.