In this cheat sheet tutorial I have consolidated a list of Linux commands with examples and man page link to give you an overview on Linux day to day usage. We know Linux is one of the preferred choice for most of the IT domains so having basic knowledge of Linux is mandatory for everyone. I have divided the Linux commands into different section so you can choose to only concentrate on the commands which suits your domain.
I will keep adding and updating this article from time to time to add more commands.
Environment Variables
Variables are local, which means they are specific to a process. Local
means local to a process. For example, when you log in on a terminal or
open a terminal emulator, you start a process that runs a shell and
create this variable TEST with value as deepak
# TEST=deepak
Verify the content of this variable
# echo $TEST
deepak
Now you open another terminal of the same Linux server and try to access this variable,
# echo $TEST
The output would be empty, so our variable is only accessible in the terminal where we created.
Recommended Read:
How to find the
path of any command in Linux
How
to set environment (PATH) variable permanently in Linux
Find the path of the
commands in linux
| Command | Example/Syntax | Comments | For more details |
|---|---|---|---|
| printenv | <strong># printenv</strong>
# printenv PATH |
Displays environment variable names and
values. When called with the name of an environment variable, it displays the value of that variable. |
man page for printenv |
| env | <strong># env</strong> |
The env utility runs a program as a child of the current shell, allowing you to modify the environment the current shell exports to the newly created process. | man page for env |
| export | # export TEST=deepak# env | grep TESTTEST=deepak |
When you run an export command with variable names as arguments, the shell places the names (and values, if present) of those variables in the environment. | man page for export |
| set | <strong># TEST=deepak# set | grep TEST</strong>TEST=deepak |
Display variables in the current shell These variables comprise shell variables (variables not in the environment) and environment variables. |
man page for set |
File Management
The commands under this section are very basic commands and must be known to every system administrator. This is definitely not the complete list of Linux commands for file management but can give you a kickstart and can cover most of the basic to complex scenarios.
Recommended Read:
Linux
copy directory and contents from remote to local & vice versa
5
commands to copy file from one server to another in Linux or Unix
How
to transfer files over SSH with SSHFS in Linux & Windows
Securely
transfer files between two hosts using HTTPS in Linux
10+
practical examples to create symbolic link in Linux
| Command | Example/Syntax | Comments | For more details |
|---|---|---|---|
| ls | <strong># ls</strong> |
List files | man page for ls |
<strong># ls -l</strong> |
Long list files | ||
<strong># ls -la</strong> |
Long list files including hidden files | ||
<strong># ls -ltr</strong> |
Long list files and sort by modification time. oldest placed first. | ||
| cat | <strong># cat FILENAME</strong> |
Print the content of the provided file on the terminal | man page for cat |
| less | <strong># less FILENAME</strong> |
When you want to view a file that is longer than
one screen, you can use either thelessutility or the more
utility.It will pause after displaying a screen of text You can use keyboard arrow to navigate around the file to read the text Press qto return to the shell |
man page for less |
| more | <strong># more FILENAME</strong> |
morecommand is also similar to less
but has few restrictionsWe cannot use navigation arrow from the keyboard while reading with more You must use SPACE bar to scroll through the file Press qto return to console |
man page for more |
| head | <strong># head -n 5 FILENAME</strong> |
This example displays the top 5 lines of provided
file By default the headutility displays the first ten lines of
a file. |
man page for head |
| tail | <strong># tail -n 5 FILENAME</strong> |
This example will display the last 5 lines of the
provided file By default tailwill show the last 10 lines of the
file |
man page for tail |
<strong># tail -f /var/log/messages</strong> |
To continuously monitor the incoming log messages
into/var/log/messagesfile in runtime |
||
| sort | <strong># sort FILENAME</strong> |
Displays a File in Order The sort utility displays the contents of a file in order by lines It does not change the original file. The –uoption generates a sorted list in which each line is
unique (no duplicates).The –noption puts a list of numbers in numerical
order. |
man page for sort |
| uniq | <strong># uniq FILENAME</strong> |
Theuniq(unique) utility displays a
file, skipping adjacent duplicate lines; it does not change the original
file.If a file contains a list of names and has two successive entries for the same person, uniqskips the extra line |
man page for uniq |
| file | <strong># file FILENAME</strong> |
Identifies the Contents of a File You can use the fileutility to learn about the contents of
any file on a Linux system without having to open and examine the file
yourself. |
man page for file |
<strong># file dataFile.txt</strong>dataFile.txt: ASCII text |
filecommand identified
thedataFile.txttype as ASCII text |
||
| cp | <strong># cp SOURCE-FILE DESTINATION-FILE</strong> |
Thecp(copy) utility makes a copy of
a file.The SOURCE-FILEis the name of the file
thatcpwill copy.The DESTINATION-FILEis the namecpassigns to
the resulting (new) copy of the file.If the DESTINATION-FILEexists before you give
acpcommand,cpoverwrites it. |
man page for cp |
<strong># cp /root/myfile /tmp/dir1/</strong> |
This command
copiedmyfilefrom/rootto/tmp/dir1directory |
||
| mv | <strong># mv EXISTING-FILENAME NEW-FILENAME</strong> |
Changes the Name of a File The mv(move) utility can rename a file without making a
copy of it. Themvcommand line specifies an existing file
and a new filename using the same syntax ascp |
man page for mv |
<strong># mv /root/debug.log /tmp/new_debug.log</strong> |
In this example we rename the name
ofdebug.logfile tonew_debug.logand also
changed the location of the file
from/root/to/tmp |
||
| grep | <strong># grep STRING FILENAME</strong> |
Thegreputility searches through one
or more files to see whether any contain a specified string of
characters.This utility does not change the file it searches but simply displays each line that contains the string. |
man page for grep |
<strong># grep ssh /etc/services</strong> |
In this example we search for all the lines
containing "ssh" in/etc/servicesfile |
||
| mkdir | <strong># mkdir DIR</strong> |
Create directories | man page for mkdir |
| touch | <strong># touch FILE</strong> |
Create empty file | man page for touch |
| pwd | <strong># pwd</strong> |
present working directory | man page for pwd |
Finding files and directories
Most of the time we will end up using find command to find files and directories. But I also like which command as it gives is the path of the binary which is required at multiple events when we are required to execute a binary with complete PATH.
Recommended Read:
10 find
exec multiple commands examples in Linux/Unix
How
to find and remove duplicate files using shell script in Linux
10+
practical examples to create symbolic link in Linux
| Command | Example/Syntax | Comments | For more details |
|---|---|---|---|
| which | <strong># which PROGRAMNAME</strong> |
whichwill print the full path of the
providedPROGRAMNAMEonSTDOUTIt does this by searching for an executable or script in the directories listed in the environment variable PATH. |
man page for which |
<strong># which useradd</strong>/usr/sbin/useradd |
In this example we are searching for the path
ofuseraddcommand |
||
| whereis | <strong># whereis FILENAME</strong> |
whereisattempts to locate the desired
program in the standard Linux places, and in the places specified
by$PATHand$MANPATH |
man page for whereis |
<strong># whereis sshd</strong>sshd: /usr/sbin/sshd /usr/share/man/man8/sshd.8.gz |
In this example we are searching for the path
ofsshdbinary and themanpage location
forsshdfile |
||
| locate | <strong># locate FILENAME</strong> |
Thelocateutility
(locatepackage; some distributions
usemlocate) searches for files on the local system:Before you can use locate ( mlocate),
theupdatedbutility must build or update the locate
(mlocate) database. Typically the database is updated once
a day by a cron script |
man page for locate |
<strong># locate sshd</strong>/etc/pam.d/sshd/etc/ssh/sshd_config/etc/sysconfig/sshd |
In this example we are searching for all files in
your Linux server containing stringsshdin their name |
||
| find | <strong># find PATH OPTIONS FILENAME</strong> |
findcommand will search for file or
directory based on the OPTIONS provided |
find cmnd examples |
<strong># find / -type f -name sshd</strong> |
In this example we are searching for a file
namedsshdinside/location |
Check User Information
Recommended Read:
How to check
last login time for users in Linux
How
to keep a track of all the commands run by any user in Linux
How to
check the lock status of any user account in Linux
How
to auto logout(timeout) a normal user and root user in Linux?
These are some of the commands which we use to check the last logged in user information and some other commands to get more details on existing user.
| Command | Example/Syntax | Comments | For more details |
|---|---|---|---|
| who | <strong># who -u</strong> |
Thewhoutility displays a list of
users who are logged in on the local system. |
man page for who |
| users | <strong># users</strong> |
Print the user names of users currently logged in
to the current host It does not give much information apart from usernames |
man page for users |
| last | <strong># last -a</strong> |
This command searches back through the
file/var/log/wtmp(or the file designated by the -f flag)
and displays a list of all users logged in (and out) since that file was
created. |
man page for last |
| finger | <strong># finger</strong> |
If no arguments are
specified,fingerwill print an entry for each user
currently logged into the system. |
man page for finger |
| whoami | <strong># whoami</strong> |
Print the user name associated with the current effective user ID | man page of whoami |
| id | <strong># id</strong> |
Print real and effective user and group IDs | man page for id |
| w | <strong># w</strong> |
The first line thewutility displays
is the same as the output ofuptimecommand. Following that
line, w displays a list of the users who are logged in. |
Check System Information
As a sytem and Linux administrator you must be familiar with these
commands. These will help you determine the type of server you are
working on, such as load, cpu model, hardware model, hardware type etc.
Some of the commands may be distribution specific such as hwinfo is
only available in SuSE Linux while others are expected to be found on
almost all distros.
Recommended Read:
5
commands to check if server is physical or virtual in Linux or Unix
How
to check if Hyper Threading (HT) is enabled or disabled on my Linux
server
How to get
the hardware model information in Linux
| Command | Example/Syntax | Comments | For more details |
|---|---|---|---|
| uptime | <strong># uptime</strong> |
Theuptimeutility displays a single line that includes the time of day, the period of time the computer has been running (in days, hours, and minutes), the number of users logged in, and the load average (how busy the system is). The three load average numbers represent the number of jobs waiting to run, averaged over the past 1, 5, and 15 minutes. |
<a href=“https://man7.org/linux/man-pages/man1/uptime.1.html" |
| target="_blank” rel=“noopener noreferrer” title=“man page of uptime”>man | |||
| page for uptime | |||
| free | <strong># free -m</strong> |
Thefreeutility displays the amount of physical (RAM) and swap memory in the local system. It displays columns for total, used, and free memory as well as for kernel buffers. |
<a |
| href=“https://www.golinuxcloud.com/tutorial-linux-memory-management-overview/" | |||
| target="_blank” rel=“noopener noreferrer” | |||
| title=“Tutorial: Beginners guide on linux memory management”>Linux | |||
| Memory Management | |||
| dmidecode | <strong># dmidecode -t system</strong> |
dmidecodeis a tool for dumping a computer’s DMI (some say SMBIOS) table contents in a human-readable format. This table contains a description of the system’s hardware components, as well as other useful pieces of information such as serial numbers and <a href=“https://www.golinuxcloud.com/dmidecode-command-in-linux/" |
|
| target="_blank” rel=“noopener noreferrer” | |||
| title=“20 dmidecode command examples in Linux [Cheat Sheet]">BIOS | |||
| revision | <a href=“https://linux.die.net/man/8/dmidecode" target="_blank” | ||
| rel=“noopener noreferrer” title=“man page of dmidecode”>man page for | |||
| dmidecode | |||
| lshw | <strong># lshw</strong> |
lshwis a small tool to extract detailed information on the hardware configuration of the machine. It can report exact memory configuration, firmware version, mainboard configuration, CPU version and speed, cache configuration, bus speed, etc |
<a href=“https://linux.die.net/man/1/lshw" target="_blank” |
| rel=“noopener noreferrer” title=“man page of lshw”>man page for lshw | |||
| hwinfo | <strong># hwinfo</strong> |
hwinfois used to probe for the hardware present in the system. It can be used to generate a system overview log which can be later used for support. (available only with SuSE) |
<a href=“https://sarata.com/manpages/hwinfo.8.html" target="_blank” |
| rel=“noopener noreferrer” title=“man page of hwinfo”>man page for | |||
| hwinfo | |||
| lscpu | <strong># lscpu</strong> |
lscpugathers CPU architecture information from sysfs,/proc/cpuinfoand any applicable architecture-specific libraries (e.g. librtas on Powerpc). |
<a href=“https://linux.die.net/man/1/lscpu" target="_blank” |
| rel=“noopener noreferrer” title=“man page of lscpu”>man page for | |||
| lscpu | |||
| lspci | <strong># lspci</strong> |
lspciis a utility for displaying information about PCI buses in the system and devices connected to them. |
<a href=“https://linux.die.net/man/8/lspci" target="_blank” |
| rel=“noopener noreferrer” title=“man page of lspci”>man page for | |||
| lspci | |||
| /proc/cpuinfo | <strong># cat /proc/cpuinfo</strong> |
Provides information about the CPU model, architecture, processors, available modules, and many more CPU related information.lscpugathers information from this file, |
<a href=“https://linux.die.net/man/1/lscpu" target="_blank” |
| rel=“noopener noreferrer” title=“man page of lscpu”>man page for | |||
| lscpu | |||
| uname | <strong># uname [OPTIONS]</strong> |
Print system information | <a href=“https://linux.die.net/man/1/uname" target="_blank” |
| rel=“noopener noreferrer” title=“man page for uname”>man page for | |||
| uname |
Manage System Processes
These Linux commands will help you manage the Linux processes, and will help you troubleshoot any server resource related issues. You can use these commands to monitor your server’s resource such as Memory, CPU, disk IO etc.
Recommended Read:
Tutorial
for Monitoring Tools SAR and KSAR with examples in Linux
How
to check memory usage of an individual process or application/program in
Linux
Tutorial:
Beginners guide on linux memory management
| Command | Example/Syntax | Comments | For more details |
|---|---|---|---|
| ps | <strong># ps [OPTIONS]</strong> |
psdisplays information about a
selection of the active processes. If you want a repetitive update of
the selection and the displayed information,
usetopinstead. |
man page for ps |
| nice | <strong># nice [OPTIONS]</strong> |
Run COMMAND with an adjusted niceness, which affects process scheduling. With no COMMAND, print the current niceness. Niceness values range from -20 (most favorable to the process) to 19 (least favorable to the process). | nice example |
| renice | <strong># renice [OPTIONS] PID</strong> |
renicealters the scheduling priority
of one or more running processes. The first argument is the priority
value to be used. The other arguments are interpreted as process IDs (by
default), process group IDs, user IDs, or user names. |
renice example |
<strong># renice -n 15 1121</strong>1121 (process ID) old priority 0, new priority 15 |
In this example I have changed the nice value of PID 1121 from 0 to 15 | ||
| top | <strong># top</strong> |
Thetopprogram provides a dynamic
real-time view of a running system. It can display system summary
information as well as a list of processes
or threads currently being managed by the Linux kernel. |
top examples |
| pgrep | <strong># pgrep [OPTIONS] PATTERN</strong> |
pgreplooks through the currently
running processes and lists the process IDs which match the selection
criteria to stdout. |
man page for pgrep |
| pkill | <strong># pkill [OPTIONS] PATTERN</strong> |
pkillwill send the specified signal
(by default SIGTERM) to each process instead of listing them on
stdout. |
man page for pkill |
| kill | <strong># kill [OPTIONS] PID</strong> |
The commandkillsends the specified
signal to the specified processes or process groups. |
man page for kill |
| sar | <strong># sar [OPTIONS]</strong> |
Thesarcommand writes to standard
output the contents of selected cumulative activity counters in the
operating system. The accounting system, based on the values in the
count and interval parameters, writes information the specified number
of times spaced at the specified intervals in seconds. If the interval
parameter is set to zero, thesarcommand displays the
average statistics for the time since the system was started. |
sar examples |
| vmstat | <strong># vmstat [OPTIONS]</strong> |
vmstatreports information about
processes, memory, paging, block IO, traps, disks and cpu activity. |
man page for vmstat |
| iostat | <strong># iostat [OPTIONS]</strong> |
Theiostatcommand is used for
monitoring system input/output device loading by observing the time the
devices are active in relation to their average transfer rates. |
man page for iostat |
| crond | <strong># crond [OPTIONS]</strong> |
cronddaemon is used to execute
scheduled commands. You can create new job
usingcrontab -eand provide the time when the script
should be executed and the script path |
crond examples |
Managing Users and Groups
These are some of the basic Linux commands to perform user management such as create, modify, delete user or groups.
Recommended Read:
How
to add user to sudoers with best practices & examples
4 easy
methods to check sudo access for user in Linux
10
practical examples to add or remove user from group in Linux
How
to prevent user from using old password (or re-using) again in Linux
| Command | Example/Syntax | Comments | For more details |
|---|---|---|---|
| useradd | <strong># useradd USERNAME</strong> |
Theuseraddcommand creates a new user
account using the values specified on the command line plus the default
values from the system. Depending on command line options,
theuseraddcommand will update system files and may also
create the new user's home directory and copy initial files. |
useradd examples |
| usermod | <strong># usermod OPTIONS USERNAME</strong> |
Theusermodcommand modifies the
system account files to reflect the changes that are specified on the
command line. |
man page of usermod |
<strong># usermod -G admin deepak</strong> |
In this example I am adding additional group to my
existingdeepakuser |
||
| userdel | <strong># userdel USERNAME</strong> |
Theuserdelcommand modifies the
system account files, deleting all entries that refer to the user
nameUSERNAME. The named user must exist |
man page for userdel |
| passwd | <strong># passwd USERNAME</strong> |
Thepasswdutility is used to update
user's authentication token(s).You can lock, unlock, assign passwords using passwdutility
for any system user |
passwd examples |
| groupadd | <strong># groupadd GROUPNAME</strong> |
Thegroupaddcommand creates a new
group account using the values specified on the command line plus the
default values from the system. The new group will be entered into the
system files as needed |
add or remove user from group |
| groupdel | <strong># groupdel GROUPNAME</strong> |
Thegroupdelcommand modifies the
system account files, deleting all entries that refer
toGROUPNAME. The named group must exist. |
add or remove user from group |
| groupmod | <strong># groupmod [options] GROUPNAME</strong> |
Thegroupmodcommand modifies the
definition of the specifiedGROUPby modifying the
appropriate entry in the group database. |
add or remove user from group |
<strong># groupmod -n administrator admin</strong> |
In this example I am renaming the group name from admin to administrator | ||
| sudo | <strong>$ sudo OPTIONS COMMAND</strong> |
sudoallows a permitted user to
execute a command as the superuser or another user, as specified by the
security policy. |
Add sudo permisison |
<strong>$ sudo systemctl network restart</strong> |
Observe the$sign in the beginning of
the shell, it donates a normal user shell. A root user's shell will have
hash (#)So in this example a normal user is performing network restart using sudo privilege |
Managing Permissions
Linux permission is a very vast topic and here I have only covered the basic commands which we use to assign/modify/remove permissions to files and directories.
Recommended Read:
Understanding
special permission Sticky Bit in Linux with examples
Understanding
special permission SUID in Linux with examples
Understanding
special permission SGID in Linux with examples
chmod
recursive usage guide for absolute beginners
| Command | Example/Syntax | Comments | For more details |
|---|---|---|---|
| chown | <strong># chown OPTIONS USER:GROUP TARGET</strong> |
chownchanges the user and/or group
ownership of each given file or directoryUSERrepresents the user owner of the target fileGROUPrepresents the group owner of the target fileTARGETrepresents any file or directory or PATH |
man page for chown |
<strong># chown deepak:admin /tmp/file</strong> |
In this example I am assigning user owner
permission todeepak, group owner permission to admin group
for/tmp/file |
||
| chmod | <strong># chmod PERM PATH</strong> |
chmodchanges the file mode bits
i.e.PERMof each given file according to mode, which can
be either a symbolic representation of changes to make, or an octal
number representing the bit pattern for the new mode bits |
man page for chmod |
<strong># chmod 755 /tmp/dir1</strong> |
In this example I am changing permission
of/tmp/dir1to 755 i.e. full permission for user,
read+execute permission for group and other users |
||
| chgrp | <strong># chgrp GROUPNAME FILE</strong> |
Change group ownership of files and
directoriesGROUPNAMErepresents the group to be assigned
forFILE |
man page for chgrp |
<strong># chgrp admin /tmp/file</strong> |
In this example we are changing the group ownership
to admin group for/tmp/file |
||
| groups | <strong># groups USERNAME</strong> |
Print group memberships for
eachUSERNAME |
man page for groups |
<strong># groups deepak</strong>deepak : deepak admin |
In this example we are checking the list of group
which userdeepakis part of. |
||
| newgrp | <strong>$ newgrp GROUPNAME</strong> |
Thenewgrpcommand is used to change
the current group ID during a login session |
man page for newgrp |
<strong>$ id</strong>uid=1001(deepak) gid=1001(deepak) groups=1001(deepak),1003(admin)
$ newgrp admin $ id |
In this example we are changing the primary group
of userdeepakto admin group |
||
| setfacl | <strong># setfacl OPTIONS FILE</strong> |
This utility sets Access Control Lists (ACLs) of
files and directories. It is useful to give individual permission to users and groups as chmodassigns permission of file level but here we have
more control over each user permission |
setfacl examples |
<strong># setfacl -m u:deepak:rx /tmp/file</strong> |
In this example I am giving read and execute
permission for userdeepakfor/tmp/file |
||
| getfacl | <strong># getfacl FILE</strong> |
For each file,getfacldisplays the
file name, owner, the group, and the Access Control List (ACL). If a
directory has a default ACL,getfaclalso displays the
default ACL |
getfacl examples |
<strong># getfacl /tmp/file</strong>getfacl: Removing leading '/' from absolute path names# file: tmp/file# owner: root# group: adminuser::rw-user:deepak:r-xgroup::r--mask::r-xother::r-- |
In this example I am collecting the acl permission
list from/tmp/fileIt has the permission detail which we applied earlier with setfacl |
||
| chattr | <strong># <a href="https://www.golinuxcloud.com/restrict-root-directory-extended-attributes/" title="How to restrict root user to access or modify a file and directory in Linux" target="_blank" rel="noopener noreferrer">chattr</a> OPTIONS FILE</strong> |
chattrchanges the file attributes on
a Linux file system |
chattr examples |
<strong># chattr +i /tmp/file</strong> |
In this example we have restricted the modification
permission on/tmp/file. Now not even root user can modify
the content of/tmp/fileTo remove this permission use chattr -i /tmp/file |
||
| lsattr | <strong># lsattr FILE</strong> |
list file attributes on a Linux second extended
file system. By default ls command will not show the permission
attributes applied bychattrso we must
uselsattrto get these details |
lsattr examples |
<strong># lsattr /tmp/file</strong>----i---------e---- /tmp/file |
In this example we can see the "i" attribute which
we added withchattrin the previous exampleThe 'e' attribute indicates that the file is using extents for mapping the blocks on disk. It may not be removed using chattr |
Configure and Troubleshoot Network
This section will help Network engineers who are new to Linux environment. I have tried to place the most used commands for network troubleshooting, we also have tcpdump, iperf, netperf and many other networking tools which are used for troubleshooting network related issues but they can get complicated hence those are not mentioned in this list.
Recommended Read:
How to
monitor network bandwidth in Linux using netperf
How
to use iperf3 tool to monitor network bandwidth in Linux
27
nmcli command examples (cheatsheet), compare nm-settings with if-cfg
file
| Command | Example/Syntax | Comments | For more details |
|---|---|---|---|
| ifconfig | <strong># ifconfig</strong> |
This program isobsolete! For
replacement checkip addrandip link. For
statistics useip -s link.If no arguments are given, ifconfigdisplays the status of
the currently active interfaces. If a single interface argument is
given, it displays the status of the given interface only; |
man page for ifconfig |
| ip | <strong># ip [OPTIONS]</strong> |
Newer command to monitor and set IP address and other network card–related information | ip examples |
<strong># ip link show</strong> |
This example lists the available network device on the Linux server along with their Link status | ||
| route | <strong># route [OPTIONS]</strong> |
This program isobsolete. For
replacement checkip route. |
ip route examples |
| ip route | <strong># ip route [OPTIONS]</strong> |
Manipulate route entries in the kernel routing tables keep information about paths to other networked nodes. | ip route examples |
<strong># ip route show</strong> |
In this example we are printing the configured routes on the Linux server from the routing table | ||
| ethtool | <strong># ethtool [OPTIONS] DEVICE</strong> |
query or control network driver and hardware settings | man page for ethtool |
<strong># ethtool -i eth0</strong> |
In this example we are printing the interface
details foreth0. Theinterface
namemay vary based on the environment and configuration. |
||
| ping | <strong># ping OPTIONS DESTINATION</strong> |
pingis used to check the connectivity
of remote computers |
man page for ping |
<strong># ping 192.168.0.100</strong> |
In this example we are testing the connectivity
between localhost and192.168.0.100server |
||
| traceroute | <strong># traceroute HOST</strong> |
Utility that helps you analyzing reachability of
hosts on the network. In most cases it is possible that due to firewall you may not get the list of hops towards the destination |
man page for traceroute |
<strong># traceroute 192.168.0.100</strong> |
In this example we are checking the route used to
connect192.168.0.100 |
||
| nmap | <strong># nmap OPTIONS HOST</strong> |
Utility that helps you check which services are
offered by an other host It is used to check the list of open ports on destination server |
man page for nmap |
| netstat | <strong># netstat [OPTIONS]</strong> |
Utility that helps you find out which services are
offered by the local host It will give you a list of ports which are used by different system services and any service from the network |
man page for netstat |
<strong># netstat -tunlp</strong> |
In this example I am listing the listening TCP and UDP protocols on my Linux server | ||
| nmcli | <strong># nmcli [OPTIONS]</strong> |
nmcliis a command-line tool for
controlling NetworkManager and reporting network status. |
nmcli examples |
| nmtui | <strong># nmtui</strong> |
nmtuiis a curses‐based TUI
application for interacting with NetworkManagerIt is a graphical alternative to nmcli |
nmtui examples |
| ss | <strong># ss [OPTIONS]</strong> |
ssis used to dump socket statistics.
It allows showing information similar tonetstat. It can
display more TCP and state information than other tools.When no option is used ss displays a list of open non-listening sockets (e.g. TCP/UNIX/UDP) that have established connection |
man page for ss |
Managing Partitions and Logical Volumes
One of the primary roles of system administrator would be to configure partitions, storage layouts in the Linux server. Here you can get the list of most used Linux commands for managing partitions and file systems.
Recommended Read:
Step-by-Step
Tutorial: Configure software Linear RAID 0 in Linux
Step-by-Step
Tutorial: Configure Software RAID 0 in Linux
Step-by-Step
Tutorial: Configure Software RAID 1 in Linux
Step-by-Step
Tutorial: Configure Software RAID 4 in Linux
Step-by-Step
Tutorial: Configure Software RAID 5 in Linux
Step-by-Step Tutorial:
Configure Hybrid Software RAID 10 in Linux
| Command | Example/Syntax | Comments | For more details |
|---|---|---|---|
| df | <strong># df [OPTIONS]</strong> |
Report file system disk space usage If no file name is given, the space available on all currently mounted file systems is shown |
man page for df |
| fdisk | <strong># fdisk [OPTIONS] DEVICE</strong> |
fdiskis interactive program for
creation and manipulation of partition tables. It understands GPT, MBR,
Sun, SGI and BSD partition tables |
fdisk example |
| cfdisk | <strong># cfdisk [OPTIONS] DEVICE</strong> |
Display or manipulate a disk partition table | man page for cfdisk |
| parted | <strong># parted</strong> |
partedis a program to manipulate disk
partitions. It supports multiple partition
table formats, including MS-DOS and GPT. |
parted example |
| pvcreate | <strong># pvcreate [OPTIONS] DEVICE</strong> |
Create LVM Physical Volume | pvcreate example |
<strong># pvcreate /dev/sda</strong> |
In this example we are creating a physical volume
using/dev/sdadevice |
||
| pvdisplay | <strong># pvdisplay DEVICE</strong> |
It shows the attributes of PVs, like size, physical extent size, space used for the VG descriptor area, etc | man page for pvdisplay |
| pvs | <strong># pvs</strong> |
Display information about physical volumes This can be used as an alternative to pvdisplayto display
limited information of available physical volumes |
man page for pvs |
| vgcreate | <strong># vgcreate [OPTIONS]</strong> |
It creates a new Volume Group on block devices (physical volume) | vgcreate example |
<strong># vgcreate test_vg /dev/sda</strong> |
In this example we are create a new volume group
"test_vg" by adding/dev/sdaHere /dev/sdais a new physical volume, you cannot use any
existing device used by other logical volumes |
||
| vgdisplay | <strong># vgdisplay DEVICE</strong> |
vgdisplayshows the attributes of VGs,
and the associated PVs and LVs |
man page for vgdisplay |
| vgs | <strong># vgs</strong> |
Display information about Volume Groups This can be used as an alternative to vgdisplayto display
limited information of available volume groups |
man page for vgs |
| lvcreate | <strong># lvcreate [OPTIONS]</strong> |
This is used to create logical volumes | lvcreate example |
<strong># lvcreate -L 1G -n test_lv1 test_vg</strong> |
In this example I am creating a logical
volumetest_lv1of size 1GB
undertest_vgvolume group |
||
| lvdisplay | <strong># lvdisplay DEVICE</strong> |
Display information about a logical volume | man page for lvdisplay |
| lvs | <strong># lvs</strong> |
Display information about logical volumes in
shorter output compared tolvdisplaycommand. You can use
this as an alternative tolvdisplay |
man page for lvs |
| pvscan | <strong># pvscan [OPTIONS]</strong> |
Scans storage devices for the presence of LVM
physical volumes. It is used when a new physical volume metadata is not loaded and a pvscancan load the metadata of all the available PV |
pvscan example |
| vgscan | <strong># vgscan [OPTIONS]</strong> |
Scans storage devices for the presence of LVM
volume groups. It can be used when any VG metadata is not visible and vgscancan scan the available VG |
man page for vgscan |
| lvscan | <strong># lvscan [OPTIONS]</strong> |
Scans storage devices for the presence of LVM
logical volumes. It can be used when any LV metadata is not visible and lvscancan scan the available LV |
man page for lvscan |
| vgchange | <strong># vgchange [OPTIONS]</strong> |
Changes the status from LVM volume groups and the volumes in it from active to inactive and vice versa | vgchange example |
<strong># vgchange -ay</strong> |
In this example we are activating all the available
volume groups. To deactivate all volume groups
usevgchange -an |
||
| e2fsck | <strong># e2fsck [OPTIONS]</strong> |
check a Linux ext2/ext3/ext4 file system We cannot perform a check on a mounted file system so normally this is performed during reboot or in single user mode |
e2fsck example |
| tune2fs | <strong># tune2fs [OPTIONS] DEVICE</strong> |
tune2fsallows the system
administrator to adjust various tunable filesystem parameters on Linux
ext2, ext3, or ext4 filesystems. |
tune2fs example |
| dumpe2fs | <strong># dumpe2fs [OPTIONS] DEVICE</strong> |
prints the super block and blocks group information for the filesystem present on device. | man page for dumpe2fs |
| mkfs. | <strong># mkfs.ext4# mkfs.xfs</strong> |
mkfsis used to build a Linux
filesystem on a device, usually a hard disk partition. The device
argument is either the device name (e.g. /dev/hda1, /dev/sdb2), or a
regular file that shall contain the filesystem. |
create filesystem |
| lvextend | <strong># lvextend OPTIONS</strong> |
Add space to a logical volume | lvextend example |
| vgextend | <strong># vgextend VGNAME PV</strong> |
Add physical volumes to a volume group | vgextend example |
| resize2fs | <strong># resize2fs DEVICE</strong> |
Theresize2fsprogram will resize
ext2, ext3, or ext4 file systems. It can be used to enlarge or shrink an
unmounted file system located on device |
resize2fs example |
| lsscsi | <strong># lsscsi [OPTIONS]</strong> |
list attached SCSI devices (or hosts) | man page for lsscsi |
| lsblk | <strong># lsblk</strong> |
lsblk lists information about all available or the specified block devices. The lsblk command reads the sysfs filesystem and udev db to gather information | man page for lsblk |
| blkid | <strong># blkid</strong> |
It prints the UUID valud of all the connected storage device. You can use this UUID to identify the storage device instead of using the physical name | man page for blkid |
Managing RPM and Software Repositories
With package manager such as yum, dnf, apt-get, the life of a system administrator becomes very easy. You can easily install, update, remove packages, upgrade server operating system and much more using these commands.
Recommended Read:
Step-by-Step:
YUM install specific version of Package
Install
old rpm or downgrade rpm to specific version using yum in Linux
Create local
offline yum repository in Linux
| Command | Example/Syntax | Comments | For more details |
|---|---|---|---|
| rpm | <strong># rpm [OPTIONS] FILE</strong> |
RPM Package Manager which can be used to build, install, query, verify, update, and erase individual software packages. | man page for rpm |
| yum | <strong># yum [OPTIONS] FILE</strong> |
yumis short abbreviation for Yellow
Dog Updater Modified.It is obsolete now and is replaced by dnf in most recent Linux distributions. YUM is a package manager for RPM-based Linux distributions |
yum examples |
| yumdownloader | <strong># yumdownloader [OPTIONS]</strong> |
Allows you to download packages from a repository to your system without installing them | yumdownloader examples |
| zypper | <strong># zypper [OPTIONS] FILE</strong> |
Package management utility in the SUSE works. Works
more or less the same as yum. The handling of repository is slightly different with zyppercompared toyum |
zypper examples |
| apt-get | <strong># apt-get [OPTIONS] FILE</strong> |
Ubuntu/Debian package management utility. Does a great job in installing and updating software. | man page for apt-get |
| apt-cache | <strong># apt-cache [OPTIONS] FILE</strong> |
Tool that allows you to search for packages in the locally cached index files | man page for apt-cache |
| dpkg | <strong># dpkg [OPTIONS] ACTION</strong> |
Original Debian package management utility, which
has been made more or less obsolete byapt-get |
man page for dpkg |
| dpkg-scanpackages | <strong># dpkg-scanpackages [OPTIONS]</strong> |
Tool that allows you to convert a directory containing .deb packages into a repository. | man page for dpkg-scanpackages |
| dnf | <strong># dnf [OPTIONS]</strong> |
DNF is the next upcoming major version of YUM, a package manager for RPM-based Linux distributions. | dnf examples |
| createrepo | <strong># createrepo [OPTIONS] PATH</strong> |
createrepois a program that creates
arepomd(xml-based rpm metadata) repository from a set of
rpms. |
createrepo examples |
| repoquery | <strong># repoquery [OPTIONS]</strong> |
This is part ofyum-utils, Searches
the available DNF repositories for selected packages and displays the
requested information about them |
repoquery examples |
| repotrack | <strong># repotrack [OPTIONS]</strong> |
This is part ofyum-utils, track
packages and its dependencies and download them |
repotrack examples |
| reposync | <strong># reposync [OPTIONS]</strong> |
Synchronize packages of a remote DNF or YUM repository to a local directory. | reposync examples |
| subscription-manager | <strong># subscription-manager [OPTIONS]</strong> |
Registers systems to a subscription management service and then attaches and manages subscriptions for software products | subscription-manager examples |
Manage logging
Now you know about most of the Linux commands to manager different areas of Linux server but you must be familiar of how logging works in Linux? This may vary based on different distribution, with old distros we used syslog-ng for logging but now almost all major distros have moved to rsyslog solution.
Recommended Read:
Understanding
systemd-journald and how logging works with Journal in RHEL 7
Journalctl
cheat sheet with 10+ commands to filter systemd logs
How
to enable persistent logging in systemd-journald without reboot
| Command | Example/Syntax | Comments | For more details |
|---|---|---|---|
| logger | <strong># logger [OPTIONS] MESSAGE</strong> |
loggermakes entries in the system
log.When the optional message argument is present, it is written to the log. |
logger examples |
| logrotate | <strong># logrotate [OPTIONS]</strong> |
Command that helps you to prevent log files from growing too big and rotate them after a certain amount of time or once a given size has been reached | logrotate examples |
| journalctl | <strong># journalctl [OPTIONS]</strong> |
journalctlmay be used to query the
contents of the systemd
journal as written by systemd-journald.service |
journalctl examples |
Conclusion
In this cheat sheet tutorial I have tried to consolidate most used Linux commands by different types of experts across IT domains. I am yet to add commands for many other scenarios such as Managing Linux services, archiving, firewall etc but that would just make this tutorial infinite long. I may write another article based on the response I get on this one, even writers need motivation. So that I know people are reading and loving this cheat sheet then I may decide to spend some more time to write about the remaining Linux commands in another tutorial.
Looking forward for your feedback in the comment section.


