Sling Academy
Home/DevOps/How to lock/unlock a directory in Ubuntu

How to lock/unlock a directory in Ubuntu

Last updated: January 28, 2024

Introduction

When working with Ubuntu, you might come across scenarios where you need to prevent unauthorized access to certain directories. This tutorial will walk you through multiple methods to lock and unlock directories in Ubuntu, starting from basic to advanced techniques, complete with code examples and expected outputs.

Basic File Permissions

Before we get into locking directories, it’s important to understand the basic file permissions in Linux:

  • Read (r): Allows the viewing of file contents.
  • Write (w): Allows modification of file contents.
  • Execute (x): Allows running the file as a program.

You can view these permissions using the command ls -l, which will show you the permissions for the files in the current directory.

Changing File Permissions

To lock a directory (make it inaccessible) for all users other than the owner, you can remove the read and execute permissions for group and others using the chmod command:

$ chmod go-rx /path/to/directory
$ ls -ld /path/to/directory

Expected output:

drwx------ owner group 4096 Jan 1 12:00 /path/to/directory

This indicates that only the owner has read (r), write (w), and execute (x) permissions on the directory.

Restoring Permissions

To unlock the directory, granting read and execute permissions back to group and others:

$ chmod go+rx /path/to/directory
$ ls -ld /path/to/directory

Expected output:

drwxr-xr-x owner group 4096 Jan 1 12:00 /path/to/directory

This sets the directory back to the default permissions, allowing others to read and access the directory’s contents.

Advanced Permission Management

For more fine-grained control, you might want to use Access Control Lists (ACLs).

Installing ACL Support

First, make sure ACL is installed:

$ sudo apt-get install acl

Setting ACLs

To remove access for a specific user:

$ setfacl -m u:username:--- /path/to/directory
$ getfacl /path/to/directory

Expected output should show the permissions for ‘username’ are set to ‘—‘, indicating no access.

To restore access:

$ setfacl -m u:username:r-x /path/to/directory

Directory Encryption

For additional security, directories can be encrypted with tools like ecryptfs.

Installing and Using ecryptfs

First, install ecryptfs:

$ sudo apt-get install ecryptfs-utils

To encrypt a directory:

$ sudo ecryptfs-setup-private --nopwcheck --noautomount

You’ll be prompted to take note of a passphrase that should be saved in a secure location. Your encrypted directory will be under ~/.Private.

Mounting and Unmounting the Encrypted Directory

To access your encrypted directory:

$ ecryptfs-mount-private

To lock (unmount) the directory:

$ ecryptfs-umount-private

Using ‘chmod’ with Scripts for Automation

Using ‘chmod’ can be automated with custom scripts. A script can be written to quickly lock or unlock a directory:

#!/bin/bash
DIR_PATH="/path/to/directory"
LOCK_COMMAND="$1"
if [ "$LOCK_COMMAND" == "lock" ]; then
    chmod go-rx "$DIR_PATH"
elif [ "$LOCK_COMMAND" == "unlock" ]; then
    chmod go+rx "$DIR_PATH"
else
    echo "Unknown command. Use 'lock' or 'unlock'."
fi

You would save this script as lockdir.sh, give it execute permissions using sudo chmod +x lockdir.sh and run it passing ‘lock’ or ‘unlock’ as an argument to change the directory’s state.

Handling Complex Scenarios with ‘find’ and ‘xargs’

For more complex situations, like locking multiple directories at once, you can use find and xargs:

$ find /base/path -type d -exec chmod go-rx {} +

This will recursively remove the read and execute permissions from all directories under ‘/base/path’.

Conclusion

In this tutorial, you’ve learned various methods to lock and unlock directories in Ubuntu, ranging from simple chmod commands to more advanced approaches with access control lists and encryption tools. Remember to always back up your data before applying changes to permissions, especially when dealing with encryption.

Next Article: How to use if-then statement in Bash scripting

Previous Article: Forget Ubuntu root password? Here’s how to reset it

Series: Linux Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide