| name | Linux Commands Reference |
| description | This skill should be used when the user asks to "run Linux commands", "manage users and permissions", "configure file systems", "work with LVM", "set up networking", "manage services with systemd", "configure firewalls", "implement SELinux or AppArmor", or "automate tasks with cron". It provides comprehensive Linux command reference for security professionals. |
| version | 1.0.0 |
| tags | ["linux","commands","sysadmin","security","networking","permissions","systemd"] |
Linux Commands Reference
Purpose
Master essential Linux commands for system administration, security operations, and penetration testing. This skill covers user management, file permissions, disk management, LVM, networking, firewall configuration, security frameworks (SELinux/AppArmor), and task automation.
Prerequisites
Required Environment
- Linux system (Debian/Ubuntu or RHEL-based)
- Terminal access with appropriate privileges
- Root or sudo access for administrative tasks
Required Knowledge
- Basic command-line navigation
- Understanding of Linux file system hierarchy
- Familiarity with text editors (vim/nano)
Outputs and Deliverables
- System Administration Scripts - Automated user/group management
- Security Configurations - Firewall rules, SELinux policies
- Storage Solutions - LVM configurations, mounted filesystems
- Network Configurations - Interface settings, firewall rules
Core Workflow
Phase 1: General Commands and Navigation
Essential system information commands:
ls -al /bin/sh
uname -a
uname -r
uptime
which <command>
whereis <command>
type <command>
who
id
stat <filename>
file <filename>
less <file>
more <file>
tail -f <file>
head -n 20 <file>
man -k <keyword>
Phase 2: User and Group Management
Manage users and groups:
getent passwd <user>
getent group <group>
groups <username>
id <username>
useradd <username>
useradd -m -s /bin/bash <username>
useradd -D
usermod -l new_name old_name
usermod -s /bin/zsh <username>
usermod -aG <group> <username>
usermod -L <username>
usermod -U <username>
userdel <username>
userdel -r <username>
passwd <username>
chage -l <username>
chage -E 2024-12-31 <username>
groupadd <groupname>
groupmod -n new_name old_name
groupdel <groupname>
gpasswd -a <user> <group>
gpasswd -d <user> <group>
chsh -s /bin/zsh <username>
Phase 3: File Permissions and ACLs
Manage file access:
chmod 755 <file>
chmod 644 <file>
chmod u+x <file>
chmod g+w <file>
chmod o-rwx <file>
chmod u=rw,g=r,o= <file>
chown <user>:<group> <file>
chown -R <user>:<group> <dir>
chgrp <group> <file>
getfacl <file>
setfacl -m u:<user>:rw <file>
setfacl -m g:<group>:r <file>
setfacl -x u:<user> <file>
setfacl -b <file>
setfacl -d -m u:<user>:rw <dir>
umask
umask 027
Umask values (subtract from 777/666):
- 0: rwx (read, write, execute)
- 7: no permissions
Phase 4: Disk and Filesystem Management
Manage storage:
lsblk
blkid
fdisk -l
df -h
du -sh <dir>
fdisk /dev/sda
gdisk /dev/sda
parted /dev/sda
mkfs.ext4 /dev/sda1
mkfs.xfs /dev/sda1
ls /usr/sbin/mkfs*
mount /dev/sda1 /mnt/data
umount /mnt/data
mount -a
mkswap /dev/sda3
swapon /dev/sda3
swapoff /dev/sda3
swapon --show
e2label /dev/sda1 "data"
xfs_admin -L "data" /dev/sda1
e2fsck -f /dev/sda1
resize2fs /dev/sda1 20G
Phase 5: Logical Volume Manager (LVM)
Create and manage logical volumes:
pvcreate /dev/sdb /dev/sdc
pvdisplay
pvs
pvscan
pvremove /dev/sdb
vgcreate my_vg /dev/sdb /dev/sdc
vgdisplay
vgs
vgextend my_vg /dev/sdd
vgremove my_vg
lvcreate -L 100G my_vg -n my_lv
lvcreate -l 100%FREE my_vg -n my_lv
lvdisplay
lvs
lvextend -L +10G /dev/my_vg/my_lv
lvreduce -L 50G /dev/my_vg/my_lv
lvresize -L +10G /dev/my_vg/my_lv
lvremove /dev/my_vg/my_lv
resize2fs /dev/my_vg/my_lv
xfs_growfs /mnt/mountpoint
Phase 6: Process and Service Management
Manage processes and systemd services:
ps aux
pstree
top
htop
pgrep <process>
kill <PID>
kill -9 <PID>
killall <name>
nice -n 10 <command>
renice 5 -p <PID>
<command> &
jobs
fg %1
bg %1
Ctrl+Z
lsof
lsof -i :80
lsof -u <user>
lsof <file>
systemctl status <service>
systemctl start <service>
systemctl stop <service>
systemctl restart <service>
systemctl enable <service>
systemctl disable <service>
systemctl mask <service>
systemctl list-unit-files
systemd-analyze time
systemd-analyze blame
systemd-analyze security
Phase 7: Networking
Configure and troubleshoot networking:
ip addr
ip addr add 192.168.1.10/24 dev eth0
ip addr del 192.168.1.10/24 dev eth0
ip link set eth0 up
ip link set eth0 down
ip route
ip route add default via 192.168.1.1
ip route add 10.0.0.0/8 via 192.168.1.1
nslookup <domain>
dig <domain>
cat /etc/resolv.conf
ping <host>
traceroute <host>
tracepath <host>
ss -tuln
ss -an
netstat -tuln
nmcli device status
nmcli connection show
nmcli device wifi list
nmcli device wifi connect <SSID> password <pass>
nmcli connection edit <conn>
set ipv4.addresses 192.168.1.50/24
set ipv4.gateway 192.168.1.1
set ipv4.dns 8.8.8.8
set ipv4.method manual
save
quit
Phase 8: Firewall Configuration (firewalld)
Manage firewall rules:
firewall-cmd --state
firewall-cmd --get-zones
firewall-cmd --get-default-zone
firewall-cmd --get-active-zones
firewall-cmd --permanent --new-zone=myzone
firewall-cmd --zone=public --change-interface=eth0 --permanent
firewall-cmd --reload
firewall-cmd --get-services
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --permanent --zone=public --remove-service=http
firewall-cmd --permanent --zone=public --add-port=8080/tcp
firewall-cmd --permanent --zone=public --add-port=10000-20000/tcp
firewall-cmd --permanent --zone=public --remove-port=8080/tcp
firewall-cmd --zone=public --list-services
firewall-cmd --zone=public --list-ports
firewall-cmd --zone=public --list-rich-rules
firewall-cmd --zone=public --list-all
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="ssh" accept'
Phase 9: Security Frameworks
SELinux (RHEL-based):
sestatus
getenforce
setenforce 0
setenforce 1
ls -Z <file>
ps auxZ
chcon -t httpd_sys_content_t <file>
restorecon <file>
semanage fcontext -a -t httpd_sys_content_t '/mydata(/.*)?'
restorecon -Rv /mydata
semanage port -l
semanage port -a -t http_port_t -p tcp 8080
getsebool -a
setsebool httpd_can_network_connect on
setsebool -P httpd_can_network_connect on
AppArmor (Debian-based):
aa-status
aa-unconfined
aa-genprof /usr/bin/app
aa-logprof
aa-autodep /usr/bin/app
aa-enforce /etc/apparmor.d/profile
aa-complain /etc/apparmor.d/profile
aa-disable /etc/apparmor.d/profile
aa-remove-unknown
Phase 10: Backup and Task Scheduling
Backup tools:
tar -cvf archive.tar files/
tar -xvf archive.tar
tar -czvf archive.tar.gz files/
tar -xzvf archive.tar.gz
tar -cJvf archive.tar.xz files/
tar -tvf archive.tar
tar --same-owner -xvf archive.tar
dd if=/dev/sda of=/backup/sda.img bs=4M status=progress
dd if=/backup/sda.img of=/dev/sda bs=4M status=progress
rsync -avz /source/ /dest/
rsync -avz -e ssh /local/ user@host:/remote/
rsync -avz --delete /source/ /dest/
rsync -avz --dry-run /source/ /dest/
rsync -avz --include="*.pdf" --exclude=".*" /source/ /dest/
Cron scheduling:
crontab -l
crontab -e
crontab -r
0 2 * * * /path/to/backup.sh
*/5 * * * * /path/to/check.sh
0 0 * * 0 /path/to/weekly.sh
0 0 1 * * /path/to/monthly.sh
/etc/cron.hourly/
/etc/cron.daily/
/etc/cron.weekly/
/etc/cron.monthly/
Quick Reference
File Manipulation
| Command | Purpose |
|---|
find / -name "*.txt" | Find files by name |
find / -type f -mtime -7 | Files modified in 7 days |
find / -size +100M | Files larger than 100MB |
grep -r "pattern" /path | Recursive search |
awk '{print $1}' file | Print first column |
sed 's/old/new/g' file | Find and replace |
cut -d: -f1 /etc/passwd | Extract field |
sort | uniq | Sort and deduplicate |
Vim Commands
| Mode | Command | Action |
|---|
| Normal | i/a | Insert before/after |
| Normal | o/O | New line after/before |
| Normal | dd | Delete line |
| Normal | yy | Copy line |
| Normal | p | Paste |
| Normal | /pattern | Search forward |
| Execute | :w | Save |
| Execute | :q! | Quit without saving |
| Execute | :wq or ZZ | Save and quit |
| Execute | :%s/old/new/g | Replace all |
Constraints and Limitations
Permission Requirements
- Many commands require root/sudo access
- SELinux/AppArmor may restrict operations
- File ACLs may override standard permissions
Distribution Differences
- Package managers differ (apt vs dnf/yum)
- Service management may vary
- Security frameworks differ (SELinux vs AppArmor)
Best Practices
- Always test commands with
--dry-run when available
- Use absolute paths in scripts and cron jobs
- Back up configurations before modifying
- Check logs after security changes
Troubleshooting
Permission Denied
Solutions:
- Check file permissions:
ls -la <file>
- Verify SELinux/AppArmor:
sestatus or aa-status
- Check ACLs:
getfacl <file>
- Use sudo if appropriate
Service Won't Start
Solutions:
- Check status:
systemctl status <service>
- View logs:
journalctl -u <service>
- Verify configuration files
- Check port conflicts:
ss -tuln
Disk Full
Solutions:
- Find large files:
find / -size +100M
- Check by directory:
du -sh /*
- Clear package cache:
apt clean or dnf clean all
- Review log files in
/var/log