0% found this document useful (0 votes)
30 views17 pages

Essential Linux Commands Guide

This document provides a cheat sheet of the most commonly used Linux commands organized into categories like system information, hardware information, file permissions, networking, archives, installing packages, searching, SSH logins, file transfers, disk usage, directory navigation, and security. The cheat sheet breaks down commands and provides brief descriptions and examples of their usage.

Uploaded by

Vijay Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views17 pages

Essential Linux Commands Guide

This document provides a cheat sheet of the most commonly used Linux commands organized into categories like system information, hardware information, file permissions, networking, archives, installing packages, searching, SSH logins, file transfers, disk usage, directory navigation, and security. The cheat sheet breaks down commands and provides brief descriptions and examples of their usage.

Uploaded by

Vijay Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Linux Training Academy MENU

Linux Commands Cheat Sheet


Did you know that there are literally hundreds of Linux commands? Even on a bare-bones Linux server install there are easily
over 1,000 different commands.

The interesting thing is that most people only need to use a very small subset of those commands. Below you’ll find a Linux
“cheat sheet” that breaks down some of the most commonly used commands by category.

To get your own PDF and printable copy, scroll to the bottom of the page.

Enjoy!

1 – SYSTEM INFORMATION
# Display Linux system information
uname -a

# Display kernel release information


uname -r

# Show operating system information such as distribution name and version


cat /etc/os-release
# Show how long the system has been running + load
uptime

# Show system host name


hostname

# Display all local IP addresses of the host.


hostname -I

# Show system reboot history


last reboot

# Show the current date and time


date

# Show this month's calendar


cal

# Display who is online


w

# Who you are logged in as


whoami

2 – HARDWARE INFORMATION
# Display messages in kernel ring buffer
dmesg

# Display CPU information


cat /proc/cpuinfo

# Display memory information


cat /proc/meminfo

# Display free and used memory ( -h for human readable, -m for MB, -g for GB.)
free -h

# Display PCI devices


lspci -tv

# Display USB devices


lsusb -tv

# Display DMI/SMBIOS (hardware info) from the BIOS


dmidecode

# Show info about disk sda


hdparm -i /dev/sda

# Perform a read speed test on disk sda


hdparm -tT /dev/sda

# Test for unreadable blocks on disk sda


badblocks -s /dev/sda
Thank you! Please check
your inbox for your
download.
3 – PERFORMANCE MONITORING AND STATISTICS
# Display and manage the top processes
top

# Interactive process viewer (top alternative)


htop

# Display processor related statistics


mpstat 1

# Display virtual memory statistics


vmstat 1

# Display I/O statistics


iostat 1

# Display the last 100 syslog messages (Use /var/log/syslog for Debian based systems.)
tail -100 /var/log/messages

# Capture and display all packets on interface eth0


tcpdump -i eth0

# Monitor all traffic on port 80 ( HTTP )


tcpdump -i eth0 'port 80'

# List all open files on the system


lsof

# List files opened by user


lsof -u user

# Display free and used memory ( -h for human readable, -m for MB, -g for GB.)
free -h

# Execute "df -h", showing periodic updates


watch df -h

4 – USER INFORMATION AND MANAGEMENT


# Display the user and group ids of your current user.
id

# Display the last users who have logged onto the system.
last

# Show who is logged into the system.


who

# Show who is logged in and what they are doing.


w

# Create a group named "test".


groupadd test

# Create an account named john, with a comment of "John Smith" and create the user's home directory.
useradd -c "John Smith" -m john

# Delete the john account.


userdel john

# Add the john account to the sales group


usermod -aG sales john

5 – FILE AND DIRECTORY COMMANDS


# List all files in a long listing (detailed) format
ls -al

# Display the present working directory


pwd
# Create a directory
mkdir directory

# Remove (delete) file


rm file

# Remove the directory and its contents recursively


rm -r directory

# Force removal of file without prompting for confirmation


rm -f file

# Forcefully remove directory recursively


rm -rf directory

# Copy file1 to file2


cp file1 file2

# Copy source_directory recursively to destination. If destination exists, copy source_directory into d


cp -r source_directory destination

# Rename or move file1 to file2. If file2 is an existing directory, move file1 into directory file2
mv file1 file2

# Create symbolic link to linkname


ln -s /path/to/file linkname

# Create an empty file or update the access and modification times of file.
touch file

# View the contents of file


cat file

# Browse through a text file


less file

# Display the first 10 lines of file


head file

# Display the last 10 lines of file


tail file

# Display the last 10 lines of file and "follow" the file as it grows.
tail -f file

6 – PROCESS MANAGEMENT
# Display your currently running processes
ps

# Display all the currently running processes on the system.


ps -ef

# Display process information for processname


ps -ef | grep processname

# Display and manage the top processes


top

# Interactive process viewer (top alternative)


htop

# Kill process with process ID of pid


kill pid
# Kill all processes named processname
killall processname

# Start program in the background


program &

# Display stopped or background jobs


bg

# Brings the most recent background job to foreground


fg

# Brings job n to the foreground


fg n

7 – FILE PERMISSIONS
PERMISSION EXAMPLE

U G W
rwx rwx rwx chmod 777 filename
rwx rwx r-x chmod 775 filename
rwx r-x r-x chmod 755 filename
rw- rw- r-- chmod 664 filename
rw- r-- r-- chmod 644 filename

# NOTE: Use 777 sparingly!

LEGEND
U = User
G = Group
W = World

r = Read
w = write
x = execute
- = no access

8 – NETWORKING
# Display all network interfaces and IP address
ip a

# Display eth0 address and details


ip addr show dev eth0

# Query or control network driver and hardware settings


ethtool eth0

# Send ICMP echo request to host


ping host

# Display whois information for domain


whois domain

# Display DNS information for domain


dig domain

# Reverse lookup of IP_ADDRESS


dig -x IP_ADDRESS

# Display DNS IP address for domain


host domain

# Display the network address of the host name.


hostname -i
# Display all local IP addresses of the host.
hostname -I

# Download [Link]
wget [Link]

# Display listening tcp and udp ports and corresponding programs


netstat -nutlp

9 – ARCHIVES (TAR FILES)


# Create tar named [Link] containing directory.
tar cf [Link] directory

# Extract the contents from [Link].


tar xf [Link]

# Create a gzip compressed tar file name [Link].


tar czf [Link] directory

# Extract a gzip compressed tar file.


tar xzf [Link]

# Create a tar file with bzip2 compression


tar cjf [Link].bz2 directory

# Extract a bzip2 compressed tar file.


tar xjf [Link].bz2
10 – INSTALLING PACKAGES
# Search for a package by keyword.
yum search keyword

# Install package.
yum install package

# Display description and summary information about package.


yum info package

# Install package from local file named [Link]


rpm -i [Link]

# Remove/uninstall package.
yum remove package

# Install software from source code.


tar zxvf [Link]
cd sourcecode
./configure
make
make install

11 – SEARCH
# Search for pattern in file
grep pattern file

# Search recursively for pattern in directory


grep -r pattern directory

# Find files and directories by name


locate name

# Find files in /home/john that start with "prefix".


find /home/john -name 'prefix*'

# Find files larger than 100MB in /home


find /home -size +100M

12 – SSH LOGINS
# Connect to host as your local username.
ssh host

# Connect to host as user


ssh user@host

# Connect to host using port


ssh -p port user@host

13 – FILE TRANSFERS
# Secure copy [Link] to the /tmp folder on server
scp [Link] server:/tmp

# Copy *.html files from server to the local /tmp folder.


