VK

Fundamentals of LinuxFundamentals of Linux

๐Ÿ“š Ensiklopedia ยท Fondasi KuatEnsiklopedia ยท Fondasi Kuat ๐ŸŒ Dual Bahasa (ID / EN) โšก VibeKoding Native

Ensiklopedia VibeKoding: Fundamentals of Linux.Ensiklopedia VibeKoding: Fundamentals of Linux.

๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

In the world of servers, Linux is the undisputed star. Over 90% of servers worldwide run Linux โ€” from WeChat to Google Search, Linux powers it all behind the scenes. For developers, mastering Linux basics is not optional โ€” it's a requirement.In the world of servers, Linux is the undisputed star. Over 90% of servers worldwide run Linux โ€” from WeChat to Google Search, Linux powers it all behind the scenes. For developers, mastering Linux basics is not optional โ€” it's a requirement.

What will you learn from this article?What will you learn from this article?

After completing this chapter, you will gain:After completing this chapter, you will gain:

ChapterContentCore Concepts
Chapter 1File SystemDirectory structure, everything is a file
Chapter 2Common CommandsFiles, text, processes, networking
Chapter 3Permission ModelUsers, groups, rwx, sudo
Chapter 4Shell BasicsPipes, redirection, variables, scripts
Chapter 5Practical ScenariosLog troubleshooting, performance diagnostics

------

1. File System: Everything Is a File1. File System: Everything Is a File

One of Linux's most fundamental philosophies is that everything is a file. Regular files are files, directories are files, hard drives are files, even network connections and process information are files. This unified abstraction lets you use the same set of tools (read, write, permission control) to operate on virtually all system resources.One of Linux's most fundamental philosophies is that everything is a file. Regular files are files, directories are files, hard drives are files, even network connections and process information are files. This unified abstraction lets you use the same set of tools (read, write, permission control) to operate on virtually all system resources.

Directory Structure Cheat SheetDirectory Structure Cheat Sheet

Think of the Linux file system as an upside-down tree:Think of the Linux file system as an upside-down tree:

CODE
/ โ† Root directory (tree root) โ”œโ”€โ”€ home/ โ† Users' home (your files live here) โ”œโ”€โ”€ etc/ โ† Configuration files (the system's "settings panel") โ”œโ”€โ”€ var/ โ† Variable data (logs, cache) โ”œโ”€โ”€ usr/ โ† User-installed programs โ”œโ”€โ”€ tmp/ โ† Temporary files (gone after reboot) โ”œโ”€โ”€ proc/ โ† Process info (virtual, no disk usage) โ”œโ”€โ”€ dev/ โ† Device files (hard drives, terminals) โ”œโ”€โ”€ bin/ โ† Basic commands (ls, cp, mv) โ”œโ”€โ”€ sbin/ โ† System admin commands (requires root) โ”œโ”€โ”€ opt/ โ† Third-party software โ””โ”€โ”€ root/ โ† root user's home directory

Two Types of PathsTwo Types of Paths

TypeFormatExampleDescription
Absolute pathStarts from //home/alice/code/app.jsStarts from root, unambiguous
Relative pathStarts from current directory./code/app.js or ../config. is current directory, .. is parent
๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

Want to check CPU info? Read a file: cat /proc/cpuinfo Want to check memory usage? Read a file: cat /proc/meminfo Want to generate random numbers? Read a file: cat /dev/urandom Want to discard output? Write to a file: echo "no thanks" > /dev/null No special API needed โ€” reading and writing files is enough. This is the elegance of Unix philosophy.Want to check CPU info? Read a file: cat /proc/cpuinfo Want to check memory usage? Read a file: cat /proc/meminfo Want to generate random numbers? Read a file: cat /dev/urandom Want to discard output? Write to a file: echo "no thanks" > /dev/null No special API needed โ€” reading and writing files is enough. This is the elegance of Unix philosophy.

------

2. Common Commands2. Common Commands

Linux commands follow a consistent format: command [options] [arguments]. For example, in ls -la /home, ls is the command, -la is the option, and /home is the argument.Linux commands follow a consistent format: command [options] [arguments]. For example, in ls -la /home, ls is the command, -la is the option, and /home is the argument.

