Linux Interview Preparation Guide

Linux Interview Prep

Preparing for a Linux interview goes beyond just memorizing commands. It requires clarity, structured understanding, and exposure to real-world scenarios. Whether you're a beginner building foundational skills, or an experienced engineer revising core concepts and troubleshooting techniques — this guide has been crafted for you. Navigate through each section to strengthen your Linux knowledge step by step. 
 
Linux Interview prep

 

Basics and Intermediate

1. What is Linux?
Linux is an open-source operating system used to manage hardware and software resources. It provides a command-line and graphical interface for users to interact with the system.
2. Difference between Linux and Unix?
Linux is open-source and widely used in modern servers and desktops. Unix is proprietary and mostly found in enterprise and legacy systems. Linux has various distributions like Ubuntu, CentOS, while Unix includes AIX, HP-UX.
3. Explain File System Hierarchy in Linux?
The Linux file system is organized like a tree, starting from the root directory /. Here are some common and important directories:

- /: Root directory (top of the hierarchy)
- /root: Home directory of the root (admin) user
- /etc: Contains all configuration files
- /home: Personal files and folders for users
- /var: Logs and variable data (like print spool, mail, etc.)
- /bin: Basic system commands
- /usr: User-installed software and libraries

Understanding this hierarchy helps in locating files and troubleshooting system issues.
4. What is Shell in Linux?
A Shell is a program that lets you interact with the operating system using commands. It acts as a bridge between the user and the Linux kernel.

You can type commands in the shell to run programs, manage files, and perform system tasks.

Example shells:
\n - bash (most common)
- sh
- zsh

Example command:
echo \"Hello World\" — This tells the shell to print text to the screen.
5. How do you find a file in Linux?
You can use the find command to search for files and directories in Linux.

Basic syntax:
find [path] -name [filename]

Example:
find / -name \"test.txt\"
- /: Search from root directory
- -name: Specify the file name

This command will look for a file named test.txt across the entire system.
6. Explain the use of grep command?
The grep command is used to search for specific text or patterns inside files.

It is very helpful for finding errors, logs, or specific content in large files.

Example:
grep "error" /var/log/syslog
This will show all lines that contain the word error in the syslog file.

Common options:
\n - -i: Ignore case
- -r: Recursive search
- -n: Show line numbers
7. What’s the difference between Soft Link and Hard Link? What is a Symbolic Link in Linux?
- A Soft Link (also called Symbolic Link) is like a shortcut to the original file. If the original file is deleted, the soft link will not work.

- A Hard Link is an exact copy that points to the same inode as the original file. Even if the original file is deleted, the hard link still works.

Example to create a soft link:
ln -s /path/to/original.txt softlink.txt

Example to create a hard link:
ln /path/to/original.txt hardlink.txt

Use soft links when you want to link across directories or file systems. Use hard links when you want a backup-like mirror of the file.
8. How do you manage services in Linux?
Services in Linux are managed using commands like systemctl (for systemd-based systems) and service (for older systems).

Using systemctl:
- Start a service: sudo systemctl start nginx
- Stop a service: sudo systemctl stop nginx
- Check status: sudo systemctl status nginx
- Enable on boot: sudo systemctl enable nginx

Using service:
- Start a service: sudo service nginx start
- Stop a service: sudo service nginx stop
- Status: sudo service nginx status

systemctl is more modern and widely used in most Linux distributions today.
9. Explain the difference between Shell Scripting and a Programming Language?
Shell Scripting is mainly used to automate repetitive tasks in a Linux environment using commands.

Programming Languages (like Python, Java, C++) are designed for building full applications with complex logic, data structures, and object-oriented features.

Key Differences:
- Shell scripts are interpreted directly by the shell (like bash)
- Programming languages usually need compilers or interpreters
- Shell scripts are better for task automation
- Programming languages are better for software development

Example Shell Script:
#!/bin/bash
echo \"Backup starting...\"
10. What is SSH and how is it used?
SSH (Secure Shell) is a protocol used to securely connect to remote Linux systems over a network.

It uses encryption to protect the data being transferred between the client and the server, making it safe to use even over the internet.

Basic SSH command:
ssh username@server_ip
Example:
ssh omkar@192.168.1.10

SSH is commonly used by system administrators to remotely manage servers and execute commands.
11. How do you check system resources in Linux?
You can use several commands in Linux to check the system's resource usage like CPU, memory, disk, and more.

top – Shows real-time CPU and memory usage of processes.
Example: top

free – Displays available and used memory (RAM and swap).
Example: free -h (-h for human-readable format)

df – Shows disk usage of mounted file systems.
Example: df -h

du – Displays the size of files and directories.
Example: du -sh /home/omkar (-s: summary, -h: human-readable)

These commands help you monitor system performance and troubleshoot issues easily.
12. What is a Package Manager in Linux?
A Package Manager is a tool that helps you install, update, configure, and remove software packages in Linux systems.

Different Linux distributions use different package managers:
- apt → for Debian/Ubuntu
- yum or dnf → for RHEL/CentOS/Fedora
- pacman → for Arch Linux

Examples:
- Install a package: sudo apt install nginx
- Update packages: sudo apt update
- Remove a package: sudo apt remove nginx

Package managers handle dependencies automatically and make software management easier.
13. Explain the chmod command?
The chmod command is used to change file or directory permissions in Linux.

Every file has permissions for the owner, group, and others — which control read (r), write (w), and execute (x) access.

Numeric values:
- r = 4
- w = 2
- x = 1

So chmod 755 file.sh means:
- Owner: rwx (7)
- Group: rx (5)
- Others: rx (5)

Example:
chmod 644 myfile.txt → read/write for owner, read-only for group and others.
14. What is Kernel in Linux?
The Kernel is the core part of the Linux operating system. It manages all interactions between the hardware and software.

It handles important tasks like:
- Process management
- Memory management
- Device control
- File system operations

When you run a command, the shell passes it to the kernel, and the kernel communicates with the hardware to execute it.

Example command to check your kernel version:
uname -r
15. How do you archive and compress in Linux?
Use tar to archive multiple files or folders into one file. To compress the archive, combine it with tools like gzip or bzip2.

Example using tar + gzip:
tar -czvf archive.tar.gz folder/
- -c: Create an archive
- -z: Compress using gzip
- -v: Show progress (verbose)
- -f: Specify file name

To extract:
tar -xzvf archive.tar.gz
16. How to create a new user in Linux?
To create a new user, use the useradd or adduser command. These commands help you add a new user account to the system.

Example:
sudo adduser omkar

This will:
- Create a user named omkar
- Create a home directory at /home/omkar
- Ask for a password and other optional details

You can check if the user is created by running:
id omkar
17. What is a Root User or what is Root in Linux?
The root user is the superuser in Linux with full administrative access to the entire system.

The root user can:
- Install or remove software
- Modify system files and settings
- Manage all users and permissions
- Access and control everything on the system

You should use root privileges carefully, because even a small mistake can affect the entire system.

To perform commands as root, use sudo:
sudo apt update

You can switch to the root user (if allowed) using:
sudo su
18. What is File Permission and how to change it in Linux?
File permissions control who can read, write, or execute a file or directory in Linux. There are three types of users:
- Owner
- Group
- Others

And three types of permissions:
- r: Read
- w: Write
- x: Execute

To view permissions:
ls -l
Example output: -rwxr-xr--

To change permissions, use the chmod command:
chmod 755 script.sh
This gives: Owner - full access, Group and Others - read + execute

You can also use symbolic mode:
chmod u+x filename.sh → adds execute permission to the owner
19. What is a Daemon in Linux?
A daemon is a background process that runs automatically without user interaction. It usually starts during system boot and keeps running to perform specific tasks or wait for requests.

Common examples of daemons:
- sshd – handles SSH connections
- cron – schedules tasks
- httpd or nginx – web server processes

Daemons often have a d at the end of their name (which stands for daemon).

You can check running daemons using:
ps -ef | grep d
or
systemctl list-units --type=service
20. What is Inode in Linux?
An inode (Index Node) is a data structure used by the Linux file system to store information about a file or directory, except its name and actual data.

