Top 50 Linux Commands You Must Know as a Regular User
- ls – The most frequently used command in Linux to list directories
- pwd – Print working directory command in Linux
- cd – Linux command to navigate through directories
- mkdir – Command used to create directories in Linux
- mv – Move or rename files in Linux
- cp – Similar usage as mv but for copying files in Linux
- rm – Delete files or directories
- touch – Create blank/empty files
- ln – Create symbolic links (shortcuts) to other files
- clear – Clear the terminal display
- cat – Display file contents on the terminal
- echo – Print any text that follows the command
- less – Linux command to display paged outputs in the terminal
- man – Access manual pages for all Linux commands
- uname – Linux command to get basic information about the OS
- whoami – Get the active username
- tar – Command to extract and compress files in linux
- grep – Search for a string within an output
- head – Return the specified number of lines from the top
- tail – Return the specified number of lines from the bottom
- diff – Find the difference between two files
- cmp – Allows you to check if two files are identical
- comm – Combines the functionality of diff and cmp
- sort – Linux command to sort the content of a file while outputting
- export – Export environment variables in Linux
- zip – Zip files in Linux
- unzip – Unzip files in Linux
- ssh – Secure Shell command in Linux
- service – Linux command to start and stop services
- ps – Display active processes
- kill and killall – Kill active processes by process ID or name
- df – Display disk filesystem information
- mount – Mount file systems in Linux
- chmod – Command to change file permissions
- chown – Command for granting ownership of files or folders
- ifconfig – Display network interfaces and IP addresses
- traceroute – Trace all the network hops to reach the destination
- wget – Direct download files from the internet
- ufw – Firewall command
- iptables – Base firewall for all other firewall utilities to interface with
- apt, pacman, yum, rpm – Package managers depending on the distribution
- sudo – Command to escalate privileges in Linux
- cal – View a command-line calendar
- alias – Create custom shortcuts for your regularly used commands
- dd – Majorly used for creating bootable USB sticks
- whereis – Locate the binary, source, and manual pages for a command
- whatis – Find what a command is used for
- top – View active processes live with their system usage
- useradd and usermod – Add a new user or change existing user data
- passwd – Create or update passwords for existing user
File and Directory Commands
| Command | Description | Example |
|---|---|---|
| ls | List directory contents. | ls |
| cd | Change directory. | cd /path/to/directory |
| pwd | Show current directory. | pwd |
| mkdir | Create a new directory. | mkdir new_directory |
| rmdir | Remove an empty directory. | rmdir empty_directory |
| rm | Delete files or directories. | rm file.txt |
| touch | Create an empty file. | touch new_file.txt |
| cp | Copy files or directories. | cp file.txt /path/to/destination |
| mv | Move or rename files. | mv file.txt /path/to/new_location |
| cat | Display file contents. | cat file.txt |
| nano / vim | Edit files in terminal. | nano file.txt |
| find | Search for files in a directory hierarchy. | find . -name "file.txt" |
| grep | Search text using patterns. | grep "pattern" file.txt |
| tar | Archive and compress files. | tar -cvf archive.tar file1.txt file2.txt |
| df | Show disk usage of file systems. | df |
| du | Show directory/file size. | du -sh /path/to/directory |
| chmod | Change file permissions. | chmod 755 file.txt |
| chown | Change file owner. | chown user:group file.txt |
| mount | Mount a filesystem. | mount /dev/sdb1 /mnt |
| umount | Unmount a filesystem. | umount /mnt |
1. ls – List directory contents
................................
ls → Lists files in the current directory
ls -l → Long listing format (includes permissions, owner, size, date)
ls -a → Shows hidden files (those starting with .)
2. cd – Change directory
.........................
cd /path/to/directory → Go to specific directory
cd .. → Move up one directory
cd ~ or just cd → Go to home directory
3. pwd – Print working directory
.................................
Shows the full path of your current directory
Example: /home/username/Documents
4. mkdir – Make new directory
.............................
mkdir newfolder → Creates a folder named newfolder
mkdir -p folder1/folder2 → Creates nested folders
5. rmdir – Remove an empty directory
....................................
rmdir myfolder → Removes the folder only if it's empty
6. rm – Remove files or directories
....................................
rm file.txt → Deletes a file
rm -r folder/ → Deletes a folder and all contents recursively
rm -rf folder/ → Same as above, but forces deletion without asking (⚠️ dangerous!)
7. cp – Copy files and directories
..................................
cp file1.txt file2.txt → Copies file1.txt to file2.txt
cp -r dir1/ dir2/ → Copies an entire directory
8. mv – Move or rename files and directories
............................................
mv file.txt /new/location/ → Moves file
mv oldname.txt newname.txt → Renames file
9. touch – Create an empty file
................................
touch newfile.txt → Creates a blank file
(Also updates timestamp if the file already exists)
10. find – Search for files
...........................
find / -name filename.txt → Searches for a file starting from root
find . -name "*.txt" → Finds all .txt files in current directory and subdirectories
11. cat – View contents of a file
.................................
cat file.txt → Displays file contents
Combine with > to create files: cat > newfile.txt
12. less or more – View long files one page at a time
.....................................................
less file.txt → Scroll up/down with arrow keys
more file.txt → Similar, but less flexible than less
13. stat – Show file or directory status
........................................
stat file.txt → Gives detailed info (size, permissions, timestamps)
Networking Commands
| Command | Description | Sample Usage |
|---|---|---|
| ping | Test connectivity to a host. | ping google.com |
| ifconfig / ip a | Display network interfaces. | ifconfig or ip a |
| netstat / ss | Show network connections. | netstat -tuln or ss -tuln |
| wget | Download files via HTTP/FTP. | wget http://example.com/file.zip |
| curl | Transfer data using URL syntax. | curl -O http://example.com/file.zip |
| nc (Netcat) | Network debugging and data transfer. | nc -zv 192.168.1.1 80 |
| tcpdump | Capture and analyze network packets. | tcpdump -i eth0 |
| iptables | Configure firewall rules. | iptables -A INPUT -p tcp --dport 22 -j ACCEPT |
| traceroute | Trace the path packets take to a network host. | traceroute example.com |
| nslookup | Query DNS to obtain domain name or IP address mapping. | nslookup example.com |
| ssh | Securely connect to a remote host. | ssh user@example.co |
Basic Networking Commands
| Command | Description | Example |
|---|---|---|
ip a or ip addr | Show IP addresses and interfaces | ip a |
ip link | Show or manage network interfaces | ip link show |
ip route | Display routing table | ip route |
Connectivity Testing
| Command | Description | Example |
|---|---|---|
ping | Test if host is reachable | ping google.com |
traceroute | Show route packets take to a host | traceroute google.com |
mtr | Real-time traceroute (advanced) | mtr google.com |
telnet | Test open ports on remote system | telnet example.com 80 |
nc (netcat) | Test connections or transfer data | nc -zv google.com 443 |
DNS Tools
| Command | Description | Example |
|---|---|---|
nslookup | Query DNS for records | nslookup google.com |
dig | Advanced DNS querying | dig google.com |
host | Simple DNS lookup | host google.com |
Monitoring and Interface Info
| Command | Description | Example |
|---|---|---|
ifconfig | Show interface info (deprecated) | ifconfig |
ip -s link | Show stats for interfaces | ip -s link |
ethtool | View/set Ethernet device settings | ethtool eth0 |
iwconfig | Configure wireless interfaces | iwconfig wlan0 |
nmcli | NetworkManager CLI (for desktops) | nmcli device status |
Port and Socket Monitoring
| Command | Description | Example |
|---|---|---|
netstat | Show network connections (old) | netstat -tuln |
ss | Show open sockets (modern) | ss -tuln |
lsof -i | List open files using network | lsof -i |
nmap | Port scanner / network mapper | nmap 192.168.1.1 |
Downloading and Transfers
| Command | Description | Example |
|---|---|---|
curl | Transfer data from URLs | curl https://example.com |
wget | Download files from web | wget https://file.com/file.zip |
scp | Secure copy over SSH | scp file.txt user@host:/path |
rsync | Sync files/directories | rsync -avz /src user@host:/dest |
Troubleshoot Network Issue
Check your IP:
ip aTest DNS:
dig google.comPing a site:
ping google.comCheck open ports:
ss -tulnTraceroute to a server:
traceroute google.com
Process and System Monitoring Commands
| Command | Description | Example Command |
|---|---|---|
| ps | Show running processes. | ps aux |
| top | Dynamic process viewer. | top |
| htop | Enhanced version of top. | htop |
| kill | Send a signal to a process. | kill <PID> |
| killall | Kill processes by name. | killall <process_name> |
| uptime | System uptime and load. | uptime |
| whoami | Current logged-in user. | whoami |
| env | Display environment variables. | env |
| strace | Trace system calls of a process. | strace -p <PID> |
| systemctl | Manage systemd services. | systemctl status <service_name> |
| journalctl | View system logs. | journalctl -xe |
| free | Display memory usage. | free -h |
| vmstat | Report virtual memory statistics. | vmstat 1 |
| iostat | Report CPU and I/O statistics. | iostat |
| lsof | List open files by processes. | lsof |
| dmesg | Print kernel ring buffer messages. | dmesg |
User and Permission Management Command
| Command | Description | Example Command |
|---|---|---|
| passwd | Change user password. | passwd <username> |
| adduser / useradd | Add a new user. | adduser <username> or useradd <username> |
| deluser / userdel | Delete a user. | deluser <username> or userdel <username> |
| usermod | Modify user account. | usermod -aG <group> <username> |
| groups | Show group memberships. | groups <username> |
| sudo | Execute commands as root. | sudo <command> |
| chage | Change user password expiry information. | chage -l <username> |
| id | Display user identity information. | id <username> |
| newgrp | Log in to a new group. | newgrp <group> |
👤 1. User Management Commands
| Command | Description | Example |
|---|---|---|
adduser | Add a new user (interactive, preferred) | sudo adduser john |
useradd | Add user (non-interactive, more control) | sudo useradd -m john |
passwd | Set/change a user’s password | sudo passwd john |
usermod | Modify a user account | sudo usermod -aG sudo john |
deluser | Delete a user | sudo deluser john |
userdel | Delete a user (and optionally their home dir) | sudo userdel -r john |
id | Show user ID and group info | id john |
whoami | Show the current logged-in username | whoami |
👥 2. Group Management Commands
| Command | Description | Example |
|---|---|---|
addgroup | Add a new group | sudo addgroup developers |
groupadd | Add group (alternative command) | sudo groupadd staff |
groupdel | Delete a group | sudo groupdel staff |
usermod -aG | Add a user to a group | sudo usermod -aG developers john |
groups | Show groups of a user | groups john |
gpasswd | Administer group membership | sudo gpasswd -a john developers |
🗂️ 3. File Ownership and Permissions
| Command | Description | Example |
|---|---|---|
chown | Change ownership of a file/directory | sudo chown john:developers file.txt |
chmod | Change file/directory permissions | chmod 755 script.sh |
chgrp | Change group ownership | chgrp developers file.txt |
ls -l | View file permissions and ownership | ls -l /home/john/ |
umask | Show or set default permission mask | umask |
4. Understanding chmod (Permissions)
Permission format: rwxr-xr--
| Symbol | Meaning | Binary |
|---|---|---|
r | Read | 4 |
w | Write | 2 |
x | Execute | 1 |
Example:
chmod 755 file.sh
→ Owner: read/write/execute
→ Group: read/execute
→ Others: read/execute
| Numeric | Meaning |
|---|---|
7 | read + write + execute |
6 | read + write |
5 | read + execute |
4 | read only |
0 | no permissions |
5. Useful Combinations
| Task | Command |
|---|---|
| Add user and assign to a group | sudo adduser john developers |
| Add user to sudoers | sudo usermod -aG sudo john |
| Change file to be executable | chmod +x script.sh |
| Give read/write to user only | chmod 600 file.txt |
| Change owner to root | sudo chown root:root file.txt |
6. Audit User Activity
| Command | Description | Example |
|---|---|---|
last | Show recent logins | last |
who | Show who is logged in | who |
w | Show logged in users + what they’re doing | w |
history | Show command history | history |
sudo | Execute as another user (usually root) | sudo apt update |
Summary Cheat Table
| Area | Key Commands |
|---|---|
| User | adduser, useradd, passwd, userdel |
| Group | addgroup, usermod -aG, groups, groupdel |
| Permissions | chmod, chown, chgrp, ls -l |
| Audit | last, who, w, history |
How to change pemisson ownership?
..................................
sudo chown -R username:group directory
centos - chown :groupnamr
chown
How to show users?
...................
getent passwd
How to show group?
...................
getent group
How to check user which group join?
...................................
groups
How to show all gropus?
.......................
compgen -g
How to show all users?
......................
compgen -u
How to List All Groups in Linux?
................................
cat /etc/group
How to List All Members of a Group on Ubuntu ?
.............................................
getent group
How to add User in a Group on Ubuntu?
......................................
sudo usermod -aG
How to remove User from a Group on Ubuntu ?
...........................................
sudo gpasswd -d
How to change password users self ?
...................................
passwd
How to normal users permisson to root privilage?
................................................
usermod -aG sudo
sudo some-command
sudo visudo
sudo passwd -e bob
sudo passwd --expire sumita
How to Account Lokking Unlokking?
.................................
sudo passwd -l bob
sudo passwd -u bob
skill -u USERNAME
How to delete user forcefully kill?
....................................
sudo killall -u username
sudo userdel -f username
How to del group?
delgroup -f
Text Processing Commands
| Command | Description | Example Command |
|---|---|---|
| awk | Pattern scanning and processing. | awk '{print $1}' file.txt |
| sed | Stream editor for filtering/modifying text. | sed 's/old/new/g' file.txt |
| cut | Remove sections from lines of text. | cut -d':' -f1 /etc/passwd |
| sort | Sort lines of text. | sort file.txt |
| grep | Search for patterns in text. | grep 'pattern' file.txt |
| wc | Count words, lines, and characters. | wc -l file.txt |
| paste | Merge lines of files. | paste file1.txt file2.txt |
| join | Join lines of two files on a common field. | join file1.txt file2.txt |
| head | Output the first part of files. | head -n 10 file.txt |
| tail | Output the last part of files. | tail -n 10 file.txt |
| Command | Description | Example Command |
|---|---|---|
| alias | Create shortcuts for commands. | alias ll='ls -la' |
| unalias | Remove an alias. | unalias ll |
| history | Show previously entered commands. | history |
| clear | Clear the terminal screen. | clear |
| reboot | Reboot the system. | reboot |
| shutdown | Power off the system. | shutdown now |
| date | Display or set the system date and time. | date |
| echo | Display a line of text. | echo "Hello, World!" |
| sleep | Delay for a specified amount of time. | sleep 5 |
| time | Measure the duration of command execution. | time ls |
| watch | Execute a program periodically, showing output fullscreen. | watch -n 5 df -h |
Linux LVM Commnds
What is LVM?
LVM (Logical Volume Manager) allows you to manage disk space flexibly by abstracting physical storage into logical volumes. You can resize, combine, and manage volumes more easily than with traditional partitions.
1. Install LVM (if not already installed)
sudo apt update
sudo apt install lvm2
2. Basic LVM Workflow Overview
| Step | Task | Command |
|---|---|---|
| 1 | Create physical volume (PV) | pvcreate /dev/sdX |
| 2 | Create volume group (VG) | vgcreate my_vg /dev/sdX |
| 3 | Create logical volume (LV) | lvcreate -L 10G -n my_lv my_vg |
| 4 | Format LV with a file system | mkfs.ext4 /dev/my_vg/my_lv |
| 5 | Mount the LV | mount /dev/my_vg/my_lv /mnt |
3. Physical Volume (PV) Commands
| Command | Description |
|---|---|
pvcreate /dev/sdX | Initialize a physical volume |
pvdisplay | Show detailed info about PVs |
pvs | Show a summary of all PVs |
pvscan | Scan all disks for PVs |
pvremove /dev/sdX | Remove a physical volume (wipe LVM data) |
4. Volume Group (VG) Commands
| Command | Description |
|---|---|
vgcreate my_vg /dev/sdX | Create a volume group |
vgextend my_vg /dev/sdY | Add PV to VG |
vgreduce my_vg /dev/sdY | Remove PV from VG |
vgs | Show summary of all VGs |
vgdisplay | Detailed info about VGs |
vgremove my_vg | Remove a VG |
vgchange -a y | Activate all volume groups |
vgchange -a n | Deactivate all volume groups |
5. Logical Volume (LV) Commands
| Command | Description |
|---|---|
lvcreate -L 10G -n my_lv my_vg | Create a logical volume |
lvextend -L +5G /dev/my_vg/my_lv | Extend a logical volume |
lvresize -L 15G /dev/my_vg/my_lv | Resize LV to specific size |
lvreduce -L 8G /dev/my_vg/my_lv | Reduce LV size (⚠️ careful!) |
lvremove /dev/my_vg/my_lv | Remove an LV |
lvdisplay | Detailed info about LVs |
lvs | Show summary of all LVs |
lvscan | Scan for logical volumes |
6. Resizing File Systems with LVM
⚠️ Always backup before resizing!
Extend Logical Volume and File System:
sudo lvextend -L +5G /dev/my_vg/my_lv
sudo resize2fs /dev/my_vg/my_lv # For ext4
⚠️ Shrink Logical Volume and File System:
sudo umount /dev/my_vg/my_lv
sudo e2fsck -f /dev/my_vg/my_lv
sudo resize2fs /dev/my_vg/my_lv 8G
sudo lvreduce -L 8G /dev/my_vg/my_lv
sudo mount /dev/my_vg/my_lv /mnt
7. Removing LVM Components
| Task | Command |
|---|---|
| Unmount the LV | umount /mnt |
| Remove LV | lvremove /dev/my_vg/my_lv |
| Remove VG | vgremove my_vg |
| Remove PV | pvremove /dev/sdX |
8. Useful Monitoring and Status Commands
| Command | Description |
|---|---|
lsblk | Lists all block devices (including LVMs) |
df -h | Disk space usage (mounted volumes) |
mount | Check mounted file systems |
blkid | Display block device UUIDs and types |
lvs, vgs, pvs | Show LVM components in summary |
How To Cheking Linux Ubantu Disk Status?
.........................................
lsblk
fdisk -l
df -hT
lsblk -f
mount -av
How To Create Linux Ubantu LVM?
...............................
Step#01 fdisk /dev/sdb
Step#02 n, p, t, 8e, w
Step#03 partprobe /dev/sdb
Step#04 pvcreate /dev/sdb1
Step#05 vgcreate nextcloud /dev/sdb1
Step#06 lvcreate -L 500G -n nextcloud nextcloudlv
Step#07 mkfs.ext4 /dev/mapper/nextcloud-nextcloudlv
Step#08 mount /dev/mapper/nextcloud-nextcloudlv /var/www/html
Step#09 df -hT
Step#10 cat/etc/mtab
Step#11 nano /etc/fstab
Step#11 /dev/mapper/nextcloud-nextcloudlv /var/www/html ext4 errors=remount-ro 0 1
How To Cheking Linux Ubantu LVM Status?
........................................
pvdisplay
vgdisplay
lvdisplay
lvs
vgs
pvs
lvscan
pvscan
vgscan
How To Remove Linux Ubantu LVM?
...............................
lvremove /dev/nextcloud/nextcloudlv
Ubantu LVM Root Partion Extend
...............................
1.sudo fdisk -l : find new drive
or lsblk or df -h
2.sudo pvscan
3.sudo fdisk /dev/sdb
n
p
1
[enter]
[enter]
t
8e
w
5.lsblk and see /dev/sdb1 added
if /dev/sdb1 is not seen then reboot before continuing further
6.sudo pvcreate /dev/sdb1
7.sudo vgdisplay : look for VG Name value (ubuntu-vg, vg00 etc)
8.sudo vgextend ubuntu-vg /dev/sdb1
9.sudo pvscan
10.sudo lvdisplay : look for LV Path value
11.sudo lvextend /dev/ubuntu-vg/ubuntu-lv /dev/sdb1
OR
sudo lvextend -L +5G /dev/my_vg/my_lv sudo resize2fs /dev/my_vg/my_lv # For ext4
12.sudo resize2fs /dev/ubuntu-vg/ubuntu-lv
lsblk will now show your size increase
Disk Management
How to check hdd?
.................
sudo fdisk -l
How to check partition MBR/GPT
..............................
gdisk /dev/sda
lsblk
How to create partition?
........................
sudo fdisk /dev/sda
help- m
n
enter
enter
+20G
p- print
w-save
fdisk -l
How to format partition?
........................
mkfs.ext4 /dev/sda
lsblk -f -chcek format or not partition
How to mount partiton?
......................
sudo mount /dev/sda /
check - sudo mount
lsblk
| Tool | Command | Output Tells You |
|---|---|---|
parted | sudo parted /dev/sdX print | gpt or msdos |
gdisk | sudo gdisk -l /dev/sdX | MBR or GPT |
fdisk | sudo fdisk -l /dev/sdX | Disklabel type |
lsblk | lsblk -f | Filesystem info |
blkid | sudo blkid | UUIDs/labels |
Convert MBR to GPT using gdisk
...............................
sudo apt update
sudo apt install gdisk
Step 2: Identify the target disk
.................................
List available disks:lsblk
Find your disk (e.g., /dev/sda, /dev/sdb, etc.)
Step 3: Run gdisk to convert MBR to GPT
.......................................
sudo gdisk /dev/sdX
Replace /dev/sdX with your actual disk (not a partition, e.g., /dev/sda)
You’ll see a message like:MBR detected. Do you want to convert it? (Y/N)
Enter Y to proceed with the conversion.
Step 4: Write the new partition table
.....................................
Type the following in gdisk:
w → Write changes to disk
Y → Confirm
This writes the GPT structure and exits.
Step 5: Verify the new partition table
.......................................
sudo parted /dev/sdX print
It should now say: Partition Table: gpt
1. Using lsblk -f + blkid (to check GPT UUIDs)
...............................................
sudo lsblk -f
sudo blkid
2. Using fdisk (Shows Partition Type)
.....................................
sudo fdisk -l /dev/sdX
How to create multiplefile touch commend?
.........................................
for i in {1..10}; do touch file$i.txt; done
How to create tar file?
......................
tar cvf .tar then enter
How to create tar file untar?
..............................
tar xvf .tar then enter
tar –xvzf .tar then enter
How to create compress reduce size tar file?
.............................................
tar cjf .tar.gz cvf then enter
How to create gzip?
....................
gzip
How to create gzip extarct ?
............................
gunzip
zcat
How to view file size check?
............................
du -sh
VPS Server Install Apache WordPress
1. Install Apache
sudo apt update && sudo apt install apache2
............................................
2. Configure Firewall
sudo ufw allow OpenSSH
sudo ufw allow in "Apache Full"
sudo ufw enable
sudo ufw status
3. Test Apache
...............
sudo service apache2 status
4. Install MySQL
................
sudo apt update && sudo apt install mysql-server
sudo service mysql status
5. MySQL Security
..................
sudo mysql_secure_installation
-> ENTER | Y | Y | Y | Y
sudo mysqladmin -p -u root version
6. Install PHP
...............
sudo apt update && sudo apt install php libapache2-mod-php php-mysql
php -version
7. Installing phpMyAdmin
........................
sudo apt update && sudo apt install phpmyadmin php-mbstring
sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf
sudo a2enconf phpmyadmin.conf
sudo service apache2 reload
8. Create MySQL User
....................
sudo mysql
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
FLUSH PRIVILEGES;
CREATE USER 'username'@'ip_address' IDENTIFIED BY 'password';
exit
9. Secure PhpMyAdmin
....................
sudo nano /etc/apache2/conf-available/phpmyadmin.conf
Alias /your_url /usr/share/phpmyadmin
sudo service apache2 reload
10. Install ZIP/Unzip
......................
apt-get update
apt-get install zip unzip
11. Enable .htaacess
.....................
sudo apt-get update
sudo a2enmod rewrite
systemctl restart apache2
sudo nano /etc/apache2/sites-enabled/000-default.conf
AllowOverride All
12. Configure Apache File
..........................
sudo nano /etc/apache2/apache2.conf
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
Options -Indexes
ServerSignature Off
ServerTokens Prod
13. install SSL Certificate
............................
sudo apt update && sudo apt install certbot python3-certbot-apache
sudo certbot --apache
service apache2 restart
Redhat RHEL,Cent-OS User Group Commnds
1. Add / Create Users
| Command | Description |
|---|---|
sudo adduser username | Add a user (interactive) |
sudo useradd username | Add user (scriptable, non-interactive) |
sudo passwd username | Set password for a user |
sudo useradd -m -s /bin/bash username | Create user with home directory and shell |
Example:
sudo useradd -m -s /bin/bash alice
sudo passwd alice
2. Delete / Remove Users
| Command | Description |
|---|---|
sudo userdel username | Delete a user |
sudo userdel -r username | Delete user and home directory |
3. Modify Existing Users
| Command | Description |
|---|---|
sudo usermod -l newname oldname | Rename user |
sudo usermod -d /new/home -m username | Move home directory |
sudo usermod -s /bin/zsh username | Change user’s shell |
sudo usermod -aG groupname username | Add user to a group (append) |
Example:
sudo usermod -aG wheel alice
4. Lock / Unlock User Accounts
| Command | Description |
|---|---|
sudo usermod -L username | Lock account (disable login) |
sudo usermod -U username | Unlock account |
sudo passwd -l username | Lock password |
sudo passwd -u username | Unlock password |
5. Add / Manage Groups
| Command | Description |
|---|---|
sudo groupadd groupname | Add new group |
sudo groupdel groupname | Delete a group |
sudo groupmod -n newname oldname | Rename a group |
sudo gpasswd -a username groupname | Add user to group |
sudo gpasswd -d username groupname | Remove user from group |
Example:
sudo groupadd devs
sudo gpasswd -a alice devs
6. View User & Group Info
| Command | Description |
|---|---|
id username | Show UID, GID, and groups |
groups username | List groups a user belongs to |
getent passwd username | Show user entry |
getent group groupname | Show group entry |
cat /etc/passwd | List all users |
cat /etc/group | List all groups |
7. Sudo (Admin) Access
Add user to sudo group (Red Hat uses wheel):
sudo usermod -aG wheel username
Check sudo access:
sudo -l -U username
8. Change User Shell
| Command | Description |
|---|---|
chsh -s /bin/bash username | Change user’s default shell |
cat /etc/shells | List available shells |
Example:
sudo chsh -s /bin/zsh alice9. Account Expiry and Password Aging
| Command | Description |
|---|---|
chage -l username | Show password aging info |
sudo chage -E 2025-12-31 username | Set account expiration date |
sudo chage -M 90 username | Set max password age to 90 days |
sudo chage -m 7 username | Set min password age to 7 days |
sudo chage -W 10 username | Warn 10 days before expiry |
11. Switch Users and Become Root
| Command | Description |
|---|---|
su - username | Switch to another user |
sudo su - | Become root user |
sudo -i | Open a root login shell |
Quick Summary Table
| Task | Command |
|---|---|
| Add user | adduser / useradd |
| Delete user | userdel -r |
| Add group | groupadd |
| Add user to group | usermod -aG |
| Set password | passwd |
| Lock/unlock user | usermod -L/-U |
| View groups | groups / id |
| Give sudo access | Add to wheel group |
Ubantu Server Firewall UFW Commnds
1. What is UFW?
UFW (Uncomplicated Firewall) is a frontend for iptables that makes it easier to manage firewall rules on Ubuntu. It’s ideal for most users who want simple, secure, and readable firewall configurations.
2. Enable or Disable UFW
| Task | Command |
|---|---|
| Enable firewall | sudo ufw enable |
| Disable firewall | sudo ufw disable |
| Check firewall status | sudo ufw status |
| Verbose status | sudo ufw status verbose |
| Reset firewall (clear all rules) | sudo ufw reset |
3. Allow or Deny Services and Ports
Allow Traffic
| Task | Command |
|---|---|
| Allow specific port | sudo ufw allow 22 |
| Allow service by name | sudo ufw allow ssh |
| Allow port with protocol | sudo ufw allow 80/tcp |
| Allow port range | sudo ufw allow 1000:2000/tcp |
| Allow IP access to port | sudo ufw allow from 192.168.1.100 to any port 22 |
Deny Traffic
| Task | Command |
|---|---|
| Deny specific port | sudo ufw deny 23 |
| Deny service | sudo ufw deny telnet |
4. Managing Incoming/Outgoing Rules
| Task | Command |
|---|---|
| Allow outgoing traffic | sudo ufw default allow outgoing |
| Deny all incoming by default | sudo ufw default deny incoming |
| Allow all incoming | sudo ufw default allow incoming |
| Deny all outgoing | sudo ufw default deny outgoing |
5. Deleting Rules
| Task | Command |
|---|---|
| Delete by rule | sudo ufw delete allow 22 |
Delete by number (from ufw status numbered) |
sudo ufw status numbered
sudo ufw delete [number]
6. Logging & Monitoring
| Command | Description |
|---|---|
sudo ufw logging on | Enable logging |
sudo ufw logging off | Disable logging |
sudo ufw status verbose | Show with logging info |
| Logs are stored in | /var/log/ufw.log |
7. Application Profiles
Many services define UFW profiles (e.g., OpenSSH, Apache, Samba, etc.)
| Task | Command |
|---|---|
| List available profiles | sudo ufw app list |
| Show details of a profile | sudo ufw app info Apache |
| Allow a profile | sudo ufw allow 'Apache Full' |
8. Enable UFW on Boot
UFW is usually enabled to start at boot automatically once you enable it:
sudo systemctl enable ufw
9. Disable UFW Temporarily
| Task | Command |
|---|---|
| Disable firewall | sudo ufw disable |
| Re-enable firewall | sudo ufw enable |
10. Common Examples
| Task | Command |
|---|---|
| Allow SSH | sudo ufw allow ssh or sudo ufw allow 22 |
| Allow HTTP & HTTPS | sudo ufw allow 80,443/tcp |
| Allow MySQL from local network | sudo ufw allow from 192.168.1.0/24 to any port 3306 |
| Block all except SSH |
sudo ufw default deny incoming
sudo ufw allow ssh
Check Summary
sudo ufw status numbered
sudo ufw status verbose
Important Tips
Always allow SSH before enabling the firewall remotely:
sudo ufw allow ssh sudo ufw enable
Redhat,Cent-OS Server Firewall Commnds
Basics of firewalld on CentOS / RHEL
Service:
firewalldCommand-line tool:
firewall-cmdZones: Used to define trust levels for network connections (e.g.,
public,internal,external, etc.)Default zone: Usually
public
Start/Enable/Disable firewalld
...............................
sudo systemctl start firewalld # Start the firewall
sudo systemctl enable firewalld # Enable at boot
sudo systemctl stop firewalld # Stop the firewall
sudo systemctl disable firewalld # Disable at boot
sudo systemctl status firewalld # Check status
Check firewall status & rules
.............................
firewall-cmd --state # Running or not
firewall-cmd --get-active-zones # Shows active zones
firewall-cmd --get-default-zone # Default zone
firewall-cmd --list-all # List all rules for default zone
firewall-cmd --list-all --zone=public # List rules for a specific zone
firewall-cmd --list-services # List allowed services
Add / Remove Ports and Services
................................
Add service (permanent and runtime)
# Runtime (immediate)
sudo firewall-cmd --zone=public --add-service=http
# Permanent (persists after reboot)
sudo firewall-cmd --zone=public --add-service=http --permanent
sudo firewall-cmd --reload # Reload to apply permanent rules
Remove service
...............
sudo firewall-cmd --zone=public --remove-service=http --permanent
sudo firewall-cmd --reload
Add port
.........
sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
sudo firewall-cmd --reload
Remove port
...........
sudo firewall-cmd --zone=public --remove-port=8080/tcp --permanent
sudo firewall-cmd --reload
Rich Rules (Advanced)
Example: Allow IP through firewall
...................................
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.100" accept'
sudo firewall-cmd --reload
Example: Block an IP
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.200" reject'
sudo firewall-cmd --reload
Reload / Reset Firewall
.......................
sudo firewall-cmd --reload # Reload after changes
sudo firewall-cmd --complete-reload # Full reload (drops runtime configs)
sudo firewall-cmd --permanent --reset-default-zone # Reset default zone
Lockdown Mode
..............
sudo firewall-cmd --lockdown-on # Enable lockdown
sudo firewall-cmd --lockdown-off # Disable lockdown
sudo firewall-cmd --query-lockdown # Check lockdown status
Predefined Services
...................
firewall-cmd --get-services
File Transfer Linux Server SCP Commnds
SCP stands for Secure Copy Protocol
...................................
Copy from local to remote
..........................
scp /path/to/local/file username@remote_host:/path/to/remote/destination
Example:
scp file.txt root@192.168.1.100:/home/root/
Copy from remote to local
.........................
scp username@remote_host:/path/to/remote/file /path/to/local/destination
Example:
scp file.txt root@192.168.1.100:/home/root/
Copy a directory recursively
............................
scp -r /local/dir username@remote_host:/remote/dir
Example:
scp -r myfolder user@192.168.1.101:/var/www/
Specify a non-default port
..........................
scp -P 2222 file.txt user@host:/path/
Upload file to remote server
............................
scp index.html root@192.168.1.10:/var/www/html/
Download file from remote server
.................................
scp root@192.168.1.10:/var/log/messages ~/logs/
Copy folder to remote server with port
......................................
scp -P 2200 -r website/ user@host:/home/user/
Transfer a ZIP file via SCP
............................
From Local to Remote
scp website_backup.zip root@192.168.1.10:/home/root/backups/
From Remote to Local
.....................
scp root@192.168.1.10:/home/root/website_backup.zip ~/Downloads/
Combine zip and scp in one line (local to remote)
.................................................
zip -r myfolder.zip myfolder && scp myfolder.zip user@host:/destination/
SSH uses a non-default port (e.g., port 2222)
.............................................
scp -P 2222 myfile.zip user@host:/remote/path/
Common Options:
-r: Recursively copy entire directories.
-P: Specify a custom SSH port (note the uppercase P).
-i: Specify a private key file for authentication.
-v: Enable verbose mode to see more detailed output for debugging.
-C: Enable compression to speed up the transfer for large files.
Linux Server File Compression
ls -lh
Compressing a Big file
.......................
gzip myfile.txt # Produces myfile.txt.gz
Unompressing a Big file
.......................
gunzip myfile.txt
Directory Compress
...................
mkdir backup
tar -cvf backup.tar backup/
ls -ltr
gzip backup.tar
ls
D Compress
..........
tar -xzvf backup.tar.gz
ls -ltr
Without backup.tar.gz Extrat File Show
.......................................
tar -tzvf backup.tar.gz
Or
zcat backup.tar
Single Step Commnd Compress
............................
tar -czvf backup.tar.gz backup
tar -xzvf backup.tar.gz backup
Zip Commnd
..........
Example- file1 file2
Compress:- zip file.zip file1 file2
ls -ltr
Without file.zip Extrat File Contain Show
..........................................
zipinfo files.zip
unzip -l files.zip
UnZip Commnd
.............
unzip file.zip
scp -v backup.tar.gz user@192.168.1.100:/home/user/backups/
Check the File on Remote
........................
ssh user@192.168.1.100
ls -lh /home/user/backups/
rsync on Linux Server
What is rsync?
rsync is a powerful, efficient tool used to copy and sync files/directories between locations, either locally or over a network (e.g., between two servers). It only transfers changed parts of files, making it much faster than scp for repeated syncs.
1. Installing rsync on Ubuntu
.............................
Open a terminal and run:
sudo apt update
sudo apt install rsync
Check the version to confirm installation
..........................................
rsync --version
2. Basic rsync Usage
.....................
Local Sync (Directory to Directory):
rsync -av /source/folder/ /destination/folder/
-a: Archive mode (preserves permissions, timestamps, etc.)
-v: Verbose (shows progress)
Remote Sync (Over SSH)
......................
From Local to Remote:
rsync -av /local/folder/ user@remote_ip:/remote/folder/
You can add -z for compression if sending over the internet:
rsync -avz /local/folder/ user@remote:/remote/folder/
Example
rsync -av backup.tar.gz user@192.168.1.100:/home/user/backups/
Using SSH Key (Optional, Recommended)
.....................................
To avoid typing the password every time:
1.Generate SSH key (if you don’t have one):
ssh-keygen
2.Copy it to remote server:
ssh-copy-id user@remote_ip
3.Now rsync will work without asking for a password.