Top 10 Most Used CommandsTop 10 Most Used Commands

If you can only remember 10 commands, remember these:If you can only remember 10 commands, remember these:

CommandPurposeMemory Tip
lsList fileslist
cdChange directorychange directory
catView file contentsconcatenate
grepSearch textglobal regular expression print
findFind filesjust "find"
psView processesprocess status
tail -fWatch logs in real-timeView file "tail", -f means follow
chmodChange permissionschange mode
curlSend HTTP requestsclient URL
sshRemote loginsecure shell

The Art of Combining CommandsThe Art of Combining Commands

Linux's power lies not in individual commands, but in command composition. By connecting multiple simple commands with pipes |, you can solve complex problems:Linux's power lies not in individual commands, but in command composition. By connecting multiple simple commands with pipes |, you can solve complex problems:

bash
# Find the top 5 processes by CPU usage ps aux --sort=-%cpu | head -6 # Count the most frequent error types in logs grep "ERROR" app.log | awk '{print $4}' | sort | uniq -c | sort -rn | head -10 # Find files larger than 100MB find / -size +100M -type f 2>/dev/null # Monitor errors in logs in real-time tail -f /var/log/app.log | grep --color "ERROR"
๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

"Do one thing, and do it well." Each command handles only one function; complex operations are achieved through pipe composition. That's why Linux commands are so compact โ€” they're building blocks, not Swiss Army knives."Do one thing, and do it well." Each command handles only one function; complex operations are achieved through pipe composition. That's why Linux commands are so compact โ€” they're building blocks, not Swiss Army knives.

------

3. Permission Model3. Permission Model

Linux is a multi-user system, and the permission model is the foundation of security. Every file has three sets of permissions controlling what Owner, Group, and Others can do.Linux is a multi-user system, and the permission model is the foundation of security. Every file has three sets of permissions controlling what Owner, Group, and Others can do.

Reading ls -l OutputReading ls -l Output

bash
$ ls -l app.js -rwxr-xr-- 1 alice developers 2048 Jan 15 10:30 app.js โ”‚โ”œโ”€โ”€โ”คโ”œโ”€โ”€โ”คโ”œโ”€โ”€โ”ค โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ File size โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ Group โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ Owner โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ Others permission: r-- (read-only) โ”‚ โ”‚ โ””โ”€โ”€ Group permission: r-x (read + execute) โ”‚ โ””โ”€โ”€ Owner permission: rwx (read + write + execute) โ””โ”€โ”€ File type: - regular file, d directory, l link

Three Permission OperationsThree Permission Operations

PermissionLetterNumberMeaning for FilesMeaning for Directories
Readr4View file contentsList directory contents (ls)
Writew2Modify file contentsCreate/delete files in directory
Executex1Run programs/scriptsEnter directory (cd)

Numeric Permission Quick CalculationNumeric Permission Quick Calculation

Three numbers represent Owner, Group, and Others permissions respectively. Each number is the sum of r(4) + w(2) + x(1):Three numbers represent Owner, Group, and Others permissions respectively. Each number is the sum of r(4) + w(2) + x(1):

CODE
chmod 755 script.sh 7 = rwx (4+2+1) โ†’ Owner: read + write + execute 5 = r-x (4+0+1) โ†’ Group: read + execute 5 = r-x (4+0+1) โ†’ Others: read + execute
Common PermissionMeaningTypical Use
644rw-r--r--Regular files (owner writable, others read-only)
755rwxr-xr-xExecutable files / directories
777rwxrwxrwxEveryone can read, write, execute (dangerous, avoid)

sudo: Temporarily Gain Superuser Privilegessudo: Temporarily Gain Superuser Privileges

Regular users have limited permissions. Some operations require root privileges. sudo lets you execute commands as root temporarily:Regular users have limited permissions. Some operations require root privileges. sudo lets you execute commands as root temporarily:

bash
# Regular user cannot modify system configuration $ vim /etc/nginx/nginx.conf # Permission denied # Use sudo to temporarily elevate privileges $ sudo vim /etc/nginx/nginx.conf # Enter your password, then you can edit # Switch to root user (use with caution) $ sudo su -
โš ๏ธ Catatan Keamanan / Peringatanโš ๏ธ Warning / Security Note

