Linux Basics for DevOps Beginners

ESHNITHIN YADAVESHNITHIN YADAV
25 min read

Introduction

In DevOps, Linux is a fundamental skill because most servers, cloud instances, and tools run on Linux-based systems. Today, we’ll dive deep into essential Linux commands, the vim editor, and user management. These skills are critical for a DevOps engineer to manage files, edit configurations, and control team access on a server.


Understanding the Linux Environment

User Types and Directories
  • Normal User ($): A regular user with limited permissions. Their home directory is typically /home/username. For example, if my username is suresh, my home directory is /home/suresh.

  • Root User (#): The admin or superuser with unrestricted permissions. Their home directory is /root. As the root user, I can perform any action, such as installing software or deleting critical system files.

  • Root Directory (/): The top-level directory in Linux, analogous to the "C:" drive in Windows. All other directories (e.g., /home, /etc, /var) are nested within it.

Example: If I log in as suresh, my terminal prompt might look like suresh@my-server:~$, where $ indicates I’m a normal user. If I switch to the root user with sudo -i, the prompt changes to root@my-server:/root#, where # indicates I’m the root user.

Command Structure

Linux commands follow this format:
command <options> <inputs>

  • Command: The action to perform (e.g., ls to list files).

  • Options: Flags to modify the command’s behavior (e.g., -l for a detailed list).

  • Inputs: The target of the action (e.g., a file or directory name).

Example: ls -l /home lists the contents of /home in a detailed (long) format.


Basic Linux Commands

Let’s explore essential Linux commands with detailed examples, showing the exact commands, their execution, and the resulting outputs.

Listing Files with ls

The ls command lists files and directories in your current directory. It’s one of the most frequently used commands in Linux. Here are its key variations:

ls -l: Shows a long listing with details like permissions, owner, size, and modification date, sorted alphabetically by default.
What Happens: It displays each file or directory on a new line with detailed information.
Example: Suppose my current directory (/home/suresh) contains two files (file1.txt, file2.txt) and a folder (docs). I create them with specific sizes and timestamps to match the scenario:

printf "123456789\n" > file1.txt              # 10 bytes (9 chars + newline)
printf "1234567890123456789\n" > file2.txt    # 20 bytes (19 chars + newline)
mkdir docs
touch -t 202505290041 file1.txt               # 2 minutes ago
touch -t 202505290042 file2.txt               # 1 minute ago
touch -t 202505290043 docs                    # Current time
ls -l

Output:

drwxr-xr-x 2 suresh suresh 4096 May 29 00:43 docs
-rw-r--r-- 1 suresh suresh   10 May 29 00:41 file1.txt
-rw-r--r-- 1 suresh suresh   20 May 29 00:42 file2.txt
  • Order: Alphabetical (docs, file1.txt, file2.txt) since d comes before f, and file1.txt comes before file2.txt (1 before 2 in ASCII).

  • Fields:

    • drwxr-xr-x or -rw-r--r--: Permissions (d for directory; owner has read/write, others have read; directory has execute permissions for navigation).

    • 1 or 2: Number of links (1 for files, 2 for an empty directory due to . entry).

    • suresh suresh: Owner and group.

    • 10, 20, 4096: File sizes in bytes.

    • May 29 00:41 to 00:43: Last modified dates.

    • docs, file1.txt, file2.txt: Names.

ls -lr: Long listing in reverse alphabetical order.
What Happens: Same as ls -l, but the order is reversed.
Example: Using the same directory:

ls -lr

Output:

-rw-r--r-- 1 suresh suresh   20 May 29 00:42 file2.txt
-rw-r--r-- 1 suresh suresh   10 May 29 00:41 file1.txt
drwxr-xr-x 2 suresh suresh 4096 May 29 00:43 docs
  • Order: Reverse alphabetical (file2.txt, file1.txt, docs).

ls -lt: Long listing, sorted by modification time, newest first.
What Happens: Files are sorted by their last modified time, not alphabetically.
Example:

ls -lt

Output:

drwxr-xr-x 2 suresh suresh 4096 May 29 00:43 docs
-rw-r--r-- 1 suresh suresh   20 May 29 00:42 file2.txt
-rw-r--r-- 1 suresh suresh   10 May 29 00:41 file1.txt
  • Order: Newest to oldest (docs at 00:43, file2.txt at 00:42, file1.txt at 00:41).

ls -ltr: Long listing, sorted by time, oldest first (newest at the bottom).
What Happens: Same as ls -lt, but the order is reversed.
Example:

ls -ltr

Output:

-rw-r--r-- 1 suresh suresh   10 May 29 00:41 file1.txt
-rw-r--r-- 1 suresh suresh   20 May 29 00:42 file2.txt
drwxr-xr-x 2 suresh suresh 4096 May 29 00:43 docs
  • Order: Oldest to newest (file1.txt, file2.txt, docs).

ls -la: Shows all files, including hidden ones (files starting with a dot, like .bashrc), in long format.
What Happens: It lists everything, including hidden files that ls -l skips.
Example: Add a hidden file .config (50 bytes, same timestamp as file1.txt):

printf "1234567890123456789012345678901234567890123456789\n" > .config  # 50 bytes
touch -t 202505290041 .config
ls -la

Output:

drwxr-xr-x 3 suresh suresh 4096 May 29 00:43 .
drwxr-xr-x 5 root  root  4096 May 29 00:00 ..
-rw-r--r-- 1 suresh suresh   50 May 29 00:41 .config
drwxr-xr-x 2 suresh suresh 4096 May 29 00:43 docs
-rw-r--r-- 1 suresh suresh   10 May 29 00:41 file1.txt
-rw-r--r-- 1 suresh suresh   20 May 29 00:42 file2.txt
  • Order: Alphabetical, including hidden files (., .., .config, docs, file1.txt, file2.txt).

  • Notes: . (current directory) and .. (parent directory) are always shown with -a.

Why Useful in DevOps?: I use ls -lt to find the latest logs on a server (e.g., /var/log), ls -la to check for hidden config files, and ls -l to verify file ownership before changing permissions.


Creating and Modifying Files

touch <file-name>: Creates an empty file or updates the timestamp of an existing file.
What Happens: If the file doesn’t exist, it creates a new empty file. If it exists, it updates the "last modified" timestamp to the current time.
Example:

touch newfile.txt
ls -l newfile.txt

Output:

-rw-r--r-- 1 suresh suresh    0 May 29 00:43 newfile.txt
  • The file is created with size 0 (empty). If I run touch newfile.txt again (e.g., at 00:44):
touch newfile.txt
ls -l newfile.txt

Output:

-rw-r--r-- 1 suresh suresh    0 May 29 00:44 newfile.txt
  • The timestamp updates to the current time.

Why Useful?: In DevOps, I might use touch to create a placeholder file or update a file’s timestamp to trigger a monitoring script.

cat > <file-name>: Creates a file (or overwrites it if it exists) and lets you type text. Press Ctrl+D to save.
What Happens: It opens an input mode where you type text, and when you save, the file is created or overwritten with that text.
Example:

cat > myfile.txt
Hello, DevOps!
<Ctrl+D>
cat myfile.txt

Output:

Hello, DevOps!
  • If myfile.txt already had content (e.g., "Old text"), it’s overwritten with "Hello, DevOps!".

Why Useful?: I might use this to quickly create a small config file on a server.

cat >> <file-name>: Appends text to a file without overwriting it.
What Happens: It adds your new text to the end of the file, keeping existing content.
Example: Starting with myfile.txt from above:

cat >> myfile.txt
I’m learning Linux!
<Ctrl+D>
cat myfile.txt

Output:

Hello, DevOps!
I’m learning Linux!
  • The new text is appended on a new line.

Why Useful?: In DevOps, I might append error messages to a log file without losing previous entries.

Redirection Explained: The > symbol redirects output to a file, overwriting it, while >> appends. For example:

  • echo "Test" > test.txt creates/overwrites test.txt with "Test".

  • echo "More" >> test.txt appends "More", resulting in:

      Test
      More
    

Managing Files and Directories (CRUD Operations)

CRUD stands for Create, Read, Update, Delete—basic operations for managing files and folders.

mkdir <name>: Creates a directory.
What Happens: It creates a new folder in your current directory.
Example:

mkdir myproject
ls -l

Output:

drwxr-xr-x 2 suresh suresh 4096 May 29 00:43 myproject
  • A new directory named myproject is created. If I run mkdir myproject again:
mkdir myproject

Output (Error):

mkdir: cannot create directory ‘myproject’: File exists

Why Useful?: In DevOps, I might create a directory to store application files, like mkdir /var/www/myapp.

rmdir <directory-name>: Removes an empty directory.
What Happens: It deletes the directory, but only if it’s empty (no files or subdirectories inside).
Example:

rmdir myproject
ls -l

Output: myproject is no longer listed—it’s deleted. If myproject had files inside (e.g., touch myproject/file.txt):

rmdir myproject

Output (Error):

rmdir: failed to remove 'myproject': Directory not empty

Why Useful?: I might use this to clean up empty directories after a project ends, but it’s limited since it only works on empty directories.

rm -f <file-name>: Forcefully deletes a file without asking for confirmation.
What Happens: It deletes the file immediately, even if it’s write-protected, without prompting.
Example:

touch file1.txt
rm -f file1.txt
ls -l

Output: file1.txt is no longer listed—it’s deleted. Without -f, if the file is write-protected (e.g., chmod 444 file1.txt), rm might prompt for confirmation, but -f skips that.

Why Useful?: In DevOps, I might use rm -f in a script to delete temporary files without interruptions.

rm -rf <directory-name>: Recursively and forcefully deletes a directory and all its contents.
What Happens: It deletes the directory, all files, and subdirectories inside it, without asking for confirmation. Be very careful—this cannot be undone!
Example: Suppose myproject contains:

myproject/
├── file1.txt
└── subfolder/
    └── file2.txt

I create this structure:

mkdir -p myproject/subfolder
touch myproject/file1.txt myproject/subfolder/file2.txt

Then run:

rm -rf myproject
ls -l

Output: The entire myproject directory and its contents are gone—nothing named myproject is listed anymore.

Why Useful?: In DevOps, I might use this to clean up a project directory before redeploying an application, but I always double-check to avoid deleting critical data.

cp <source> <destination>: Copies files or folders.
What Happens: It creates a copy of the source at the destination. Use -r for directories to copy recursively (including subdirectories).
Example (File Copy):

touch file1.txt
cp file1.txt file1_backup.txt
ls -l

Output:

-rw-r--r-- 1 suresh suresh    0 May 29 00:43 file1.txt
-rw-r--r-- 1 suresh suresh    0 May 29 00:43 file1_backup.txt

Example (Directory Copy):

mkdir source_dir
touch source_dir/file.txt
cp -r source_dir dest_dir
ls -l dest_dir

Output:

-rw-r--r-- 1 suresh suresh    0 May 29 00:43 file.txt
  • The entire source_dir structure is copied to dest_dir.

Why Useful?: In DevOps, I might back up a config file before editing it, like cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak.

mv <source> <destination>: Moves or renames files/folders (like cut and paste).
What Happens: It moves the source to the destination, deleting the source from its original location. It can also rename if the destination is a new name in the same directory.
Example (Rename):

touch file1.txt
mv file1.txt file2.txt
ls -l

Output:

-rw-r--r-- 1 suresh suresh    0 May 29 00:43 file2.txt
  • file1.txt is renamed to file2.txt.

Example (Move):

mkdir new_dir
mv file2.txt new_dir/
ls -l
ls -l new_dir

Output (Current Directory):

drwxr-xr-x 2 suresh suresh 4096 May 29 00:43 new_dir

Output (Inside new_dir):

-rw-r--r-- 1 suresh suresh    0 May 29 00:43 file2.txt
  • file2.txt is moved into new_dir.

Why Useful?: In DevOps, I might use mv to move log files to an archive directory, like mv /var/log/app.log /archive/app.log.2025-05-29.


Downloading and Viewing Files

wget <URL>: Downloads a file from the internet to your current directory.
What Happens: It fetches the file from the URL and saves it locally with the same name as in the URL.
Example:

wget https://example.com/sample.txt
ls -l

Output:

-rw-r--r-- 1 suresh suresh   21 May 29 00:43 sample.txt
  • Assuming sample.txt contains "This is a sample file\n" (21 bytes: 20 characters + newline). If the file already exists, wget might create a new version (e.g., sample.txt.1).

Why Useful?: In DevOps, I might download a script or tool, like wget https://get.docker.com -O docker-install.sh.

curl <URL>: Displays the content of a URL on the terminal (stdout).
What Happens: It fetches the content and prints it to the screen. To save it, redirect the output with >.
Example:

curl https://example.com/sample.txt

Output:

This is a sample file

Example (Save Output):

curl https://example.com/sample.txt > sample.txt
ls -l

Output:

-rw-r--r-- 1 suresh suresh   21 May 29 00:43 sample.txt

Why Useful?: In DevOps, I might use curl to test an API, like curl https://api.example.com/status, to check if a service is up.

cat <file-name> | grep <word-to-search> or grep <word-to-search> <file>: Searches for a word in a file.
What Happens: It prints lines from the file that contain the search term. The | (pipe) sends cat’s output to grep.
Example:

echo -e "Hello, DevOps\nLinux is fun\nDevOps rocks" > log.txt
cat log.txt | grep DevOps

Output:

Hello, DevOps
DevOps rocks

Alternative:

grep DevOps log.txt

Output: Same as above. Only lines with "DevOps" are shown.

Why Useful?: In DevOps, I might search logs for errors, like grep "ERROR" /var/log/app.log.

tail -f <log-file>: Shows the last lines of a file and updates in real-time as new lines are added.
What Happens: It displays the last 10 lines by default and keeps showing new lines as they’re written to the file.
Example:

echo "Line 1" > app.log
echo "Line 2" >> app.log
tail -f app.log

Output:

Line 1
Line 2
  • If I append to the file in another terminal:
echo "Line 3" >> app.log

The tail -f output updates:

Line 3
  • Press Ctrl+C to stop.

Why Useful?: In DevOps, I use tail -f to monitor logs in real-time, like tail -f /var/log/nginx/access.log to watch website traffic.


Extracting Data from Text

cut: Extracts parts of text using a delimiter.
What Happens: It splits each line by the delimiter (-d) and prints the specified field (-f).
Example:

echo "https://raw.githubusercontent.com/daws-84s/notes/main/session-02.txt" | cut -d "/" -f1

Output:

https:
  • The URL is split by /, and the first field (https:) is printed.

Example (Third Field):

echo "https://raw.githubusercontent.com/daws-84s/notes/main/session-02.txt" | cut -d "/" -f3

Output:

raw.githubusercontent.com

Why Useful?: In DevOps, I might extract parts of log entries, like usernames from a CSV log file.

awk: A powerful tool to extract and manipulate text data.
What Happens: It splits each line by a delimiter (-F) and lets you print specific fields or perform calculations.
Example:

echo "https://raw.githubusercontent.com/daws-84s/notes/main/session-02.txt" | awk -F "/" '{print $1}'

Output:

https:

Example (Third Field):

echo "https://raw.githubusercontent.com/daws-84s/notes/main/session-02.txt" | awk -F "/" '{print $3}'

Output:

raw.githubusercontent.com

Why Useful?: In DevOps, I might use awk to parse logs, like awk '{print $1}' /var/log/access.log to extract IP addresses from a web server log.


Finding Files

find <where to search> -name <file-name>: Searches for a file by name in the specified directory and its subdirectories.
What Happens: It looks recursively for files matching the name and prints their paths.
Example:

mkdir -p /home/suresh/docs
touch /home/suresh/docs/myfile.txt
find /home -name myfile.txt

Output:

/home/suresh/docs/myfile.txt
  • If the file isn’t found, there’s no output. To search the current directory:
cd /home/suresh
find . -name myfile.txt

Output:

./docs/myfile.txt

Why Useful?: In DevOps, I might use find to locate a misplaced config file, like find /etc -name nginx.conf.


Using the vim Editor

vim (Vi Improved) is a powerful text editor in Linux, widely used by DevOps engineers to edit configuration files. It has three modes: Command Mode, Insert Mode, and Last Line Mode (plus a navigation mode often called Normal Mode, entered by pressing Esc).

Opening and Writing in vim

Run vim <file-name> to open a file.
Example:

vim config.txt

What Happens: vim opens config.txt. If the file doesn’t exist, it creates it when you save.

Modes in vim

Command Mode (Normal Mode): For navigation and quick edits. You start in this mode when you open vim. Press Esc to return to this mode from others.

  • u: Undo the last change.
    Example: I type "Line 1", press Esc, type dd (delete line), then u.
    Output: "Line 1" reappears—deletion is undone.

  • yy: Copy the current line.
    Example: My file has "Line 1". I move the cursor to that line, type yy, move down, type p.
    Output:

      Line 1
      Line 1
    
  • p: Paste the copied line below the cursor.
    Example: After yy on "Line 1", I type p. Output is as above.

  • dd: Cut (delete) the current line.
    Example: My file has:

      Line 1
      Line 2
    

    I move to "Line 1", type dd.
    Output:

      Line 2
    
    • "Line 1" is deleted and copied to the clipboard.
  • gg: Go to the top of the file.
    Example: In a 100-line file, I’m at line 50. I type gg.
    Output: Cursor moves to line 1.

  • Shift+G: Go to the bottom of the file.
    Example: Same file, I type Shift+G.
    Output: Cursor moves to line 100.

Insert Mode: For typing text.

  • Press i to enter Insert Mode, type your text, then press Esc to return to Command Mode.
    Example: I open vim config.txt, press i, type "Listen 80", press Esc, then save (see below).
    Output: cat config.txt shows:

      Listen 80
    

Last Line Mode: For running commands (like saving or quitting). Enter this mode by typing : in Command Mode.

  • :q: Quit the file (only works if no changes were made).
    Example: I open vim config.txt, make no changes, type :q, and press Enter.
    Output: vim closes, and I’m back at the terminal. If I made changes, I’d get an error: E37: No write since last change.

  • :wq: Save (write) and quit.
    Example: I type "ServerName localhost" in config.txt, press Esc, then type :wq and Enter.
    Output: vim saves the file and exits. cat config.txt shows:

      ServerName localhost
    
  • :wq!: Force save and quit (useful if there are permission issues).
    Example: If config.txt is read-only, :wq might fail, but :wq! forces the save (if I have sudo permissions).
    Output: File is saved, and vim exits.

  • :q!: Quit without saving.
    Example: I add "Port 8080" to config.txt, press Esc, then type :q!.
    Output: vim exits without saving changes. cat config.txt still shows:

      ServerName localhost
    
  • :/<word>: Search for a word from the top.
    Example: In a file with:

      ServerName localhost
      Port 8080
      ServerName example.com
    

    I type :/ServerName and press Enter.
    Output: The cursor jumps to the first "ServerName" (line 1). Press n to jump to the next match (line 3).

  • :?<word>: Search for a word from the bottom.
    Example: I type :?ServerName.
    Output: The cursor jumps to the last "ServerName" (line 3). Press n to go to the previous match (line 1).

  • :noh: Removes search highlighting.
    Example: After searching, matches are highlighted. I type :noh.
    Output: Highlighting disappears, but I can still press n to jump to matches.

  • :27d: Deletes the 27th line.
    Example: In a file with 30 lines, I type :27d.
    Output: Line 27 is deleted; the file now has 29 lines. If there’s no line 27, nothing happens.

  • :%d: Deletes all lines in the file (empties it).
    Example: My file has:

      Line 1
      Line 2
    

    I type :%d.
    Output: The file is now empty. I save with :wq. cat shows nothing.

  • :3s/sbin/SBIN: In the 3rd line, replaces the first occurrence of "sbin" with "SBIN".
    Example: My file has:

      Line 1
      Line 2
      sbin is here sbin
    

    I type :3s/sbin/SBIN.
    Output:

      Line 1
      Line 2
      SBIN is here sbin
    
    • Only the first "sbin" in line 3 is replaced.
  • :3s/sbin/SBIN/g: Replaces all occurrences of "sbin" with "SBIN" in the 3rd line.
    Example: Same file, I type :3s/sbin/SBIN/g.
    Output:

      Line 1
      Line 2
      SBIN is here SBIN
    
    • Both "sbin"s in line 3 are replaced.
  • :%s/sbin/SBIN/g: Replaces all occurrences of "sbin" with "SBIN" in the entire file.
    Example: My file has:

      sbin on line 1
      Line 2
      sbin is here sbin
    

    I type :%s/sbin/SBIN/g.
    Output:

      SBIN on line 1
      Line 2
      SBIN is here SBIN
    

Practical Example with vim:

  1. Run vim myfile.txt.

  2. Press i, type:

     ServerName localhost
     Port 8080
     ServerName example.com
    
  3. Press Esc, type :/ServerName to search—it jumps to the first "ServerName".

  4. Type :noh to remove highlighting.

  5. Type :%s/ServerName/HostName/g to replace all "ServerName"s with "HostName".
    File now looks like:

     HostName localhost
     Port 8080
     HostName example.com
    
  6. Type :wq to save and quit.

Why Useful?: In DevOps, I use vim to edit server configs, like changing a port in /etc/nginx/nginx.conf or updating a script on a remote server.


Linux Administration: User Management

In DevOps, managing users on a server is crucial, such as creating accounts for team members or removing them when they leave.

CRUD Operations for Users

useradd <user-name>: Creates a new user.
What Happens: It adds the user to the system, creates a primary group with the same name as the user, and optionally creates a home directory (with -m).
Example:

sudo useradd -m suresh
id suresh

Output:

uid=1001(suresh) gid=1001(suresh) groups=1001(suresh)
  • A user suresh is created with UID and GID 1001 (numbers may vary). The -m flag ensures the home directory /home/suresh is created. Without -m, the home directory might not be created, depending on the system’s configuration (e.g., /etc/login.defs).

Why Useful?: In DevOps, I create users for team members to access a server.

id <user-name>: Shows user details (user ID, group ID, etc.).
What Happens: It displays the user’s UID, GID, and group memberships.
Example:

id suresh

Output:

uid=1001(suresh) gid=1001(suresh) groups=1001(suresh)
  • If the user doesn’t exist:
id nosuchuser

Output:

id: ‘nosuchuser’: no such user

Why Useful?: I use id to verify a user was created correctly or to check their group memberships.

passwd <user-name>: Sets or changes the user’s password.
What Happens: It prompts you to enter a new password for the user.
Example:

sudo passwd suresh

Output (Interaction):

Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
  • I enter a password (e.g., mypassword123). If the passwords don’t match:
Sorry, passwords do not match
passwd: Authentication token manipulation error

Why Useful?: In DevOps, I set passwords to allow users to log in securely.

Managing Groups

A group is a collection of users with similar permissions. For example, a DevOps team might have 20 members, so you create a devops group and add them to it.

Primary vs. Secondary Groups:

  • Every user has one primary group (created automatically with the same name as the user).

  • Users can belong to multiple secondary groups for additional permissions.

groupadd <group-name>: Creates a group.
What Happens: It adds a new group to the system, visible in /etc/group.
Example:

sudo groupadd devops
grep devops /etc/group

Output:

devops:x:1002:
  • A group devops is created with GID 1002 (number may vary).

Why Useful?: I create groups to manage permissions for teams, like giving the devops group access to a project folder.

usermod -g <group-name> <user-name>: Changes the user’s primary group.
What Happens: It updates the user’s primary group in /etc/passwd, replacing the previous primary group.
Example:

sudo usermod -g devops suresh
id suresh

Output:

uid=1001(suresh) gid=1002(devops) groups=1002(devops)
  • suresh’s primary group is now devops (GID 1002).

Why Useful?: I might change a user’s primary group to align with a new team structure.

usermod -aG <group-name> <user-name>: Adds the user to a secondary group. The -a (append) flag ensures existing secondary groups aren’t overwritten.
What Happens: It adds the user to the specified group in /etc/group, keeping other secondary groups intact.
Example:

sudo groupadd testers
sudo usermod -aG testers suresh
id suresh

Output:

uid=1001(suresh) gid=1002(devops) groups=1002(devops),1003(testers)
  • suresh is now in the testers group as a secondary group (GID 1003). Without -a, it would overwrite other secondary groups, a common mistake.

Why Useful?: In DevOps, I add users to secondary groups to grant additional permissions, like access to a testing environment.

Removing a User (e.g., Employee Leaving the Organization)

When an employee leaves, you must remove their user account to revoke access. Here’s the standard approach:

  1. Backup the User’s Data: Save their files in case they’re needed later.
    Example:

     sudo cp -r /home/suresh /backups/suresh_backup_$(date +%F)
     ls -l /backups
    

    Output:

     drwxr-xr-x 3 suresh suresh 4096 May 29 00:43 suresh_backup_2025-05-29
    
    • This creates /backups/suresh_backup_2025-05-29 with all of suresh’s files.
  2. Kill the User’s Running Processes: Ensure the user isn’t running any processes.
    Example:

     ps -u suresh
    

    Output:

       PID TTY          TIME CMD
      1234 pts/0    00:00:00 bash
      1235 pts/0    00:00:00 vim
    

    Kill them:

     sudo pkill -u suresh
     ps -u suresh
    

    Output: No processes listed—suresh’s processes are terminated.

  3. Delete the User: Use userdel to remove the user.

    • userdel suresh: Removes the user but leaves their home directory.

    • userdel -r suresh: Removes the user and their home directory (including their mail spool).
      Example:

    sudo userdel -r suresh
    ls -l /home

Output: /home/suresh is no longer listed—it’s deleted along with the user.

  1. Verify Removal: Check if the user is gone.
    Example:

     id suresh
    

    Output:

     id: ‘suresh’: no such user
    
  2. Remove from Groups (Optional): Users are automatically removed from groups upon deletion, but I’d verify.
    Example:

     getent group devops
    

    Output:

     devops:x:1002:
    
    • suresh is no longer listed in the group.
  3. Clean Up (Optional): Remove backups after a retention period (e.g., 30 days).
    Example: After 30 days:

     sudo rm -rf /backups/suresh_backup_2025-05-29
     ls -l /backups
    

    Output: The backup directory is no longer listed.

Why This Approach?: Backing up data ensures nothing important is lost. Killing processes prevents errors during deletion. Deleting the user and their home directory ensures they can’t access the system, maintaining security. Verifying removal confirms the process worked.


Why Learn This for DevOps?

These Linux skills are crucial for DevOps because:

  • You’ll manage servers (e.g., creating files, downloading tools with wget, checking logs with tail).

  • You’ll edit configuration files (e.g., using vim to update a server config).

  • You’ll set up team access on servers (e.g., creating users and groups for your DevOps team, or removing users when they leave).


Interview Questions for DevOps and Cloud Engineer Roles

  1. What are some common ls command options, and how do they help in DevOps?
    Answer: The ls command lists files and directories, and its options are useful for DevOps tasks. For example, ls -l gives a long listing with details like permissions, owner, and size—helpful for checking who owns a file on a server. ls -lt sorts by time with the latest files on top, useful for finding recently modified logs or configs. ls -la shows hidden files (like .bashrc), which might contain server settings. In DevOps, I use ls -ltr to see the oldest files at the top when cleaning up old logs, ensuring I don’t delete recent ones.

  2. What’s the difference between > and >> in Linux, and how would you use them?
    Answer: The > operator redirects output to a file and overwrites it, while >> appends to the file without overwriting. For example, echo "Hello" > myfile.txt creates myfile.txt with "Hello". Running echo "World" > myfile.txt overwrites it to just "World". But echo "World" >> myfile.txt appends, so the file has:

     Hello
     World
    

    In DevOps, I might use > to create a new config file (e.g., curl https://example.com/config > config.txt) and >> to append logs (e.g., echo "Error occurred" >> error.log).

  3. How does the vim editor work, and what are some useful commands for a DevOps engineer?
    Answer: vim has three modes: Command Mode (for navigation), Insert Mode (for typing, entered with i), and Last Line Mode (for commands like saving, entered with :). To edit a file, I run vim config.txt, press i to type changes, press Esc to return to Command Mode, and use :wq to save and quit. Useful commands include :q! to quit without saving, :%s/old/new/g to replace all occurrences of "old" with "new", dd to delete a line, and u to undo. In DevOps, I use vim to edit server configs (e.g., /etc/nginx/nginx.conf). For example, I might use :%s/listen 80/listen 8080/g to change the port number.

  4. How do you create and manage users and groups in Linux? Why is this important for DevOps?
    Answer: To create a user, I use useradd -m suresh to create the user and their home directory, then passwd suresh to set a password. To create a group, I use groupadd devops. I can change a user’s primary group with usermod -g devops suresh or add them to a secondary group with usermod -aG testers suresh. The -a flag is crucial to append the group without overwriting others. In DevOps, this is vital for managing server access. For example, I might create a devops group for my team, add members to it, and set permissions so only they can access certain project folders, ensuring security and collaboration.

  5. What’s the difference between wget and curl, and how would you use them in a DevOps task?
    Answer: wget downloads a file directly to your directory, while curl displays the content on the screen unless redirected. For example, wget https://example.com/script.sh downloads script.sh, but curl https://example.com/script.sh shows its content. To save with curl, I’d use curl https://example.com/script.sh > script.sh. In DevOps, I might use wget to download a tool (e.g., wget https://get.docker.com -O docker-install.sh) or curl to check an API response (e.g., curl https://api.example.com/status) while troubleshooting a server issue.

  6. How do you search for a file or text in Linux, and why is this useful in DevOps?
    Answer: To search for a file, I use find /path -name filename, like find /home -name config.txt. To search for text in a file, I use grep, like grep "error" logfile.txt. I can also combine commands, such as tail -f /var/log/app.log | grep "exception" to monitor logs in real-time for errors. In DevOps, this is useful for debugging. For example, I might use find /etc -name nginx.conf to locate a config file I need to edit.

  7. What are primary and secondary groups in Linux, and how do you assign them to a user?
    Answer: Every user has one primary group, created with the same name as the user during useradd. They can also belong to multiple secondary groups for additional permissions. For example, useradd suresh creates a user and a primary group suresh. To change the primary group, I use usermod -g devops suresh. To add a secondary group, I use usermod -aG testers suresh. In DevOps, this helps manage permissions—for example, I might add all developers to a dev group to give them access to a project directory.

  8. How do you remove a user in Linux when an employee leaves an organization, and what is the standard approach?
    Answer: I follow a standard approach to remove a user securely. First, I back up their data with cp -r /home/suresh /backups/suresh_backup_$(date +%F). Second, I kill their processes with pkill -u suresh. Third, I delete the user with userdel -r suresh, which removes the user and their home directory. Fourth, I verify removal with id suresh (should return "no such user"). Finally, I check group memberships with getent group devops and may clean up backups after 30 days. This ensures security, preserves data if needed, and confirms complete removal.

  9. How do you manage file permissions in Linux, and why is this important for a DevOps engineer?
    Answer: File permissions are managed with chmod and chown. Permissions are in three parts: owner, group, and others, with read (4), write (2), and execute (1) permissions. For example, chmod 644 myfile.txt gives the owner read/write (6) and others read-only (4). chown suresh:devops myfile.txt sets suresh as the owner and devops as the group. In DevOps, this is critical for security. For example, I might set chmod 600 ssh_key.pem to ensure only the owner can access an SSH key, preventing unauthorized access to a server.

  10. What is SSH, and how would you use it to access a remote server in a DevOps role?
    Answer: SSH (Secure Shell) is a protocol to securely access a remote server. I use ssh user@hostname to connect. For example, ssh suresh@192.168.1.100 logs me into the server at 192.168.1.100 as suresh. If I have a private key, I use ssh -i ssh_key.pem suresh@192.168.1.100. In DevOps, SSH is used to manage servers—for example, I might SSH into an AWS EC2 instance to check logs, restart a service, or deploy an application.

  11. What are some basic cloud concepts a DevOps engineer should know, and how do they relate to Linux?
    Answer: DevOps engineers often work with cloud platforms like AWS, Azure, or GCP. Key concepts include virtual machines (like AWS EC2, which often run Linux), storage (like AWS S3), and networking (like VPCs). For example, I might launch an EC2 instance running Ubuntu, SSH into it, and install an application. Linux skills are crucial because most cloud servers run Linux. I’d use wget to download a package, vim to edit configs, and systemctl to manage services on the cloud server, ensuring the application runs smoothly.


0
Subscribe to my newsletter

Read articles from ESHNITHIN YADAV directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

ESHNITHIN YADAV
ESHNITHIN YADAV