Leaving presentation mode.

Control Access to Files

Dennis Kibbe

Mesa Community College

Keyboard Shortcuts

Today we're diving into one of the most fundamental concepts in Linux system administration - file permissions and access control. Understanding how Linux controls who can read, write, and execute files is essential for keeping systems secure and organized. We'll learn how to view current permissions, modify them, and set up directories where groups can collaborate effectively. This slide presentation was created by B6Plus. The audio accompanying this presentation is AI-generated.

Module Outline

Learning Objectives

After completing the work in this module you will be able to:

Interpret Linux File System Permissions

Introduction

Let's start with the big picture - every single file and directory on a Linux system has three key properties: an owner, a group it belongs to, and a set of permissions that control access. Think of this like a filing cabinet where each folder has labels showing who owns it, which department it belongs to, and what each person is allowed to do with the contents. This system ensures that sensitive files stay protected while allowing appropriate sharing when needed.

Linux File-system Permissions

Permission Effect on Files Effect on Directories
r (read) Content of files can be read. Contents can be listed.
w (write) Content of files can be changed. Files can be created or deleted.
x (execute) File can be run as a commands. The cd command can be used.

Here's where things get interesting - permissions work differently for directories than they do for regular files. For a directory, read permission means you can list what's inside it, write permission lets you create or delete files within it, and execute permission allows you to actually enter the directory using the cd command. It's like the difference between being able to see through a window, having keys to rearrange furniture, and having permission to walk through the door.

View File and Directory Permissions and Ownership

[student@workstation ~]$ ls -l myscript.sh
-rwxrwxr--. 1 student student 1520 May  18 07:36 myscript.sh
      

The ls -l command is your primary tool for examining file permissions and ownership - think of it as your detective magnifying glass. When you run this command, you get a detailed listing that shows not just filenames, but all the security information attached to each file. Let's break down what each column tells us about our files.

File Type

-rwxrwxr--. 1 student student 1520 May  18 07:36 myscript.sh
    

The very first character in the permission string tells us what type of file we're looking at. A dash (-) means it's a regular file like a document or program, a 'd' indicates it's a directory or folder, and an 'l' shows it's a symbolic link - basically a shortcut pointing to another file. This is important because the same permission settings can mean different things depending on the file type.

Permissions

-rwxrwxr--. 1 student student 1520 May  18 07:36 myscript.sh
    

After the file type, the next nine characters show permissions in three groups of three - first for the owner, then for the group, and finally for everyone else on the system. Each group shows read (r), write (w), and execute (x) permissions, with dashes indicating where permissions are missing. It's like reading a security badge that shows exactly what each category of person is allowed to do.

SELinux

-rwxrwxr--. 1 student student 1520 May  18 07:36 myscript.sh
    

That period you might see after the permission string indicates that SELinux is also controlling access to this file. SELinux is an additional security layer that provides mandatory access control - think of it as an extra security guard that enforces rules beyond the basic file permissions. We'll cover SELinux in more detail in later courses.

Number of Links

-rwxrwxr--. 1 student student 1520 May  18 07:36 myscript.sh
    

The number after permissions shows how many links point to this file - usually 1 for regular files, but directories always have at least 2 because of the way the filesystem works.

File Owner

-rwxrwxr--. 1 student student 1520 May  18 07:36 myscript.sh
    

Next you'll see the owner's username and the group name. Regular users typically have their own private group with the same name as their username, which helps organize permissions in multi-user environments.

File Size in Bytes

-rwxrwxr--. 1 student student 1520 May  18 07:36 myscript.sh
    

The size field shows how many bytes the file contains - adding the -h option to ls makes these numbers human-readable with K, M, or G suffixes.

Creation or Modification Date

-rwxrwxr--. 1 student student 1520 May 18 07:36 myscript.sh
    

The timestamp shows when the file was last modified, which is crucial for troubleshooting and understanding when changes were made.

File Name

-rwxrwxr--. 1 student student 1520 May 18 07:36 myscript.sh
    

The final field is simply the filename or directory name.

Graded Quiz

Interpret Linux File System Permissions

clipboard.png

After reading this section in the textbook complete the quiz in Canvas.

🛠️ Manage File System Permissions from the Command Line

Now let's learn how to actually change permissions using the chmod command - short for "change mode." This powerful command lets you modify who can access files and what they can do with them. You can use chmod in two different ways: symbolic method using letters, or octal method using numbers.

🛠️ Change File and Directory Permissions

[student@workstation ~]$ man -k permissions
access (2)        - check user's permissions for a file
chmod (2)         - change permissions of a file
console.perms (5) - permissions control file for users at the system console
...
        

The chmod or "change mode" command is used to change file permissions. The command can be used to change permissions by the symbolic method or the octal method.

🛠️ Change Permissions with the Symbolic Method

🛠️ Operator

🛠️ Permissions

🛠️ Symbolic Permission Example

[joe@localhost ~]$ ls -l myscript.sh
-rw-r--r-- 1 joe joe 1824 Sep 14 12:47 myscript.sh
[joe@localhost ~]$ chmod +x myscript.sh
[joe@localhost ~]$ ls -l myscript.sh
-rwxr-xr-x 1 joe joe 1824 Sep 14 12:47 myscript.sh
      

