Skip to content

File Permissions & Ownership

File permissions control who can read, write, or execute files on Linux systems. This is crucial for:

  • Running bash scripts
  • Sharing datasets between users
  • Securing sensitive files
  • Managing group access on shared systems

# Make script executable
chmod +x script.sh

# Make readable by everyone
chmod a+r data.txt

# Recursive permission change (directories)
chmod -R 755 my_datasets/

# Change ownership
chown user:group file.txt

# Recursive ownership change
chown -R user:group datasets/

Make script executable and run it
# Give execute permission
chmod +x /path/to/yourscript.sh

# Run from absolute path
/path/to/yourscript.sh

# Or run from current directory
./yourscript.sh
# If script is in current directory
chmod +x myscript.sh
./myscript.sh

The ./ indicates “current directory”


chmod 755 file.txt
      ^^^
      |||
      ||+-- Others (everyone else)
      |+--- Group (users in same group)
      +---- Owner (you)
NumberPermissionsMeaning
7rwxRead + Write + Execute
6rw-Read + Write
5r-xRead + Execute
4r--Read only
0---No permissions
# Owner: read+write+execute, Others: read+execute
chmod 755 script.sh

# Owner only can execute (secure)
chmod 700 secure_script.sh

Instead of numbers, you can use letters:

# Add execute for owner
chmod u+x script.sh

# Add read for everyone
chmod a+r data.txt

# Remove write from others
chmod o-w sensitive.txt

# Set group to read+write
chmod g=rw shared.csv

Breakdown:

  • u = user (owner)

  • g = group

  • o = others

  • a = all

  • + = add permission

  • - = remove permission

  • = = set exact permission


# Change owner
sudo chown username file.txt

# Change owner and group
sudo chown username:groupname file.txt

# Recursive for directories
sudo chown -R username:groupname datasets/
Setup shared dataset for team
# Create shared directory
sudo mkdir -p /data/shared_datasets

# Change ownership to group
sudo chown -R :ml_team /data/shared_datasets

# Set permissions: group can read/write
sudo chmod -R 775 /data/shared_datasets

# Set default group for new files
sudo chmod g+s /data/shared_datasets

Problem: bash: ./script.sh: Permission denied

Solution:

chmod +x script.sh
./script.sh

Problem: Need to share large dataset with research group

# Add users to group
sudo usermod -aG ml_team alice
sudo usermod -aG ml_team bob

# Set directory permissions
sudo chown -R :ml_team /datasets/imagenet
sudo chmod -R 775 /datasets/imagenet

# New files inherit group (setgid)
sudo chmod g+s /datasets/imagenet

Scenario 3: Secure Private Keys/Credentials

Section titled “Scenario 3: Secure Private Keys/Credentials”

Problem: Protect sensitive files from other users

# SSH keys, API keys, credentials
chmod 600 ~/.ssh/id_rsa
chmod 600 credentials.json

# Private scripts
chmod 700 my_private_script.sh

Scenario 4: Fix “Others Can Read” Warning

Section titled “Scenario 4: Fix “Others Can Read” Warning”

Problem: Warning about file being readable by others

# Remove all permissions for 'others'
chmod o-rwx sensitive_file.txt

# Or set to 600 (owner read/write only)
chmod 600 sensitive_file.txt

# List with permissions
ls -l file.txt
# Output: -rwxr-xr-x 1 user group 1234 Jan 01 12:00 file.txt
#         |--owner-| |-group-| |-other-|

# List directory contents
ls -lh datasets/

# Check specific file
stat file.txt

# Make all .sh files executable
find ~/scripts -name "*.sh" -exec chmod +x {} \;

# Or using chmod directly
chmod +x ~/scripts/*.sh
# Files: 644 (rw-r--r--)
find /datasets -type f -exec chmod 644 {} \;

# Directories: 755 (rwxr-xr-x)
find /datasets -type d -exec chmod 755 {} \;

Error: chmod: changing permissions of 'file': Operation not permitted

Causes:

  1. Not the owner - Use sudo or ask owner to change
  2. Filesystem mounted read-only - Check with mount | grep ro
  3. File attributes - Check with lsattr file

Permissions Look Correct But Still Can’t Execute

Section titled “Permissions Look Correct But Still Can’t Execute”
# Check if file is actually a script
file script.sh

# Check shebang line
head -1 script.sh
# Should be: #!/bin/bash or #!/usr/bin/env python3

# Check if interpreter exists
which bash
which python3

  1. Scripts: 755 (owner can edit, everyone can execute)
  2. Data files: 644 (owner can edit, everyone can read)
  3. Shared directories: 775 + setgid bit (chmod g+s)
  4. Private files: 600 (owner only)
  5. SSH keys: 600 (required by SSH)