Introduction: Your First Steps into the Server World
Welcome, aspiring DevOps engineer! You’re about to embark on an exciting journey that will transform you into a master of modern software delivery. Our first stop? The foundational bedrock of nearly all cloud infrastructure and server environments: Linux.
Why start with Linux? Because almost every tool, every service, and every critical piece of infrastructure in the DevOps world runs on it. From powerful cloud servers to tiny containers, understanding Linux isn’t just helpful; it’s absolutely essential. Think of it as learning to walk before you can run marathons – you need to be comfortable navigating, manipulating, and understanding a Linux system to truly excel in DevOps.
In this chapter, we’ll demystify the Linux operating system. We’ll start with the very basics of interacting with it through the command line, then explore how it organizes files, manages user access, handles running applications, and even communicates over a network. By the end, you’ll feel confident and capable of performing essential tasks on any Linux machine, setting a strong foundation for everything that comes next. Let’s dive in!
1.1 The Command Line Interface (CLI): Your Superpower
Forget clicking icons; in the server world, your primary interface is the Command Line Interface (CLI). It might look intimidating at first, just a blinking cursor on a black screen, but it’s incredibly powerful and efficient once you get the hang of it. We’ll be using a program called a shell, specifically Bash (Bourne Again SHell), which is the default on most Linux distributions.
Ready to type your first command?
1.1.1 Finding Your Way: pwd, ls, and cd
When you first open a terminal, you’re placed in your home directory. Think of it as your personal workspace.
pwd(Print Working Directory): This command tells you exactly where you are in the filesystem hierarchy.Let’s try it:
pwdYou’ll likely see something like
/home/yourusername. This is your current location.ls(List): This command lists the contents of your current directory. It’s like looking inside a folder.Try listing:
lsYou might see some default directories like
Desktop,Documents,Downloads, etc.What if you want more information? We can add options or flags to commands.
ls -lThe
-lflag stands for “long listing format”. This shows you more details like permissions, ownership, size, and modification date. Pretty neat, right?cd(Change Directory): This is how you move around the filesystem. You providecdwith the path to the directory you want to go to.Let’s go into your
Documentsdirectory (if it exists):cd DocumentsNow, verify your location:
pwdYou should see
/home/yourusername/Documents.To go back up one level, you can use
..:cd ..And back to your home directory:
cd ~The
~(tilde) is a shortcut for your home directory.
1.1.2 Creating and Deleting: mkdir, touch, rm, rmdir
Now that you can navigate, let’s create some things!
mkdir(Make Directory): Creates new directories (folders).Let’s make a new directory for our DevOps learning:
mkdir devops_journeyVerify it was created:
lsYou should see
devops_journeyin the list.touch: This command creates an empty file or updates the timestamp of an existing file.Let’s create a file inside our new directory:
cd devops_journey touch my_first_file.txt lsYou should now see
my_first_file.txt.rm(Remove): This command deletes files. Be careful withrm– there’s usually no “recycle bin” in the CLI!Let’s delete
my_first_file.txt:rm my_first_file.txt lsIt’s gone!
What about directories?
rmdir(Remove Directory): This deletes empty directories.cd .. # Go back up to your home directory rmdir devops_journeyThis would work if
devops_journeywas empty. But wait, we just deleted the file inside it, so it should be empty.What if a directory isn’t empty?
rmdirwill fail. For non-empty directories, you usermwith the-r(recursive) flag. This tellsrmto delete the directory and everything inside it.- Warning: Be extremely careful with
rm -r. A typo can lead to data loss. Always double-check your path!
Let’s recreate our directory and a file, then delete it recursively:
mkdir devops_journey cd devops_journey touch another_file.txt cd .. # Go back up rm -r devops_journey lsAnd it’s gone, along with
another_file.txtinside it!- Warning: Be extremely careful with
1.1.3 Viewing File Content: cat and less
Once you have files, you’ll want to see what’s inside them.
cat(Concatenate): This command displays the entire content of a file to your terminal. It’s great for small files.Let’s create a new file with some content:
mkdir my_files cd my_files echo "Hello, DevOps world!" > greeting.txt echo "This is my first Linux file." >> greeting.txtHere,
echoprints text,>redirects it to a new file (overwriting if it exists), and>>appends to an existing file.Now, view its content:
cat greeting.txtYou should see:
Hello, DevOps world! This is my first Linux file.less: For larger files,catcan flood your screen.lessallows you to view file content page by page, scroll up and down, and search.Let’s simulate a larger file:
for i in {1..50}; do echo "Line $i of important log data." >> large_log.txt; doneNow, try
less:less large_log.txtYou can use the
Up/Downarrow keys,Page Up/Page Down, orqto quit.
Mini-Challenge: Your First Directory Structure!
It’s time to get hands-on!
Challenge:
- Navigate to your home directory (
~). - Create a new directory named
devops_workspace. - Inside
devops_workspace, create two subdirectories:scriptsandconfigs. - Inside
scripts, create a file namedsetup.shand add the text “This is a setup script.” to it. - Inside
configs, create a file namednginx.conf(leave it empty for now). - List the contents of
devops_workspaceto verify your structure. - Display the content of
setup.sh.
- Navigate to your home directory (
Hint: Remember
cd,mkdir,touch,echo,>(for creating/overwriting),cat.What to observe/learn: How to combine commands to build a basic file structure, and how to verify your work.
1.2 Understanding the Linux Filesystem Hierarchy
The Linux filesystem is like a meticulously organized library. Everything starts from the root directory, represented by a single forward slash /. All other directories and files branch off from this root. Understanding this structure is crucial for knowing where to find things and where to put them.
Here’s a simplified look at some key directories:
/: The root directory. The top of the hierarchy./bin: (Binary) Essential user command binaries (e.g.,ls,cat,mkdir)./etc: (Etc.) Configuration files for the system and installed applications./home: Home directories for regular users (e.g.,/home/yourusername)./var: (Variable) Variable data like logs, mail queues, web server content (/var/log,/var/www)./tmp: (Temporary) Temporary files, often cleared on reboot./usr: (Unix System Resources) User programs and data, libraries, documentation.
Let’s visualize this with a simple diagram:
This diagram shows how different important directories branch off directly from the root /. Your personal user files live under /home/yourusername, system configuration is in /etc, and logs often end up in /var/log.
1.3 File and Directory Permissions: Who Can Do What?
In a multi-user environment like Linux, security is paramount. Permissions dictate who can read, write, or execute files and directories. This prevents unauthorized access or accidental changes.
When you use ls -l, you see a string of characters like -rw-r--r--. Let’s break it down:
drwxr-xr-x (for directories) or -rw-r--r-- (for files)
First character (
-ord):d: It’s a directory.-: It’s a regular file.
Next nine characters (e.g.,
rwxr-xr-x): These are grouped into three sets of three, representing permissions for:- Owner: The user who owns the file. (
rwx) - Group: The group associated with the file. (
r-x) - Others: Everyone else on the system. (
r-x)
Within each set, the characters mean:
r: Read permission (can view content, list directory contents).w: Write permission (can modify content, create/delete files in a directory).x: Execute permission (can run a file as a program, enter a directory).-: No permission.
- Owner: The user who owns the file. (
1.3.1 Changing Permissions with chmod
The chmod (change mode) command modifies file and directory permissions. We often use octal notation for simplicity. Each permission (r, w, x) has a numerical value:
r(read) = 4w(write) = 2x(execute) = 1-(no permission) = 0
You sum these values for each of the three groups (owner, group, others).
Common examples:
777:rwx rwx rwx(Everyone can read, write, and execute - usually too permissive!)755:rwx r-x r-x(Owner can do anything, group and others can read and execute - common for directories and executable scripts).644:rw- r-- r--(Owner can read/write, group and others can only read - common for regular files).
Let’s try it:
cd ~/my_files # Go to the directory we created earlier
touch my_script.sh
ls -l my_script.sh
You’ll likely see -rw-rw-r-- (or similar depending on your system’s default umask). This means the owner and group can read/write, and others can only read.
Now, let’s make it executable:
chmod 755 my_script.sh
ls -l my_script.sh
Notice the permissions changed to -rwxr-xr-x. The x is now present for owner, group, and others.
1.3.2 Changing Ownership with chown
The chown (change owner) command changes the user and/or group owner of a file or directory. This often requires sudo (we’ll cover that soon!) because you’re modifying system-level attributes.
# To change user owner:
# sudo chown newuser file_name
# To change group owner:
# sudo chown :newgroup file_name
# To change both:
# sudo chown newuser:newgroup file_name
For now, we’ll just acknowledge chown exists and is used for ownership changes, as setting up new users/groups is outside our immediate scope.
Mini-Challenge: Secure Your Script!
Challenge:
- Navigate to your
~/my_filesdirectory. - Create a new file called
secret_notes.txt. - Add some text to
secret_notes.txt(e.g., “My secret recipe for success!”). - Change the permissions of
secret_notes.txtso that only the owner can read and write it, and no one else (group or others) has any access. - Verify the permissions using
ls -l.
- Navigate to your
Hint: What octal value represents
rw-for the owner and---for everyone else?What to observe/learn: How to apply specific permission sets to restrict access.
1.4 Managing Processes: What’s Running on Your System?
A process is an instance of a running program. Every time you open an application or run a command, a process is created. Understanding how to view and manage these processes is critical for troubleshooting and system administration.
1.4.1 Viewing Processes: ps and top
ps(Process Status): This command shows you a snapshot of the currently running processes.A common way to use it is with the
auxflags:ps auxa: Show processes for all users.u: Display output in a user-oriented format.x: Show processes not attached to a terminal.
You’ll see a lot of output! Look for columns like
USER,PID(Process ID),%CPU,%MEM,COMMAND. TheCOMMANDcolumn tells you what program is running.top: This command provides a real-time, dynamic view of your system’s processes. It’s like a task manager for your terminal.topYou’ll see a constantly updating list of processes, sorted by CPU usage by default. It also shows system summary information like CPU load, memory usage, and swap space.
- Press
qto quittop.
- Press
1.4.2 Stopping Processes: kill
Sometimes a process misbehaves or gets stuck. You might need to stop it manually using the kill command. kill sends a signal to a process, usually to terminate it.
To use kill, you need the process’s PID (Process ID).
Let’s try a safe example:
Open a new terminal window (keep your current one open).
In the new terminal, start a simple background process. The
sleepcommand just waits for a specified number of seconds. The&puts it in the background.sleep 300 &You’ll see a PID returned, like
[1] 12345. Note down the PID (e.g.,12345).Go back to your original terminal. Find your
sleepprocess:ps aux | grep sleepThe
| grep sleeppart is a pipe. It takes the output ofps auxand “pipes” it as input togrep, which filters lines containing “sleep”. You should find yoursleep 300process and its PID.Now, use
killwith its PID:kill YOUR_SLEEP_PID(Replace
YOUR_SLEEP_PIDwith the actual number you found).Check if it’s gone:
ps aux | grep sleepYou should see fewer
sleepprocesses, or none if you only had one running. Thesleepcommand in your other terminal should have terminated.
Mini-Challenge: Background Task Management
Challenge:
- Start a
sleep 600process in the background (&). - Use
ps auxandgrepto find its PID. - Use
topto observe your system, noting if yoursleepprocess appears (it might have very low CPU usage, so it might not be at the very top). - Kill the
sleepprocess using its PID. - Verify it’s no longer running.
- Start a
Hint: Remember the
&for backgrounding,ps aux | grep,top(andqto exit), andkill PID.What to observe/learn: The lifecycle of a basic process, how to identify it, and how to gracefully terminate it.
1.5 Basic Networking Concepts: Connecting to the World
DevOps is all about connected systems. Understanding basic networking on Linux is crucial for deploying applications, troubleshooting connectivity, and configuring services.
1.5.1 Identifying Network Interfaces: ip addr
Your computer has network interfaces (like Wi-Fi or Ethernet cards) that allow it to connect to networks. Each interface has an IP address, which is like its unique postal address on the network.
The modern command to view network interfaces and IP addresses is ip addr. (You might see older tutorials mention ifconfig, but ip addr from the iproute2 suite is the preferred and more powerful tool in modern Linux distributions since around 2010).
ip addr
You’ll see output for interfaces like lo (loopback, your internal address 127.0.0.1) and eth0 or enpXsX (Ethernet) or wlan0 (Wireless). Look for inet addresses – these are your IPv4 addresses.
1.5.2 Testing Connectivity: ping
ping is a simple utility to check if you can reach another host on the network (e.g., a website or another server). It sends small data packets and measures the response time.
ping google.com
You’ll see a continuous stream of replies. This means your computer can successfully communicate with Google’s servers.
- Press
Ctrl+Cto stopping.
1.5.3 Making Web Requests: curl
curl (Client URL) is a versatile command-line tool for making requests to web servers. It’s incredibly useful for testing APIs, downloading files, and interacting with web services.
Let’s make a simple request to a well-known API endpoint:
curl https://api.github.com/zen
This command sends an HTTP GET request to GitHub’s “Zen of GitHub” API, which returns a random philosophical quote. You should see a short quote printed to your terminal.
Mini-Challenge: Network Detective
Challenge:
- Find your machine’s primary IPv4 address using
ip addr. - Ping a public DNS server, like Cloudflare’s
1.1.1.1, for 3 counts only. - Use
curlto fetch information from a public API endpoint, for example,https://ipinfo.io/ip(which simply returns your public IP address).
- Find your machine’s primary IPv4 address using
Hint: For pinging a specific number of times, look into
man pingorping --helpfor options like-c.What to observe/learn: How to identify your network identity, test external connectivity, and interact with web services from the command line.
1.6 User Management: sudo for Superpowers
In Linux, you typically operate as a regular user. However, many administrative tasks (like installing software, changing system configurations, or managing services) require elevated privileges. This is where sudo comes in.
sudo(SuperUser DO): This command allows a permitted user to execute a command as the superuser (root) or another user. When you prefix a command withsudo, you’re temporarily borrowing root’s power.When you first use
sudo, it will ask for your user password (not the root password). This is a security measure to ensure you’re an authorized user.Let’s try a common administrative task: updating the package lists. (Note: This command requires an internet connection and is specific to Debian-based systems like Ubuntu. If you’re on Fedora/CentOS, use
sudo dnf updateinstead.)sudo apt updateEnter your password when prompted. You’ll see your system fetch updates for its package repositories. This command doesn’t install anything, it just updates the list of available software.
Why is
sudoimportant?- Security: It prevents accidental damage to the system by forcing you to explicitly request elevated privileges. You don’t stay logged in as root all the time.
- Auditing:
sudologs who ran which command as root, which is vital for security and troubleshooting.
Modern best practice is to avoid logging in directly as the
rootuser for daily operations. Instead, use a regular user account andsudofor specific administrative tasks.
Mini-Challenge: System Update Check
Challenge:
- Use
sudoto update your system’s package lists. - After running
sudo apt update(orsudo dnf check-update), try runningapt update(ordnf check-update) withoutsudo. What difference do you observe in the output or behavior?
- Use
Hint: Pay attention to any permission-related messages.
What to observe/learn: The distinction between user-level and root-level commands and why
sudois necessary for system-wide changes.
Mini-Challenge: DevOps Workspace Setup
Now, let’s combine several of the commands you’ve learned to simulate setting up a basic project environment.
Challenge:
- Navigate to your home directory (
~). - Create a directory named
my_devops_project. - Inside
my_devops_project, create subdirectories:app,infra,docs. - Inside the
appdirectory, create a file namedmain.pyand add the content:# My Python App. - Inside the
infradirectory, create a file namedserver_config.yaml. - Change the permissions of
main.pyso that the owner can read, write, and execute (rwx), but the group and others can only read (r--). - List the contents of
my_devops_projectrecursively (look upls -R). - Display the content and permissions of
main.py.
- Navigate to your home directory (
Hint: Remember
mkdir,cd,echo >,touch,chmod,ls -l, andls -R.What to observe/learn: How to orchestrate multiple commands to achieve a specific setup, simulating a real-world project structure. This reinforces navigation, creation, and permission management.
Common Pitfalls & Troubleshooting
Even experienced users encounter issues. Here are a few common ones and how to approach them:
“Command not found”:
- Cause: You might have a typo in the command name, or the command isn’t installed on your system, or it’s not in your system’s
PATH. - Fix: Double-check your spelling. If it’s a known command, ensure it’s installed (e.g.,
sudo apt install <package_name>on Ubuntu). You can usewhich <command_name>to see if it’s in yourPATH.
- Cause: You might have a typo in the command name, or the command isn’t installed on your system, or it’s not in your system’s
“Permission denied”:
- Cause: You’re trying to perform an action (read, write, execute, delete) on a file or directory for which your current user lacks the necessary permissions. This often happens when trying to write to system directories (
/etc,/var). - Fix: Check permissions with
ls -l. If you need to perform an administrative task, usesudo. If it’s your own file, you might need tochmodit.
- Cause: You’re trying to perform an action (read, write, execute, delete) on a file or directory for which your current user lacks the necessary permissions. This often happens when trying to write to system directories (
Incorrect Path / “No such file or directory”:
- Cause: The file or directory you’re trying to access doesn’t exist at the specified path, or you’ve made a typo in the path.
- Fix: Use
pwdto confirm your current location. Uselsto see available files/directories. Double-check your path for typos. Remember that Linux paths are case-sensitive!
Summary
Phew! You’ve just taken a massive leap into the world of Linux. Here are the key takeaways from this chapter:
- The Command Line (CLI): Your primary tool for interacting with Linux, using a shell like
Bash. - Navigation:
pwd(print working directory),ls(list contents),cd(change directory). - File Management:
mkdir(make directory),touch(create file),rm(remove file),rm -r(remove directory recursively),cat(view small files),less(view large files). - Filesystem Hierarchy: Understanding the root
/and key directories like/home,/etc,/var. - Permissions:
rwx(read, write, execute) for owner, group, and others, managed withchmod(change mode) using octal notation (e.g.,755,644).chown(change owner) modifies ownership. - Process Management:
ps aux(snapshot of processes),top(real-time monitoring),kill PID(terminate a process). - Networking Basics:
ip addr(view IP addresses),ping(test connectivity),curl(make web requests). sudo: Temporarily grants superuser privileges for administrative tasks, enhancing security.
You’ve built a solid foundation! These fundamental Linux skills will be invaluable as we progress through more advanced DevOps topics. In the next chapter, we’ll dive into Git and GitHub, learning how to manage your code and collaborate effectively. Get ready to version control your world!
References
- The Linux Command Line: A Complete Introduction
- GNU Bash Manual
- Linux Filesystem Hierarchy Standard
- Mermaid.js Documentation - Flowcharts
- iproute2 Utility Suite (Arch Linux Wiki - comprehensive guide for
ipcommand) - curl Official Website
This page is AI-assisted and reviewed. It references official documentation and recognized resources where relevant.