
The Day I Locked Myself Out: Linux Permissions
Okay, so picture this: it was my first year in college, diving headfirst into the wild, wonderful world of Linux. I was so excited to ditch Windows for a bit, thinking I was going to be the next Linus...
r5yn1r4143
2w ago
Okay, so picture this: it was my first year in college, diving headfirst into the wild, wonderful world of Linux. I was so excited to ditch Windows for a bit, thinking I was going to be the next Linus Torvalds in no time. My professor assigned us a project that involved playing around with file permissions, and I, in my infinite (and naive) wisdom, decided to "secure" my home directory. Fast forward a few hours, and I’m staring at a black screen with a terrifying message: Permission denied. I couldn't log in. Not into my terminal, not into my graphical desktop, nothing. I was locked out of my own home directory, the very place my user account was supposed to call home! It felt like I had accidentally bricked my own digital existence. Insert dramatic thunderclap here.
The "Oops, I Did It Again" Moment
So, what exactly did I do? It all started with a command I found online, probably from some forum post promising ultimate security. The gist was to change the ownership and permissions of my home directory to be super restrictive. I remember typing something along the lines of:
sudo chown -R root:root /home/myusername
sudo chmod 700 /home/myusername
My logic? If only root could access and modify my files, then no one else could, right? Famous last words. I thought I was being so clever, so secure. I was so wrong. The chown command changed the owner and group of everything in my home directory to root, and chmod 700 made it so only the owner (which was now root) had any access. My user account, which was definitely not root, suddenly had zero permissions to its own files. When I tried to log in, the system couldn't find or read the configuration files in my home directory, hence the dreaded Permission denied error. Even worse, when I tried to switch to my user from the root shell (which I thankfully still had access to via a recovery mode), and tried to cd into my directory:
cd /home/myusername
bash: cd: /home/myusername: Permission denied
My entire user environment was built around that directory. Without access to it, my user account was essentially useless. I couldn't even create a new file or directory within it, let alone access my desktop settings or documents. It was a digital prison, and I had built the walls myself.
The Rescue Mission: Operation Bring My User Back
Panic set in, naturally. My project files, my half-finished essays, my meticulously organized (or so I thought) downloads folder – all trapped. I spent a good hour Googling frantically, trying every variation of "Linux permission denied login" I could think of. Most solutions involved being able to log in first, which was precisely my problem! Thankfully, I remembered that Linux has a recovery mode. Booting into recovery mode gave me a root shell, a lifeline in my moment of digital despair.
From the root shell, I had the power to fix my mistake. The key was to reverse the chown and chmod commands I had so foolishly executed.
First, I needed to change the ownership back to my user. I knew my username was myusername and my primary group was also myusername (a common setup in many Linux distributions).
chown -R myusername:myusername /home/myusername
This command recursively (-R) changes the owner and group of everything inside /home/myusername back to myusername. It's crucial to get this right, otherwise, you're just swapping one problem for another.
Next, I needed to fix the permissions. While chmod 700 was too restrictive, I didn't want to make it too open either. A common and secure set of permissions for a home directory is 755 for the directory itself, allowing the owner full control, and the group and others to execute (list directory contents). However, for the files within the home directory, a more common and secure setting is 644 (owner read/write, group/others read) and for directories within, 755. But since I had recursively changed everything to root and then myusername, I needed to be careful.
The safest bet, especially after a chown disaster, was to set the main directory permissions appropriately and then recursively set permissions for subdirectories and files.
To give my user full control over their home directory, and allow members of the myusername group to read and execute (list contents), and others to read and execute, I used:
chmod 755 /home/myusername
Then, to ensure that files within my home directory were generally readable by my group but only writable by me, and subdirectories were executable, I ran:
find /home/myusername -type d -exec chmod 755 {} \;
find /home/myusername -type f -exec chmod 644 {} \;
The find command is powerful. -type d finds all directories, and -exec chmod 755 {} \; applies chmod 755 to each one. Similarly, -type f finds all files, and -exec chmod 644 {} \; applies chmod 644. This combination is a good balance for most home directories.
After running these commands, I rebooted the system. And lo and behold, I was greeted by my familiar login screen, and I could log in without a hitch! The relief was immense.
Beyond the Terminal: Broader Lessons Learned
This whole ordeal, while terrifying at the time, taught me some invaluable lessons that go way beyond just Linux commands.
Understand, Don't Just Copy-Paste: The biggest takeaway is to always understand what a command does before you execute it, especially with sudo. That sudo chown and sudo chmod were powerful tools used blindly, leading to a self-inflicted lockout. This applies to all areas of IT: when configuring servers, writing scripts, or even setting up cloud infrastructure, know why you’re doing something.
Backup is Your Best Friend: If I had backed up my home directory before this "security experiment," the whole ordeal would have been a minor inconvenience, not a full-blown crisis. Regular backups are non-negotiable, whether it’s for personal files, project code, or critical system configurations.
Recovery Modes are Lifesavers: Knowing how to access recovery modes or single-user modes in Linux is crucial. It’s your "get out of jail free" card when things go terribly wrong. This is relevant in cloud environments too – understanding how to access boot logs or rescue instances can save you from major outages.
**
Comments
Sign in to join the discussion.