Most Important Linux commands that Nobody Teaches You
Linux is a powerful operating system with a vast array of
tools and utilities available to users. These tools make Linux an attractive
option for system administrators, developers, and power users who require a
flexible and customizable environment. In this article, we will look at some
essential Linux tools and utilities that can help you manage your system more
efficiently.
Rsync
Rsync is a popular command-line utility that is used for file synchronization and data backup. It allows users to copy files and directories to a destination, similar to the cp command, but with some added features. One of the key benefits of using Rsync is that it allows you to copy files to remote locations, making it an excellent choice for backup purposes.
Example usage:
$ rsync -vap --ignore-existing <source_file> <destination_file>
Key flags:
v = verbose
r = recursive
p = preserve permissions
g = group
o = owner
a = archive
--progress = progress bar
Mkpasswd
Mkpasswd is a simple but useful command that generates complex random passwords at the specified length. This tool is particularly useful for generating secure passwords for user accounts or system services.
Example usage:
$ mkpasswd -l 8
iwF1g2Lo
Screen
Screen is a full-screen window manager that creates a single window with a shell running and allows multiple screen windows to run inside a single session. This tool is particularly useful when running long tasks remotely and worried about your SSH session dropping and ruining everything. Screen will continue running 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-have tool. This command-line 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
Uptime and w
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. The ‘w’ command combines uptime and who commands to show you who is currently logged in and what they are doing.
Example usage:
$ uptime
$ w
Wall
Wall is a handy command for system administrators that 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.
Example usage:
$ wall "Maintenance scheduled for 13:30"
Broadcast message from Joel@localhost: Maintenance scheduled for 13:30
Top
Top is a command-line tool that displays an auto-refreshing list of processes for the CPU and critical memory use and CPU usage metrics. It is an excellent tool for monitoring system resources and identifying performance issues.
Example usage:
$ 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.
Example usage:
$ 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.
Example usage:
$ lsof
Netcat (nc)
Netcat is a versatile networking utility that can be used for a variety of tasks such as port scanning, file copying, port forwarding, proxy servers, and hosting servers. With Netcat, you can check the connection between two hosts on a given port, create a proxy server, and more.
x
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
Netstat
Netstat is a command line tool that returns various network details such as routing tables, network connections, memberships, stats, flags, and more. It's useful for monitoring network activity and troubleshooting network issues.
Example Usage:
$ netstat -a # List all network ports
$ netstat -tlpn # List all listening ports
Nslookup
Nslookup is a tool 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
TCPDump
TCPDump is a tool used to capture and analyze traffic coming to and from your system. It's a powerful and versatile tool that specializes 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
Ad-hoc commands are one-liners that can be used to accomplish specific tasks quickly. Here are some examples:
Pretty printing API Responses: You can use Python's JSON tool module to pretty print out JSON documents for easier reading.
Example Usage:
$ cat test.json | python -m json.tool
Searching through apt for available packages: Use the apt-cache search command to search for packages by keyword.
Example Usage:
$ apt-cache search <keyword>
Diff the output of any two commands: Use the diff command to compare the output of two commands.
Example Usage:
$ diff -u <(ls -l /directory/) <(ls -l /directory/) | colordiff
Convert a Unix timestamp to human-readable format: Use the date command to convert a Unix timestamp to a human-readable format.
Example Usage:
$ date -d 1656685875
Squashing Git commits: Use the git rebase command to squash multiple Git commits into a single commit.
Example Usage:
$ git rebase -i HEAD~x # x = number of commits you've made
List all Systemd services: Use the systemctl command to list all Systemd services.
Example Usage:
$ systemctl -l -t service | less
In conclusion, mastering these Linux command line tools can be extremely useful for any system administrator or network engineer. With their versatility and power, these tools can help you efficiently manage your infrastructure and troubleshoot network issues. It's no wonder that Linux is so popular on servers, with 96.3% of the top one million web servers running on.
Rsync is a popular command-line utility that is used for file synchronization and data backup. It allows users to copy files and directories to a destination, similar to the cp command, but with some added features. One of the key benefits of using Rsync is that it allows you to copy files to remote locations, making it an excellent choice for backup purposes.
Example usage:
$ rsync -vap --ignore-existing <source_file> <destination_file>
Key flags:
v = verbose
r = recursive
p = preserve permissions
g = group
o = owner
a = archive
--progress = progress bar
Mkpasswd
Mkpasswd is a simple but useful command that generates complex random passwords at the specified length. This tool is particularly useful for generating secure passwords for user accounts or system services.
Example usage:
$ mkpasswd -l 8
iwF1g2Lo
Screen
Screen is a full-screen window manager that creates a single window with a shell running and allows multiple screen windows to run inside a single session. This tool is particularly useful when running long tasks remotely and worried about your SSH session dropping and ruining everything. Screen will continue running 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-have tool. This command-line 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
Uptime and w
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. The ‘w’ command combines uptime and who commands to show you who is currently logged in and what they are doing.
Example usage:
$ uptime
$ w
Wall
Wall is a handy command for system administrators that 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.
Example usage:
$ wall "Maintenance scheduled for 13:30"
Broadcast message from Joel@localhost: Maintenance scheduled for 13:30
Top
Top is a command-line tool that displays an auto-refreshing list of processes for the CPU and critical memory use and CPU usage metrics. It is an excellent tool for monitoring system resources and identifying performance issues.
Example usage:
$ 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.
Example usage:
$ 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.
Example usage:
$ lsof
Netcat (nc)
Netcat is a versatile networking utility that can be used for a variety of tasks such as port scanning, file copying, port forwarding, proxy servers, and hosting servers. With Netcat, you can check the connection between two hosts on a given port, create a proxy server, and more.
x
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
Netstat
Netstat is a command line tool that returns various network details such as routing tables, network connections, memberships, stats, flags, and more. It's useful for monitoring network activity and troubleshooting network issues.
Example Usage:
$ netstat -a # List all network ports
$ netstat -tlpn # List all listening ports
Nslookup
Nslookup is a tool 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
TCPDump
TCPDump is a tool used to capture and analyze traffic coming to and from your system. It's a powerful and versatile tool that specializes 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
Ad-hoc commands are one-liners that can be used to accomplish specific tasks quickly. Here are some examples:
Pretty printing API Responses: You can use Python's JSON tool module to pretty print out JSON documents for easier reading.
Example Usage:
$ cat test.json | python -m json.tool
Searching through apt for available packages: Use the apt-cache search command to search for packages by keyword.
Example Usage:
$ apt-cache search <keyword>
Diff the output of any two commands: Use the diff command to compare the output of two commands.
Example Usage:
$ diff -u <(ls -l /directory/) <(ls -l /directory/) | colordiff
Convert a Unix timestamp to human-readable format: Use the date command to convert a Unix timestamp to a human-readable format.
Example Usage:
$ date -d 1656685875
Squashing Git commits: Use the git rebase command to squash multiple Git commits into a single commit.
Example Usage:
$ git rebase -i HEAD~x # x = number of commits you've made
List all Systemd services: Use the systemctl command to list all Systemd services.
Example Usage:
$ systemctl -l -t service | less
In conclusion, mastering these Linux command line tools can be extremely useful for any system administrator or network engineer. With their versatility and power, these tools can help you efficiently manage your infrastructure and troubleshoot network issues. It's no wonder that Linux is so popular on servers, with 96.3% of the top one million web servers running on.
Comments
Post a Comment