1. Linux File Attributes
In Linux systems, file and directory attributes include: inode, type, permission attributes, number of hard links, owner and group, and last modification time.
Execute ls -lhi command to view these attributes:
total 4.0K
24563 drwxr-xr-x 2 root root 4.0K Jun 28 21:30 123
6464 -rw-r--r-- 1 root root 0 Jun 28 21:37 file
inode file type & permissions hard links owner group size last modified filename
View file modification, access, and creation times using stat:
[root@localhost linux-study]# stat file
File: `file'
Size: 5 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 6464 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2015-06-28 21:37:27.652640852 +0800
Modify: 2015-06-28 21:43:01.275627486 +0800
Change: 2015-06-28 21:43:01.275627486 +0800
2. Inode
Each storage device or partition (e.g., hard dissk, floppy disk, USB drive) formatted with a filesystem consists of two parts: inodes and blocks. Blocks store actual data, while inodes store metadata about the file, such as size, owner, group, permissions, etc.
The inode provides an index for each file. The operating system uses the inode number to quickly locate the corresponding file. Think of it like a book: the storage device or partition is the book, blocks are the pages, and inodes are the table of contents. The table of contents helps you quickly find the content you need.
Use ls -i to view inode numbers. Files with the same inode number are hard links to each other.
3. Linux File Permissions
3.1 Permission Overview
Linux file/directory permissions are closely tied to users and groups. Each file/directory has a set of 9 permission bits, grouped into three triads: owner permissions (first three characters), group permissions (next three), and other users' permissions (last three). Example: rwxr-xr-x.
-
Permission bits explanation:
- Owner: Read (R), Write (W), Execute (X)
- Group: Read (R), Write (W), Execute (X)
- Other: Read (R), Write (W), Execute (X)
- If a permission is denied, the corresponding character is
-.
-
File permissions:
- Read (r): Ability to view the file's content.
- Write (w): Ability to add or modify file content (note: deleting or renaming is controlled by the parent directory's permissions).
- Execute (x): Ability to run the file as a program.
-
Directory permissions:
- Read (r): Ability to list the directory's contents (but not necessarily to access files in side; that requires execute permission).
- Write (w): Ability to create, delete, or rename files within the directory.
- Execute (x): Ability to enter the directory (e.g., via
cd).
-
Permission comparison table:
| Permission | File | Directory |
|---|---|---|
| r (read) | Read file content | List directory contents |
| w (write) | Modify file content | Create, delete, rename files in directory |
| x (execute) | Execute file (if it's a program) | Enter directory (cd) |
| - (none) | No access | No access |
Important: Deleting or moving a file/directory depends only on the write permission of its parent directory, not on the file's own permissions. Modifying a file's content, however, depends on the file's own write permission.
3.2 Changing Permissions: chmod
chmod changes file/directory permissions. Only the file owner or root can use it. Two syntaxes:
-
Numeric method:
chmod 755 file- r=4, w=2, x=1, -=0
- Permissions are summed per group: owner, group, other. Example: 755 = owner rwx (4+2+1=7), group r-x (4+0+1=5), other r-x (5).
-
Symbolic method:
chmod u+x,go-x file- User types:
uowner,ggroup,oother,aall. - Operators:
+add,-remove,=set exactly. - Permission letters:
r,w,x.
- User types:
Examples:
chmod u=rw file– set owner to read+write.chmod ug=rwx,o=r file– owner and group get all, others get read.chmod a+x file– add execute for everyone.chmod g=u file– make group permissions match owner.- For directories, use recursive flag
-R.
3.3 Default Permissions: umask
umask defines default permissions for newly created files/directories using an octal value that represents denied permissions.
- File default: Files start with 666 (rw-rw-rw-). Final permissions = 666 - umask.
- Directory default: Directories start with 777 (rwxrwxrwx). Final permissions = 777 - umask.
Example: umask 022 → file 644, directory 755.
666 (file base) 777 (directory base)
-022 (umask) -022 (umask)
------ ------
644 755
Verification: umask 044 && touch file1 && mkdir test && ls -al
System defaults: On CentOS, umask 077 is set in /etc/login.defs for user home directories (result: 700, i.e., rwx------). umask can be set in user shell config (.bashrc, .profile) or global config (/etc/profile, /etc/bashrc, /etc/login.defs).
Note: umask is rarely used in production environments.
4. Special Permissions
4.1 SetUID and SetGID
Warning: These permissions introduce security risks and are not recommended for general use. Prefer su or sudo for privilege escalation.
SetUID and SetGID allow ordinary users to execute a program with the privileges of the file owner (usually root) or group. Example: the passwd command.
[root@localhost ~]# ls -l /etc/passwd
-rw-r--r--. 1 root root 904 Jun 16 23:10 /etc/passwd
[root@localhost ~]# ls -l /usr/bin/passwd
-rwsr-xr-x. 1 root root 30768 Feb 22 2012 /usr/bin/passwd
Becuase /usr/bin/passwd has the SetUID bit set (s in owner execute position), any user can run it with root privileges to modify /etc/passwd and change their password.
Example of granting rm to a normal user:
chmod 4755 /bin/rm
Then user longge can remove files as root.
Setting SetUID/SetGID:
- Numeric: SetUID = 4000, SetGID = 2000. Example:
chmod 4775 commandorchmod 2755 filename. - Symbolic:
chmod u+s file(add SetUID),chmod g+s file(add SetGID). - If the corresponding execute bit is set, the special flag appears as lowercase (
s,t); if not, as uppercase (S,T).
4.2 Sticky Bit
The sticky bit is used on directories like /tmp to prevent users from deleting files they don't own.
[root@localhost ~]# ls -ld /tmp/
drwxrwxrwt. 4 root root 4096 Jun 28 21:26 /tmp/
The t at the end indicates the sticky bit is set. Numeric representation: 1000. Example: chmod 1777 directory. Even with rwxrwxrwx permissions, only the file owner or root can delete files inside such a directory. Commonly used for shared directories, but risky in production.
5. Changing Ownership
5.1 chown – Change Owner and Group
Only root or the current owner (if also a member of the new group) can change ownership.
Syntax: chown [OPTIONS] [OWNER][:[GROUP]] FILE
- Owner and group are separated by
.or:. - Either owner or group can be omitted. If owner is omitted, use
:group; if group is omitted, omit the separator.
Example: chown root:root file or chown root: file (keeps current group).
5.2 chgrp – Change Group Only
chgrp [OPTIONS] GROUP FILE
This is essentially a subset of chown, only changing the group. Use -R for recursive changes. In practice, chown is sufficient.
Numeric owner/group display: If a user is deleted but their home directory remains, the owner may appear as a numeric UID/GID.
[root@localhost /]# ls /home/longge/file -l
-rw-rw-r-- 1 500 500 0 Jun 29 00:06 /home/longge/file
To avoid this, use userdel -r longge to remove the user and their home directory together.
6. File Timestamps
File timestamps change upon access or modification. touch can modify timestamps without changing content.
7. File Attributes and Filesystem Attributes
Some file attributes depend on the underlying filesystem. For example, chattr +i file (make a file immutable) works on ext2/ext3/ext4 but not on ReiserFS.
chattr +i file # file becomes undeletable
This locks the file against any modification or deletion, even by root, until the attribute is removed.