Essential Linux Commands Every DevOps Engineer Should Know

Essential Linux Commands Every DevOps Engineer Should Know

ยท

12 min read

Basics of Linux operating system

Linux, a name that often rings through the world of technology, is more than just an operating system; it's the foundation of modern computing. In this blog, we'll demystify Linux's operating system and architecture, breaking down complex concepts into digestible, non-technical insights for everyone to understand.

What Is an Operating System?

Let's begin by understanding what an operating system is in simple terms. Imagine your computer as a well-organized kitchen. You, the user, are the chef, and you have numerous kitchen appliances and tools at your disposal. Now, every well-functioning kitchen needs someone to manage and coordinate all these tools, ensuring that everything runs smoothly. That's where the operating system comes in.

In this analogy, Linux is your kitchen manager. It ensures that all the components of your computer work together harmoniously, enabling you to run applications, manage files, and perform tasks seamlessly.

Linux Architecture: Building the Foundation

Now, let's explore how Linux, as a kitchen manager, is structured.

1.Hardware Layer-Hardware consists of all the devices of the operating system(RAM/HDD/CPU ..etc)

2. Kernel: The Heart of Linux

Think of the kernel as the core ingredient of your kitchen manager's recipe. It's the central part of Linux that communicates directly with your computer's hardware. In our kitchen, the kernel represents the chef's secret sauce, the crucial element that makes everything taste just right.

The kernel manages your computer's resources, such as memory, processor, and hardware devices. It ensures that each component works in harmony, making your computer a versatile and powerful tool.

3. Shell: Your Command Center

Imagine the shell as the chef's workstation, a place where the chef (you) gives commands to the kitchen manager (Linux). In the Linux world, the shell is your command center, a text-based interface where you can interact with the operating system by typing commands.

This is where you tell Linux what you want it to do. It's like giving orders to the kitchen manager to prepare a specific dish or organize ingredients.

3. File System: Organizing the Ingredients

Every kitchen needs a storage system, and your computer has a digital filing cabinetโ€”the file system. This is where all your digital "ingredients" (files and data) are kept.

Linux's file system is exceptionally organized, akin to a chef's dream pantry. It neatly arranges files and directories in a tree-like structure, making it easy to find and manage everything you need.

How Linux Works

Now that we have an idea of the components, let's see how they come together:

  • When you turn on your computer, Linux (the kitchen manager) starts working.

  • The kernel (the secret sauce) communicates with your computer's hardware to make sure everything is ready.

  • The shell (your command center) is where you give instructions to the kitchen manager.

  • The file system (the organized pantry) keeps everything in its right place, ensuring you can access your digital ingredients.

A Non-Technical Guide to Common Terminal Commands ๐Ÿ“‚๐Ÿ’ป

The command line interface (CLI) can be a daunting place for non-IT folks, but don't fret! We're here to simplify things for you. Here's a list of essential terminal commands that can help you navigate your computer like a pro. Let's dive in with the help of emojis and bullet points.5

Navigating Directories ๐Ÿ“

  • ๐Ÿ  pwd: Shows your current location in the file system.

  • ๐Ÿšถ cd: Changes your current directory.

    If you are in the home directory, and you want to navigate the 'linux_script' folder then simply run the above command.

    • cd ~: This command helps navigate to your home directory

    • cd .. : Navigate one directory up

    • cd - : Navigate to previous directories or folder

  • ๐Ÿ“‚ mkdir: Creates a new folder.

    • Let's say you want to create a new folder inside the Basic_linux folder, simple run the below command

$mkdir Basic_linux/Basic_command

  • let's say you want to create a new folder between two existing folders Simply run the below command