Each file has a unique inode that contains details like:
- File size
- Ownership (user/group)
- Permissions
- Timestamps (created, modified, accessed)
- Disk location

You can view a file’s inode number using:
ls -i filename

Inodes help the system track files efficiently, even if multiple files share the same content (like in hard links).
21. What’s the purpose of crontab?
crontab is used to schedule tasks automatically in Linux at specific times or intervals.

It is commonly used for:
- Running backup scripts daily
- Cleaning up temporary files
- Monitoring system health periodically

To view current crontab entries:
crontab -l

To edit crontab:
crontab -e

Example:
0 2 * * * /home/user/backup.sh
This will run the script every day at 2:00 AM.

Crontab uses a 5-field time format: minute hour day month weekday.
22. How to check the IP of a server?
To check the IP address of a Linux server, you can use the following commands:

1. Using ip command:
ip addr show
or simply:
ip a
This will display all network interfaces and their IP addresses.

2. Using hostname command:
hostname -I
This shows the primary IP address of the system.

These commands are helpful to identify how your server is connected in the network.
23. What’s the purpose of the /etc/passwd file?
The /etc/passwd file stores basic information about all user accounts on the Linux system.

Each line in the file represents one user and contains details like:
- Username
- User ID (UID)
- Group ID (GID)
- Home directory
- Login shell

Example entry:
omkar:x:1001:1001:/home/omkar:/bin/bash

This file is readable by all users, but only the root user can modify it. Passwords are not stored here directly—they are stored securely in /etc/shadow.
24. What is Environment in Linux? Explain with an example.
An environment in Linux is a collection of variables that define the behavior of the shell and other processes. These variables can control things like the default editor, system paths, user info, and more.

Common environment variables:
- PATH: Directories to search for executable files
- HOME: User's home directory
- USER: Current logged-in username

To view all environment variables:
printenv

To see a specific variable:
echo $HOME

To temporarily set a variable:
export MYVAR=hello
echo $MYVAR → Output: hello

Environment variables are useful in scripting and automation.
25. What is the /etc/shadow file?
The /etc/shadow file stores encrypted user passwords and related security information for each account on a Linux system.

It is a more secure companion to /etc/passwd because this file is only accessible by the root user.

Each line in the file corresponds to a user and contains:
- Encrypted password
- Password expiration details
- Account lock status

Example entry:
omkar:$6$...$encryptedpassword:19000:0:99999:7:::

This ensures that even if someone can read /etc/passwd, they cannot see the actual passwords.
26. Why do we use the ping command?
The ping command is used to check the connectivity between your system and another host (like a server or website) over a network.

It sends ICMP echo requests and waits for replies to measure:
- Whether the destination is reachable
- How long it takes to respond (latency)
- If any packets are lost in transit

Example:
ping google.com
This checks if your system can reach Google's server and shows response time.

ping is a very useful tool for basic network troubleshooting.
27. What is the difference between wget and curl?
Both wget and curl are command-line tools used to transfer data over the internet, but they have some differences in usage and capabilities:

wget:
- Mainly used to download files from the web
- Supports downloading recursively (entire websites)
- Automatically resumes interrupted downloads
- Simple and non-interactive

Example:
wget https://example.com/file.zip

curl:
- More flexible and supports a wide range of protocols (HTTP, FTP, SMTP, etc.)
- Can be used for API testing (GET, POST, PUT, DELETE)
- Requires manual options to resume downloads
- Supports uploading as well

Example:
curl -O https://example.com/file.zip

In summary: wget is ideal for simple downloading tasks, while curl is more powerful and script-friendly for APIs and advanced use cases.
28. What’s the purpose of xargs command?
The xargs command is used to build and execute commands from standard input. It takes the output of one command and passes it as arguments to another command.

It’s especially useful when dealing with long lists of items or when the output of a command cannot be directly used as input for another.

Example:
find . -name "*.log" | xargs rm
This finds all .log files in the current directory and deletes them using rm.

Without xargs, you'd need to manually loop or write a script. xargs makes the command execution faster and cleaner.
29. What is the difference between a Process and a Service in Linux?
A process is an instance of a running program. A service is a special kind of process that usually runs in the background and performs specific system functions.

Process:
- Started by the user or system when an application runs
- Can be short-lived or long-running
- Examples: firefox, top, ls
- Viewed using: ps aux, top

Service:
- A background process that usually starts during system boot
- Managed by service managers like systemd or init
- Examples: sshd, cron, nginx
- Managed using: systemctl status servicename

In short, all services are processes, but not all processes are services. Services are meant to run continuously in the background to provide functionality.
30. What is LVM?
LVM stands for Logical Volume Manager. It is a system used in Linux to manage disk space more flexibly than traditional partitioning.

With LVM, you can:
- Combine multiple physical disks into one logical volume
- Resize (extend or reduce) volumes easily
- Create snapshots for backup

Key components:
- PV: Physical Volume (actual disk)
- VG: Volume Group (collection of PVs)
- LV: Logical Volume (usable storage created from VG)

Example commands:
pvcreate /dev/sdb
vgcreate my_vg /dev/sdb
lvcreate -L 5G -n my_lv my_vg

LVM makes disk management more dynamic and efficient, especially in server environments.
31. What is nohup command?
The nohup command is used to run a command or script in the background and make it immune to hangups (like closing the terminal).

It ensures that the process keeps running even after the user logs out or the terminal is closed.

Example:
nohup python script.py &
This runs the script in the background and keeps it running after logout. Output is saved in nohup.out by default.

Useful for:
- Long-running jobs
- Remote execution over SSH
- Background services without a terminal session

nohup is short for “no hangup”.
32. What is the purpose of the /var directory?
The /var directory stands for "variable" and contains files that are expected to change frequently during system operation.

It is used to store:
- Log files/var/log/
- Mail and print spool data
- Temporary files for system services
- PID files and lock files

Examples:
- /var/log/syslog → stores system logs
- /var/www/ → default directory for web content

The /var directory plays a key role in system monitoring, debugging, and service management.
33. What is the difference between apt update and apt upgrade?
Both commands are used with Debian-based Linux systems (like Ubuntu) to manage software packages, but they serve different purposes:

apt update
- Updates the local package index (list of available versions)
- Doesn’t install or upgrade anything
- Must be run before apt upgrade

Example: sudo apt update

apt upgrade
- Installs the latest versions of currently installed packages
- Doesn’t remove or install new dependencies by default

Example: sudo apt upgrade

In short: apt update refreshes the list, apt upgrade applies the updates.
34. Where are the network configurations located in Linux?
Network configuration files in Linux are located in different places depending on the distribution. Common locations include:

For Debian/Ubuntu:
- /etc/network/interfaces → Traditional config file
- /etc/netplan/ → Modern config system used in newer Ubuntu versions

For RHEL/CentOS/Fedora:
- /etc/sysconfig/network-scripts/ → Contains interface files like ifcfg-eth0
- /etc/sysconfig/network → General network settings

To view active configuration:
ip addr show or nmcli device show

These files and tools are used to set IP addresses, gateways, DNS, and other network settings.
35. How to move a user to a specific group in Linux?
To add (or move) a user to a specific group, use the usermod command.

Command:
sudo usermod -aG groupname username

- -a: Append the user to the group (without removing from other groups)
- -G: Specifies the group

Example:
sudo usermod -aG docker omkar
This adds user omkar to the docker group.

After running the command, the user may need to log out and log in again for changes to take effect.

You can verify group membership using:
groups username

Advanced

1. What is the difference between a process and a thread?
A process is an independent program in execution with its own memory space, while a thread is the smallest unit of execution within a process that shares the same memory with other threads of the same process.

Key differences:
- Processes are heavy-weight, have their own memory, and switching between processes is slower.
- Threads are light-weight, share memory with the parent process, and are faster to create and switch.

Use cases:
- Use processes for isolated tasks (e.g., different services)
- Use threads for parallel tasks within the same application (e.g., multi-threaded web server)