scp server:/var/www/*.html /tmp
# Copy all files and directories recursively from server to the current system's /tmp folder.
scp -r server:/var/www /tmp

# Synchronize /home to /backups/home


rsync -a /home /backups/

# Synchronize files/directories between the local and remote system with compression enabled
rsync -avz /home server:/backups/

14 – DISK USAGE
# Show free and used space on mounted filesystems
df -h

# Show free and used inodes on mounted filesystems


df -i

# Display disks partitions sizes and types


fdisk -l

# Display disk usage for all files and directories in human readable format
du -ah

# Display total disk usage off the current directory


du -sh

15 – DIRECTORY NAVIGATION
# To go up one level of the directory tree. (Change into the parent directory.)
cd ..

# Go to the $HOME directory


cd

# Change to the /etc directory


cd /etc

16 – SECURITY
# Change the current user's password.
passwd

# Switch to the root account with root's environment. (Login shell.)


sudo -i

# Execute your current shell as root. (Non-login shell.)


sudo -s

# List sudo privileges for the current user.


sudo -l

# Edit the sudoers configuration file.


visudo

# Display the current SELinux mode.


getenforce

# Display SELinux details such as the current SELinux mode, the configured mode, and the loaded policy
sestatus
# Change the current SELinux mode to Permissive. (Does not survive a reboot.)
setenforce 0

# Change the current SELinux mode to Enforcing. (Does not survive a reboot.)
setenforce 1

# Set the SELinux mode to enforcing on boot by using this setting in the /etc/selinux/config file.
SELINUX=enforcing

# Set the SELinux mode to permissive on boot by using this setting in the /etc/selinux/config file.
SELINUX=permissive

# Set the SELinux mode to disabled on boot by using this setting in the /etc/selinux/config file.
SELINUX=disabled

17 – LOGGING AND AUDITING


# Display messages in kernel ring buffer.
dmesg

# Display logs stored in the systemd journal.


journalctl

# Display logs for a specific unit (service).


journalctl -u servicename

18 – Linux Command Line Cheat Sheet


Download My Linux Cheat Sheet!
Enter your email address below to download this Linux command line cheat sheet in an easy-to-read and ready-to-print format.

Your Best Email Address 

Send me my cheat sheet >

We respect your privacy.

Related Articles:
Linux ip Command Networking Cheat Sheet
Vim Cheat Sheet
Managing Linux Users and Groups
23 Handy Bash Shell Aliases For Unix, Linux, and Mac OS X

AlmaLinux, CentOS, cheat sheet, Command Line, Linux, RedHat, Server, Shell Scripting, SSH, Ubuntu
VirtualBox Troubleshooting Tips
Vim Cheat Sheet

Most Popular

Linux Commands Cheat Sheet


Linux ip Command Networking Cheat Sheet
Vim Cheat Sheet
Linux System Administrator Projects
1,500 Coding Project Ideas

Common questions

Powered by AI

'wget' and 'curl' are both commendable command-line tools for downloading files and interacting with web resources on Linux servers, but they serve slightly different purposes and have distinct advantages. 'wget' is ideal for downloading files because it supports retrieving files via HTTP, HTTPS, and FTP protocols and can recursively retrieve documents from web servers. It also maintains a session during failed transfers (resuming downloads automatically), making it robust in dealing with unstable connections. Moreover, 'wget' is highly suited for mirroring directories and is capable of creating local versions of web sites, handling complex download scenarios with ease. Conversely, 'curl' provides a broader range of options for transferring data across networks, supporting multiple protocols like SCP, SFTP, LDAP, and SMTP. It shines in scenarios demanding more connections within a single command or where specific HTTP request methods (POST, PUT) are needed. Both have ample capabilities, but for batch downloading or simple file retrievals, 'wget's retry and recursive features make it more convenient in typical web-related troubleshooting and data acquisition .

The 'top' and 'htop' commands are both used for monitoring processes on a Linux system, but they have distinct differences. 'top' is a well-known command line tool that provides a real-time view of all running processes, including their CPU and memory usage, in a straightforward tabular format. It is widely available on most Unix-like systems and is sufficient for basic needs, but it lacks interactivity and a detailed visual interface. On the other hand, 'htop' offers a more visually appealing and user-friendly interface with color-coded metrics, making it easier to understand at a glance. Unlike 'top', 'htop' allows for vertical scrolling and mouse interaction, and you can easily kill processes without typing their PID manually. 'htop' is particularly advantageous for users who prefer a detailed, interactive navigation of running processes and want to customize information display in real time. 'Top' might be preferable in scenarios where essential, no-frills monitoring is required over systems with resource constraints where installing additional software like 'htop' is impractical .

The 'ping' and 'traceroute' commands are essential tools in network diagnostics, each serving a complementary role. 'ping' is used to check the connectivity between the local host and a destination host by sending ICMP echo requests and measuring the time it takes to receive the echo reply. This helps verify that a host is reachable and provides a basic round-trip time measurement. However, 'ping' does not provide details on the route taken or where potential issues might exist. This is where 'traceroute' becomes valuable: it maps the complete path from the source to the destination host, listing each hop in the journey and measuring the time taken for each leg. If 'ping' reveals connectivity issues, 'traceroute' can be used to pinpoint where along the path the issue is occurring, such as identifying a slow or non-responsive hop. Using these tools in tandem allows network administrators to both confirm the presence of connection issues and diagnose their potential causes, forming a comprehensive toolkit for network troubleshooting .

'find' and 'grep' are powerful Linux commands that can be combined to perform advanced file searches efficiently. 'find' allows you to search for files based on criteria such as name, size, type, or modification time. 'grep', on the other hand, searches the content within files for specific patterns. Used together, this combination can pinpoint both the file and the data within it that meets specific conditions. For example, you can use 'find /path -name '*.txt'' to locate all text files within a directory and subdirectories, and pipe the result to 'grep' to find text occurrences within those files like 'find /path -name '*.txt' -exec grep 'pattern' {} \;'. This is especially useful in scenarios where you need to find files containing specific code snippets, configurations, or logs spread across a large file system, allowing users to quickly track down the necessary information without scanning manually .

On a Linux system, file permissions can be managed using the 'chmod' command, which changes the read (r), write (w), and execute (x) permissions for the user (U), group (G), and others (W). These permissions are represented numerically, with rwx equating to 7, rw- to 6, and r-- to 4. The 'chmod' command can be used to set various permission levels, such as 'chmod 755' for user rwx, group r-x, and others r-x. The command 'chmod 777' grants full permissions to the user, group, and others, allowing anyone to read, write, and execute the file. This is generally discouraged because it poses a security risk as it allows any user to modify the file, potentially leading to data loss, corruption, or unauthorized execution of harmful scripts .

'Rsync' is a powerful utility for file transfers in Linux, especially crucial for handling large datasets over unreliable networks due to its capability of incremental copying. Rather than copying all files again, 'rsync' only transfers the differences between the source and the destination, significantly saving bandwidth and reducing transfer times. The command also supports error detection, partial transfers, and checksum verification, which ensures the integrity and completeness of transferred data. Additionally, 'rsync' offers compression and can synchronize files recursively, making it highly efficient for large directory trees. Its robustness and efficiency make 'rsync' ideal for creating backups and mirroring data across systems where connection interruptions might otherwise necessitate a complete restart of the transfer process .

Security-Enhanced Linux (SELinux) provides a mechanism for supporting access control security policies. Adjusting the SELinux mode can be advantageous during troubleshooting or configuration of servers where standard policy enforcement interferes with the operation of services. SELinux operates in three modes: Enforcing, Permissive, and Disabled. Enforcing mode enforces the SELinux security policy, blocking unauthorized access attempts, which is advantageous for ensuring high security. Permissive mode enacts the same policy without enforcement, logging policy violations without restricting access, useful in troubleshooting situations to ensure services run smoothly while diagnosing policy issues. Disabled mode turns SELinux off entirely, removing any impact from SELinux policies. While permissing temporarily lowers security to facilitate policy de-bugging, persistent use might lead to security lapses. It is critical to plan necessary access controls and consider security implications before making long-term changes to SELinux modes .

The 'df -h' command is widely used for monitoring disk space in Linux due to its ability to display the size, used space, available space, and file-system mount points in a human-readable format, with size units like MB and GB for easier comprehension. This makes it highly practical for quickly assessing the storage situation at a glance, particularly beneficial for system administrators who need to manage disk usage efficiently. Compared to other commands, 'df -h' simplifies interpreting disk usage statistics without the need for mathematical conversions and calculations. It differs from 'du', which provides detailed usage for directories and files rather than overall filesystem statistics, hence, 'df -h' is more suited for general filesystem monitoring rather than deep-dive analysis at the individual directory level .

The 'journalctl' command is integral to system logging and auditing on Linux, allowing administrators to access and query messages stored in the systemd journal. This command supports viewing logs by specifying time periods, priorities, or by filtering based on the services generating the logs. It plays a key role in system auditing by facilitating the examination of critical events, errors, and warning messages, helping identify issues affecting system services or applications. Usage scenarios include reviewing system boots with 'journalctl -b', investigating specific service logs using 'journalctl -u servicename', and analyzing security logs. These capabilities make 'journalctl' an especially valuable tool for maintaining system health, troubleshooting, and ensuring compliance with security policies .

To efficiently backup and compress data using the 'tar' command on a Linux system, you start by creating a tar archive using 'tar cf archive.tar directory', where 'archive.tar' is the name of the archive file you're creating and 'directory' is the path to the data being archived. For compression, you can use gzip or bzip2, which can be achieved by adding 'z' or 'j' respectively in the 'tar' options (e.g., 'tar czf archive.tar.gz directory' for gzip compression). It's important to choose the right compression format based on the required compression level and speed, with gzip generally faster but less compressive than bzip2. You should also consider the effect of compression on CPU load and disk space. Managing permissions and ownership during the archiving process, and ensuring that the operation includes symbolic links if necessary, is also crucial .

You might also like