$mkdir -p Basic_linux/linux/linux_script

  • Suppose you want to set permission for a created folder, simple run the below command, and give read, write, and execute permission

    $mkdir -m 777 Test_perm

  • If you want to print a message for each created directory, then simply run the below command

    mkdir -v Test

  • ๐Ÿ—‘๏ธ rmdir: Deletes an empty folder.

    $rmdir Music

    To force delete a non-empty directory, use the -p option.

  • ๐Ÿ—‘๏ธ rm: Removes a file permanently within a directory.

    $rm File1.txt

    Use the below options as per your requirements

    • -i โ€“Asking for confirmation before deletion.

    • -f โ€“ allows file removal without a confirmation.

    • -r โ€“ deletes files and directories recursively.

    • ls command -This command shows lists of files and directories in your system

    • The ls command will show the current working directoryโ€™s content. You can modify the command using these options:

      • -R โ€“ lists all the files in the subdirectories.

        -a โ€“ shows all files, including hidden ones.

      • Here '.' '..' these are hidden files

      • -lh โ€“ converts sizes to readable formats, such as MB, GB, and TB.

  • Managing Files and Folders ๐Ÿ“„๐Ÿ“

  • ๐Ÿ“‹ cp: Copies files and folders.

    $cp file1.txt /home/ubuntu/Basic_linux/linux

  • ๐Ÿ“ฆ mv: Moves or renames files and folders.

    $mv file2.txt /home/ubuntu/Basic_linux/linux

  • ๐Ÿ“ƒ touch: Creates an empty file.

    $touch file3.txt

  • ๐Ÿ“… file: Tells you what type a file is.

    $file file3.txt

  • ๐Ÿ“ฆ๐Ÿ” zip and unzip: Create and extract ZIP archives.

    $zip file3.zip file3.txt

  • ๐Ÿ“ฆ tar: Archives files without compression.

    $tar -cvf file1.tar file1.txt

    Text Editing โœ๏ธ๐Ÿ“

  • โœ๏ธ nano, vi, vim, and jed: Edit files using different text editors.

    $vim file1.txt $nano file1.txt

Viewing File Content ๐Ÿ‘€

  • ๐Ÿ“– cat: Lists, combines, and writes file contents. There are various ways to use the cat command:

    $cat file1.txt

    • cat > file4.txt โ€“ creates a new file.

    • cat file1.txt > file4.txt โ€“ merges file1.txt with file4.txt and stores the output in filename4.txt.

    • tac file1.txt โ€“ displays content in reverse order.

  • ๐Ÿ”Ž grep(global regular expression): Searches for a specific string in a file.

  • โœ‚๏ธ sed: Finds, replaces, or deletes patterns in a file.

  • ๐Ÿ‘€ head: Displays the first ten lines of a file.

    The head command has several options, such as:

    • -n โ€“ changes the number of lines printed. For example, head -n 5 shows the first five lines.

    • -c โ€“ prints the fileโ€™s first customized number of bytes.

    • -q โ€“ disables headers specifying the file name.

  • ๐Ÿ‘€ tail: Prints the last ten lines of a file.

Data Manipulation ๐Ÿงฎ

  • ๐Ÿ•ต๏ธ awk: Finds and manipulates patterns in a file.

  • ๐Ÿงฉ sort: Reorders file content.

    By default, this command will sort the lines in alphabetical order, from A to Z. To modify the sorting, use these options:

    • -o โ€“ redirects the command outputs to another file.

    • -r โ€“ reverses the sorting order to descending.

    • -n โ€“ sorts the file numerically.

    • -k โ€“ reorders data in a specific field.

  • โœ‚๏ธ cut: Sections and prints lines from a file.

File Comparison and Output ๐Ÿ“Š

  • ๐Ÿ“Š diff: Compares two files' content.

    Below are some acceptable options:

    • -c โ€“ displays the difference between two files in a context form.

    • -u โ€“ shows the output without redundant information.

    • -i โ€“ makes the diff command case insensitive.

  • ๐Ÿ–จ๏ธ tee: Prints command outputs in the Terminal and a file.

  • ๐Ÿ“‚ locate: Finds files in the system's database.

    Add the -i option to turn off case sensitivity and an asterisk (*) to find content with multiple keywords.

  • ๐Ÿ” find: Outputs a file or folder's location.

