csariyildiz.github.io


layout: post3 title: “File And Directory Attributes” category: lonux cat: System tags:

In this chapter we will focus on advanced permissions on linux including File And Directory Attributes and special bits like SUID, SGID, and sticky bit.

These features offer granular control over file and directory access, going beyond the basic rwx permissions. Similar to other security mechanisms we focus on later. Such as ACL.

Command Overview

man chattr

lsattr newfile
chattr +a newfile
chattr -a newfile
chattr +i newfile
chattr -i newfile
lsattr -aR /var/log | grep '----i'
lsattr -aR /var/log | grep '---ia'
lsattr -aR /var/log | grep '----i' | awk '{print $2}'

ls -ltr

chmod u+s examplefile
chmod u-s examplefile
chmod g+s examplefile
chmod g-s examplefile

chmod +t /stickydir/
chmod -t /stickydir/

Concepts

File And Directory Attributes

Append Only And Immutable

Creating Reading Updating And Deleting Attributes

lsattr newfile
chattr +a newfile
lsattr newfile
chattr -a newfile
lsattr newfile
man chattr

Search for Attributes

Find Immutable Files (+i) Recursively

lsattr -aR /var/log | grep '----i'

Files with both +i and +a attributes:

lsattr -aR /var/log | grep '---ia'

To display only filenames (and not their attributes), use awk:

lsattr -aR /var/log | grep '----i' | awk '{print $2}'

List Of File And Directory Attributes

SUID SGID Bits

chmod u+s examplefile
-rwsr-xr-x 1 root root 12345 Jan 1 12:00 examplefile
chmod u-s examplefile
-rwSr-xr-x 1 root root 12345 Jan 1 12:00 examplefile
chmod 2664 spiders.jpg
chmod 2674 spiders.jpg

Sticky Bit

chmod +t /stickydir/
chmod 1777 /stickydir/

Steps

1. List file and directory attributes.

sudo lsattr /home
---------------------- /home/acs
---------------------- /home/jack

sudo lsattr test.txt 
---------------------- test.txt

2. Make file append only.

chattr +a test.txt 

3. Verify

sudo lsattr test.txt
-----a---------------- test.txt