DEV Community

Cover image for Linux Fundamentals
FARUKH KHAN
FARUKH KHAN

Posted on

Linux Fundamentals

A DevOps Engineer’s Guide to Linux: Mastering the Essential Commands
In the world of DevOps, Linux stands as the backbone of most IT infrastructures. Understanding and mastering Linux commands is crucial for system management, automation, and seamless deployment in a professional environment. This guide provides an in-depth look at the most essential Linux commands every DevOps engineer should know.

Why Linux Matters in DevOps
Linux, a powerful open-source operating system, is known for its stability, flexibility, and widespread use in server environments. As a DevOps engineer, working with Linux is unavoidable, as it powers most cloud platforms, containers, and server environments. Mastering the basics ensures that you can efficiently manage systems, troubleshoot issues, and automate repetitive tasks.

A Brief History of Linux
Linux was born from a desire for freedom. In 1991, Linus Torvalds, a Finnish computer science student, released the first version of what would become the Linux kernel, an open-source alternative to proprietary Unix systems. Torvalds wanted a free operating system kernel that mimicked the features of Unix but was free to use and modify by anyone.

Basic Linux Commands Every DevOps Engineer Should Know
🔧 1. Navigating the File System

Working with files and directories is a fundamental task in Linux. The following commands help you navigate, create, and manage the file system.

ls: Displays a list of files and directories in the current location.

  ls          # Lists files and directories
  ls -l       # Lists in long format (detailed view)
  ls -a       # Includes hidden files
Enter fullscreen mode Exit fullscreen mode

cd: Changes the current working directory.

  cd /path/to/directory   # Navigates to a specific directory
  cd ..                   # Moves up one directory level
  cd ~                    # Returns to the home directory
Enter fullscreen mode Exit fullscreen mode

pwd: Prints the current working directory (useful to check where you are).

  pwd
Enter fullscreen mode Exit fullscreen mode

mkdir: Creates a new directory.

  mkdir new_directory
Enter fullscreen mode Exit fullscreen mode

rmdir: Removes an empty directory.

  rmdir directory_name
Enter fullscreen mode Exit fullscreen mode

📁 2. Managing Files and Directories

After navigating the file system, you will frequently need to copy, move, and remove files or directories. The following commands help with file and directory management:

cp: Copies files or directories.

  cp file.txt /destination/path/     # Copies a file
  cp -r dir1/ /destination/path/     # Recursively copies a directory
Enter fullscreen mode Exit fullscreen mode

mv: Moves or renames files and directories.

  mv file.txt /new/path/             # Moves a file
  mv oldname.txt newname.txt         # Renames a file
Enter fullscreen mode Exit fullscreen mode

rm: Removes files or directories.

  rm file.txt                # Deletes a file
  rm -r directory_name       # Deletes a directory and its contents
  rm -f file.txt             # Forces deletion without confirmation
Enter fullscreen mode Exit fullscreen mode

touch: Creates an empty file or updates the timestamp of an existing file.

  touch newfile.txt

Enter fullscreen mode Exit fullscreen mode

🛠 3. Viewing and Editing Files

Managing and reviewing file content is another critical task. Here are commands to display and modify file content:

cat: Displays the entire content of a file.

  cat file.txt

Enter fullscreen mode Exit fullscreen mode

less: Views file content one page at a time (useful for large files).

  less file.txt

Enter fullscreen mode Exit fullscreen mode

head: Displays the first few lines of a file.

  head -n 10 file.txt       # Displays the first 10 lines

Enter fullscreen mode Exit fullscreen mode

tail: Displays the last few lines of a file (useful for checking log files).

  tail -n 20 file.txt       # Displays the last 20 lines
  tail -f logfile.log       # Follows the file as it gets updated in 
                              real-time
Enter fullscreen mode Exit fullscreen mode

🔒 4. Managing Permissions and Ownership

File permissions in Linux are essential for maintaining security. The system uses a permission model that defines what actions users can perform on files and directories.

chmod: Changes the file permissions (read, write, execute) for user, group, and others.

  chmod 755 script.sh    # Grants read, write, and execute to owner, and read/execute to others
  chmod 644 file.txt     # Read/write for owner, read-only for group and others
Enter fullscreen mode Exit fullscreen mode

chown: Changes the ownership of a file or directory.

  chown user:group file.txt    # Changes ownership of the file to the specified user and group
Enter fullscreen mode Exit fullscreen mode

📊 5. System Monitoring and Resource Usage

Monitoring system performance is a critical part of DevOps. Here are commands that help you keep track of system resources:

top: Provides a dynamic, real-time view of running processes and resource usage (CPU, memory, etc.).

  top

Enter fullscreen mode Exit fullscreen mode

htop: A more user-friendly version of top that offers an interactive interface for viewing processes.

  htop

Enter fullscreen mode Exit fullscreen mode

df: Shows disk space usage of file systems.

  df -h        # Shows disk space in human-readable format

Enter fullscreen mode Exit fullscreen mode

du: Displays the size of directories and their contents.

  du -sh /path/to/directory/     # Summarizes total size in human-readable format
Enter fullscreen mode Exit fullscreen mode

free: Displays memory (RAM) usage.

  free -h

Enter fullscreen mode Exit fullscreen mode

🌐 6. Networking Commands

Linux provides powerful tools to monitor and manage networking. These commands are essential for troubleshooting connectivity issues and managing network configurations.

ping: Tests network connectivity to a host.

  ping google.com          # Sends ICMP packets to check connectivity

Enter fullscreen mode Exit fullscreen mode

ifconfig: Displays or configures network interfaces.

  ifconfig                 # Shows current network configurations

Enter fullscreen mode Exit fullscreen mode

netstat: Displays network connections, routing tables, and interface statistics.

  netstat -tuln            # Shows listening ports and services

Enter fullscreen mode Exit fullscreen mode

curl: Transfers data from or to a server, useful for testing APIs and endpoints.

  curl http://example.com   # Sends a request to the URL and displays the response
Enter fullscreen mode Exit fullscreen mode

💾 7. Package Management

Managing software packages is a crucial aspect of system administration. The package management tool depends on the Linux distribution:

apt (Debian/Ubuntu): Installs, updates, and removes software packages.

  sudo apt update           # Updates package list
  sudo apt install nginx    # Installs nginx
  sudo apt remove nginx     # Removes nginx
Enter fullscreen mode Exit fullscreen mode

yum (CentOS/RedHat): Installs, updates, and removes software packages.

  sudo yum update           # Updates package list
  sudo yum install nginx    # Installs nginx
  sudo yum remove nginx     # Removes nginx
Enter fullscreen mode Exit fullscreen mode

⚙️ 8. User Management

Managing users and permissions is key to maintaining a secure Linux environment. These commands help manage user accounts and permissions:

useradd: Adds a new user to the system.

  sudo useradd newuser

Enter fullscreen mode Exit fullscreen mode

passwd: Changes a user’s password.

  sudo passwd newuser

Enter fullscreen mode Exit fullscreen mode

usermod: Modifies user properties, such as adding a user to a group.

  sudo usermod -aG sudo newuser    # Adds user to the sudo group

Enter fullscreen mode Exit fullscreen mode

whoami: Displays the current logged-in user.

  whoami

Enter fullscreen mode Exit fullscreen mode

Conclusion

Understanding the what, why, and how of basic Linux commands is essential for any DevOps engineer. These commands form the foundation of daily system administration, automation, and troubleshooting tasks. As you build on this knowledge, you’ll be better equipped to streamline your workflows and manage complex infrastructures efficiently. So, dive in and start mastering Linux today! 🚀

Top comments (0)