System Operations โš™๏ธ

  • ๐Ÿ‘‘ sudo: Runs a command with superuser privileges.

    sudo is one of the most basic commands in Linux. It runs your command with administrative or root permissions.

    using sudo command I have added a new user 'A1'

  • ๐Ÿ‘ค su: Switches to another user.

    It is useful to connect via SSH while the root user is disabled

  • ๐Ÿ” chmod: Modifies file permissions.

    Each file is associated with three user classes โ€“ owner, group member, and others. It also has three permissions โ€“ read, write, and execute

  • ๐Ÿ“ฆ๐Ÿ‘ค chown: Changes ownership of files and directories.

  • ๐Ÿ‘ค๐Ÿ”‘ useradd and userdel: Create and remove user accounts.

    For user add you require root user privileges to use 'sudo' before your command

    $sudo useradd ABCSD

    $sudo userdel ABCSD

  • ๐Ÿ“๐Ÿ’ฝ df: Displays overall disk space usage.

    These are some acceptable options:

    • -m โ€“ displays information on the file system usage in MBs.

    • -k โ€“ prints file system usage in KBs.

    • -T โ€“ shows the file system type in a new column.

$df -m Basic_linux $df -k file1.txt $df -T file1.txt

  • ๐Ÿ’พ du: Checks storage consumption of a file or directory.

    The du command has several options, such as:

    • -s โ€“ shows the specified folderโ€™s total size.

    • -m โ€“ provides folder and file information in MB.

    • -k โ€“ displays information in KB.

    • -h โ€“ informs the displayed folders and filesโ€™ last modification date.

      $du Basic_linux

Monitoring and Processes ๐Ÿ“Š๐Ÿ”„

  • ๐Ÿ“ˆ top: Displays running processes and system resource usage.

    $top

  • ๐Ÿ“Š๐Ÿ”„ htop: Interactive system monitoring.

    Unlike top, it offers additional features like mouse operation and visual indicators.

    It supports options such as:

    • -d โ€“ shows the delay between updates in tenths of seconds.

    • -C โ€“ enables monochrome mode.

    • -h โ€“ displays the help message and exits.

$htop $htop -d $htop -C $htop -h

  • ๐Ÿ“Š ps: Lists all running processes.

    'ps' command shows all processing time-wise. It does not care about the process state (running or sleeping).

    The ps command accepts several options, including:

    • -T โ€“ displays all processes associated with the current shell session.

    • -u username โ€“ lists processes associated with a specific user.

    • -A โ€“ shows all the running processes

  • ๐Ÿ–ฅ๏ธ uname: Shows system information including its hardware, system name, and Linux kernel.

    The uname command accepts several options, including:

    • -a โ€“ prints all the system information.

    • -s โ€“ outputs the kernel name.

    • -n โ€“ shows the systemโ€™s node hostname.

  • ๐Ÿ  hostname: Displays your system's hostname.

    You can run it without an option or use the following:

    • -a โ€“ displays the hostnameโ€™s alias.

    • -A โ€“ shows the machineโ€™s Fully Qualified Domain Name (FQDN).

    • -i โ€“ outputs the machineโ€™s IP address.

Timing โฑ๏ธ

  • โฑ๏ธ time: Calculates command execution time.

System Services and Management โš™๏ธ๐Ÿ”ง

  • ๐Ÿš€ systemctl: Manages system services.

Process Control ๐Ÿšฆ

  • ๐Ÿšฆ watch: Runs a command continuously.

    To modify its behavior, use the following options:

    • -d โ€“ displays the differences between command executions.

    • -n โ€“ changes the default two-second interval.

    • -t โ€“ disables the header containing the time interval, command, timestamp, and hostname.

$watch -n 5 date

  • โฒ๏ธ jobs: Lists running processes.

    You can also add the following options:

    • -l โ€“ lists process IDs and their information.

    • -n โ€“ shows jobs whose statuses have changed since the last notification.

    • -p โ€“ displays process IDs only.

  • โ›” kill: Terminates a running process.

    $ps ux $kill 20775

  • ๐Ÿšซ๐Ÿ”„ shutdown: Turns off or restarts the system.

    $shutdown -h now

Networking ๐ŸŒ

  • ๐ŸŒ ping: Checks network connectivity.

    $ping google.com

  • ๐ŸŒ๐Ÿ“ฅ wget: Downloads files from a URL.

    $ wget https://wordpress.org/latest.zip

  • ๐ŸŒ๐Ÿ”„ curl: Transmits data between servers.

    $ curl https://www.gnu.org/gnu/gnu.html

  • ๐Ÿ“‚๐Ÿš€ scp: Securely copies files or directories.

    scp command securely copies files or directories between systems over a network

  • ๐Ÿ”„๐Ÿ“‚ rsync: Synchronizes content between directories or machines.