Never use chmod 777 to solve permission problems โ€” that's like removing the door lock. The correct approach is to figure out who needs what permissions and grant them precisely. Similarly, don't operate as root long-term; use sudo only when necessary.Never use chmod 777 to solve permission problems โ€” that's like removing the door lock. The correct approach is to figure out who needs what permissions and grant them precisely. Similarly, don't operate as root long-term; use sudo only when necessary.

------

4. Shell Basics4. Shell Basics

The Shell is the "translator" between you and the Linux kernel. You type commands, and the Shell interprets and passes them to the kernel for execution. The most common shells are Bash (default on most Linux distributions) and Zsh (default on macOS).The Shell is the "translator" between you and the Linux kernel. You type commands, and the Shell interprets and passes them to the kernel for execution. The most common shells are Bash (default on most Linux distributions) and Zsh (default on macOS).

Pipes and RedirectionPipes and Redirection

These are the two most powerful Shell features:These are the two most powerful Shell features:

SymbolNameFunctionExample
``PipePass output of one command as input to another`cat log \grep ERROR`
>Output redirectWrite output to file (overwrite)echo "hello" > file.txt
>>Append redirectAppend output to end of fileecho "world" >> file.txt
<Input redirectRead input from filewc -l < file.txt
2>Error redirectWrite errors to filecmd 2> error.log
2>&1Merge outputMerge errors with standard outputcmd > all.log 2>&1

Environment VariablesEnvironment Variables

Environment variables are "global configuration" in the Shell that affect command behavior:Environment variables are "global configuration" in the Shell that affect command behavior:

bash
# View all environment variables env # View a specific variable echo $PATH echo $HOME # Set temporarily (only in current Shell) export API_KEY="abc123" # Set permanently (write to config file) echo 'export API_KEY="abc123"' >> ~/.bashrc source ~/.bashrc # Apply config immediately
Common VariableMeaningExample Value
$PATHCommand search paths/usr/local/bin:/usr/bin:/bin
$HOMEUser home directory/home/alice
$USERCurrent usernamealice
$PWDCurrent working directory/var/log
$SHELLCurrent shell/bin/bash

Shell Scripting PrimerShell Scripting Primer

Writing multiple commands into a file creates a Shell script. It's the starting point for automated operations:Writing multiple commands into a file creates a Shell script. It's the starting point for automated operations:

bash
#!/bin/bash # deploy.sh - Simple deployment script APP_DIR="/opt/myapp" LOG_FILE="/var/log/deploy.log" echo "$(date) - Starting deployment..." >> $LOG_FILE # Pull latest code cd $APP_DIR && git pull origin main # Install dependencies npm install --production # Restart service pm2 restart myapp echo "$(date) - Deployment complete" >> $LOG_FILE
bash
# Give script execute permission and run it chmod +x deploy.sh ./deploy.sh
๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

Add set -ex at the beginning of scripts: -e makes the script exit immediately on error (instead of continuing), and -x prints each executed command (helpful for troubleshooting). These two options are almost standard in production scripts.Add set -ex at the beginning of scripts: -e makes the script exit immediately on error (instead of continuing), and -x prints each executed command (helpful for troubleshooting). These two options are almost standard in production scripts.

------

5. Practical Scenarios5. Practical Scenarios

Now that the theory is covered, let's look at some practical scenarios you'll encounter most often in development.Now that the theory is covered, let's look at some practical scenarios you'll encounter most often in development.

5.1 Log Troubleshooting5.1 Log Troubleshooting

When a service has issues, the first instinct should be to check logs. Here are common log troubleshooting techniques:When a service has issues, the first instinct should be to check logs. Here are common log troubleshooting techniques:

bash
# 1. Follow logs in real-time (most common) tail -f /var/log/app/error.log # 2. Search for errors in a specific time range grep "2024-01-15 14:" error.log | grep "ERROR" # 3. Count errors per hour grep "ERROR" app.log | awk '{print substr($1,1,13)}' | uniq -c # 4. View last 100 lines of log tail -100 app.log # 5. Search across multiple log files grep -r "OutOfMemory" /var/log/app/

