Weekly Shaarli
Week 18 (April 28, 2025)
Finding System Information
cat /etc/redhat-release→ Red Hat versionuname -a→ Linux hostname, kernel, architecturedmidecode→ hardware, BIOS, system infoarch→ x86_64 or i386/32-bit
Common Keyboard Shortcuts
Ctrl + U→ erase command lineCtrl + C→ stop/kill current commandCtrl + Z→ suspend commandCtrl + D→ exit interactive session
Record Terminal Session
-
script mylog.log→ record shell session- Use
exitto stop recording
- Use
Root Password Recovery
- Reboot → enter GRUB menu
- Press
e→ edit boot params - Add
rd.breakat end of kernel line Ctrl + Xto boot- Mount root with
chroot /sysroot passwd root→ change passwordtouch /.autorelabel→ SELinux relabeling on reboot
View Environment Variables
printenv,echo $PATH,echo $SHELL
Set Environment Variables
- Temporary:
export TEST=1,echo $TEST -
Permanent:
vi ~/.bashrc TEST=123 export TEST
Global → vi /etc/profile or /etc/bashrc
-
Basic Replace Syntax:
-
sed -i 's/KENNY/LENNY/g' filename -
Substitute all occurrences of "KENNY" with "LENNY"
-
-
Delete Line Containing String:
sed -i '/SEINFELD/d' filename
-
Delete Empty Lines:
sed -i '/^$/d' filename
-
Delete First Line:
sed '1d' filename
-
sed '1,2d' filename→ delete the first two lines -
sed 's/\t/ /g' filename→ replace tabs with spaces -
sed -n '12,18p' filename→ print only lines 12 to 18 -
sed '12,18d' filename→ delete lines 12 to 18 -
sed G filename→ insert an empty line after every line -
sed '8!s/seinfeld/S1/' filename→ replace "seinfeld" with "S1" on every line except line 8
Crontab Basics
crontab -e→ edit crontabcrontab -l→ list crontab entriescrontab -r→ remove crontab entriessystemctl status crond→ check crond service status
AT: One-time Scheduled Tasks
- Used to schedule jobs only once.
Scheduling Examples:
at HH:MM PM→ schedule a job interactively-
at 11:23AM 12/24/25- Enter job (e.g.,
echo "test") and pressCtrl + Dto finish
- Enter job (e.g.,
at 2:45PM 01/30/25at 4PM + 4 daysat now + 5 hoursat 8:00AM Sunat 10:00AM next month
Managing AT Jobs
atq→ list the scheduled at jobsatrm #→ remove a specific at job (use number fromatq)systemctl status atd→ check atd daemon status
File Display Commands
cat → Show entire content
cat -A → Show non-printable characters
more → Paginate output
less → Same as more but allows navigation with arrow keys
head → Show top lines (default: 10)
tail → Show bottom lines (default: 10)
head -2 myfile
tail -2 myfile
Filters / Text Processing Input
cut→ Cut inputawk→ List by columnsgrep&egrep→ Search by keywordsort→ Sort in alphabetical orderuniq→ Remove duplicate lineswc→ Word count (including lines)
cut Command Examples
cut -c1 filename→ First letter of each linecut -c1,2,4 filename→ Characters 1, 2, and 4cut -c1-3 filename→ Range: characters 1 to 3cut -c1-3,6-8 filename→ Ranges: characters 1-3 and 6-8cut -b1-3 filename→ Byte range 1 to 3cut -d: -f6 /etc/passwd→ Field 6 using:delimitercut -d: -f6-7→ Fields 6 and 7ls -l | cut -c2-4→ Extract character range 2 to 4 fromlsoutput
awk Command Examples
awk '{print $1}' file→ Print 1st column of filels -l | awk '{print $1, $3}'→ Print 1st and 3rd columns fromls -lls -l | awk '{print $NF}'→ Print last columnawk '/jerry/ {print}' file→ Search for "jerry" and print matching linesawk -F: '{print $1}' /etc/passwd→ Use:as delimiter, print 1st fieldecho "hello tom" | awk '{$2="adam"; print}'→ Replace column 2 with "adam"awk 'length($0) > 15' file→ Print lines longer than 15 characters
grep Command Examples
grep keyword file→ Search for keywordgrep -c keyword file→ Count matching linesgrep -i keyword file→ Ignore casegrep -n keyword file→ Show line numbersgrep -v keyword file→ Invert match (everything except keyword)grep keyword file | awk '{print $1}'→ Pipe grep to awk to extract 1st columnls -l | grep keyword→ Filterls -loutput by keywordegrep -i "keyword1|keyword2" file→ Search for multiple keywords (case-insensitive)
sort Command Examples
sort file→ Sort alphabeticallysort -r file→ Sort in reverse ordersort -u file→ Sort and remove duplicatessort file | uniq→ Same as above with explicit uniqsort file | uniq -d→ Show only duplicate linessort file | uniq -c→ Count duplicatessort -k4 -n file→ Sort by 4th field (numeric)ls -l | sort -k4 -n→ Sort by size fromls -l
wc Command Examples
wc -c filename→ Byte countwc -w filename→ Word countwc -l filename→ Line countls -l | wc -l→ Count lines fromls -lwc -l filename→ Count number of lines in file
cmp / diff
diff→ Compare files line by linecmp→ Compare files byte by byte
tar / gzip
tar cvf export.tar somedir→ Compress directorytar xvf export.tar→ Extract tar archivegzip export.tar→ Compress with gzipgzip -d export.tar.gz→ Decompress gzip archive
truncate Command
truncate -s10 filename→ Chop file to 10 bytestruncate -s60 filename→ Extend file to 60 bytes
Combining & Splitting Files
cat file1 file2 file3 > fileN→ Combine multiple files into one
Disk and Memory Info
df→ disk partition infodf -h→ human-readable formatdu→ size of file/directoryfree→ memory infocat /proc/cpuinfo→ CPU infocat /proc/meminfo→ memory info
System Logs & Diagnostics
dmesg→ check kernel ring buffer logsiostat→ check disk I/O statsip route | column -t→ check routing tabless→ view open ports/sockets
Log Directory Structure
-
Logs in
/var/log/boot→ startup recordschronyd→ NTP infocron→ cron jobsmaillog→ mail daemonsecure→ login/logout trackingmessages→ general trace logshttpd→ web server errors
Log Monitoring
tail -f filename→ follow log file in real time
System Shutdown & Boot Control
shutdown→ graceful shutdowninit 0-6→ runlevels (0=off, 3=multi-user, 6=reboot)reboot→ reboot systemhalt→ force shutdown immediately
Hostname Management
cat /etc/hostname→ current hostnamehostnamectl set-hostname myhostname→ change hostname
-
Essential Commands:
useradd,groupadd,userdel,groupdel,usermod- Modify 3 files:
/etc/passwd,/etc/group,/etc/shadow
Create User with Custom Options
useradd -G mygroup -s /bin/bash \
-c "User Description" -m \
-d "/home/myuser" myuser
Other Useful User Commands
useradd myuser→ create userid myuser→ check if user existsgroupadd→ create groupcat /etc/group→ check groupuserdel -r myuser→ delete user and home directorygroupdel mygroup→ delete groupusermod -G mygroup myuser→ change user's groupchgrp -R mygroup myuser→ change group ownership recursivelypasswd myuser→ set or update user's password
Password Aging with chage
-
chage→ manage password aging-m→ minimum days-M→ maximum days-d→ last password change-I→ inactive days-E→ expiration date-W→ warning days
-
Example:
chage -M 5 -M 90 -W 10 -I 10 username
Edit Password Policies
-
File:
/etc/login.defsPASS_MAX_DAYS 9999PASS_MIN_DAYS 0PASS_MIN_LEN 5PASS_WARN_AGE 7
Switch User Privileges
-
visudo→ edit sudo permissions config (/etc/sudoers)- Add user to
wheelgroup for sudo access
- Add user to
usermod -aG wheel myuser→ grant sudo rights
User Info and Monitoring Commands
who→ see logged in userslast→ list last login recordsw→ who is logged in with more detailsfinger→ show user detailsid→ show UID, GID and groupsid myuser→ check user info
Send Messages to Users
-
wall→ broadcast message to all users- Type message, then
Ctrl + Dto send
- Type message, then
-
write myuser→ send message to specific user- Type message, then
Ctrl + Dto send
- Type message, then
-
touch one two three→ Create 3 files -
mkdir folder→ Make directory -
mv -R/cp -R→ Move or copy folder including subfolders -
find . -name "george"→ Search from current directory -
locate→ Faster, but relies on a database -
updatedb→ Update the locate database
Wildcards
| Symbol | Meaning |
|---|---|
| * | Zero or more characters |
| ? | Single character |
| [] | Range of characters |
| \ | Escape character |
| ^ | Beginning of the line |
| \$ | End of the line |
Links
Soft Link (Symbolic Link)
ln -s- Link will be removed if the original file is removed or renamed.
Hard Link
ln- Deleting, renaming, or moving the original file will not affect the hard link.
Diagram
INODE
↑
MY HARD LINK ← MYFILE.TXT → MY-SOFT-LINK
Command to check links:
ls -lTri
File Permissions
Permission Types
| Symbol | Permission | Example (Mnemonic) |
|---|---|---|
| R | Read | Riri (4) |
| W | Write | Winnie (2) |
| X | Execute | Xena (1) |
Each Permission (R, W, X) Can Be Controlled at Three Levels
- U → User
- G → Group
- O → Others (Everyone)
Permission Strings
Example:
-rwxrwxrwx
| | |
| | └── Others
| └───── Group
└───────── User
To remove write permission from all:
chmod a-w filename
To add read and write permission for user:
chmod u+rw filename
Directory Permissions
Example:
drwxrwxrwx
^ ^ ^
| | └── Others
| └───── Group
└───────── User
Numeric Permission Mapping
| Value | Permissions | Symbol | Mnemonic |
|---|---|---|---|
| 0 | No permission | --- | |
| 1 | Execute | --x | Xena |
| 2 | Write | -w- | Winnie |
| 3 | Execute + Write | -wx | |
| 4 | Read | r-- | Riri |
| 5 | Read + Execute | r-x | |
| 6 | Read + Write | rw- | |
| 7 | Read + Write + Exec | rwx |
Example to set permissions:
chmod 764 file
Breakdown:
- User (7) → rwx
- Group (6) → rw-
- Others (4) → r--
Basic System Info Commands
uptime→ time, uptime, users, load averagehostname→ current hostnameip hostname→ show IP & hostnameuname -a→ current OS and kernel infowhich command→ path to a command-
cal→ calendar for current month/yearcal 2025→ full year
bc→ basic calculator
System Monitoring
systemctl→ interact with systemd system managerps→ view current processestop→ dynamic view of process usage
Systemctl Usage
systemctl start|stop|status servicename.service→ manage service statesystemctl enable servicename.service→ start service at bootsystemctl restart|reload servicename→ restart or reload servicesystemctl list-units --all→ list all units-
To add a service:
- Create a unit file in
/etc/systemd/system/servicename.service
- Create a unit file in
Process Management
-
Start a process in the background:
Ctrl + Zthenjobs→ suspend and backgroundbg→ continue in backgroundfg→ bring back to foreground
-
Run process detached from terminal:
nohup process &→ keep running after logoutnohup process > /dev/null 2>&1 &→ suppress all logs/output
Killing and Prioritizing
pkill name→ kill process by name-
nice→ start process with a priority (range -20 to 19)- lower = higher priority
nice -20 top
Jobs Monitoring
jobs→ list background/foreground job states
Vi Editor Commands
i→ insert modeI→ insert at line startESC→ escape to command modeR→ replace modex→ delete characterdd→ delete lineyy→ copy linep→ pastev→ visual modeo→ open new line below and insertO→ open new line above and inserta→ append after cursorA→ append at end of line:q!→ quit without saving:wqorZZ→ save and quit
ps Command
ps→ Current shell process statusps -E→ All running processesps aux→ All in BSD style with detailsps -EF→ Running process with detailsps -u root→ Processes of specific userps -EF | grep ?→ Find process by pattern
top Command
top -u username→ Show only that user's processestop +d c→ Absolute pathtop +d k→ Kill by PIDtop +d Mor+d P→ Sort by memory or process usage
kill Commands
kill -l→ List all signalskill PID→ Default terminatekill -1→ Restartkill -2→ Equivalent to Ctrl+Ckill -9→ Force killkill -15→ Graceful killkillall→ Kill all processes by namepkill→ Kill by process name
File Ownership
chown→ Change ownershipchgrp→ Change group
Access Control List (ACL)
setfacl→ Set file ACLgetfacl→ Get file ACL
Add Permission to User
setfacl -m u:user:rwx /path/to/file
setfacl -m g:group:rw /path/to/file
Recursive Inheritance from Folder
setfacl -R -m entry /path/to/dir
Remove Specific ACL Entry
setfacl -x u:user /path/to/file
Remove All ACL Entries
setfacl -b /path/to/file
Check ACL
ls -ltr
Example output:
-rw-rw-r--+
Help Commands
whatiscommandcommand --helpmancommand
Add Text to a File
echo "my text" > myfile # Overwrite
cat myfile # Read the text
echo "hello" >> myfile # Append
Input and Output Redirects
3 Redirect Types
stdin→ 0 →<or<<→ Feeding file content to a commandstdout→ 1 →>or>>stderr→ 2 →2>or2>>→ Write error output to a file
tee Command
tee→ Output + Save
echo "test" | tee myfile
tee -a file # Appendls -l→ Listpwd→ Print working directory
dr-xr-xr-x
|
directories
-rw-r--r--
|
file
Linux File Types
| Symbol | Type |
|---|---|
| - | Regular file |
| d | Directory |
| l | Link |
| c | Special file or device file |
| s | Socket → Network communication / process data between external processes |
| p | Named pipe → FIFO → First In First Out, data between internal processes |
| b | Block device |
Inode = Pointer or number of a file on a hard disk.
/boot -> Grub.cfg
/root -> home of root
/dev -> system device (mouse, keyboard)
/etc -> configuration files
/bin -> /usr/bin -> everyday user commands
/sbin -> /usr/sbin -> system/filesystem commands
/opt -> third party app (not part of os)
/proc -> running process (only in memory)
/lib -> /usr/lib -> C prog lib
/tmp -> temporary folder
/home -> user dir
/var -> system logs
/run -> system daemon -> store temporary runtime / PID file
/mnt -> mount external file system
/media -> CDrom