Network Information ๐ŸŒ๐Ÿ“ก

  • ๐Ÿ“ก ifconfig: Displays network interfaces and their configurations.

    use the following options:

    • โ€“s โ€“ summarizes the network interfaces and their configuration. This option goes before the interface name.

    • up and down โ€“ enables and disables a network interface.

    • inet and inet6 โ€“ assigns an IPv4 and IPv6 address to a network interface.

    • netmask โ€“ specifies the subnet mask to use with an IPv4 address.

  • ๐Ÿ“ก๐ŸŒ netstat: Shows network information, like routing and sockets.

    use the below options with netstat:

    • -a โ€“ displays listening and closed sockets.

    • -t โ€“ shows TCP connections.

    • -u โ€“ lists UDP connections.

    • -r โ€“ displays routing tables.

    • -i โ€“ shows information about network interfaces.

    • -p โ€“ lists programsโ€™ names and process IDs.

    • -c โ€“ continuously outputs network information for real-time monitoring.

  • ๐ŸŒ๐Ÿ“ traceroute: Tracks the path of network packets.

    Add the following options for more detailed packet monitoring:

    • -m โ€“ sets each packetโ€™s maximum hops.

    • -n โ€“ prevents the command from resolving IP addresses to hostnames for quicker tracing.

    • -I โ€“ changes the default UDP packets to UCMP.

    • -w โ€“ adds a timeout in seconds.

  • ๐ŸŒ๐Ÿ” nslookup: Queries domain IP addresses.

    Below options are commonly used :

    • -type= โ€“ queries specific information, like the IP address type or MX record.

    • -port= โ€“ sets the DNS serverโ€™s port number for the query.

    • -retry= โ€“ repeats the query a specific number of times upon failure.

    • -debug โ€“ enables the debug mode to provide more information about the query.

  • ๐ŸŒ๐Ÿ” dig: Displays DNS information, including record types.

Command History and Documentation ๐Ÿ“œ

  • ๐Ÿ“œ history: Lists previously run commands.

  • ๐Ÿ“˜ man: Shows a command's manual.

    $man ls

Output and Linking ๐Ÿ“ค๐Ÿ”—

  • ๐Ÿ“ค echo: Prints a message.

    The echo command displays a line of text as a standard output

    This command supports many options, such as:

    • -n โ€“ displays the output without the trailing newline.

    • -e โ€“ enables the interpretation of the following backslash escapes:

    • \b โ€“ removes spaces in between a text.

    • \c โ€“ produces no further output.

  • ๐Ÿ”— ln: Links files or directories.

    you create links between files or directories to simplify system management.

Customizing Commands ๐Ÿ› ๏ธ

  • ๐Ÿ”„ alias and unalias: Sets and removes aliases for commands.

    It allows you to create a shortcut for a program, file name, or text.

  • ๐Ÿ“… cal: Displays a calendar in the terminal.

    To modify the command output, add the following options:

    • -1 โ€“ outputs the calendar in a single line.

    • -3 โ€“ shows the previous, current, and next month.

    • -A and -B โ€“ displays the specified number of months after and before the current one.

    • -m โ€“ starts the calendar with Monday instead of Sunday.

Package Management ๐Ÿ“ฆ

  • ๐Ÿ“ฆ apt-get: Manages packages on Debian-based systems.

    This command lets you manage, update, remove, and install software, including its dependencies.

    These are the most common commands to use with apt-get:

    • update โ€“ synchronizes the package files from their sources.

    • upgrade โ€“ installs the latest version of all installed packages.

    • check โ€“ updates the package cache and checks broken dependencies

Now, you've got a handy guide to the world of terminal commands. Go ahead and explore, but remember, with great power comes great responsibility! ๐Ÿ˜„๐Ÿ’ช๐Ÿ”๐Ÿš€๐Ÿ“

If this post was helpful, please do follow and click the like button below to show your support ๐Ÿ˜„

_ Thank you for reading๐Ÿ’š

_Sprasad ๐ŸŒปโœจ

ย