5.2 Process Troubleshooting5.2 Process Troubleshooting

Application freeze, CPU spike, memory leak โ€” these all require starting from the process level:Application freeze, CPU spike, memory leak โ€” these all require starting from the process level:

bash
# View processes with highest CPU usage ps aux --sort=-%cpu | head -10 # View processes with highest memory usage ps aux --sort=-%mem | head -10 # Find a specific process ps aux | grep "node" # View detailed process info (including threads) top -Hp <PID> # View files opened by a process lsof -p <PID> # Gracefully terminate a process (SIGTERM) kill <PID> # Force kill (SIGKILL, last resort) kill -9 <PID>

5.3 Network Diagnostics5.3 Network Diagnostics

Service unreachable? First determine if it's a network issue or an application issue:Service unreachable? First determine if it's a network issue or an application issue:

bash
# Test if target is reachable ping -c 4 google.com # Check if a port is open telnet db-server 3306 # Or use nc nc -zv db-server 3306 # View ports listening on this machine ss -tlnp # Or netstat -tlnp # DNS resolution check dig api.example.com nslookup api.example.com # Test HTTP endpoint curl -v http://localhost:3000/health # View network connection statistics ss -s

5.4 Disk Space Troubleshooting5.4 Disk Space Troubleshooting

Full disk is one of the most common production issues:Full disk is one of the most common production issues:

bash
# View partition usage df -h # Find the largest directories du -sh /* 2>/dev/null | sort -rh | head -10 # Drill down into large directories du -sh /var/log/* | sort -rh | head -10 # Find large files (>100MB) find / -type f -size +100M 2>/dev/null | head -20 # Clean up common space hogs # Clean old logs sudo journalctl --vacuum-size=500M # Clean unused Docker images docker system prune -a
๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

"First logs, second processes, third network, fourth disk." 90% of production issues can be traced to their root cause through these four steps. Once it becomes a habit, your troubleshooting efficiency will improve dramatically."First logs, second processes, third network, fourth disk." 90% of production issues can be traced to their root cause through these four steps. Once it becomes a habit, your troubleshooting efficiency will improve dramatically.

------

SummarySummary

Linux is an essential skill for developers. Mastering the basics is enough to handle most daily development and operations scenarios.Linux is an essential skill for developers. Mastering the basics is enough to handle most daily development and operations scenarios.

Key takeaways from this chapter:Key takeaways from this chapter:

  1. Everything is a file: Linux uses file abstraction to unify access to hardware, processes, networking, and other resourcesEverything is a file: Linux uses file abstraction to unify access to hardware, processes, networking, and other resources
  2. Command composition: Individual commands are simple; true power comes from combining them with pipes |Command composition: Individual commands are simple; true power comes from combining them with pipes |
  3. Permission model: Owner/Group/Others x Read/Write/Execute, set quickly with numbers (like 755)Permission model: Owner/Group/Others x Read/Write/Execute, set quickly with numbers (like 755)
  4. Shell basics: Pipes, redirection, environment variables, and scripts are the building blocks of automationShell basics: Pipes, redirection, environment variables, and scripts are the building blocks of automation
  5. Practical troubleshooting: Logs -> Processes -> Network -> Disk โ€” four steps to diagnose most production issuesPractical troubleshooting: Logs -> Processes -> Network -> Disk โ€” four steps to diagnose most production issues
  6. Further ReadingFurther Reading

    • [Linux Man Pages](https://man7.org/linux/man-pages/) - Official Linux man pages documentation[Linux Man Pages](https://man7.org/linux/man-pages/) - Official Linux man pages documentation
    • [The Linux Command Line](https://linuxcommand.org/tlcl.php) - Free introductory book on the Linux command line[The Linux Command Line](https://linuxcommand.org/tlcl.php) - Free introductory book on the Linux command line
    • [Linux Journey](https://linuxjourney.com/) - Interactive Linux learning website[Linux Journey](https://linuxjourney.com/) - Interactive Linux learning website
    • [explainshell.com](https://explainshell.com/) - Enter a command and it automatically explains each parameter[explainshell.com](https://explainshell.com/) - Enter a command and it automatically explains each parameter