Basic Linux commands

10 Basic Linux commands every DevOps Engineer should learn

Introduction

In the day-to-day life of a DevOps professional, efficiency and agility are essential to ensure a reliable and high-performance IT environment. In an ever-evolving landscape, we face challenges that require advanced skills in analysis and problem-solving. In this context, knowledge of lesser-known but highly useful commands and scripts can make all the difference.

When dealing with troubleshooting situations, these lesser-known commands become powerful tools. Monitoring the resource consumption of a specific process, for example, allows us to identify performance bottlenecks and optimize system resource utilization. This is especially valuable in production environments where every millisecond counts.

Analyzing the latency of a specific URL is another important use case. With this command, we can identify possible connectivity issues or network instabilities. This information is crucial for ensuring the availability and reliability of essential services.

Real-time network traffic monitoring is another critical scenario. With commands like tcpdump, we can capture and analyze packets in real-time, identifying possible anomalies or network attacks. This capability is vital for security and data protection in an increasingly threat-exposed environment.

Furthermore, analyzing disk performance and I/O activity of specific processes provides valuable insights into system performance. Commands like iostat and iotop help identify I/O bottlenecks, optimize storage utilization, and improve operational efficiency.

In the context of development and deployment, these basic linux commands are also useful. For example, converting JSON files to CSV can facilitate the manipulation and analysis of structured data in various tools. Listing Python package dependencies with “pipdeptree” assists in version management and resolving dependency conflicts.

Basic Linux commands for DevOps

Basic Linux commands

Retrieve the public IP address of a server:

curl ifconfig.me

This command utilizes the ifconfig.me service to retrieve the server’s public IP address. This is useful in scenarios where you need to provide the public IP address for configuration or remote access purposes. The command is simple and straightforward, requiring no additional configuration, and returns the IP address in text format. It serves the same purpose as the popular website https://whatismyipaddress.com/, but via the command line.

Monitor resource consumption of a specific process:

top -p <PID> -b -n 1

This command allows monitoring resource consumption (such as CPU and memory) of a specific process using its PID. It is useful for identifying performance bottlenecks in individual processes and optimizing system resource usage. The “-b” option ensures it runs in the background, and the “-n 1” option specifies the number of iterations.

Analyze the latency of a specific URL:

ping -c 5 <URL>

The ping command is commonly used to check network connectivity, but its lesser-known “-c” option allows specifying the number of packets to send. This is useful for analyzing latency and packet loss on a specific URL, aiding in diagnosing connectivity issues and verifying network stability, being one of the most used basic linux commands for devops when troubleshooting network problems.

Monitor network traffic in real-time:

sudo tcpdump -i <network-interface>

The tcpdump command is a powerful tool for analyzing network traffic. With the “-i” option followed by the network interface name, you can monitor and capture packets in real-time. This is useful for network debugging, traffic analysis, and detecting security issues. Remember to run the command with superuser privileges (sudo) to access all interfaces.

Analyze disk performance in real-time:

iostat -x 1

The iostat command is used to monitor system performance, including disk utilization. With the “-x” option, detailed statistics about I/O usage are displayed. The 1-second interval (“-x 1”) provides frequent updates, allowing real-time analysis of disk performance. This is useful for identifying I/O bottlenecks and fine-tuning storage configuration.

Monitor I/O activity of a specific process:

iotop -p <PID>

The iotop command displays information about I/O activity of running processes on the system. By providing the PID of a specific process, you can monitor its real-time read and write activity. This is useful for identifying processes performing intensive I/O operations and optimizing their performance.

Remove all stopped Docker containers:

docker container prune -f

The docker container prune command allows you to remove all stopped Docker containers. This is useful for cleaning up the Docker environment and freeing up storage resources. The “-f” option indicates that the removal should be done without requiring additional confirmation. It can also be used in conjunction with docker system prune.

List and remove files older than X days:

find /path/to/directory -type f -mtime +X -exec rm {} ;

This script uses the find command to locate files in a directory based on their modification time. The “-type f” option specifies that only regular files should be considered. The “-mtime +X” option sets the age limit in days. By using the “-exec rm {} ;” argument, the found files are removed. This is useful for cleaning up old log directories or temporary files.

Convert a JSON file to CSV:

jq -r 'map(values) | @csv' < input.json > output.csv

The jq command is a command-line tool for processing JSON. With this script line, you can convert a JSON file to the CSV format. The “map(values)” command extracts the values of the JSON fields, and the “@csv” expression formats them as a CSV line. This conversion is useful when you need to manipulate and analyze JSON data in tools that support the CSV format.

List Python package dependencies:

pipdeptree <package-name>

The pipdeptree command displays a tree of dependencies for Python packages installed in a virtual environment or the system. This is useful for visualizing the dependency hierarchy and their versions. With this information, you can troubleshoot compatibility issues, manage package versions, and check for unnecessary or conflicting dependencies.

Conclusion

These lesser-known basic Linux commands offer efficient solutions for a variety of tasks commonly encountered in the work of a DevOps professional. With a deeper understanding of these commands, DevOps professionals can optimize monitoring tasks, troubleshooting, and resource management, making their work more efficient and effective.

FAQs

  1. How do I find the public IP address of my server? Use the command curl ifconfig.me to obtain the public IP address quickly and easily.
  2. Which command should I use to monitor resources of a specific process? The command top -p <PID> -b -n 1 allows you to monitor CPU and memory usage of a specific process.
  3. How can I analyze the latency of a URL? The command ping -c 5 <URL> helps you check the connectivity and latency of a specific URL.
  4. Which command can I use to analyze real-time network traffic? Use sudo tcpdump -i <network-interface> to capture and analyze network packets in real-time.
  5. How do I convert JSON files to CSV? The command jq -r 'map(values) | @csv' < file.json > file.csv converts JSON to CSV, making data analysis easier.

Learn More

Enjoyed the article? Stay updated with the latest DevOps insights and tips by subscribing to our newsletter. Visit our homepage and sign up under ‘Get the latest updates via email’. Don’t miss out—level up your DevOps/SRE skills with our other Linux posts and mastering basic linux commands for devops!

Fernando Müller Junior
Fernando Müller Junior

I am Fernando Müller, a Tech Lead SRE with 16 years of experience in IT, I currently work at Appmax, a fintech located in Brazil. Passionate about working with Cloud Native architectures and applications, Open Source tools and everything that exists in the SRE world, always looking to develop and learn constantly (Lifelong learning), working on innovative projects!

Articles: 28

Leave a Reply

Your email address will not be published. Required fields are marked *