Change Permissions with the Octal Method

Number Description
4 Permission to read file.
2 Permission to write to file.
1 Permission to run file as a program.
0 No permissions

The octal or numeric method often feels easier once you understand the pattern. Each permission has a number value: read is 4, write is 2, and execute is 1. You add these numbers together to get the permission value for each category. For example, read plus write equals 6 (4+2), while read plus execute equals 5 (4+1).

Octal Example

Here's a real-world example where permissions matter critically. SSH private keys must be readable only by their owner for security reasons - if others can read your private key, they could impersonate you on remote systems. The chmod 600 command sets read and write for the owner (6) and no permissions for group or others (0,0).

Change File and Directory User or Group Ownership

Every file has both an owner and a group, and understanding ownership is just as important as understanding permissions. When you create a new file, you automatically become its owner, and it belongs to your primary group. Only the root user can change file ownership, but you can change a file's group to any group you belong to.

Group Ownership

[student@workstation ~]$ ls -l ~/mydoc.md
-rw-rw-rw- 1 student student 2675 Jul 29 2022 mydoc.md
[student@workstation ~]$ chown :consultants mydoc.md
[student@workstation ~]$ ls -l ~/mydoc.md
-rw- - - - - - - 1 student consultants 2675 Jul 29 2022 mydoc.md
        

The chown command changes file ownership, and notice the syntax uses a colon to separate the user and group. In this example, we're changing the group to "consultant" while leaving the owner unchanged. This is commonly done when files need to be shared among team members.

Guided Exercise

Manage File System Permissions from the Command Line

Check Canvas for any Lab Notes for this Guided Exercise.

Manage Default Permissions and File Access

One of the most practical applications of file permissions is creating directories where team members can collaborate. We'll learn how to set up a directory where all members of a specific group can add and delete files while maintaining security. This involves understanding default permissions and special permission bits.

Special Permissions (suid)

[student@workstation ~]$ which passwd
/usr/bin/passwd
[student@workstation ~]$ ls -l /usr/bin/passwd 
-rwsr-xr-x 1 root root 59976 Nov 24  2022 /usr/bin/passwd
      

Sometimes programs need special privileges to do their job. The passwd command is a perfect example - when you change your password, it needs to write to the /etc/shadow file, which only root can normally modify. The setuid bit allows the program to run with the permissions of the file's owner rather than the user running it.

Special Permissions (sticky bit)

[student@workstation ~]$ ls -ld /tmp
drwxrwxrwt 19 root root 4096 Feb  7 08:26 /tmp/
      

The sticky bit is a special permission that's commonly used on directories like /tmp. When set on a directory, it prevents users from deleting or modifying files they don't own, even if they have write permission to the directory. This allows shared directories to function safely without users accidentally damaging each other's files.

🛠️ Setting Special Permissions (Symbolic)

🛠️ Setting Special Permissions (Octal)

🛠️ Examples of Special Permissions

Default File Permissions

Terminal window showing code in Vim, editing the login.defs file.

When you create new files, they don't start with random permissions - they inherit default permissions that are controlled by a setting called umask. The umask value is defined in /etc/login.defs and determines what permissions are automatically removed from new files. Think of umask as a template that shapes every new file you create.

🛠 Effect of umask Utility on Permissions

🛠️ The umask command

[student@workstation ~]$ umask
0022
      

The umask command without an arguments displays the current value of the umask.

Change the umask

[student@workstation ~]$ umask 0002
      

Regular users can temporarily change their umask for the current session using the umask command with a new value. This is useful when you need to create files with different default permissions than usual. The change only lasts until you log out or open a new shell session.

Changing Default Permissions

To make permanent umask changes for regular users, you can create a script in the /etc/profile.d directory. This script will run every time users log in, setting their umask to your desired value. Use the Vim editor to create this script, and remember that changes here will affect all users on the system.

Guided Exercise

Manage Default Permissions and File Access

Control the permissions on files that are created in a directory by using umask settings and the setgid permission.

Module Summary

Control Access to Files

  1. The ls -l command displays file permissions and file ownership.
  2. Add the -d option to ls -l to display the permissions for the directory itself.
  3. The chmod command changes permissions.
  4. The chown command changes the owner of a file or the group to which a file belongs.
  5. Only the superuser can change the owner of a file.
  6. The umask command displays or changes temporarily the default permissions for files.
  7. The suid, sgid, and sticky bit special permissions provide additional access-related features to files.

Resources

Here are some resources so you can continue your learning.

Graded Lab

Control Access to Files

This lab is an excellent opportunity to practice using the Esc+. keyboard shortcut in the command line, which inserts the last argument from your previous command. This technique, covered in Chapter 2, can significantly speed up your workflow when working with file permissions and paths.

Thanks for Watching

We've covered the essential concepts of Linux file permissions - from reading permission displays to modifying ownership and creating collaborative directories. These skills form the foundation of Linux system security and file management. Practice these commands regularly, as you'll use them constantly in real Linux administration work. This is the end of the presentation.

Created on 17 February 2025 by Dennis Kibbe. Last modified on 26 July by DNK.