Example to view processes and threads:
ps -ef → shows processes
ps -eLf → shows threads within processes
2. How does the strace command help in debugging?
strace is a diagnostic tool available in Linux that helps in debugging by tracing system calls and signals used by a program.

It shows every interaction between a process and the Linux kernel, which is useful for understanding what the program is doing in the background.

Common use cases:
- Troubleshooting a program that is hanging or crashing
- Identifying missing files or permission issues
- Checking what files or network resources are accessed

Basic usage:
strace ./my_script.sh
This will show a live trace of system calls made by the script.

To trace an already running process:
strace -p <PID>

strace helps developers and system admins understand low-level behavior without modifying the actual code.
3. How do c-groups (control groups) work in Linux?
cgroups (short for control groups) are a Linux kernel feature that allows you to limit, monitor, and isolate resource usage (CPU, memory, disk, etc.) for a group of processes.

They are very useful for managing resources in large systems or container environments (like Docker and Kubernetes).

Main features:
- Limit CPU or memory usage per group of processes
- Prioritize or restrict I/O bandwidth
- Monitor performance and kill runaway processes

Example: create and assign a memory limit using cgroups v1
mkdir /sys/fs/cgroup/memory/mygroup
echo 100M > /sys/fs/cgroup/memory/mygroup/memory.limit_in_bytes
echo <PID> > /sys/fs/cgroup/memory/mygroup/tasks

Note: In modern systems, cgroups v2 is used, which simplifies the hierarchy and improves management.

cgroups are essential for system administrators, especially in virtualization and containerized environments.
4. What is SELinux and how does it secure Linux?
SELinux (Security-Enhanced Linux) is a security module built into the Linux kernel that provides advanced access control policies to improve system security.

Unlike traditional Linux permissions (like rwx), SELinux enforces mandatory access control (MAC), meaning even root users are restricted by its rules.

How SELinux secures the system:
- Limits what processes can access, modify, or execute
- Prevents unauthorized access to files and services
- Enforces security labels on files, users, and processes

SELinux modes:
- Enforcing: Fully applies policies (default in secure systems)
- Permissive: Logs violations but doesn’t block
- Disabled: SELinux is turned off

To check SELinux status:
sestatus

To temporarily change the mode:
setenforce 0 → Permissive
setenforce 1 → Enforcing

SELinux adds a strong layer of security, especially useful in enterprise and multi-user environments.
5. How do you manage kernel modules in Linux?
Kernel modules are pieces of code that can be loaded or unloaded into the kernel at runtime to extend its functionality (like drivers).

You can manage them using the following commands:

1. lsmod – Lists all currently loaded modules
lsmod

2. modprobe – Loads a module into the kernel
sudo modprobe module_name

3. rmmod – Removes a loaded module from the kernel
sudo rmmod module_name

4. modinfo – Displays information about a module
modinfo module_name

Managing kernel modules is helpful when working with device drivers, security modules, or customizing kernel features.
6. Explain the purpose of the /proc directory in Linux?
The /proc directory is a virtual filesystem in Linux that provides a window into the kernel and the current state of the system.

It doesn’t contain real files but shows real-time system and process information.

Main purposes of /proc:
- View details about running processes (e.g., /proc/1234/)
- Check system-wide configuration and stats (e.g., /proc/meminfo, /proc/cpuinfo)
- Tune kernel parameters via /proc/sys/

Examples:
- cat /proc/cpuinfo → Shows processor info
- cat /proc/meminfo → Shows memory usage
- cat /proc/uptime → Shows system uptime

The /proc directory is essential for monitoring, debugging, and system administration tasks.
7. How to optimize the performance of a Linux OS?
Optimizing Linux performance involves monitoring system resources and tuning configurations for better efficiency and responsiveness.

Key ways to optimize Linux:
- Monitor processes:
Use top, htop, or ps to identify high CPU/memory processes.

- Manage startup services:
Disable unnecessary services using systemctl disable servicename

- Optimize memory usage:
Check free -h and use vmstat to observe memory behavior.
Use swapoff or swapon as needed.

- Clean disk space:
Use du -sh * and df -h to find and remove large or unused files.

- Use tuned or sysctl:
tuned can auto-tune profiles for performance (e.g., throughput-performance)
sysctl -p applies kernel-level tweaks (like TCP stack optimizations).

- Keep the system updated:
Run sudo apt update && sudo apt upgrade regularly for performance and security fixes.

Performance optimization is an ongoing task that depends on system usage and workloads.
8. What is the difference between Hard and Soft Real-Time Systems?
A Real-Time System is designed to respond to inputs or events within a strict time constraint. There are two types: Hard and Soft real-time systems.

Hard Real-Time System:
- Guarantees that critical tasks are completed within a strict deadline
- Missing a deadline can cause system failure
- Used in life-critical systems like medical devices, aircraft control, industrial robots

Soft Real-Time System:
- Tries to meet deadlines, but occasional delays are acceptable
- Performance degrades gracefully if a deadline is missed
- Used in applications like video streaming, gaming, VoIP

Summary:
- Hard real-time = strict & must meet deadlines
- Soft real-time = flexible & tolerates slight delays

Linux is typically used for soft real-time tasks, though special versions (like PREEMPT-RT kernel) can support hard real-time capabilities.
9. How does the iptables command work in Linux?
iptables is a command-line tool used to set up, maintain, and inspect firewall rules in Linux.

It allows system administrators to control incoming and outgoing network traffic based on rules defined for IP addresses, ports, and protocols.

iptables works with 3 main chains:
- INPUT: Controls traffic coming into the system
- OUTPUT: Controls traffic going out of the system
- FORWARD: Controls traffic passing through the system (router/gateway use)

Basic Examples:
- Block incoming traffic from an IP:
iptables -A INPUT -s 192.168.1.10 -j DROP

- Allow SSH (port 22):
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

- View current rules:
iptables -L

Note: Modern Linux systems may use nftables or firewalld as replacements, but iptables is still widely used and supported.
10. What are namespaces in Linux and how are they used?
Namespaces in Linux are a feature of the kernel that isolates system resources for different processes, creating lightweight containers.

They are used to create isolated environments where processes think they are running on their own system. This is a key concept in container technologies like Docker.

Main types of namespaces:
- PID: Isolates process IDs
- NET: Isolates network interfaces
- MNT: Isolates filesystem mount points
- UTS: Isolates hostname and domain name
- IPC: Isolates interprocess communication
- USER: Isolates user and group IDs

How they are used:
- To build containers (e.g., Docker, LXC)
- For process isolation in multi-tenant environments
- To simulate multiple systems on a single host

Example with unshare:
unshare --pid --fork bash
This starts a new shell with a separate PID namespace.

Namespaces are fundamental to modern Linux-based virtualization and containerization.

Real Scenario-Based Questions

1. You are unable to SSH into a node – what could be the problem?
Just saying "SSH is not working" is too generic – it's like saying "I have a pain" without saying where. So we need to narrow down the issue.

Step 1: Get GUI access (if possible)
This allows you to log in directly and check logs without needing SSH.

Step 2: Check SSH-related logs
Log file paths may differ based on the Linux distro. Look into:
- /var/log/secure
- /var/log/auth.log
- /var/log/sshd.log
- /var/log/messages

Step 3: Analyze the error message
Based on the logs or the SSH error message, debug accordingly. Here are common causes:

1. Host not allowed: The client IP might be blocked or restricted.
2. Root login disabled: SSH config might not permit direct root access (PermitRootLogin no).
3. AllowUsers / AllowGroups set: If defined in /etc/ssh/sshd_config, only those users/groups are allowed to login.
4. SSH key permission issue: Incorrect permissions on ~/.ssh/ or authorized_keys can block access.

Tips:
- .ssh directory should have 700 permission
- authorized_keys file should have 600 permission

Always start from the error and then dig deeper using logs and configs. This way, you're not guessing — you're analyzing.
2. How do you mount and unmount filesystems in Linux?
In Linux, you use the mount and umount commands to attach and detach storage devices like partitions or external drives.

