Linux: The most important Linux commands, that nobody teaches you.

Linux: The most important Linux commands, that nobody teaches you.

Whether you are a Sysadmin, Developer, DevOps, Security, or Ops … effectively using Linux and its tooling is the most fundamental skill you can learn. Linux is the backbone of the majority of servers and applications around the world.

In the past few months, I’ve read many articles along the lines of ’20 Linux Commands you must know’ or ‘Linux survival guide’. The problem I’ve found with nearly every single of these is that they are targetted at beginners, teaching the use of ls or echo . I believe most of my audience is familiar with at least the basic commands that the Linux Command line offers. This article will not be that.

If you want to go throgh it hit the mentioned url : A to Z Linux Commands (linuxhandbook.com)

I will document and present my cheatsheet of commands that I use daily in my job. This curated list goes beyond that of a complete beginner and instead focuses on commands that will help you push further, be more efficient and manage a Linux System and its vital tooling.

This will be split up into two sections:

  • Linux Tools — Essential Linux tools and how to best utilise them.

  • AdHoc Commands — AdHoc commands can be incredibly useful in a pinch.

Linux Tools

Utilities

rsync

Used to copy files and directories to a destination, similar to the cp command. However, it also allows copying to remote locations and can provide a progress bar, as is often used for backups

# Example Usage
$ rsync -vap --ignore-existing <source_file> <destination_file>#  Key flags:
v = verbrose, r = recursive, p = preserve permissions, g = group, o = owner, a = archive, --progress = progresss bar

mkpasswd

mkpasswd is a simple but very useful command, it generates a complex random password at the specified length.

$ mkpasswd -l 8
> iwF1g2Lo

screen

Screen is a full-screen window manager; it creates a single window with a shell running and allows multiple screen windows to run inside a single session. It’s most beneficial when you’re running a long task remotely and worried about your SSH session dropping and ruining everything. Screen will continue through disconnection and continue to run your commands even when the window is not visible to you.

# Example Usage
$ screen # Start a screen session
$ screen -ls # List running services
$ screen -r # Attach to session

Ldapsearch

If you regularly work with LDAP databases, then Ldapsearch is a must. The tool opens a connection to an LDAP server and allows you to search, find and debug entries in your database.

# Example Usage
$ ldapsearch -x -W -D <username | less# Key Flags
-x = simple authentication, -W = prompt for password, -D = Use distinguished binddn name to bind to LDAP directory

Monitoring Tools

Uptime

Uptime returns metrics on how long a server has been running, the current time, the number of users and memory usage averages. If something goes wrong on your server, this is often the first port of call.

‘w’ — yes, one letter. This is a fantastic combination of uptime and who commands run one after another. $ w

Wall

Wall is a handy command for any system administrator; it allows you to send a message to everybody’s terminal who’s currently logged into the system. This can be very useful for system-wide announcements.

$ wall "Maintenance scheduled for 13:30"Broadcast message from Joel@localhost: Maintenance scheduled for 13:30

Top

Displays an auto-refreshing list of processes for the CPU and critical memory use and CPU usage metrics.

$ top

Ncdu

The ncdu command provides a quick, convenient view for disk usage. You can use it to see what directories are using the most disk space quickly and easily.

$ ncdu

lsof

lsof is a single command used for one fundamental purpose: LiSt Open Files. This is especially useful when experiencing mounting problems that say files are being used. This command quickly identifies which files are in use by which processes.

$ lsof

Networking tools

Netcat

Netcat or nc is primarily used for port scanning but is actually a great utility networking tool for system administrators to have in their back pocket for any task. Netcat can support portscanning, file copying, port forwarding, proxy servers, and hosting servers … safe to say, it’s incredibly versatile.

# Example Usage:
$ nc -vz <host> <port> # Checks the connection between two hosts on a given port
$ nc -l 8080 | nc <host> 80 # Creating a proxy server

Netcat is so customizable that I can’t include all the possibilities in one post, so if you would like to see more, I’ll link my favorite Medium article on the subject below written by

7 Fundamental Use-cases of Netcat

A guide to Netcat — TCP/IP Swiss Army knife

medium.com

NetStat

Netstat returns various network details such as routing tables, network connections, memberships, stats, flags etc.

# Example Usage 
$ netstat -a # List all network ports
$ netstat -tlpn # List all listening ports# Key Flags
-s = Show statistics, -v = verbrose, -r = show routing tables, -i display interface table, -g = show group memeberships

Nslookup

Used to get information about servers on the internet or your local network. It queries DNS to find the name server information and can be useful for network debugging.

# Example Usage
$ nslookup medium.com/tags/devops# Key Flags
-port = Change port number for connection, -type = Change type of query. -domain = Sets search list to name

TCPDump

Used to capture and analyse traffic coming to and from your system. It is a potent and versatile tool that specialises in debugging and troubleshooting network issues but can also be used as a security tool.

# Example Usage
$ tcpdump
$ tcpdump -i <interface> <ipaddress or hostname> <port>

Ad-Hoc Commands

Pretty printing API Responses

Reading JSON data from the terminal can be highly frustrating when working with APIs. As you can see below, even a small data set quickly becomes a mess when displayed to the command line making it very difficult to read.

$ cat test.json
{"title":"Person","type":"object","properties":{"firstName":{"type":"string"},"lastName":{"type":"string"},"age":{"description":"Age in years","type":"integer","minimum":0}},"required":["firstName","lastName"]}

Luckily Python has an answer. By piping your output to python we can invoke the JSON tool module. This will pretty print out JSON document which is so much easier to read and nicer on the eye.

$ cat test.json | python -m json.tool
{
    "properties": {
        "age": {
            "description": "Age in years",
            "minimum": 0,
            "type": "integer"
        },
        "firstName": {
            "type": "string"
        },
        "lastName": {
            "type": "string"
        }
    },
    "required": [
        "firstName",
        "lastName"
    ],
    "title": "Person",
    "type": "object"
}

Also worth referencing JQ, which is a command-line JSON interface

$ jq . file.json

Searching through apt for available packages

$ apt-cache seach <keyword>

Diff the Output of any two commands

# Example usage of comparing output of two ls commands$ diff -u <(ls -l /directory/) <(ls -l /directory/) | colordiff

Convert a Unix timestamp to human-readable format

# Convert Unix timestamp to human readable
$ date -d 1656685875
Fri, 01 Jul 2022 14:31:15 +0000# Current time as UNIX timestamp
$ date "+%s"

Squashing Git commits

$ git log # See how many commits you've made
$ git rebase -i HEAD~x # x = number of commits you've made# Make changes on the text editor, keeping the last commit as pick and changing the rest to sqash# Edit the commit messages as you'd like, preferbly removing ones from previous commits$ git push --force-with-lease

List all Systemd services

$ systemctl -l -t service | less

I hope you’ve learned something from this list; mastering Linux can be extremely useful due to its popularity on servers, so I’ll leave you with this staggering statistic below.

96.3% of the top one million web servers are running Linux.

- ZDNet