Basic Linux Commands: Difference between revisions
Created page with "= Linux Commands = '''Linux command-line tools''' are the building blocks of interaction, enumeration, file manipulation, and system control during any hacking operation. This page breaks down commonly used Linux commands, their usage, categories, and real-world hacking relevance. == <span id="basics"></span>Essential Commands == === Navigation === {| class="wikitable" ! Command !! Description !! Example |- | <code>pwd</code> || Show current working directory || <code..." |
|||
(14 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
= Linux Commands = | = Linux Commands = | ||
'''Linux | '''Linux command‑line tools''' are the backbone of reconnaissance, exploitation, and post‑exploitation on Unix‑like systems. | ||
Understanding Linux commands provides direct access to system internals, automation, and control. Many security tools and scripts rely on command-line operations for efficiency and precision. In ethical hacking, the ability to navigate filesystems, manage processes, inspect network configurations, and manipulate data through the terminal creates a strong foundation for tasks such as enumeration, privilege escalation, and persistence. | |||
=== | Proficiency with core commands accelerates workflow, reduces reliance on graphical interfaces, and improves situational awareness during engagements. Whether working locally or through remote shells, command-line skills increase control and adaptability across diverse environments. | ||
{| class="wikitable" | |||
! Command !! Description !! | |||
== <span id="commands"></span>Common Commands == | |||
=== Navigation & File Management === | |||
{| class="wikitable sortable" | |||
! Command !! Description !! Usage | |||
|- | |||
| [[#ex-pwd|<code>pwd</code>]] || Print working directory || <code>pwd</code> | |||
|- | |||
| [[#ex-cd|<code>cd</code>]] || Change directory || <code>cd [DIRECTORY]</code> | |||
|- | |||
| [[#ex-ls|<code>ls</code>]] || List directory contents || <code>ls [OPTIONS] [FILE...]</code> | |||
|- | |||
| [[#ex-tree|<code>tree</code>]] || Recursively list directories as tree || <code>tree [DIRECTORY]</code> | |||
|- | |||
| [[#ex-mkdir|<code>mkdir</code>]] || Create directory || <code>mkdir [OPTIONS] DIRECTORY</code> | |||
|- | |||
| [[#ex-rmdir|<code>rmdir</code>]] || Remove empty directory || <code>rmdir DIRECTORY</code> | |||
|- | |||
| [[#ex-cp|<code>cp</code>]] || Copy files or directories || <code>cp SOURCE DEST</code> | |||
|- | |||
| [[#ex-mv|<code>mv</code>]] || Move or rename files/directories || <code>mv SOURCE DEST</code> | |||
|- | |||
| [[#ex-rm|<code>rm</code>]] || Delete files or directories || <code>rm [OPTIONS] FILE...</code> | |||
|- | |||
| [[#ex-touch|<code>touch</code>]] || Create empty file or update timestamp || <code>touch FILENAME</code> | |||
|- | |||
| [[#ex-ln|<code>ln</code>]] || Create hard or symbolic links || <code>ln [OPTIONS] TARGET LINK_NAME</code> | |||
|- | |||
| [[#ex-stat|<code>stat</code>]] || Display detailed file information || <code>stat FILE</code> | |||
|- | |||
| [[#ex-file|<code>file</code>]] || Detect file type || <code>file FILE</code> | |||
|- | |||
| [[#ex-basename|<code>basename</code>]] || Strip directory and suffix from filename || <code>basename PATH</code> | |||
|- | |||
| [[#ex-dirname|<code>dirname</code>]] || Extract directory part of file path || <code>dirname PATH</code> | |||
|- | |||
| [[#ex-find|<code>find</code>]] || Search for files and directories || <code>find [PATH] [OPTIONS] [EXPRESSION]</code> | |||
|- | |||
| [[#ex-locate|<code>locate</code>]] || Find files using database (faster than find) || <code>locate FILENAME</code> | |||
|- | |||
| [[#ex-chmod|<code>chmod</code>]] || Change file permissions || <code>chmod [OPTIONS] MODE FILE</code> | |||
|- | |||
| [[#ex-chown|<code>chown</code>]] || Change file ownership (user) || <code>chown [OPTIONS] USER FILE</code> | |||
|- | |||
| [[#ex-chgrp|<code>chgrp</code>]] || Change group ownership || <code>chgrp [OPTIONS] GROUP FILE</code> | |||
|- | |||
| [[#ex-umask|<code>umask</code>]] || Set default file permission mask || <code>umask [MASK]</code> | |||
|- | |||
| [[#ex-tar|<code>tar</code>]] || Archive files into tarball || <code>tar -czf ARCHIVE.tar.gz FILES</code> | |||
|- | |||
| [[#ex-gzip|<code>gzip</code>]] || Compress files using Gzip || <code>gzip FILE</code> | |||
|- | |||
| [[#ex-gunzip|<code>gunzip</code>]] || Decompress Gzip files || <code>gunzip FILE.gz</code> | |||
|- | |||
| [[#ex-zip|<code>zip</code>]] || Compress files into ZIP archive || <code>zip ARCHIVE.zip FILES</code> | |||
|- | |||
| [[#ex-unzip|<code>unzip</code>]] || Extract ZIP archive || <code>unzip ARCHIVE.zip</code> | |||
|- | |||
| [[#ex-dd|<code>dd</code>]] || Copy and convert data (low-level) || <code>dd if=SOURCE of=DEST bs=BLOCKSIZE</code> | |||
|- | |||
| [[#ex-mount|<code>mount</code>]] || Mount a filesystem || <code>mount [DEVICE] [MOUNTPOINT]</code> | |||
|- | |||
| [[#ex-umount|<code>umount</code>]] || Unmount a filesystem || <code>umount [MOUNTPOINT]</code> | |||
|- | |||
| [[#ex-namei|<code>namei</code>]] || Follow a file path, showing each component || <code>namei PATH</code> | |||
|- | |||
| [[#ex-readlink|<code>readlink</code>]] || Resolve symbolic link to actual path || <code>readlink FILE</code> | |||
|- | |||
| [[#ex-realpath|<code>realpath</code>]] || Print the resolved absolute file path || <code>realpath FILE</code> | |||
|- | |||
| [[#ex-mktemp|<code>mktemp</code>]] || Create a temporary file or directory || <code>mktemp [TEMPLATE]</code> | |||
|} | |||
=== Viewing & Text Processing === | |||
{| class="wikitable sortable" | |||
! Command !! Description !! Usage | |||
|- | |||
| [[#ex-cat|<code>cat</code>]] || Concatenate & display files || <code>cat [OPTIONS] FILE...</code> | |||
|- | |||
| [[#ex-less|<code>less</code>]] || Paginate large files || <code>less FILE</code> | |||
|- | |||
| [[#ex-more|<code>more</code>]] || View file one page at a time || <code>more FILE</code> | |||
|- | |||
| [[#ex-head|<code>head</code>]] || Show first lines of a file || <code>head [OPTIONS] FILE</code> | |||
|- | |||
| [[#ex-tail|<code>tail</code>]] || Show last lines / follow file || <code>tail [OPTIONS] FILE</code> | |||
|- | |||
| [[#ex-grep|<code>grep</code>]] || Search for patterns || <code>grep [OPTIONS] PATTERN FILE</code> | |||
|- | |- | ||
| <code> | | [[#ex-awk|<code>awk</code>]] || Pattern scanning & processing language || <code>awk 'pattern { action }' FILE</code> | ||
|- | |- | ||
| <code> | | [[#ex-sed|<code>sed</code>]] || Stream editor for filtering & transforming text || <code>sed [OPTIONS] 'SCRIPT' FILE</code> | ||
|- | |- | ||
| <code> | | [[#ex-cut|<code>cut</code>]] || Remove sections from each line || <code>cut [OPTIONS] FILE</code> | ||
|- | |- | ||
| <code> | | [[#ex-tr|<code>tr</code>]] || Translate or delete characters || <code>tr SET1 SET2</code> | ||
|- | |||
| [[#ex-sort|<code>sort</code>]] || Sort lines of text files || <code>sort [OPTIONS] FILE</code> | |||
|- | |||
| [[#ex-uniq|<code>uniq</code>]] || Filter out repeated lines || <code>uniq [OPTIONS] FILE</code> | |||
|- | |||
| [[#ex-wc|<code>wc</code>]] || Count lines, words, bytes || <code>wc [OPTIONS] FILE</code> | |||
|- | |||
| [[#ex-nl|<code>nl</code>]] || Number lines of a file || <code>nl FILE</code> | |||
|- | |||
| [[#ex-diff|<code>diff</code>]] || Show differences between files || <code>diff FILE1 FILE2</code> | |||
|- | |||
| [[#ex-tee|<code>tee</code>]] || Read from stdin and write to file and stdout || <code>COMMAND | tee FILE</code> | |||
|} | |} | ||
=== | === System & User Info === | ||
{| class="wikitable" | {| class="wikitable sortable" | ||
! Command !! Description !! | ! Command !! Description !! Usage | ||
|- | |- | ||
| <code> | | [[#ex-whoami|<code>whoami</code>]] || Show current user || <code>whoami</code> | ||
|- | |- | ||
| <code> | | [[#ex-id|<code>id</code>]] || Display user/group IDs || <code>id [USERNAME]</code> | ||
|- | |- | ||
| <code> | | [[#ex-df|<code>df</code>]] || Disk usage overview || <code>df [OPTIONS]</code> | ||
|- | |- | ||
| <code> | | [[#ex-du|<code>du</code>]] || Show disk usage for files/directories || <code>du [OPTIONS] [PATH]</code> | ||
|- | |- | ||
| <code> | | [[#ex-uptime|<code>uptime</code>]] || Show how long the system has been running || <code>uptime</code> | ||
|- | |- | ||
| <code> | | [[#ex-uname|<code>uname</code>]] || System information (kernel, OS, architecture) || <code>uname -a</code> | ||
|- | |- | ||
| <code> | | [[#ex-hostname|<code>hostname</code>]] || Show or set system hostname || <code>hostname</code> | ||
|- | |- | ||
| <code> | | [[#ex-date|<code>date</code>]] || Display or set system date/time || <code>date</code> | ||
|- | |||
| [[#ex-cal|<code>cal</code>]] || Display a calendar || <code>cal</code> | |||
|- | |||
| [[#ex-env|<code>env</code>]] || Show environment variables || <code>env</code> | |||
|- | |||
| [[#ex-set|<code>set</code>]] || Display shell variables and functions || <code>set</code> | |||
|- | |||
| [[#ex-history|<code>history</code>]] || List previously executed commands || <code>history</code> | |||
|- | |||
| [[#ex-ps|<code>ps</code>]] || Display running processes || <code>ps aux</code> | |||
|- | |||
| [[#ex-top|<code>top</code>]] || Real-time system process monitor || <code>top</code> | |||
|- | |||
| [[#ex-kill|<code>kill</code>]] || Terminate process by PID || <code>kill [-9] PID</code> | |||
|- | |||
| [[#ex-pkill|<code>pkill</code>]] || Kill processes by name || <code>pkill PROCESS_NAME</code> | |||
|- | |||
| [[#ex-pgrep|<code>pgrep</code>]] || Search for processes by name || <code>pgrep PROCESS_NAME</code> | |||
|- | |||
| [[#ex-w|<code>w</code>]] || Show who is logged in and what they are doing || <code>w</code> | |||
|- | |||
| [[#ex-who|<code>who</code>]] || Show who is logged in || <code>who</code> | |||
|- | |||
| [[#ex-last|<code>last</code>]] || Show login history || <code>last</code> | |||
|- | |||
| [[#ex-free|<code>free</code>]] || Show memory usage || <code>free -h</code> | |||
|} | |} | ||
=== User | === User & Group Management === | ||
{| class="wikitable" | {| class="wikitable sortable" | ||
! Command !! Description !! | ! Command !! Description !! Usage | ||
|- | |||
| [[#ex-useradd|<code>useradd</code>]] || Add new user || <code>useradd [OPTIONS] USERNAME</code> | |||
|- | |||
| [[#ex-usermod|<code>usermod</code>]] || Modify existing user || <code>usermod [OPTIONS] USERNAME</code> | |||
|- | |||
| [[#ex-userdel|<code>userdel</code>]] || Remove user || <code>userdel [OPTIONS] USERNAME</code> | |||
|- | |||
| [[#ex-groupadd|<code>groupadd</code>]] || Create new group || <code>groupadd GROUP</code> | |||
|- | |- | ||
| <code> | | [[#ex-groupdel|<code>groupdel</code>]] || Delete group || <code>groupdel GROUP</code> | ||
|- | |- | ||
| <code> | | [[#ex-passwd|<code>passwd</code>]] || Set/modify user password || <code>passwd USERNAME</code> | ||
|} | |} | ||
=== Permissions === | === Security & Permissions === | ||
{| class="wikitable" | {| class="wikitable sortable" | ||
! Command !! Description !! | ! Command !! Description !! Usage | ||
|- | |- | ||
| <code> | | [[#ex-sudo|<code>sudo</code>]] || Execute command as another user (root by default) || <code>sudo COMMAND</code> | ||
|- | |||
| [[#ex-su|<code>su</code>]] || Switch user context || <code>su [USER]</code> | |||
|- | |||
| [[#ex-setfacl|<code>setfacl</code>]] || Set POSIX ACL on files/directories || <code>setfacl -m u:USER:rwx FILE</code> | |||
|- | |||
| [[#ex-getfacl|<code>getfacl</code>]] || View POSIX ACL || <code>getfacl FILE</code> | |||
|} | |} | ||
=== | === Process & Job Control === | ||
{| class="wikitable" | {| class="wikitable sortable" | ||
! Command !! Description !! | ! Command !! Description !! Usage | ||
|- | |||
| [[#ex-jobs|<code>jobs</code>]] || List background/paused jobs || <code>jobs -l</code> | |||
|- | |||
| [[#ex-bg|<code>bg</code>]] || Resume job in background || <code>bg %1</code> | |||
|- | |||
| [[#ex-fg|<code>fg</code>]] || Bring job to foreground || <code>fg %1</code> | |||
|- | |||
| [[#ex-nohup|<code>nohup</code>]] || Run command immune to hangup || <code>nohup COMMAND &</code> | |||
|- | |||
| [[#ex-disown|<code>disown</code>]] || Remove job from shell’s job table || <code>disown %1</code> | |||
|- | |||
| [[#ex-nice|<code>nice</code>]] || Start task with priority || <code>nice -n 10 COMMAND</code> | |||
|- | |- | ||
| <code> | | [[#ex-renice|<code>renice</code>]] || Change priority of running PID || <code>renice +5 PID</code> | ||
|} | |} | ||
== | === System Monitoring & Performance === | ||
{| class="wikitable sortable" | |||
! Command !! Description !! Usage | |||
|- | |||
| [[#ex-htop|<code>htop</code>]] || Interactive process viewer || <code>htop</code> | |||
|- | |||
| [[#ex-vmstat|<code>vmstat</code>]] || Report virtual memory stats || <code>vmstat 1</code> | |||
|- | |||
| [[#ex-iostat|<code>iostat</code>]] || CPU & I/O statistics || <code>iostat -xz 1</code> | |||
|- | |||
| [[#ex-iotop|<code>iotop</code>]] || Real‑time I/O usage || <code>iotop</code> | |||
|- | |||
| [[#ex-dstat|<code>dstat</code>]] || Versatile resource stats || <code>dstat -tcm --top-io</code> | |||
|} | |||
=== | === Disk & Filesystem Management === | ||
{| class="wikitable" | {| class="wikitable sortable" | ||
! Command !! Description !! | ! Command !! Description !! Usage | ||
|- | |||
| [[#ex-lsblk|<code>lsblk</code>]] || List block devices || <code>lsblk -f</code> | |||
|- | |||
| [[#ex-blkid|<code>blkid</code>]] || Locate/print block device attributes || <code>blkid DEVICE</code> | |||
|- | |||
| [[#ex-fdisk|<code>fdisk</code>]] || Partition table editor (MBR) || <code>fdisk /dev/sda</code> | |||
|- | |- | ||
| <code> | | [[#ex-parted|<code>parted</code>]] || Partition tool (GPT/MBR) || <code>parted /dev/sda</code> | ||
|- | |- | ||
| <code> | | [[#ex-mkfs|<code>mkfs</code>]] || Create filesystem on device || <code>mkfs.ext4 /dev/sda1</code> | ||
|- | |- | ||
| <code> | | [[#ex-fsck|<code>fsck</code>]] || Check/repair filesystem || <code>fsck -f /dev/sda1</code> | ||
|- | |||
| [[#ex-tune2fs|<code>tune2fs</code>]] || Adjust ext2/3/4 filesystem params || <code>tune2fs -l /dev/sda1</code> | |||
|} | |} | ||
=== | === Networking === | ||
{| class="wikitable" | {| class="wikitable sortable" | ||
! Command !! Description !! | ! Command !! Description !! Usage | ||
|- | |- | ||
| <code> | | [[#ex-ping|<code>ping</code>]] || Test network connectivity (ICMP) || <code>ping [OPTIONS] DESTINATION</code> | ||
|- | |||
| [[#ex-curl|<code>curl</code>]] || Retrieve data from URLs / APIs || <code>curl [OPTIONS] URL</code> | |||
|- | |||
| [[#ex-ssh|<code>ssh</code>]] || Secure remote shell & tunnelling || <code>ssh [OPTIONS] USER@HOST</code> | |||
|} | |} | ||
== | === Package & Software Management === | ||
{| class="wikitable sortable" | |||
! Command !! Description !! Usage | |||
|- | |||
| [[#ex-apt|<code>apt</code>]] || Debian/Ubuntu package tool || <code>apt update && apt upgrade</code> | |||
|- | |||
| [[#ex-dnf|<code>dnf</code>]] || Fedora/RHEL package manager || <code>dnf install PACKAGE</code> | |||
|- | |||
| [[#ex-yum|<code>yum</code>]] || Legacy RHEL/CentOS package tool || <code>yum remove PACKAGE</code> | |||
|- | |||
| [[#ex-pacman|<code>pacman</code>]] || Arch Linux package manager || <code>pacman -Syu</code> | |||
|- | |||
| [[#ex-dpkg|<code>dpkg</code>]] || Debian low‑level package tool || <code>dpkg -i PACKAGE.deb</code> | |||
|- | |||
| [[#ex-rpm|<code>rpm</code>]] || RPM low‑level package tool || <code>rpm -q PACKAGE</code> | |||
|- | |||
| [[#ex-snap|<code>snap</code>]] || Universal package system || <code>snap install APP</code> | |||
|- | |||
| [[#ex-flatpak|<code>flatpak</code>]] || Sandboxed app manager || <code>flatpak install REMOTE APP</code> | |||
|} | |||
=== | === Scripting & Automation === | ||
{| class="wikitable" | {| class="wikitable sortable" | ||
! Command !! Description !! | ! Command !! Description !! Usage | ||
|- | |||
| [[#ex-bash|<code>bash</code>]] || GNU Bourne‑Again Shell || <code>#!/usr/bin/env bash</code> | |||
|- | |- | ||
| <code> | | [[#ex-sh|<code>sh</code>]] || POSIX shell || <code>sh SCRIPT.sh</code> | ||
|- | |- | ||
| <code> | | [[#ex-crontab|<code>crontab</code>]] || Schedule recurring jobs || <code>crontab -e</code> | ||
|- | |- | ||
| <code> | | [[#ex-at|<code>at</code>]] || Schedule one‑time tasks || <code>echo "script.sh" | at 02:00</code> | ||
|- | |||
| [[#ex-alias|<code>alias</code>]] || Create command shortcuts || <code>alias ll='ls -alh'</code> | |||
|- | |||
| [[#ex-export|<code>export</code>]] || Set environment variables || <code>export PATH=$PATH:/custom/bin</code> | |||
|} | |||
=== Help & Documentation === | |||
{| class="wikitable sortable" | |||
! Command !! Description !! Usage | |||
|- | |||
| [[#ex-man|<code>man</code>]] || Read manual pages || <code>man COMMAND</code> | |||
|- | |||
| [[#ex-echo|<code>echo</code>]] || Display text / variables || <code>echo [STRING]</code> | |||
|} | |||
|} | |} | ||
== Examples == | == Examples == | ||
=== <span id="ex- | === <span id="ex-pwd"></span>Print Working Directory === | ||
<syntaxhighlight lang="bash"> | |||
pwd | |||
# /home/user | |||
</syntaxhighlight> | |||
''[[#commands|↑ Commands]]'' | |||
=== <span id="ex-cd"></span>Change Directory === | |||
<syntaxhighlight lang="bash"> | |||
cd /var/www | |||
# (no output) | |||
</syntaxhighlight> | |||
''[[#commands|↑ Commands]]'' | |||
=== <span id="ex-ls"></span>List All Files (long + hidden) === | |||
<syntaxhighlight lang="bash"> | |||
ls -la | |||
# total 12 | |||
# drwxr-xr-x 3 user user 4096 May 16 22:15 . | |||
# drwxr-xr-x 18 user user 4096 May 16 21:00 .. | |||
# -rw-r--r-- 1 user user 0 May 16 22:15 index.html | |||
</syntaxhighlight> | |||
''[[#commands|↑ Commands]]'' | |||
=== <span id="ex-mkdir"></span>Create Directory === | |||
<syntaxhighlight lang="bash"> | |||
mkdir backups | |||
# (no output) | |||
</syntaxhighlight> | |||
''[[#commands|↑ Commands]]'' | |||
=== <span id="ex-rmdir"></span>Remove Empty Directory === | |||
<syntaxhighlight lang="bash"> | |||
rmdir backups | |||
# (no output) | |||
</syntaxhighlight> | |||
''[[#commands|↑ Commands]]'' | |||
=== <span id="ex-cp"></span>Copy File === | |||
<syntaxhighlight lang="bash"> | |||
cp secrets.txt /tmp/secrets.bak | |||
# (no output) | |||
</syntaxhighlight> | |||
''[[#commands|↑ Commands]]'' | |||
=== <span id="ex-mv"></span>Move & Rename === | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
mv old.log archive/old.log | |||
# (no output) | |||
</syntaxhighlight> | |||
''[[#commands|↑ Commands]]'' | |||
=== <span id="ex-rm"></span>Force-Delete Directory === | |||
<syntaxhighlight lang="bash"> | |||
rm -rf /tmp/testdir | |||
# (no output) | |||
</syntaxhighlight> | |||
''[[#commands|↑ Commands]]'' | |||
=== <span id="ex-cat"></span>Show File Contents === | |||
<syntaxhighlight lang="bash"> | |||
cat /etc/passwd | grep ":/bin/bash" | |||
# root:x:0:0:root:/root:/bin/bash | |||
# user:x:1000:1000:User,,,:/home/user:/bin/bash | |||
</syntaxhighlight> | |||
''[[#commands|↑ Commands]]'' | |||
=== <span id="ex-less"></span>View Large Log === | |||
<syntaxhighlight lang="bash"> | |||
less /var/log/auth.log | |||
# (opens file in pager; press q to quit) | |||
</syntaxhighlight> | |||
''[[#commands|↑ Commands]]'' | |||
=== <span id="ex-head"></span>First 10 Lines === | |||
<syntaxhighlight lang="bash"> | |||
head -n 10 notes.txt | |||
# Line 1 | |||
# Line 2 | |||
# Line 3 | |||
# Line 4 | |||
# Line 5 | |||
# Line 6 | |||
# Line 7 | |||
# Line 8 | |||
# Line 9 | |||
# Line 10 | |||
</syntaxhighlight> | |||
''[[#commands|↑ Commands]]'' | |||
=== <span id="ex-tail"></span>Follow File Growth === | |||
<syntaxhighlight lang="bash"> | |||
tail -f /var/log/nginx/access.log | |||
# 192.168.1.5 - - [16/May/2025:22:30:01 +0200] "GET / HTTP/1.1" 200 1234 | |||
# 192.168.1.5 - - [16/May/2025:22:30:02 +0200] "GET /favicon.ico HTTP/1.1" 404 564 | |||
</syntaxhighlight> | |||
''[[#commands|↑ Commands]]'' | |||
=== <span id="ex-grep"></span>Search Pattern === | |||
<syntaxhighlight lang="bash"> | |||
grep -R "passwd" /etc | |||
# /etc/login.defs:PASS_MAX_DAYS 99999 | |||
# /etc/pam.d/common-password:password requisite pam_pwquality.so retry=3 | |||
</syntaxhighlight> | |||
''[[#commands|↑ Commands]]'' | |||
=== <span id="ex-whoami"></span>Current User === | |||
<syntaxhighlight lang="bash"> | |||
whoami | |||
# user | |||
</syntaxhighlight> | |||
''[[#commands|↑ Commands]]'' | |||
=== <span id="ex-id"></span>User & Group IDs === | |||
<syntaxhighlight lang="bash"> | |||
id | |||
# uid=1000(user) gid=1000(user) groups=1000(user),27(sudo) | |||
</syntaxhighlight> | |||
''[[#commands|↑ Commands]]'' | |||
=== <span id="ex-df"></span>Human-Readable Disk Usage === | |||
<syntaxhighlight lang="bash"> | |||
df -h | |||
# Filesystem Size Used Avail Use% Mounted on | |||
# /dev/sda1 30G 12G 17G 42% / | |||
</syntaxhighlight> | |||
''[[#commands|↑ Commands]]'' | |||
=== <span id="ex-history"></span>Show Last 20 Commands === | |||
<syntaxhighlight lang="bash"> | |||
history | tail -n 20 | |||
# 981 ls | |||
# 982 cd /var/www | |||
# 983 vim index.html | |||
# ... | |||
# 1000 exit | |||
</syntaxhighlight> | |||
''[[#commands|↑ Commands]]'' | |||
=== <span id="ex-ping"></span>Ping Host 4 Times === | |||
<syntaxhighlight lang="bash"> | |||
ping -c 4 example.com | |||
# PING example.com (93.184.216.34) 56(84) bytes of data. | |||
# 64 bytes from 93.184.216.34: icmp_seq=1 ttl=56 time=12.3 ms | |||
# 64 bytes from 93.184.216.34: icmp_seq=2 ttl=56 time=12.1 ms | |||
# 64 bytes from 93.184.216.34: icmp_seq=3 ttl=56 time=12.0 ms | |||
# 64 bytes from 93.184.216.34: icmp_seq=4 ttl=56 time=12.2 ms | |||
# --- example.com ping statistics --- | |||
# 4 packets transmitted, 4 received, 0% packet loss, time 3005ms | |||
# rtt min/avg/max/mdev = 12.012/12.173/12.325/0.123 ms | |||
</syntaxhighlight> | |||
''[[#commands|↑ Commands]]'' | |||
=== <span id="ex-curl"></span>Download Web Page === | |||
<syntaxhighlight lang="bash"> | |||
curl -o index.html http://example.com | |||
# % Total % Received % Xferd Average Speed Time Time Time Current | |||
# Dload Upload Total Spent Left Speed | |||
# 100 1270 100 1270 0 0 2500 0 --:--:-- --:--:-- --:--:-- 2500 | |||
</syntaxhighlight> | </syntaxhighlight> | ||
''[[#commands|↑ Commands]]'' | |||
=== <span id="ex- | === <span id="ex-ssh"></span>SSH on Custom Port === | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
ssh -p 2222 user@target | |||
# The authenticity of host 'target (192.168.1.50)' can't be established. | |||
# ED25519 key fingerprint is SHA256:abc123... | |||
# Are you sure you want to continue connecting (yes/no/[fingerprint])? | |||
</syntaxhighlight> | </syntaxhighlight> | ||
''[[#commands|↑ Commands]]'' | |||
=== <span id="ex- | === <span id="ex-man"></span>Read Manual Page === | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
man ssh | |||
# (opens manual page; press q to quit) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
''[[#commands|↑ Commands]]'' | |||
=== <span id="ex- | === <span id="ex-echo"></span>Write to File === | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
echo "hacked" > /tmp/proof.txt | |||
# (no output) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
''[[#commands|↑ Commands]]'' | |||
== | == See Also == | ||
* [[ | * [[Bash Scripting Basics]] | ||
* [[Privilege Escalation]] | |||
* [[Netcat]] |
Latest revision as of 21:33, 16 May 2025
Linux Commands
Linux command‑line tools are the backbone of reconnaissance, exploitation, and post‑exploitation on Unix‑like systems.
Understanding Linux commands provides direct access to system internals, automation, and control. Many security tools and scripts rely on command-line operations for efficiency and precision. In ethical hacking, the ability to navigate filesystems, manage processes, inspect network configurations, and manipulate data through the terminal creates a strong foundation for tasks such as enumeration, privilege escalation, and persistence.
Proficiency with core commands accelerates workflow, reduces reliance on graphical interfaces, and improves situational awareness during engagements. Whether working locally or through remote shells, command-line skills increase control and adaptability across diverse environments.
Common Commands
Command | Description | Usage |
---|---|---|
pwd |
Print working directory | pwd
|
cd |
Change directory | cd [DIRECTORY]
|
ls |
List directory contents | ls [OPTIONS] [FILE...]
|
tree |
Recursively list directories as tree | tree [DIRECTORY]
|
mkdir |
Create directory | mkdir [OPTIONS] DIRECTORY
|
rmdir |
Remove empty directory | rmdir DIRECTORY
|
cp |
Copy files or directories | cp SOURCE DEST
|
mv |
Move or rename files/directories | mv SOURCE DEST
|
rm |
Delete files or directories | rm [OPTIONS] FILE...
|
touch |
Create empty file or update timestamp | touch FILENAME
|
ln |
Create hard or symbolic links | ln [OPTIONS] TARGET LINK_NAME
|
stat |
Display detailed file information | stat FILE
|
file |
Detect file type | file FILE
|
basename |
Strip directory and suffix from filename | basename PATH
|
dirname |
Extract directory part of file path | dirname PATH
|
find |
Search for files and directories | find [PATH] [OPTIONS] [EXPRESSION]
|
locate |
Find files using database (faster than find) | locate FILENAME
|
chmod |
Change file permissions | chmod [OPTIONS] MODE FILE
|
chown |
Change file ownership (user) | chown [OPTIONS] USER FILE
|
chgrp |
Change group ownership | chgrp [OPTIONS] GROUP FILE
|
umask |
Set default file permission mask | umask [MASK]
|
tar |
Archive files into tarball | tar -czf ARCHIVE.tar.gz FILES
|
gzip |
Compress files using Gzip | gzip FILE
|
gunzip |
Decompress Gzip files | gunzip FILE.gz
|
zip |
Compress files into ZIP archive | zip ARCHIVE.zip FILES
|
unzip |
Extract ZIP archive | unzip ARCHIVE.zip
|
dd |
Copy and convert data (low-level) | dd if=SOURCE of=DEST bs=BLOCKSIZE
|
mount |
Mount a filesystem | mount [DEVICE] [MOUNTPOINT]
|
umount |
Unmount a filesystem | umount [MOUNTPOINT]
|
namei |
Follow a file path, showing each component | namei PATH
|
readlink |
Resolve symbolic link to actual path | readlink FILE
|
realpath |
Print the resolved absolute file path | realpath FILE
|
mktemp |
Create a temporary file or directory | mktemp [TEMPLATE]
|
Viewing & Text Processing
Command | Description | Usage |
---|---|---|
cat |
Concatenate & display files | cat [OPTIONS] FILE...
|
less |
Paginate large files | less FILE
|
more |
View file one page at a time | more FILE
|
head |
Show first lines of a file | head [OPTIONS] FILE
|
tail |
Show last lines / follow file | tail [OPTIONS] FILE
|
grep |
Search for patterns | grep [OPTIONS] PATTERN FILE
|
awk |
Pattern scanning & processing language | awk 'pattern { action }' FILE
|
sed |
Stream editor for filtering & transforming text | sed [OPTIONS] 'SCRIPT' FILE
|
cut |
Remove sections from each line | cut [OPTIONS] FILE
|
tr |
Translate or delete characters | tr SET1 SET2
|
sort |
Sort lines of text files | sort [OPTIONS] FILE
|
uniq |
Filter out repeated lines | uniq [OPTIONS] FILE
|
wc |
Count lines, words, bytes | wc [OPTIONS] FILE
|
nl |
Number lines of a file | nl FILE
|
diff |
Show differences between files | diff FILE1 FILE2
|
tee |
Read from stdin and write to file and stdout | tee FILE |
System & User Info
Command | Description | Usage |
---|---|---|
whoami |
Show current user | whoami
|
id |
Display user/group IDs | id [USERNAME]
|
df |
Disk usage overview | df [OPTIONS]
|
du |
Show disk usage for files/directories | du [OPTIONS] [PATH]
|
uptime |
Show how long the system has been running | uptime
|
uname |
System information (kernel, OS, architecture) | uname -a
|
hostname |
Show or set system hostname | hostname
|
date |
Display or set system date/time | date
|
cal |
Display a calendar | cal
|
env |
Show environment variables | env
|
set |
Display shell variables and functions | set
|
history |
List previously executed commands | history
|
ps |
Display running processes | ps aux
|
top |
Real-time system process monitor | top
|
kill |
Terminate process by PID | kill [-9] PID
|
pkill |
Kill processes by name | pkill PROCESS_NAME
|
pgrep |
Search for processes by name | pgrep PROCESS_NAME
|
w |
Show who is logged in and what they are doing | w
|
who |
Show who is logged in | who
|
last |
Show login history | last
|
free |
Show memory usage | free -h
|
User & Group Management
Command | Description | Usage |
---|---|---|
useradd |
Add new user | useradd [OPTIONS] USERNAME
|
usermod |
Modify existing user | usermod [OPTIONS] USERNAME
|
userdel |
Remove user | userdel [OPTIONS] USERNAME
|
groupadd |
Create new group | groupadd GROUP
|
groupdel |
Delete group | groupdel GROUP
|
passwd |
Set/modify user password | passwd USERNAME
|
Security & Permissions
Command | Description | Usage |
---|---|---|
sudo |
Execute command as another user (root by default) | sudo COMMAND
|
su |
Switch user context | su [USER]
|
setfacl |
Set POSIX ACL on files/directories | setfacl -m u:USER:rwx FILE
|
getfacl |
View POSIX ACL | getfacl FILE
|
Process & Job Control
Command | Description | Usage |
---|---|---|
jobs |
List background/paused jobs | jobs -l
|
bg |
Resume job in background | bg %1
|
fg |
Bring job to foreground | fg %1
|
nohup |
Run command immune to hangup | nohup COMMAND &
|
disown |
Remove job from shell’s job table | disown %1
|
nice |
Start task with priority | nice -n 10 COMMAND
|
renice |
Change priority of running PID | renice +5 PID
|
System Monitoring & Performance
Command | Description | Usage |
---|---|---|
htop |
Interactive process viewer | htop
|
vmstat |
Report virtual memory stats | vmstat 1
|
iostat |
CPU & I/O statistics | iostat -xz 1
|
iotop |
Real‑time I/O usage | iotop
|
dstat |
Versatile resource stats | dstat -tcm --top-io
|
Disk & Filesystem Management
Command | Description | Usage |
---|---|---|
lsblk |
List block devices | lsblk -f
|
blkid |
Locate/print block device attributes | blkid DEVICE
|
fdisk |
Partition table editor (MBR) | fdisk /dev/sda
|
parted |
Partition tool (GPT/MBR) | parted /dev/sda
|
mkfs |
Create filesystem on device | mkfs.ext4 /dev/sda1
|
fsck |
Check/repair filesystem | fsck -f /dev/sda1
|
tune2fs |
Adjust ext2/3/4 filesystem params | tune2fs -l /dev/sda1
|
Networking
Command | Description | Usage |
---|---|---|
ping |
Test network connectivity (ICMP) | ping [OPTIONS] DESTINATION
|
curl |
Retrieve data from URLs / APIs | curl [OPTIONS] URL
|
ssh |
Secure remote shell & tunnelling | ssh [OPTIONS] USER@HOST
|
Package & Software Management
Command | Description | Usage |
---|---|---|
apt |
Debian/Ubuntu package tool | apt update && apt upgrade
|
dnf |
Fedora/RHEL package manager | dnf install PACKAGE
|
yum |
Legacy RHEL/CentOS package tool | yum remove PACKAGE
|
pacman |
Arch Linux package manager | pacman -Syu
|
dpkg |
Debian low‑level package tool | dpkg -i PACKAGE.deb
|
rpm |
RPM low‑level package tool | rpm -q PACKAGE
|
snap |
Universal package system | snap install APP
|
flatpak |
Sandboxed app manager | flatpak install REMOTE APP
|
Scripting & Automation
Command | Description | Usage |
---|---|---|
bash |
GNU Bourne‑Again Shell | #!/usr/bin/env bash
|
sh |
POSIX shell | sh SCRIPT.sh
|
crontab |
Schedule recurring jobs | crontab -e
|
at |
Schedule one‑time tasks | at 02:00 |
alias |
Create command shortcuts | alias ll='ls -alh'
|
export |
Set environment variables | export PATH=$PATH:/custom/bin
|
Help & Documentation
Command | Description | Usage |
---|---|---|
man |
Read manual pages | man COMMAND
|
echo |
Display text / variables | echo [STRING]
|
|}
Examples
Print Working Directory
pwd
# /home/user
Change Directory
cd /var/www
# (no output)
ls -la
# total 12
# drwxr-xr-x 3 user user 4096 May 16 22:15 .
# drwxr-xr-x 18 user user 4096 May 16 21:00 ..
# -rw-r--r-- 1 user user 0 May 16 22:15 index.html
Create Directory
mkdir backups
# (no output)
Remove Empty Directory
rmdir backups
# (no output)
Copy File
cp secrets.txt /tmp/secrets.bak
# (no output)
Move & Rename
mv old.log archive/old.log
# (no output)
Force-Delete Directory
rm -rf /tmp/testdir
# (no output)
Show File Contents
cat /etc/passwd | grep ":/bin/bash"
# root:x:0:0:root:/root:/bin/bash
# user:x:1000:1000:User,,,:/home/user:/bin/bash
View Large Log
less /var/log/auth.log
# (opens file in pager; press q to quit)
First 10 Lines
head -n 10 notes.txt
# Line 1
# Line 2
# Line 3
# Line 4
# Line 5
# Line 6
# Line 7
# Line 8
# Line 9
# Line 10
Follow File Growth
tail -f /var/log/nginx/access.log
# 192.168.1.5 - - [16/May/2025:22:30:01 +0200] "GET / HTTP/1.1" 200 1234
# 192.168.1.5 - - [16/May/2025:22:30:02 +0200] "GET /favicon.ico HTTP/1.1" 404 564
Search Pattern
grep -R "passwd" /etc
# /etc/login.defs:PASS_MAX_DAYS 99999
# /etc/pam.d/common-password:password requisite pam_pwquality.so retry=3
Current User
whoami
# user
User & Group IDs
id
# uid=1000(user) gid=1000(user) groups=1000(user),27(sudo)
Human-Readable Disk Usage
df -h
# Filesystem Size Used Avail Use% Mounted on
# /dev/sda1 30G 12G 17G 42% /
Show Last 20 Commands
history | tail -n 20
# 981 ls
# 982 cd /var/www
# 983 vim index.html
# ...
# 1000 exit
Ping Host 4 Times
ping -c 4 example.com
# PING example.com (93.184.216.34) 56(84) bytes of data.
# 64 bytes from 93.184.216.34: icmp_seq=1 ttl=56 time=12.3 ms
# 64 bytes from 93.184.216.34: icmp_seq=2 ttl=56 time=12.1 ms
# 64 bytes from 93.184.216.34: icmp_seq=3 ttl=56 time=12.0 ms
# 64 bytes from 93.184.216.34: icmp_seq=4 ttl=56 time=12.2 ms
# --- example.com ping statistics ---
# 4 packets transmitted, 4 received, 0% packet loss, time 3005ms
# rtt min/avg/max/mdev = 12.012/12.173/12.325/0.123 ms
Download Web Page
curl -o index.html http://example.com
# % Total % Received % Xferd Average Speed Time Time Time Current
# Dload Upload Total Spent Left Speed
# 100 1270 100 1270 0 0 2500 0 --:--:-- --:--:-- --:--:-- 2500
SSH on Custom Port
ssh -p 2222 user@target
# The authenticity of host 'target (192.168.1.50)' can't be established.
# ED25519 key fingerprint is SHA256:abc123...
# Are you sure you want to continue connecting (yes/no/[fingerprint])?
Read Manual Page
man ssh
# (opens manual page; press q to quit)
Write to File
echo "hacked" > /tmp/proof.txt
# (no output)