Whether you are a Sysadmin, Developer, DevOps, Security or Ops… using Linux effectively and its tools is the most basic skill you can learn. Linux is the backbone of the majority of servers and applications around the world.
“47% of developers use a Linux-based operating system.”
Over the past few months, I’ve read many articles like ’20 Linux Commands You Must Know’ or ‘The Linux Survival Guide’. The problem that I have found mainly these articles are mainly for beginners, teach how to use ls
or echo
. As for this article, you will be introduced to Linux commands that you may have never encountered.
Utilities
rsync
Used to copy files and folders similar to cp
. However, it also allows copying to remote locations like to Google Drive, OneDrive, FTP… and provides a progress bar, as is commonly used for backups.
# Ví dụ $ rsync -vap --ignore-existing <source_file> <destination_file> # Tham số: 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 of the specified length.
$ mkpasswd -l 8 > iwF1g2Lo
screen
Screen
It’s most beneficial when you’re remote using SSH and worry if the network goes down that could ruin everything. Screen
will continue to execute Linux commands even though your SSH connection has been broken.
# Ví dụ $ screen # Chạy phiên làm việc $ screen -ls # Hiên thị các services đang chạy $ screen -r # Attach to session
Ldapsearch
If you often work with LDAP databases, then Ldapsearch
is a Linux command that should be used mandatory. The opener connects to the LDAP server and allows you to search, find, and debug entries in the database.
Uptime
Uptime
returns metrics for how long the server has been running, current time, number of users, and average memory usage. If something goes wrong on your server, this is the first command you should use.
You can use the command w
– only letter w to see information about server uptime.
Wall
Wall
is a handy command for any Sysadmin; it allows you to send messages to the devices of everyone currently logged into the system. This can be very useful for system-wide notifications.
$ wall "Thong bao - He thong se bao tri vao luc 13:30" Thong bao duoc phat tu AnonyViet [email protected]: Maintenance scheduled for 13:30
top
Command top
Displays a list of active CPU processes, RAM usage and CPU usage metrics.
Ncdu
Command ncdu
provides a convenient view of hard disk usage. You can use ncdu
to quickly and easily see which folders are using the most disk space.
lsof
lsof
is a single command used for one basic purpose: LiSt Open Files. This command quickly determines which files are being used by which processes.
Network-related commands – Network
Netcat
Netcat
or nc
Mainly used for port scanning – port scan but is actually a very useful utility Network tool for Sysadmin. Netcat can support port scanning, file copying, Nat Port, proxy servers, and hosting servers… this command is very flexible and full of features
NetStat
netstat
returns various network details like routing table, network connection, membership, stats, flags etc
#Ví dụ $ netstat -a # Xem các Port của mạng $ netstat -tlpn # Xem các port đang lắng nghe listening ports # Tham số -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 your internet or LAN. It queries DNS for server information and can be useful for network debugging.
# Ví dụ Cách sử dụng $ nslookup media.com/tags/devops # Key Flags -port = Thay đổi số cổng cho kết nối, -type = Thay đổi loại truy vấn. -domain = Đặt danh sách tìm kiếm thành tên
TCPDump
Used to capture and analyze traffic to and from your system. tcpdump
is a powerful and flexible tool that specializes in network debugging and troubleshooting but can also be used as a security tool.
# Cách sử dụng $ tcpdump $ tcpdump -i <interface> <ipaddress or hostname> <port>
Ad-Hoc command
See beautiful JSON format
Reading JSON data in Terminal can be very frustrating when working with APIs. As you can see below, even a small set of data quickly becomes a mess when displayed on 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"]}
By reading the JSON file in python, you will see the result appear nicer and easier to see.
$ 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" }
Compare the results of two commands
Example comparing the results of 2 commands ls
:
$ diff -u <(ls -l /directory/) <(ls -l /directory/) | colordiff
List all Systemd . services
systemctl -l -t service | less
Hope you learned something from the Linux commands above; Linux mastery can be extremely helpful given the popularity of free Linux and its large support community, statistically 96.3% of a million web hosts are running Linux.