Step-by-step for Mounting:
1. Identify the partition using:
sudo fdisk -l or lsblk

2. Create a mount point (a folder to mount into):
sudo mkdir /mnt/mountpnt

3. Mount the partition:
sudo mount /dev/sdX1 /mnt/mountpnt
(Replace /dev/sdX1 with the actual device name.)

Unmounting:
- First, ensure the mount point is not in use (no open files)
- Then run:
sudo umount /mnt/mountpnt

Layman’s Example:
Think of it like going camping with your friends:
- Mounting is like setting up your tent — you find a flat spot (partition), make space (mount point), and set up the tent (filesystem).
- Unmounting is packing up the tent before heading home.

Simple and clean!
3. How do you troubleshoot network connectivity issues in Linux?
Troubleshooting network issues requires a step-by-step approach to identify where the problem lies — from physical connectivity to configuration and services.

Step 1: Check Physical Connectivity
- Make sure the network cable is connected properly
- Ensure Wi-Fi or network interface is enabled

Step 2: Verify Network Configuration
- Check if the network interface has an IP address:
ip addr or ifconfig

- Check the default gateway is set:
ip route

- Verify DNS settings:
cat /etc/resolv.conf

Step 3: Test Internet Connectivity
- Ping a public server to check connectivity:
ping 8.8.8.8 (Google DNS)
- Ping a domain name to check DNS resolution:
ping google.com

Step 4: Check Firewall Rules
- Firewall may block access:
sudo ufw status
sudo iptables -L

Step 5: Restart Network Interface
- Bring interface down and up:
sudo ifdown eth0
sudo ifup eth0

Tip: Reboot the system if changes don't apply immediately.

Follow this checklist to trace and resolve network problems effectively.
4. How do you list all the processes running in Linux?
In Linux, you can view running processes using several commands. Each command offers a different level of detail and interactivity.

1. ps Command
- Displays a snapshot of current processes
- Useful options:
ps -f → Full-format output
ps -ef → Shows all running processes
ps auxf → Detailed view with tree structure

2. top Command
- Shows real-time process activity
- Displays CPU, memory usage, and system stats
- Interactive — lets you kill or renice processes

3. htop Command
- Enhanced version of top with color-coded output
- Easier to use with arrow-key navigation
- Supports filtering, sorting, and mouse interaction

These tools help you monitor and manage system performance by showing what's running and how much resource each process is consuming.
5. You can SSH from one Linux box (192.168.10.12) to another (192.168.10.11), but not from a Windows machine (192.169.10.29) — what could be the problem?
This is most likely a network routing issue.

