Top Linux Commands Every DevOps Engineer Should Know

haythammostafa

Haytham Mostafa

Posted on September 1, 2024

Top Linux Commands Every DevOps Engineer Should Know

Networking

Here are some of the most commonly used networking commands in Linux that are essential for DevOps:

  • ifconfig: Displays and configures network interfaces.
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.100  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::20c:29ff:fe09:ff74  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:09:ff:74  txqueuelen 1000  (Ethernet)
        RX packets 12345  bytes 12345678 (12.3 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 54321  bytes 87654321 (87.6 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
Enter fullscreen mode Exit fullscreen mode
  • ip: A versatile command for network configuration, routing tables, and more.
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic eth0
       valid_lft 84605sec preferred_lft 84605sec
Enter fullscreen mode Exit fullscreen mode
  • ping: Tests network connectivity to another host.
PING google.com (172.217.6.14) 56(84) bytes of data.
64 bytes from lga34s28-in-f14.1e100.net (172.217.6.14): icmp_seq=1 ttl=56 time=10.2 ms
Enter fullscreen mode Exit fullscreen mode
  • traceroute: Determines the route packets take to reach a destination.
traceroute to google.com (172.217.6.14), 30 hops max, 60 byte packets
 1  192.168.1.1 (192.168.1.1)  1.234 ms  2.345 ms  3.456 ms
 2  10.10.10.1 (10.10.10.1)  4.567 ms  5.678 ms  6.789 ms
 3  8.8.8.8 (8.8.8.8)  7.890 ms  8.901 ms  9.012 ms
Enter fullscreen mode Exit fullscreen mode
  • netstat: Displays network statistics, connections, routing tables, and more.
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp6       0      0 :::80                   :::*                    LISTEN
Enter fullscreen mode Exit fullscreen mode
  • ss: A tool to investigate sockets, network connections, and more.
State      Recv-Q Send-Q          Local Address:Port            Peer Address:Port
LISTEN     0      128                    *:22                      *:*
LISTEN     0      100            192.168.1.100:80          0.0.0.0:*
Enter fullscreen mode Exit fullscreen mode
  • dig: A DNS lookup utility for querying DNS servers.
;; ANSWER SECTION:
example.com.        86400   IN      A       93.184.216.34
Enter fullscreen mode Exit fullscreen mode
  • host: Another DNS lookup utility for translating hostnames to IP addresses and vice versa.
example.com has address 93.184.216.34
Enter fullscreen mode Exit fullscreen mode
  • nslookup: Yet another DNS lookup utility for querying DNS servers.
Server:         8.8.8.8
Address:        8.8.8.8#53

Non-authoritative answer:
Name:   example.com
Address: 93.184.216.34
Enter fullscreen mode Exit fullscreen mode
  • route: Displays and manipulates the IP routing table.
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
Enter fullscreen mode Exit fullscreen mode
  • iptables: A powerful firewall utility for configuring packet filtering rules.
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
Enter fullscreen mode Exit fullscreen mode
  • nmap: A network scanning tool for discovering devices on a network.
Starting Nmap 7.80 ( https://nmap.org ) at 2024-09-01 12:00 UTC
Nmap scan report for example.com (93.184.216.34)
Host is up (0.0050s latency).
Not shown: 998 closed ports
PORT     STATE SERVICE
80/tcp   open  http
443/tcp  open  https
Enter fullscreen mode Exit fullscreen mode
  • curl: Used for transferring data with URLs and can test web services.
HTTP/1.1 200 OK
Date: Thu, 01 Sep 2024 12:00:00 GMT
Server: Apache
Content-Length: 1234
Content-Type: text/html; charset=UTF-8
Enter fullscreen mode Exit fullscreen mode
  • wget: Retrieves content from web servers.
--2024-09-01 12:00:00--  http://example.com/file.zip
Resolving example.com (example.com)... 93.184.216.34
Connecting to example.com (example.com)|93.184.216.34|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12345678 (12M) [application/zip]
Enter fullscreen mode Exit fullscreen mode
  • ssh: Securely connects to remote servers.
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-77-generic x86_64)

Last login: Thu Sep  1 11:00:00 2024 from 192.168.1.2
Enter fullscreen mode Exit fullscreen mode
  • scp: Securely copies files between hosts.
file.txt                                    100%  1234     1.2MB/s   00:00
Enter fullscreen mode Exit fullscreen mode
  • telnet: Connects to remote hosts using the Telnet protocol.
Trying 192.168.1.1...
Connected to 192.168.1.1.
Escape character is '^]'.
Enter fullscreen mode Exit fullscreen mode
  • arp: Displays and modifies the Address Resolution Protocol (ARP) cache.
Address                  HWtype  HWaddress           Flags Mask            Iface
192.168.1.1              ether   00:1a:2b:3c:4d:5e   C                     eth0
Enter fullscreen mode Exit fullscreen mode
  • ethtool: Displays or changes ethernet card settings.
Settings for eth0:
        Supported ports: [ TP ]
        Supported link modes:   10baseT/Half 10baseT/Full
Enter fullscreen mode Exit fullscreen mode
  • tcpdump: A packet analyzer that captures and displays network packets.
12:00:00.123456 IP 192.168.1.2.12345 > 8.8.8.8.80: Flags [S], seq 1234567890, win 1024, options [mss 1460]
Enter fullscreen mode Exit fullscreen mode

These networking commands are crucial for troubleshooting, network configuration, and monitoring.

Monitoring

Here are some of the most commonly used monitoring commands in Linux that are essential for DevOps:

  • top: Displays real-time system information, including CPU and memory usage.
top - 12:00:00 up 1 day,  1:00,  1 user,  load average: 0.08, 0.07, 0.06
Tasks: 201 total,   1 running, 200 sleeping,   0 stopped,   0 zombie
%Cpu(s):  3.2 us,  0.8 sy,  0.0 ni, 95.8 id,  0.2 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7847.4 total,   4986.4 free,    804.5 used,    2056.6 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   6847.7 avail Mem
Enter fullscreen mode Exit fullscreen mode
  • htop: An interactive system-monitor process-viewer and process-manager.

Interactive process view, similar to top but with a more user-friendly interface

  • vmstat: Reports information about processes, memory, paging, block IO, traps, and CPU activity.
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0   4986.4  2056.6  804.5   0    0    0     0   0    0  3  0 95  0  0
Enter fullscreen mode Exit fullscreen mode
  • iostat: Reports CPU utilization and disk I/O statistics.
Linux 5.4.0-77-generic (hostname)   09/01/24    _x86_64_    (4 CPU)
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           3.2    0.0    0.8    0.2    0.0   95.8
Enter fullscreen mode Exit fullscreen mode
  • sar: Collects, reports, or saves system activity information.
Linux 5.4.0-77-generic (hostname)  09/01/24
12:00:00        CPU     %user     %nice   %system    %iowait    %steal     %idle
12:05:00          all      3.2           0.0         0.8         0.2           0.0          95.8
Enter fullscreen mode Exit fullscreen mode
  • free: Displays the amount of free and used memory in the system.
total        used        free      shared  buff/cache   available
Mem:         7847        804         4986         0            2056        6847
Swap:        2048          0         2048
Enter fullscreen mode Exit fullscreen mode
  • uptime: Shows how long the system has been running, as well as load averages.
12:00:00 up 1 day,  1:00,  1 user,  load average: 0.08, 0.07, 0.06
Enter fullscreen mode Exit fullscreen mode
  • ps: Reports a snapshot of the current processes.
PID    TTY    TIME    CMD
123    tty1   00:00:05  bash
456    tty2   00:02:10  python
Enter fullscreen mode Exit fullscreen mode
  • pidstat: Monitors system resources, such as CPU, memory, and I/O usage for a specific process.
Linux 5.4.0-77-generic (hostname)  09/01/24
12:00:00      UID       PID    %usr %system  %guest   %wait    CPU
12:00:05        0       123     3.0    0.5     0.0     0.0      all
Enter fullscreen mode Exit fullscreen mode
  • dstat: Combines vmstat, iostat, and ifstat and presents it in a more user-friendly way.
----total-cpu-usage---- -dsk/total- -net/total- ---paging-- ---system--
usr sys idl wai hiq siq| read writ| recv send| in out | int csw
  3   1  95   1   0   0| 123  456 |  78   90 |  12  34 |  56  78
Enter fullscreen mode Exit fullscreen mode
  • nmon: A system performance monitor for Linux that displays performance data in a clear, concise way.

Output is typically interactive and graphical, providing real-time performance metrics in a comprehensive dashboard format

  • perf: A performance analyzing tool in Linux that supports various types of analysis.
Performance counter stats for 'programname':
      1000.123456 task-clock                #    1.000 CPUs utilized
            12345 context-switches          #    0.123 M/sec
             6789 CPU-migrations            #    0.0678 M/sec
      1.234567890 seconds time elapsed
Enter fullscreen mode Exit fullscreen mode
  • mpstat: Reports processors related statistics.
Linux 5.4.0-77-generic (hostname)  09/01/24  _x86_64_  (4 CPU)
12:00:00     CPU   %usr  %nice   %sys %iowait   %irq  %soft  %steal  %guest  %gnice  %idle
12:00:05     all    2.0    0.0    1.0    0.2    0.0    0.0    0.0     0.0     0.0    97.8
Enter fullscreen mode Exit fullscreen mode
  • iftop: Displays bandwidth usage on an interface by host.
12:00:00    up   1 day,  1:00,  1 user,  load average: 0.08, 0.07, 0.06
Interface        RX           TX        Total
eth0             1.2KB/s      0.8KB/s   2.0KB/s
Enter fullscreen mode Exit fullscreen mode
  • iotop: Monitors I/O usage information on a per-process basis.
Total DISK READ :       0.00 B/s | Total DISK WRITE :       0.00 B/s
Actual DISK READ:       0.00 B/s | Actual DISK WRITE:       0.00 B/s
Enter fullscreen mode Exit fullscreen mode
  • lsof: Lists open files and the processes that opened them.
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd     1234   root   3u   IPv4  12345      0t0  TCP *:22 (LISTEN)
Enter fullscreen mode Exit fullscreen mode
  • strace: Traces system calls and signals.
strace -c ls

% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
  0.00    0.000000           0         5           read
  0.00    0.000000           0         2           write
  0.00    0.000000           0         7           open
  0.00    0.000000           0         9           close
  0.00    0.000000           0         7           fstat
  0.00    0.000000           0        18           mmap
  0.00    0.000000           0        13           mprotect
  0.00    0.000000           0         2           munmap
  0.00    0.000000           0         3           brk
  0.00    0.000000           0         1         1 access
  0.00    0.000000           0         1           execve
  0.00    0.000000           0         1           arch_prctl
------ ----------- ----------- --------- --------- ----------------
100.00    0.000000                    69         1 total
Enter fullscreen mode Exit fullscreen mode

These monitoring and performance commands are crucial for analyzing system performance, identifying bottlenecks, troubleshooting issues, and optimizing system resources.

Managing Processes

In a DevOps environment, monitoring and managing processes is crucial for maintaining system performance and stability. Here are some of the most commonly used process-related commands in Linux for DevOps tasks:

  • ps: Provides information about currently running processes.
PID   USER     TIME  COMMAND
1234  root     0:02  /usr/sbin/apache2
5678  user     0:00  python script.py
Enter fullscreen mode Exit fullscreen mode
  • pidstat: Reports statistics for processes and threads, including CPU, memory, and I/O usage.
Linux 5.4.0-77-generic (hostname)  09/01/24
12:00:00          UID      PID    %usr  %system  %guest  %CPU   CPU  Command
12:05:00            0      1234   5.0    2.0      0.0    7.0    0    apache2
Enter fullscreen mode Exit fullscreen mode
  • kill: Terminates a process by sending a signal.
kill -9 PID
Enter fullscreen mode Exit fullscreen mode
  • pkill: Kills processes based on their name or other attributes. Example:
pkill process_name
Enter fullscreen mode Exit fullscreen mode
  • pgrep: Lists processes based on name or other attributes.
pgrep -u username
Enter fullscreen mode Exit fullscreen mode
  • killall: Kills processes by name.
killall process_name
Enter fullscreen mode Exit fullscreen mode
  • pstree: Displays processes in a tree structure.
init─┬─apache2───5*[apache2]
     ├─cron
     ├─sshd
     ├─rsyslogd
     ├─...
Enter fullscreen mode Exit fullscreen mode
  • nice: Runs a command with a specified priority.
nice -n 10 ./my_script.sh
Enter fullscreen mode Exit fullscreen mode

In this example, the nice command is used to launch my_script.sh with a lower priority (higher nice value), allowing other processes to take precedence.

  • renice: Changes the priority of a running process.
renice -n 5 -p 1234
Enter fullscreen mode Exit fullscreen mode

This renice command changes the priority of the process with PID 1234 to a higher priority (lower nice value), giving it more CPU time compared to other processes.

💖 💪 🙅 🚩
haythammostafa
Haytham Mostafa

Posted on September 1, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related