Wx0xo6FsZRyx4rLE66hBR56d1ftvUDQRSK2eJM5q
Bookmark

Leveling Up: An Advanced Linux Tutorial for Real World Ops

The Day I Ran Out of Inodes (Not Space)

I still remember the cold sweat I broke into about six years ago. I was managing a mail server for a mid-sized client running CentOS 7. The monitoring alerts started screaming that the disk was full, but when I ran df -h, it showed 40% free space. I stared at the terminal, confused and honestly a bit panicked because emails were bouncing and the client was calling.

It took me twenty minutes of frantic Googling to realize I hadn't run out of block space; I had run out of inodes. A runaway script had generated nearly two million tiny 4KB temp files, eating up every available index node on the filesystem. That was the day I realized knowing how to navigate directories isn't enough. You need to understand how the OS actually thinks.

Moving from a "Linux user" to an "Advanced Admin" isn't about memorizing flags for the tar command (I still look those up, seriously). It's about understanding the subsystems—kernel, memory, processes, and the filesystem hierarchy—and knowing how to interrogate them when they misbehave. So, let's look at the stuff that actually matters in production environments, mostly based on mistakes I've made so you don't have to.

1. Process Management: Please Stop Using Kill -9

Look, I get it. A process is stuck, you're annoyed, and you just want it gone. kill -9 feels like a power move. But using SIGKILL (9) as your first resort is reckless. It doesn't give the process a chance to clean up child processes, close open file descriptors, or flush buffers to disk. You end up with zombie processes and corrupted data.

Here is how I handle a hung process these days:

  1. Identify with precision: Don't just ps aux | grep. Use pgrep -a [name]. It gives you the full command line and PID without the noise.
  2. Ask nicely first: Send a SIGTERM (15). This is the default for the kill command. kill [PID] tells the app, "Hey, wrap it up, time to go."
  3. The escalation ladder: If it ignores you for 10 seconds, then you escalate.

I rely heavily on htop over standard top. If you are on a minimal server, install it. Being able to filter by tree view (F5) lets you see exactly which parent process spawned that army of workers. Seeing the hierarchy has saved me from killing a master process when I just meant to restart a worker thread.

2. The Text Processing Triad: Grep, Sed, and Awk

You can't open a 5GB log file in nano. It just won't happen. Early in my career, I tried to download huge logs to my local machine to open them in Excel. Don't do that. It takes forever and you look like an amateur.

You need to get comfortable manipulating streams of text directly on the server. Here is a real scenario I dealt with last month: An Nginx server was under heavy load, and I needed to find which IP address was hitting the login endpoint the most.

Instead of installing log analysis tools, I just ran this:

grep "POST /login" access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -n 10

Let's break that down because this specific chain is my bread and butter:

  • grep filters for the specific endpoint.
  • awk '{print $1}' grabs just the first column (the IP address).
  • sort | uniq -c groups them and counts occurrences.
  • sort -nr sorts by the count numerically, descending.

I probably use awk more than Python for quick data extraction. It’s installed everywhere, it’s instant, and it has zero dependencies. If you only learn one advanced text tool, make it awk.

3. File Permissions and ACLs (The Sticky Bit Matters)

Most tutorials stop at chmod 755. But in a multi-user environment, or a shared web hosting setup, basic permissions fall short. I learned this the hard way when two developers kept overwriting each other's files in a shared /var/www directory.

We fixed it with the setgid bit and Access Control Lists (ACLs).

If you set the setgid bit on a directory (chmod g+s directory_name), any new file created inside that directory inherits the group ownership of the directory, not the user's primary group. This is massive for collaboration.

But sometimes that's not enough. That's where setfacl comes in. I rarely see people use this, but it's built into most modern Linux filesystems (ext4, xfs).

setfacl -m u:john:rwx /var/www/project

This gives the user 'john' read/write/execute access without changing the file's owner or group. You can check these hidden permissions with getfacl. If you ever see a + sign at the end of a permission string in ls -l (like drwxr-xr-x+), that means ACLs are active. Don't ignore that plus sign.

4. Systemd: Writing Your Own Service Files

I resisted Systemd for years. I missed the simplicity of init scripts. But honestly? I was wrong. Systemd is robust, and writing a unit file is actually cleaner than writing a reliable bash wrapper.

Recently, I needed a custom Python script to run a data ingestion job at boot, and crucially, it needed to restart automatically if it crashed. Doing this with cron is messy.

Here is the template I use for almost everything. I keep this in /etc/systemd/system/myapp.service:

[Unit]
Description=My Custom Data Ingestor
After=network.target

[Service]
User=deploy
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/python3 /opt/myapp/main.py
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

The Restart=always and RestartSec=5 directives are the killer features here. If the script crashes, Systemd waits 5 seconds and brings it back. No complex monitoring scripts required. Once you save it, just run systemctl daemon-reload and systemctl enable myapp.

5. Networking Diagnosis with ss and tcpdump

For the longest time, I used netstat. But it's technically deprecated (and slow on systems with tons of connections). The modern replacement is ss (socket statistics).

If a web server is hanging, my first move is usually:

ss -tunapl | grep :80

This shows TCP/UDP connections, numeric ports (faster resolve), all states, processes, and listening sockets. It's instant.

When things get really weird—like packets dropping mysteriously—I pull out tcpdump. I used to be intimidated by it, but you really only need a few flags. I was troubleshooting a firewall issue last week where a database connection was timing out. I ran this on the server:

sudo tcpdump -i eth0 port 5432 -n -vv

I could see the SYN packets arriving but no ACK going back. That told me immediately it was a local firewall rule (UFW) blocking the return traffic, not a network routing issue. Without tcpdump, I would have blamed the network team and looked foolish.

FAQ: Questions I Actually Get Asked

How do I safely test dangerous commands?

I advise using a throwaway VM or a Docker container. Do not test rm scripts or partition table changes on your laptop. I spin up a quick Ubuntu container: docker run -it ubuntu bash. I can trash it, exit, and it's gone. It's the ultimate sandbox.

Why does my script run manually but fail in Crontab?

This drives everyone crazy at least once. It is almost always environment variables. When you log in, your shell loads .bashrc or .profile which sets your $PATH. Cron runs with a minimal environment. It might not know where node or python is. Always use absolute paths in cron scripts (e.g., /usr/bin/node instead of just node), or define the PATH explicitly at the top of your crontab.

Is Zsh really better than Bash?

For interactive use? Absolutely. I switched to Zsh with the "Oh My Zsh" framework about four years ago and haven't looked back. The auto-completion, syntax highlighting, and git integration are huge productivity boosters. However, for scripting, stick to Bash (or even sh). It's the universal standard. Write in Bash, live in Zsh.

How do I check what's taking up space in real-time?

Stop using du -sh * over and over. Install ncdu (NCurses Disk Usage). It scans the directory and gives you an interactive, navigatable interface to drill down into folders and see exactly where the bloat is. It’s one of the first things I install on any new server.

A Final Thought on "Mastering" Linux

Here is the truth: you never really finish learning this operating system. I've been working with Linux for over a decade, and just the other day I learned a new flag for ssh that completely changed my workflow. The goal isn't to memorize every man page. It's to get comfortable enough that you stop fearing the error messages.

When you see a "Permission Denied" or a "Connection Refused," don't just copy-paste the error into StackOverflow immediately. Take ten seconds. Look at the logs. Check the permissions. Use the tools like strace or tcpdump to peek under the hood. That curiosity is the only difference between a user and an admin.

Dengarkan
Pilih Suara
1x
* Mengubah pengaturan akan membuat artikel dibacakan ulang dari awal.
Posting Komentar