Even though the Linux boxes can communicate (they're in the same subnet), the Windows box is in a different subnet (192.169.x.x instead of 192.168.x.x).

Root Cause:
- The Linux box 192.168.10.11 might be missing a default gateway
- Without a gateway, it can’t respond to systems outside its subnet (like 192.169.10.29)

How to troubleshoot:
- Run ip route or route -n on the Linux node to check if a gateway is defined
- Try ping and traceroute from both sides to observe where the connection fails
- Add the gateway using:
sudo ip route add default via 192.168.10.1

Layman Example:
Imagine two friends (Linux boxes) living in the same neighborhood — they can easily walk to each other’s homes. But another friend (Windows box) lives in a different neighborhood. If there's no connecting road (gateway), they can’t visit each other.

So here, a gateway (road/bridge) must be added to connect across neighborhoods (subnets).
6. How can I change the default shell to ksh and default home directory to /export/home/<username> when using useradd in Linux?
By default, when you run useradd, the shell is set to /bin/bash and the home directory is set to /home/username.

These defaults are picked from the config file:
/etc/default/useradd

Default entries in that file include:
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes


To change defaults:
- Open /etc/default/useradd
- Modify these lines:
SHELL=/bin/ksh
HOME=/export/home

This ensures that the next time you use useradd, the default shell will be ksh and the home directory will be /export/home/<username> without needing extra arguments.

Alternative:
You can also specify these values directly while adding the user:
useradd -s /bin/ksh -d /export/home/username username

Use the file method for permanent changes or command-line options for one-time use.
7. I set up password-less SSH between two Linux boxes, but it's still asking for a password. What could be wrong?
If you’ve created your SSH keys correctly but are still being prompted for a password, here’s what to check:

1. Public Key Placement
- Ensure the public key from your client machine is properly copied to the target server’s ~/.ssh/authorized_keys file.
- It's better to use:
ssh-copy-id user@remote_host
than copying it manually — it avoids format or permission errors.

2. File and Directory Permissions
Permissions must be correct, or SSH will ignore the keys:
- ~/.ssh should be 700
- ~/.ssh/authorized_keys should be 600
- The user’s home directory should not be world-writable

3. Check SSH Logs
Analyze logs on the target machine to see what went wrong:
- /var/log/secure
- /var/log/sshd
- /var/log/auth.log
- /var/log/messages

These logs will help you understand whether it's a permission issue, a wrong key, or something else.

Quick Tip: Debug your connection with:
ssh -v user@remote_host
It gives verbose output to see why the key might be rejected.
8. Why do I get “Authentication failure” when using su even with the correct password?
This error typically suggests that the password doesn’t match the one stored in /etc/shadow — but if you're sure the password is correct, there could be other reasons.

Possible Causes:
1. Account Lock: - After several failed login attempts, Linux may temporarily lock the user.
- Check using:
pam_tally2 --user username
or
faillock --user username

To unlock:
pam_tally2 --reset --user username
or
faillock --reset --user username

2. No Root Access: - If su - root fails and you don't have another user with sudo rights, it may require resetting the root password from recovery mode or using live CD access.

3. PAM Configuration: - Some systems use PAM modules for additional security, and a misconfiguration can lead to auth issues.

Recommended:
- If you have root or sudo access, check logs for detailed errors:
/var/log/secure
/var/log/auth.log

This will help identify whether it's a lockout, misconfigured authentication rule, or something else.
9. You receive a notification that disk space on your Linux server is critically low — what steps would you take?
When disk space runs low, it's important to act quickly to avoid system failure. Here's how to troubleshoot and resolve the issue:

Step 1: Identify Space Usage
- Use df -h to check disk usage per partition
- Use du -sh * inside large directories (like /var, /home) to find which files or folders are using the most space

Step 2: Clean Up Unnecessary Files
- Remove unused packages: sudo apt autoremove
- Clear package cache: sudo apt clean
- Remove old logs: sudo journalctl --vacuum-time=7d

Step 3: Compress or Move Data
- Compress large files using gzip or tar
- Move infrequently used data to external or secondary storage

Step 4: Add or Resize Storage
- If cleanup isn't enough, you may need to extend the disk size or mount additional volumes using LVM or cloud provider tools

Layman Language:
Think of it like your room getting messy —
First, see what's taking the most space. Then, toss out junk, store less-used items in a box (compress/move), and if it’s still packed, get a bigger closet (resize disk or add more storage).

Keep your Linux system clean and tidy, just like your room!
10. Your Linux server is experiencing high CPU usage, causing performance issues — how would you troubleshoot and mitigate it?
High CPU usage can severely impact system performance. Here's how to handle it step-by-step:

Step 1: Identify Resource-Heavy Processes
- Use tools like top or htop to monitor CPU consumption in real-time
- Focus on the top entries consuming CPU under the %CPU column

Step 2: Analyze the Process
- Is it a legitimate application or a runaway process?
- If it’s unnecessary, consider stopping it with:
kill -9 <PID>

Step 3: Optimize or Adjust
- If the process is critical, consider:
  • Optimizing the script or code
  • Reducing concurrency if possible
  • Adjusting priority using nice or renice

Step 4: Scale Resources
- If the load is constant and expected:
  • Add more CPU cores (vertically scale)
  • Use load balancing to distribute work across servers (horizontal scaling)

Layman Language:
Imagine your friend’s birthday party is so crowded that no one can enjoy properly. First, check which game or activity is attracting too many people. Then, either pause it or move it to a bigger area. Or bring in more organizers (servers) to balance the crowd.

That’s how you manage high CPU — detect the cause, act wisely, and balance the load.
11. Your Linux server is vulnerable to a known security exploit — how would you apply patches and ensure security?
Dealing with vulnerabilities is critical to maintaining system integrity and protecting data. Here's how to handle it step-by-step:

Step 1: Identify the Vulnerability
- Use tools like os-prober, lynis, or check security advisories
- Confirm if your server is affected and which CVE or package is vulnerable

Step 2: Check for Patches
- Use your Linux distro's package manager:
sudo apt update && apt list --upgradable
sudo yum check-update
zypper list-updates

Step 3: Plan Maintenance
- Schedule a maintenance window to avoid downtime during peak usage
- Notify users and application owners

Step 4: Backup Before Action
- Always back up critical data and configurations
- Consider using tools like rsync, tar, or snapshot features if on cloud

Step 5: Apply the Patch
- Install updates securely:
sudo apt upgrade
sudo yum update

Step 6: Post-Update Verification
- Reboot if required
- Check service status and logs
- Run security scans again to confirm the vulnerability is patched

Layman Language:
Imagine there’s a hole in the fence around your clubhouse. First, check if a new piece is available to fix it. Plan a time to fix it when no one’s using the clubhouse. Before you fix it, take a picture of everything inside in case something breaks. After the fix, double-check the fence to ensure no one can sneak in again. That’s patching a Linux system securely.
12. A critical application hosted on your Linux server is not responding — how would you diagnose and resolve the issue?
When an application stops responding, quick diagnosis is essential. Here's how to troubleshoot the issue effectively:

Step 1: Check Logs
- Inspect application-specific logs (usually under /var/log/ or app directories)
- Also check system logs like:
/var/log/syslog, /var/log/messages, /var/log/journal

Step 2: Verify Services
- Confirm whether the app and dependent services (e.g., DB, web server) are running:
systemctl status <service-name>
ps aux | grep <process>

Step 3: Check Network and Ports
- Ensure necessary ports are open using:
ss -tuln or netstat -tuln
- Verify firewall rules: sudo ufw status or iptables -L
- Test connectivity with telnet or curl

Step 4: Restart if Needed
- Gracefully restart the service:
sudo systemctl restart <service-name>
- Or reboot the server as a last resort

Layman Language:
Imagine you're playing an online game and suddenly your character freezes. First, check if any error popped up on screen (like logs). Then, make sure the game servers and internet (services and network) are working. If your friends can play but you can’t, restart your game (application service). That’s how you bring it back online smoothly!

Python Scripting for DevOps

Python Scripting for DevOps and Automation

 

 


 

In this guide, we’ll dive into Python scripting — a subset of programming where you write short, focused scripts to automate tasks, interact with systems, and process data. This article explains the key difference between scripting and programming, then walks you through essential Python scripting concepts like variables, conditionals, functions, and loops with hands-on DevOps use cases.

Technology Stack

In this article, we will cover the following topics:

What is Python Scripting?

To understand Python scripting, it’s helpful to first differentiate between general-purpose programming and scripting.

What is Programming?

Programming is the process of writing complete, modular applications using structured and reusable code. Programs are typically longer, follow software engineering practices, and are intended for large-scale use. Python, Java, C++, etc., are used in such environments.

Use Cases:

  • Building web applications using frameworks like Django or Flask
  • Creating data pipelines and backend APIs
  • Developing ML systems or automating business workflows

Examples:

  • A Django-based blog, an e-commerce site, or a dashboard for internal tools
  • Netflix using Python for recommendation systems and real-time analytics

What is Scripting?

Scripting refers to writing lightweight programs (scripts) for automating repetitive tasks. Scripts are typically smaller than full programs, focus on a single task, and are often written in interpreted languages like Python, Bash, or PowerShell.

Use Cases:

  • Parsing logs
  • Renaming files in bulk
  • Monitoring CPU or disk usage
  • Triggering alerts or backups

Examples:

  • A script that checks disk usage and sends an email if space exceeds 80%
  • A one-liner to batch rename `.txt` files to `.bak`
  • A Python script to read SolarWinds logs and notify the team on high CPU usage

Why Python for Scripting?

Python is a preferred language for scripting due to its:

  • Easy syntax and readability
  • Cross-platform support
  • Rich standard library
  • Support for automation, REST APIs, cloud SDKs, file handling, etc.

Essential Python Scripting Concepts

Intermediate Python Scripting

1. Core Data Structures:

What are data structures in Python?

Data Structure means how our Data is organized.

In Python Scripting, we have 4 most important data structures:

  • List: [23, 10.23, "Server", "jack", 3, 76, 3] - A list can store various types of data, including integers, floats, or strings, and allows duplicates.
  • Set: {1, 23, 12, 3, 40} - A set is similar to a list but does not allow duplicates and is defined using curly braces.
  • Dictionary: {"server1": "aws", "server2": "azure", "server3": "gcp"} - A dictionary stores data in key-value pairs, making it easy to retrieve data from large datasets.
  • Tuple: (2, 43, 5.76, "server", "linux") - A tuple is similar to a list but is immutable, meaning elements cannot be added or removed.

Example of Data Structures:

# List in Python
environments = ["dev", "prd", "test", "stg"]
print(f"Here are the environments: {environments}")
print(f"1st index or the second env: {environments[1]}")
print(len(environments))  # will print the length of the list

environments.append("basiton")  # will append an element to the list
print(f"Here are the environments after append: {environments}")

# Dictionary in Python
device_info = {
    "name": "Ubuntu-machine",
    "RAM": "8 GB",
    "CPU": 8,
    "IsItNew": False
}

print(device_info.get("name"))  # to get the value from a key called name
print(device_info.get("RAM"))   # to get the value from a key called RAM
print(device_info.get("CPU"))    # to get the value from a key called CPU
print(device_info.get("IsItNew"))  # to get the value from a key called IsItNew

device_info.update({"env": environments})  # adding another item to the dictionary
print(device_info)

# Sets in Python
device_id = {1, 2, 3, 4, 5, 12, 14, 15, 23}
new_device_id = {1, 2, 3, 7, 9, 11, 16, 19, 20}
print(f"Unique IDs from device_id: {device_id}")
print(f"Unique IDs from new_device_id: {new_device_id}")

print(device_id.union(new_device_id))  # shows unique IDs from both sets
print(device_id.intersection(new_device_id))  # shows common IDs from both sets

# Tuple in Python
days_of_week = ("sunday", "Monday", "Tuesday")
print(f"The days of the week: {days_of_week}")
    

Output:

Here are the environments: ['dev', 'prd', 'test', 'stg']
1st index or the second env: prd
4  # Length of the list
Here are the environments after append: ['dev', 'prd', 'test', 'stg', 'basiton']
Ubuntu-machine
8 GB
8
False
{'name': 'Ubuntu-machine', 'RAM': '8 GB', 'CPU': 8, 'IsItNew': False, 'env':
 ['dev', 'prd', 'test', 'stg', 'basiton']}
Unique IDs from device_id: {1, 2, 3, 4, 5, 12, 14, 15, 23}
Unique IDs from new_device_id: {1, 2, 3, 7, 9, 11, 16, 19, 20}
{1, 2, 3, 4, 5, 7, 9, 11, 12, 14, 15, 16, 19, 20, 23}  # Unique IDs
{1, 2, 3}  # Common IDs
The days of the week: ('sunday', 'Monday', 'Tuesday')
    

2. Networking and API Interaction:

As a DevOps Engineer, if we want to use any API created or developed by a developer to connect to the internet, we use the requests module.

To install the requests module, run the following command in the terminal:

pip install requests

Example Code:

import requests

demo_url = "https://jsonplaceholder.typicode.com/posts"  # demo URL for JSON data
response = requests.get(url=demo_url)

print(f"Response from demo URL: {response}")
print(dir(response))  # display available methods and attributes

print(type(response.json()))  # display the type of data received
print(response.json()[1])  # fetch the 1st item from the JSON response
    

Output:

Response from demo URL: 
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', 
'__format__', '__ge__', '__getitem__', '__init__', '__iter__',
 '__len__', '__lt__', '__new__', '__reduce__', '__reduce_ex__', 
'__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 
'close', 'content', 'cookies', 'elapsed', 'encoding', 
'headers', 'history', 'json', 'links', 'ok', 'raise_for_status', 
'request', 'status_code', 'text', 'url']
  # Type of data received
{'userId': 1, 'id': 2, 'title': 'qui est esse', 'body': 'est rerum 
tempore vitae\nsequi sint nihil reprehenderit 
dolor beatae et ...'}
    

3. Object Oriented Programming (OOP):

In this section, we will explore how to code with classes, which is essential in DevOps.

What is a class?

A class is a collection of data members and member functions. It allows us to create functions and variables inside the class.

Example Code:

import os

# Blueprint or a Template
class Utilities:
    def show_disk(self):
        print(os.system("df -h"))

    def show_ram(self):
        print(os.system("free -h"))

    def show_sys_details(self):
        print(os.uname().sysname)

machine_A = Utilities()  # object 1
machine_B = Utilities()  # object 2

machine_A.show_disk()
machine_A.show_ram()
    

Output:

# Output will vary based on the system
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        50G   20G   28G  43% /
              total       used       free     shared    buff/cache   available
Mem:            8G        2G        4G        0G        2G        5G
Linux  # System name
    

4. File Handling:

File handling in Python allows you to read from and write to files. This is essential for logging, data storage, and configuration management in DevOps.

Example Code:

# Writing to a file
with open("example.txt", "w") as file:
    file.write("This is a sample file.\n")
    file.write("It contains some example text.")

# Reading from a file
with open("example.txt", "r") as file:
    content = file.read()
    print(content)
    

Output:

This is a sample file.
It contains some example text.
    

5. Exception Handling:

Exception handling in Python allows you to manage errors gracefully without crashing the program. This is crucial in DevOps for maintaining system stability.

Example Code:

try:
    # Attempt to open a non-existent file
    with open("non_existent_file.txt", "r") as file:
        content = file.read()
except FileNotFoundError:
    print("Error: The file does not exist.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")
    

Output:

Error: The file does not exist.
    

Python Scripting Advanced [Hands-on Project]:

What will we do in this Advanced Scripting?

  • 1. We will take a backup of a server.
  • 2. We will connect our Python with AWS account.
  • 3. We will store the backup in AWS S3 (AWS service).
  • 4. We will create a Flask app to monitor our server.

About the Author

Written by Omkar Goswami. Feel free to connect with me on LinkedIn for any queries or discussions.

For more reference Python scripts, check out my GitHub: GitHub Repository.

Conclusion

This article has provided a comprehensive overview of Python scripting for DevOps. By mastering these concepts, you can automate tasks, manage systems efficiently, and enhance your productivity as a DevOps engineer. Embrace the power of Python and take your automation skills to the next level!

Git-GitHub For DevOps

Git & GitHub for DevOps: A Practical Guide to Version Control

How Git Came into the Picture – A Simple DevOps Story:

So one day, the creator of Linux, Linus Torvalds, was writing some code for the Linux project. He made some changes and was happy. But after a few days, he realized — “Oh no! The old version of my code was actually better!” But sadly, he didn’t have the old version saved.

That’s when Linus thought, “I need something to track my code — every change, every version.” So, he created a system called Git, which originally stood for Global Information Tracker (though people joke about this name now). This tool helped him to save every version of his code — like version 1, version 2, and so on — so he could go back anytime and see what changed.

Git became a kind of "code time machine" — a version control system (VCS) that keeps track of code history.

Later, the whole tech world saw this and said, “Wow! Linus made a brilliant tool!” Then many companies started building platforms on top of Git. These platforms helped teams work together more easily. These platforms include:

  • GitHub
  • GitLab
  • Bitbucket
  • AWS CodeCommit
  • Azure Repos, etc.

But the heart of all of them is still the same — Git.

So in DevOps, Git became a key tool. Why?
Because in DevOps, we want to work faster, safer, and together as a team. And Git helps us do exactly that — it keeps a record of code changes, lets us go back to old versions, and allows teams to collaborate smoothly.

So in short:

Git is a DevOps superhero that keeps track of your code’s full history. It makes working with code — alone or in teams — safe, easy, and smart.

File System vs Version Control System – A Simple DevOps Story

Let’s say you have a folder on your laptop. That’s a normal file system — it stores files locally. You can see when the file was created and who created it. But there’s a problem…

If someone edits the file, you don’t know who did it. If something gets deleted, you can’t bring it back. You also don’t know what the file looked like yesterday or last week.

Now comes the hero — Version Control System (VCS) like Git.

VCS does everything better:

  • It keeps the full history of every file.
  • It tells you who changed what, and when.
  • It stores code locally and can also connect to remote servers.
  • If something gets deleted or changed, you can easily go back in time.

From File System to VCS (Git) – Step by Step

Let’s imagine:

You have a folder with some important text files. Right now, it's just a normal folder — in the file system. But you want to put it under Git version control. For that, you need to do a few steps.

Step 1: Folder vs Repository

In the file system, we call it a "folder" or "directory". But in Git (VCS), we call it a repository.

Step 2: Go Inside the Folder

Open your terminal and go inside the folder:

cd 

Step 3: Initialize Git

Now run this command:

git init

This means you're creating an empty Git repository inside your folder.

After that, you’ll see a message:

"Initialized empty Git repository in …/.git"

If you run:

ls -a

You’ll see a hidden folder named .git — that means your folder is now being tracked by Git!

Now, How to Track Files in Git?

Right now, the files in your folder are not yet tracked by Git. You need to pass through some stages:

Git Stages:

  1. Untracked (usually shown in red)
  2. Staged (ready to be committed — shown in green)
  3. Tracked (after commit)

To check your Git status at any time:

git status

To Move a File from Untracked → Staged:

git add 

If You Want to Undo That (Staged → Untracked):

git rm --cached filename

Now, Final Step – Committing the File:

To move from Staged → Tracked, you have to commit it. But Git needs a message to remember what this commit is about. So you do:

git commit -m "Your message here"

Summary:

  • File system = basic storage, no version tracking
  • Git (VCS) = tracks every change, author, history, and can restore anything
  • Folder becomes repository after git init
  • Files go through: Untracked → Staged → Tracked
  • Use git add, git rm --cached, git commit to move files through stages

Understanding Branches in Git – A Simple DevOps Story

Let’s think of Git like a tree. A tree has a main trunk and several branches. Similarly, Git also starts with a main branch, and from there, we can create other branches to try out new things without touching the main code.

What is a Branch?

In Git, a branch is just a separate line of development.

  • The first branch in Git is usually called the master (or sometimes main).
  • From this master, you can create more branches like dev, feature-login, etc.
  • Each branch is like a copy of the original (at that moment).

Imagine you're writing a story. Instead of editing the main copy directly, you make a photocopy, try new changes on that, and if everything works — you merge it back.

That’s exactly how branches work in Git.

Why Do We Use Branches?

Let’s say your app is live in production and working perfectly. Suddenly, your CEO says, “Please add a new feature!”

Now, you should not change the live (master) code directly. What if your new code has bugs? Production will break!

So instead, you do this:

  1. Create a new branch:
    git checkout -b dev
  2. Now, you’ll see that all the files from master are also in dev— because it copies the code from the point you branched off.
  3. You make your changes safely in dev. You can test, fix bugs, experiment — without touching master.
  4. Once everything is working fine, and you're happy with the changes — you merge it back into master.

How to Check Branches?

To see which branches you have:

git branch

The current branch will have a * next to it.

How to Merge a Branch?

Let’s say you are on the master branch and want to bring in the changes from the dev branch. Here's what you do:

  1. First, go to the branch where you want the changes to come (e.g. master):
    git checkout master
  2. Then, run the merge command:
    git merge dev

Summary – Why Git Branching is So Useful:

  • Safe Development: You don’t break the main code while experimenting.
  • Teamwork: Different people can work on different branches at the same time.
  • Easy Testing: You can test features in a separate branch before going live.
  • Organized Workflow: Helps in DevOps pipelines like CI/CD (Continuous Integration/Delivery).

So in short:

Branches = safe space to work → test → merge to master when ready

Local to Remote & Remote to Local in Git – A Simple DevOps Story

Let’s say you’ve been working on a project in your local Git repository — everything’s going great. But now, you want to back it up, share it with others, or work with a team. For that, you need to connect it to a remote Git repository — like one on GitHub, GitLab, or Bitbucket.

As a DevOps engineer, creating a smooth bridge between local and remote is very important.

Step 1: Create a Remote Git Repository

Choose a platform like:

  • GitHub
  • GitLab
  • Bitbucket

Let’s say you choose GitHub.

  • Just log in
  • Click “New repository”
  • Give it a name, and create it.

This new repo is called your remote repository. The folder on your system (with git init) is your local repository.

Step 2: Connect Local to Remote (Using HTTPS + PAT)

There are two ways to connect your local repo to remote:

  1. PAT (Personal Access Token) – safer than a password
  2. SSH – uses key-based authentication

Let’s first look at PAT.

PAT (Personal Access Token)

GitHub doesn’t allow password-based login for pushing code anymore.

How to Generate a PAT:

  1. Go to GitHub → Settings
  2. Click on Developer settings
  3. Choose Personal Access Tokens (classic)
  4. Click Generate new token
  5. Name it, give repo access, and generate
  6. Copy the token — save it in Notepad (you won’t see it again!)

Step 3: Check if Remote is Connected

Use this to check:

git remote -v

If it shows nothing, your local repo is not yet linked to any remote.</

hr />

Step 4: Add the Remote URL

Go to your GitHub repo page, and copy the HTTPS link.

Now run this:

git remote add origin https://github.com/your-username/your-repo-name.git

Here, origin is just a nickname for your remote URL, which basically holds the URL. It’s a standard practice.

Step 5: Push to Remote (Password Won’t Work)

Run the following command:

git push origin master

It will ask for GitHub credentials — but password won’t work.

GitHub no longer accepts account passwords via Git commands.

Step 6: Use PAT to Set the Remote URL with Authentication

Instead of entering credentials manually, add your PAT directly in the remote URL like this:

git remote set-url origin https://<your-PAT>@github.com/your-username/your-repo-name.git

Now push again:

git push origin master

Success! Your local project is now uploaded to GitHub.

Step 7: Remote to Local – Pull Changes

Let’s say someone else (or you from another system) added a file to the remote repo.

To bring it into your local system:

git pull origin master

This fetches the latest changes from the remote repo into your local one.

Using SSH Instead of PAT (Optional)

The second method to connect is SSH, which is a bit more secure and preferred by many teams.

Step 1: Check or Create SSH Key

If you don’t have an SSH key, run this:

ssh-keygen

This creates two files:

  • A private key (stored on your local machine)
  • A public key (to be added to GitHub)

Step 2: Add SSH Key to GitHub

  1. Go to GitHub → Settings
  2. Click on SSH and GPG Keys
  3. Click New SSH Key
  4. Paste your public key (found in ~/.ssh/id_rsa.pub)
  5. Save the key

Step 3: Clone Using SSH

Now, to clone a repo using SSH:

git clone git@github.com:your-username/your-repo.git

No need to enter your username/password — it will use your SSH key instead.

Bonus Tip: Check What’s Changed Using git diff

Let’s say you cloned a repo and made some local changes. To compare what’s different from the original:

git diff

This will show the difference between your current code and the last commit — great for reviewing before pushing.

Summary

Action Git Command
Check remote link git remote -v
Add remote repo git remote add origin <repo-url>
Push code to remote git push origin master
Pull code from remote git pull origin master
Set remote URL with PAT git remote set-url origin <PAT-url>
Generate SSH Key ssh-keygen
Clone using SSH git clone git@github.com:username/repo.git
See file changes git diff

Conclusion

Whether you're just getting started with version control or refining your DevOps toolkit, mastering Git and GitHub is non-negotiable. SSH authentication, clean push/pull operations, and command-line insights like git diff form the backbone of modern infrastructure automation and collaboration. These aren’t just commands—they're career tools.

This guide is part of a larger initiative to empower DevOps engineers with real-world, command-line-first knowledge. Bookmark this article, revisit the commands, and apply them in your daily workflows to truly understand their power.

GitHub Repository

All code snippets and examples from this tutorial are available here:
GitGitHub_DevOps Repository on GitHub

Author

Written by Omkar Goswami — a passionate DevOps and automation engineer who loves simplifying complex workflows through hands-on guides and real-time projects.
Connect on LinkedIn for more technical insights, open-source contributions, and career-building DevOps content.

Linux For DevOps: Basics to Advanced

LINUX For DevOPS

This article covers the essential Linux basics every beginner and DevOps learner should know. From understanding the OS and Linux distributions to navigating the file system, managing users and permissions, analyzing logs with tools like grep and awk, and setting up SSH with key-based authentication, you’ll get a solid foundation to start working confidently in Linux environments.

Table of Contents

1. What is an Operating System?

An Operating System (OS) is a fundamental software layer that acts as an interface between computer hardware and the user. It manages all hardware components—such as CPU, memory, storage devices, and input/output peripherals and coordinates the execution of software applications. Without an operating system, a computer would be practically unusable, as users and applications would not be able to communicate effectively with the hardware.

Popular operating systems include:

  • Linux
  • macOS
  • Windows

Why is Linux preferred in DevOps?

In DevOps, Linux is the operating system of choice due to its stability, security, and flexibility. It provides a reliable environment where systems can run for extended periods without interruption, which is crucial for continuous deployment and high-availability services. Linux’s strong security model, frequent updates, and open-source nature make it a trusted platform for managing production workloads, reducing the risk of vulnerabilities, and ensuring system integrity.

Another reason Linux dominates DevOps is its compatibility with most modern DevOps tools like Docker, Kubernetes, Jenkins, and Ansible. These tools are often designed to run natively on Linux, making integration and automation seamless. Additionally, Linux is free and open-source, allowing organizations to scale infrastructure without costly licensing fees.

2. Client OS vs Server OS

Client OS

A Client Operating System is designed for end-users to perform everyday tasks like browsing the internet, using office applications, and playing multimedia.

Examples: Windows 11, Ubuntu Desktop, macOS

Server OS

A Server Operating System, on the other hand, is built to manage network resources, run web servers, databases, and support multiple users simultaneously. It offers advanced security, stability, and remote management features.

Examples: Red Hat Enterprise Linux, Ubuntu Server, Amazon Linux

3. Why Linux for DevOps?

Linux is the most widely used operating system in DevOps due to its open-source nature, high stability, strong security, and powerful command-line interface. It provides an ideal environment for automation, scripting, containerization, and infrastructure management, making it the go-to choice for DevOps engineers and teams.

Key Reasons Why Linux is Preferred in DevOps:
  • Open-source and Free: No licensing costs; highly customizable for different environments.
  • Tool Compatibility: Most DevOps tools, like Docker, Kubernetes, Jenkins, and Ansible are built to run on Linux.
  • Scriptable and Automatable: Bash scripting and cron jobs make automation simple and efficient.
  • Stability and Performance: Can run for months or years without needing a reboot.
  • Security: Strong user permission system and regular community-driven updates and patches.
  • Cloud and Container Support: Linux is the default OS in most cloud platforms and container ecosystems.

Fact: Created by Linus Torvalds, inspired by Unix

4. Ways to Set Up Linux

1. Install on Your System (Bare Metal Installation)
This method involves installing a Linux distribution (like Ubuntu, Fedora, or CentOS) directly onto your computer's hard drive. It can be set up as a standalone OS or as a dual-boot alongside Windows. Ideal for full performance and long-term use.

2. EC2 Instance on AWS
Amazon EC2 lets you launch Linux virtual machines (instances) in the cloud. It’s widely used for deploying web applications, running DevOps pipelines, or learning Linux without needing local resources. AWS offers many Linux-based AMIs like Ubuntu, Amazon Linux, and Red Hat.

3. VirtualBox + ISO File
Use virtualization software like VirtualBox to create a virtual machine and run Linux using an ISO image. This is perfect for testing, learning, or running Linux without modifying your existing system.

4. Docker Container
Docker allows you to run lightweight, isolated Linux environments called containers. It's widely used in DevOps for creating consistent development and production environments. Great for testing, automation, and microservices.

5. Windows Subsystem for Linux (WSL)
WSL enables you to run a Linux terminal and command-line tools directly on Windows without a virtual machine. WSL 2 even supports full Linux kernels and Docker integration. Best for developers who want Linux tools on a Windows machine.

5. Linux Architecture (ASK Model)

The Linux architecture can be understood using the ASK model, which stands for Application, Shell, and Kernel. This layered structure defines how Linux operates and interacts with users and hardware.

  • Application Layer
    This is the top layer where user-level software runs. Applications like Vim, Git, Python, and text editors operate here. These tools help users perform development, editing, or version control tasks.

  • Shell Layer
    The Shell acts as a command-line interface (CLI) between the user and the system. Tools like Bash, Zsh, or Fish accept user commands and translate them into instructions that the kernel can understand.

  • Kernel Layer
    The Kernel is the core of the Linux OS. It directly interacts with the hardware, manages system resources (CPU, memory, devices), and ensures secure and efficient operation. It acts as a bridge between the Shell and the hardware.


 

 

6. Shell & Shell Scripting

Shell is a command-line interface that allows users to interact with the operating system by typing commands. In Linux, common shells include Bash, Zsh, and Sh.

Shell Scripting is the process of writing a series of shell commands in a file (usually with .sh extension) to automate repetitive tasks like file operations, system monitoring, software installation, and more.

In short, Shell executes commands, and Shell Scripting automates them

Types of Shells: Bash, Csh, Zsh

7. Basic Linux Commands

CommandDescriptionExample
pwdShow current directorypwd
cdChange directorycd /var/log
mkdirCreate directorymkdir project
touchCreate filetouch log.txt
catView filecat file.txt
ls -aShow hidden filesls -a
rmDelete filerm file.txt

8. Important Directories

DirectoryPurpose
/homeUser-specific data
/varLogs, mail, cache
/binBasic commands (ls, cp)
/sbinSystem commands (reboot)
/rootRoot user home

9. man Command

man ls
man grep

10. APT Package Manager (Ubuntu)

sudo apt update
sudo apt upgrade
sudo apt install nginx

Use sudo to run admin-level commands.

cat /etc/group

11. systemctl for Services

sudo apt install nginx
systemctl status nginx
systemctl start nginx
systemctl restart nginx
DevOps Scenario:
You deployed a Node.js app using NGINX. After deployment, NGINX doesn’t respond.
Use: systemctl status nginx to debug.

19. User and Group Management

In a real DevOps setup, managing users and groups is crucial for security, access control, and automation.

Create a New User

sudo adduser devuser

This creates a new user named devuser with a home directory.

Give Sudo (Admin) Access

sudo usermod -aG sudo devuser

This adds devuser to the sudo group, giving them admin privileges.

Create a New Group

sudo groupadd devgroup

This creates a custom group called devgroup, useful for permission control across multiple users.

Add User to Group

sudo usermod -aG devgroup devuser

This adds devuser to devgroup. Great for grouping similar DevOps engineers, QA testers, or backend devs.

Check User Groups

groups devuser

This shows which groups a user belongs to.

Why It Matters in DevOps: When deploying applications, securing servers, or configuring automation tools like Ansible, you must manage who can read/write/execute or access critical paths. Groups help restrict or expand access cleanly.

13. Understanding File Permissions in Linux

Linux uses a permission system to manage access to files and directories. These permissions are split into three categories:

  • User (u) – The owner of the file
  • Group (g) – Users in the same group
  • Other (o) – Everyone else

Each category has three types of permissions:

  • r = read
  • w = write
  • x = execute

These permissions are represented by binary values:

rwxBinaryDecimal
0000000
0010011
0100102
0110113
1001004
1011015
1101106
1111117

So a file permission like chmod 754 file.txt means:

  • User: 7 (rwx)
  • Group: 5 (r-x)
  • Other: 4 (r--)

You can check file permissions using:

ls -l file.txt

And change them using:

chmod 755 script.sh

This gives full access to the user, and read-execute access to group and others.

14. Log File Analysis for DevOps (AWK, GREP, SED)

As a DevOps engineer, analyzing logs is a daily task. Tools like grep, awk, and sed are your best friends for troubleshooting, monitoring, and automating tasks.

Sample Log File: auth.log

Jul 02 12:10:11 server1 sshd[2345]: Failed password for invalid user admin from 192.168.1.12 port 55874 ssh2
Jul 02 12:12:33 server1 sshd[2346]: Accepted password for devuser from 192.168.1.15 port 55233 ssh2
Jul 02 12:15:44 server1 sshd[2347]: Failed password for root from 192.168.1.10 port 54421 ssh2

GREP - Search within logs

grep "Failed password" auth.log

Use case: Find all failed login attempts. You can also count how many:

grep -c "Failed password" auth.log

AWK - Extract specific fields

awk '{print $1, $2, $3, $9, $11}' auth.log

Use case: Extract Date, Time, and Username from login entries.

Output: Jul 02 12:10:11 admin

SED - Replace or clean logs

sed 's/Failed password/LOGIN FAIL/g' auth.log

Use case: Replace all "Failed password" texts to make logs more readable.

Sample Log File: nginx.log

192.168.1.11 - - [02/Jul/2025:13:15:01 +0000] "GET /index.html HTTP/1.1" 200 1024
192.168.1.12 - - [02/Jul/2025:13:16:02 +0000] "POST /login HTTP/1.1" 403 512
192.168.1.15 - - [02/Jul/2025:13:16:35 +0000] "GET /dashboard HTTP/1.1" 500 2560

Find status codes with GREP

grep " 403 " nginx.log

Use case: Find all forbidden access attempts.

AWK - Extract IP and status

awk '{print $1, $9}' nginx.log

Use case: Extract client IP and response status code.

SED - Clean logs by removing status codes

sed 's/ [0-9]\{3\} [0-9]\+/ - -/g' nginx.log

Use case: Anonymize response codes for sharing logs externally.

Recommended Resources

Pro Tip: Combine grep with awk or sed using pipes for powerful one-liners.

grep "Failed" auth.log | awk '{print $NF}'

This gives you only the IPs involved in failed login attempts.

15. Basic Networking Commands

ip a
ping google.com
traceroute google.com
netstat -tulnp
ss -tulnp

16. SSH and Key-based Authentication

ssh-keygen -t rsa -b 2048
ssh-copy-id user@remote_ip
ssh -i ~/.ssh/id_rsa user@remote_ip

Scenario: Your DevOps team deploys code to staging servers daily. Instead of typing passwords each time, use key-based SSH for seamless, secure connections.

Challenge Yourself

  • Create a user named deployadmin and give it sudo access.
  • Write a shell script to monitor disk space usage.
  • Use grep to find “error” logs in a log file.
  • Set up SSH access to a dummy EC2 instance on AWS.

👉 Connect with me on LinkedIn for more DevOps insights

About the Author

Written by Omkar Goswami, a passionate DevOps engineer sharing real-world knowledge. For suggestions or feedback, connect on LinkedIn.

Conclusion

Linux is the backbone of DevOps. From system navigation to user management, from processing log files to securing servers with SSH, mastering Linux boosts your effectiveness as a DevOps engineer. Practice these commands regularly and start building real-world projects.

Stay tuned: Next article will cover \"Linux for DevOps - Shell Scripting in Real-World Projects\".

DON'T MISS

Nature, Health, Fitness
© all rights reserved
made with by AlgoLesson