Linux

Basic commands

System Commands

File & Directory Management

List all files + details
ls -a
List with details
ls -l
Read file
cat file.txt
Look for a given word inside a file
cat file.txt | grep word 
Count number of lines in a file
wc -l myfile.txt
Print Working Directory
pwd
Print full path of a file/directory
realpath file.txt
Change Directory
cd /example/example
Move up one directory level
cd ..
Copy files
cp file.txt /home/user/
Copy recursively
cp -r dir/ /home/user/
Rename a file
mv oldname.txt newname.txt
Move a file
mv file.txt /home/user/ 
Find all .txt files
find /home/user -name "*.txt"
Find files larger than 10MB
find / -type f -size +10M
Search the entire filesystem for any match with the regex pattern
sudo find / -regex ".*alacritty.*" 2>/dev/null
Updates the database used by locate
sudo updatedb
Look file from the database
locate file.txt
Shows the full path of an executable
which binary
Creates a directory
mkdir newdir
Creates a new file/Update timestamps
touch newfile
Delete file
rm file.txt
Delete directory
rm -r directory/

Process Management

List all process running in the system
ps aux
Detailed list of all processes
ps -ef
Show a detailed process tree with wide output
ps -auxwf
Show process tree for a specific user
pstree user
Show process IDs (PIDs) with the tree view
pstree -p
Show real-time process stats
htop
Kill a process
sudo kill -9 <PID>

System Management

Check the status of a service
sudo systemctl status nginx
Start a service
sudo systemctl start nginx
Enable a service to start on boot
sudo systemctl enable nginx
Print Info about the system
uname -a 
Print Environment
env
Shows details about all block devices
lsblk -f
List PCI devices
lspci
Display Disk Usage
df -h
Show the size of files and directories in the current directory
du -sh *
Shows the 40 largest directories on the system
sudo du -hsx /* | sort -rh | head -n 40
Display RAM info
free -h
Displays the maximum allowed size of a process's lockable memory
ulimit -l
Put a process in the background
bg
List all process in the background
jobs
Puts a process into the foreground
fg
Display overall system performance stats
vmstat
Print stats every 5 seconds
vmstat 5
Print stats every 5 seconds for 10 iterations
vmstat 5 10
Show the system uptime
uptime
Show info about current login user
w

Logs

Display login records
last
Display bad login attempts
lastb
Shows log as they are written
journalctl -f 
Shows logs from the current boot
journalctl -b
Shows logs from the previous boot
journalctl -b -1
Shows logs from a specific service
journalctl -u <service>
Shows logs from a time range
journalctl --since "2024-11-01" --until "2024-11-10"
Show logs by priority level
journalctl -p <level>

Priority Levels:

  • 0 (emergency)

  • 1 (alert)

  • 2 (critical)

  • 3 (error)

  • 4 (warning)

  • 5 (notice)

  • 6 (informational)

  • 7 (debug)

Shows logs for a specific process
journalctl _PID=<pid>
Shows logs from the kernel
journalctl -k
Show kernel and boot messages
dmesg

User/Group Management

Users

Print current user name
whoami
Display user and group ID of the current user
id 
id from a specific user
id username
Edit sudoers file
sudo visudo
Switch to the root user
su
Switch to another user
su - username

User Management

Creates a new user
sudo useradd newuser
Creates a new user with home directory and bash shell
sudo useradd -m -s /bin/bash newuser
Add user to the sudo group
sudo usermod -aG sudo newuser
Set or change password
sudo passwd newuser
Set zsh as default shell
sudo usermod --shell /usr/bin/zsh alice

Groups

Create group
sudo groupadd newgroup
Delete Group
sudo groupdel group
Add user to group
sudo gpasswd -a newuser newgroup

Permission & Security

File Ownership

Set file permissions to rwxr-xr-x
sudo chmod 755 file.txt
Adds read permissions for all users
sudo chmod a+r shell.sh
Add execution permission
sudo chmod +x script.sh
Change file owner and group
sudo chown user:group file.txt
Change group ownership
sudo chgrp group file.txt
Prevent world-writable files
umask 022 

Access Control Lists

Show the ACL
getfacl file.txt
Grant read/write permissions to 'user' on file.txt
setfacl -m u:user:rw file.txt

Security Policies

Display the current SELinux mode
getenforce
Disable SELinux
sudo setenforce 0

File Integrity

Generates a SHA256 file
sha256sum file.txt
Generates a MD5 hash
md5sum file.txt
Initialize the Tripwire database
sudo tripwire --init
Check the integrity of the system
sudo tripwire --check
Generate a report
sudo tripwire --update

Networking Command

Display network interfaces and their IP addresses
ifconfig
Display IP addresses of all network interfaces
ip a 
Bring interface up
sudo ip link set eth0 up
Show networks accessible via the VPN
netstat -rn
Show listening TCP/UDP ports
netstat -tuln
Show listening ports and services
ss -tuln
Display the Routing Table
ip route show
Add a route
sudo ip route add 192.168.2.0/24 via 192.168.1.254
Delete a Route
sudo ip route del 192.168.2.0/24
Add a Default Gateway
sudo ip route add default via 192.168.1.1
Change a route's metric
sudo route change -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.254 metric 200
Trace the network path to example.com
traceroute example.com

Chroot

Decrypt the system

Decrypt partition
sudo cryptsetup luksOpen /dev/sda3 cryptdisk
Mounts the decrypted device
sudo mount /dev/mapper/cryptdisk /mnt
Mounts the boot partion
sudo mount /dev/sda1 /mnt/boot
chroot into a mounted partition
sudo arch-chroot /mnt

Connect using WPA

Shows the current connection status for the wpa
wpa_cli status
Creates the passphrase
sudo wpa_passphrase "MyWiFi" "mypassword123" | sudo tee /etc/wpa_supplicant/wpa_supplicant.conf
Connect using passphrase
sudo wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf

Boot/UEFI Management

Displays the current boot entries in the UEFI firmware
efibootmgr
deletes the boot entry "0"
sudo efibootmgr --delete-bootnum --bootnum 0

Last updated