sysctl -a # list all parameterssysctl net.ipv4.ip_forward # check IP forwardingsudo sysctl -w net.ipv4.ip_forward=1 # enable IP forwarding (temp)# Permanent: add to /etc/sysctl.conf or /etc/sysctl.d/99-custom.confsudo sysctl -p # reload sysctl config
Parameter
Purpose
Recommended Value
net.ipv4.ip_forward
Enable IP routing
1 (router) / 0 (host)
net.ipv4.tcp_syncookies
SYN flood protection
1
kernel.randomize_va_space
ASLR
2 (full)
net.ipv4.conf.all.rp_filter
Reverse path filtering
1
net.ipv4.conf.all.accept_redirects
ICMP redirects
0
kernel.dmesg_restrict
Restrict dmesg to root
1
vm.swappiness
Swap aggressiveness
10 (SSD)
fs.file-max
Max open file descriptors
2097152
System Calls & Process Internals
What are System Calls?
The interface between user space and kernel space.
When a program needs kernel services (read file, create process, allocate memory), it makes a syscall.
Linux has ~350+ system calls defined in <sys/syscall.h>.
sequenceDiagram
participant App as 🖥️ User App
participant Lib as 📚 glibc (libc)
participant SC as 🔀 Syscall Interface
participant K as ⚙️ Kernel
App->>Lib: printf() / fopen() / malloc()
Lib->>SC: syscall(SYS_write, ...) / int 0x80
SC->>K: Switches to Ring 0
K->>SC: Executes + returns result
SC->>Lib: Returns to Ring 3
Lib->>App: Returns result
Common System Calls
Syscall
Number
Description
read
0
Read from file descriptor
write
1
Write to file descriptor
open
2
Open a file
close
3
Close a file descriptor
stat
4
Get file status
mmap
9
Map memory
brk
12
Change data segment size
fork
57
Create child process
execve
59
Execute a program
exit
60
Terminate process
socket
41
Create network socket
connect
42
Connect socket to address
clone
56
Create thread/process
Tracing System Calls
strace — trace system calls
strace ls # trace all syscalls of lsstrace -e trace=open,read ls # trace specific syscallsstrace -p 1234 # attach to running processstrace -o output.txt ls # save to filestrace -c ls # count + summarize syscallsstrace -f ./program # follow child processesltrace ./program # trace library calls (libc)
Process Lifecycle
graph LR
NEW["New\nProcess created\nfork() / clone()"]
READY["Ready\nWaiting for CPU\nIn run queue"]
RUN["Running\nOn CPU\nexecve()"]
WAIT["Waiting\nBlocked on I/O\nor signal"]
ZOMBIE["Zombie\nExited, parent\nnot called wait()"]
DEAD["Dead\nParent called wait()\nPCB freed"]
NEW --> READY --> RUN
RUN --> WAIT --> READY
RUN --> ZOMBIE --> DEAD
Process Information
Process inspection
ps aux # all processesps -ef --forest # process tree with parent infopstree -p # visual tree with PIDscat /proc/1234/status # process status (PID 1234)cat /proc/1234/maps # memory map of processcat /proc/1234/fd/ # open file descriptorscat /proc/1234/cmdline # command line argumentscat /proc/1234/environ # environment variablesls -la /proc/1234/exe # executable pathlsof -p 1234 # open files by PIDlsof -i :80 # process using port 80
Signals
Signal
Number
Default Action
Description
SIGHUP
1
Terminate
Hangup (reload config)
SIGINT
2
Terminate
Interrupt (Ctrl+C)
SIGQUIT
3
Core dump
Quit (Ctrl+\)
SIGKILL
9
Terminate
Cannot be caught or ignored
SIGSEGV
11
Core dump
Segmentation fault
SIGTERM
15
Terminate
Graceful termination (default kill)
SIGSTOP
19
Stop
Cannot be caught or ignored
SIGCONT
18
Continue
Resume stopped process
SIGUSR1
10
Terminate
User-defined signal 1
SIGUSR2
12
Terminate
User-defined signal 2
Signal commands
kill -l # list all signalskill -15 1234 # SIGTERM (graceful)kill -9 1234 # SIGKILL (force)kill -HUP 1234 # SIGHUP (reload)killall -9 nginx # kill all by namepkill -f "python script.py" # kill by patterntrap 'echo "Caught SIGINT"' INT # trap signal in bash script
Memory Management
Virtual Memory Architecture
graph TD
subgraph VM["Virtual Address Space (per process)"]
K["Kernel Space\n(top, shared across all processes)"]
S["Stack\n(grows downward)\nLocal vars, return addresses"]
M["Memory-mapped files\nmmap() region"]
H["Heap\n(grows upward)\nmalloc() / new"]
BSS["BSS Segment\nUninitialized global vars"]
DATA["Data Segment\nInitialized global vars"]
TEXT["Text Segment\nProgram code (read-only)"]
end
Memory Concepts
Concept
Description
Virtual Memory
Each process has its own address space — isolated from others
Physical Memory
Actual RAM — managed by kernel page allocator
Page
Fixed-size block (4KB default) — unit of memory management
Page Table
Maps virtual addresses → physical addresses
TLB
Translation Lookaside Buffer — cache for page table lookups
Swap
Disk space used when RAM is full — slow
OOM Killer
Kernel kills processes when RAM is exhausted
ASLR
Address Space Layout Randomization — randomizes memory layout
NX/XD bit
No-Execute bit — prevents code execution in data pages
# ip — modern replacement for ifconfig/routeip addr show # all interfaces + IPsip addr add 192.168.1.100/24 dev eth0ip addr del 192.168.1.100/24 dev eth0ip link set eth0 up/downip link set eth0 mtu 9000 # jumbo framesip route show # routing tableip route add 10.0.0.0/8 via 192.168.1.1ip route del 10.0.0.0/8ip neigh show # ARP tableip neigh flush dev eth0 # flush ARP cache# ss — socket statistics (replaces netstat)ss -tulnp # TCP+UDP listening + processss -s # socket summaryss -tp # TCP connections + processss -o state established # established connectionsss 'dport = :80' # filter by destination port# Network performanceiperf3 -s # start iperf serveriperf3 -c server_ip # test bandwidth to serveriperf3 -c server_ip -u -b 100M # UDP test at 100Mbpsmtr --report google.com # traceroute + packet loss report
iptables / nftables (Firewall)
iptables — Linux firewall
# View rulesiptables -L -n -v # list all rulesiptables -L INPUT -n -v # INPUT chain onlyiptables -t nat -L -n -v # NAT table# Basic rulesiptables -A INPUT -p tcp --dport 22 -j ACCEPT # allow SSHiptables -A INPUT -p tcp --dport 80 -j ACCEPT # allow HTTPiptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPTiptables -A INPUT -j DROP # drop everything else# NAT / Masquerading (router setup)iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADEecho 1 > /proc/sys/net/ipv4/ip_forward# Port forwardingiptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080# Save/restore rulesiptables-save > /etc/iptables/rules.v4iptables-restore < /etc/iptables/rules.v4# nftables (modern replacement)nft list rulesetnft add rule inet filter input tcp dport 22 accept
Network Namespaces (Containers/VMs)
Network namespaces
ip netns list # list namespacesip netns add myns # create namespaceip netns exec myns bash # run shell in namespaceip netns exec myns ip addr # run command in namespaceip link add veth0 type veth peer name veth1 # virtual ethernet pairip link set veth1 netns myns # move veth1 to namespace# This is how Docker creates isolated networks
TCP Deep Dive
sequenceDiagram
participant C as Client
participant S as Server
Note over C,S: 3-Way Handshake (Connection Setup)
C->>S: SYN (seq=x)
S->>C: SYN-ACK (seq=y, ack=x+1)
C->>S: ACK (ack=y+1)
Note over C,S: Data Transfer
C->>S: Data
S->>C: ACK
Note over C,S: 4-Way Teardown (Connection Close)
C->>S: FIN
S->>C: ACK
S->>C: FIN
C->>S: ACK
TCP State
Description
LISTEN
Server waiting for connections
SYN_SENT
Client sent SYN, waiting for SYN-ACK
SYN_RECEIVED
Server received SYN, sent SYN-ACK
ESTABLISHED
Connection active, data transfer
FIN_WAIT_1
Sent FIN, waiting for ACK
TIME_WAIT
Waiting to ensure remote received ACK (2×MSL)
CLOSE_WAIT
Received FIN, waiting for app to close
Filesystem Internals
Inode Architecture
graph LR
FN["Filename\n(directory entry)"]
IN["Inode\nMetadata: size, permissions\ntimestamps, owner, block pointers"]
B1["Data Block 1"]
B2["Data Block 2"]
B3["Data Block N"]
FN --> IN --> B1
IN --> B2
IN --> B3
top -d 1 # real-time, refresh every 1shtop # interactive process viewermpstat -P ALL 1 # per-CPU stats every 1ssar -u 1 10 # CPU utilization 10 timespidstat -u 1 # per-process CPU usageperf top # real-time CPU profilingperf stat ./program # CPU event counts for programperf record ./program && perf report # flame graph data# Load average interpretation# /proc/loadavg: 1min 5min 15min running/total lastpid# Load = 1.0 on single core = 100% utilized# Load > num_cores = overloadednproc # number of CPU corescat /proc/cpuinfo | grep "cpu cores" | head -1