Chapter 21
Troubleshooting Linux
Master systematic approaches to diagnosing and resolving Linux boot failures, networking issues, service problems, and performance bottlenecks using GRUB2, systemd, journalctl, and essential diagnostic tools.
Mục tiêu chương 21
Sau khi hoàn thành chương này, bạn có thể xử lý sự cố Linux một cách có hệ thống.
UEFI & Firmware
Truy cập UEFI/BIOS setup, kiểm tra boot order, sử dụng firmware settings để chẩn đoán lỗi phần cứng trước khi OS khởi động.
GRUB2 Boot Loader
Ngắt quá trình boot từ GRUB menu, chỉnh sửa kernel parameters, khởi động vào rescue/emergency mode để sửa lỗi hệ thống.
systemd & Targets
Hiểu các systemd targets (graphical, multi-user, rescue, emergency), kiểm tra failed units, phân tích thứ tự khởi động dịch vụ.
journalctl & Logs
Đọc và lọc system journal với journalctl, sử dụng dmesg để xem kernel messages, theo dõi log theo unit/priority/time.
Network Troubleshooting
Chẩn đoán lỗi mạng với ip, ping, traceroute, ss, dig, nmcli. Kiểm tra interface, routing, DNS, và firewall rules.
Software & Package Issues
Giải quyết dependency conflicts với dnf, kiểm tra package integrity với rpm -V, tìm kiếm và cài đặt lại package bị lỗi.
Lý thuyết chi tiết
Các khái niệm và công cụ cốt lõi cho troubleshooting Linux hiệu quả.
1. UEFI Firmware & GRUB2 Boot Loader
Khi Linux không khởi động được, quá trình chẩn đoán bắt đầu từ firmware (UEFI/BIOS). Sau firmware, GRUB2 (Grand Unified Bootloader version 2) là thành phần tiếp theo có thể gây ra sự cố. GRUB2 cho phép bạn can thiệp vào quá trình boot để truyền thêm tham số cho kernel hoặc khởi động vào chế độ khẩn cấp.
Truy cập GRUB2 Menu:
- Giữ hoặc nhấn Shift khi boot (BIOS)
- Nhấn Esc liên tục khi boot (UEFI)
- GRUB menu xuất hiện với danh sách kernel entries
- Nhấn e để edit entry hiện tại
GRUB Config Files:
/boot/grub2/grub.cfg— generated file, do not edit/etc/default/grub— edit this for permanent changes/etc/grub.d/— script fragments- Run
grub2-mkconfig -o /boot/grub2/grub.cfgafter editing
Chỉnh sửa kernel parameters tạm thời trong GRUB:
# Tại GRUB menu, nhấn 'e' để edit entry
# Tìm dòng bắt đầu bằng 'linux' hoặc 'linux16'
linux /vmlinuz-5.14.0 root=/dev/mapper/rhel-root ro \
crashkernel=auto resume=/dev/mapper/rhel-swap \
rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet
# Thêm tham số troubleshooting, ví dụ: khởi động vào emergency target
linux /vmlinuz-5.14.0 root=/dev/mapper/rhel-root ro \
crashkernel=auto resume=/dev/mapper/rhel-swap \
rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap systemd.unit=emergency.target
# Nhấn Ctrl+X hoặc F10 để boot với tham số đã thay đổi
# Thay đổi này chỉ có hiệu lực lần boot này, không được lưu lại
Các systemd targets hữu ích cho troubleshooting:
| Target | Runlevel tương đương | Mô tả |
|---|---|---|
| graphical.target | Runlevel 5 | Full GUI mode (default) |
| multi-user.target | Runlevel 3 | CLI multi-user, networking on |
| rescue.target | Runlevel 1 | Single user, basic system, mounts filesystems |
| emergency.target | — | Minimal, root filesystem read-only, no services |
2. systemd Troubleshooting
systemd quản lý toàn bộ vòng đời của hệ thống Linux từ init đến shutdown. Khi một dịch vụ không khởi động được hoặc hệ thống chạy chậm, các công cụ systemd cung cấp thông tin chi tiết để chẩn đoán.
# Kiểm tra các unit đang bị failed
$ systemctl --failed
UNIT LOAD ACTIVE SUB DESCRIPTION
● httpd.service loaded failed failed The Apache HTTP Server
● postgresql.service loaded failed failed PostgreSQL Database Server
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state.
SUB = The low-level unit activation substate.
2 loaded units listed.
# Kiểm tra status chi tiết của một service
$ systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
Active: failed (Result: exit-code) since Mon 2025-03-10 08:23:11 EST
Process: 2847 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=1/FAILURE)
Main PID: 2847 (code=exited, status=1/FAILURE)
Mar 10 08:23:11 server httpd[2847]: AH00526: Syntax error on line 97 of /etc/httpd/conf/httpd.conf:
Mar 10 08:23:11 server httpd[2847]: Invalid command 'ServerNme', perhaps misspelled or defined
# Kiểm tra thời gian boot của hệ thống
$ systemd-analyze time
Startup finished in 1.523s (firmware) + 2.187s (loader) + 1.456s (kernel) + 3.821s (initrd) + 12.345s (userspace) = 21.332s
graphical.target reached after 12.234s in userspace
# Xem critical path của quá trình boot (blame)
$ systemd-analyze blame | head -10
5.231s NetworkManager-wait-online.service
3.421s plymouth-quit-wait.service
2.341s dracut-initqueue.service
1.872s lvm2-monitor.service
1.654s firewalld.service
1.123s tuned.service
0.987s sssd.service
0.876s chronyd.service
0.765s auditd.service
0.654s httpd.service
# Xuất SVG biểu đồ boot timeline
$ systemd-analyze plot > /tmp/boot-analysis.svg
# Mở file SVG trong trình duyệt để xem waterfall chart
# Kiểm tra các vấn đề về unit configuration
$ systemd-analyze verify httpd.service
# Không có output = không có lỗi cú pháp
# Xem dependency tree của một service
$ systemctl list-dependencies httpd.service
httpd.service
● ├─-.mount
● ├─system.slice
● └─sysinit.target
● ├─dev-hugepages.mount
● ├─dev-mqueue.mount
● └─network.target
# Thay đổi default target
$ systemctl set-default multi-user.target
$ systemctl get-default
multi-user.target
# Chuyển sang target khác ngay lập tức (không reboot)
$ systemctl isolate rescue.target
# CẢNH BÁO: Sẽ ngắt tất cả session người dùng!
3. journalctl & Log Analysis
journald (systemd's journal daemon) thu thập và lưu trữ log từ kernel, initrd, services, và applications. journalctl là công cụ CLI để query và hiển thị journal entries. Ngoài ra, các file log truyền thống trong /var/log/ vẫn tồn tại song song.
# Xem toàn bộ journal (dùng 'q' để thoát)
$ journalctl
# Xem log của một service cụ thể
$ journalctl -u httpd.service
-- Logs begin at Mon 2025-03-10 07:00:01 EST, end at Mon 2025-03-10 09:15:33 EST. --
Mar 10 08:23:10 server systemd[1]: Starting The Apache HTTP Server...
Mar 10 08:23:11 server httpd[2847]: AH00526: Syntax error on line 97 of /etc/httpd/conf/httpd.conf:
Mar 10 08:23:11 server httpd[2847]: Invalid command 'ServerNme', perhaps misspelled
Mar 10 08:23:11 server systemd[1]: httpd.service: Main process exited, code=exited, status=1/FAILURE
Mar 10 08:23:11 server systemd[1]: Failed to start The Apache HTTP Server.
# Chỉ xem log lỗi (error và critical)
$ journalctl -p err
$ journalctl -p 0..3 # 0=emerg, 1=alert, 2=crit, 3=err
# Theo dõi log real-time (như tail -f)
$ journalctl -f
$ journalctl -f -u sshd.service # follow specific service
# Lọc theo thời gian
$ journalctl --since "2025-03-10 08:00:00" --until "2025-03-10 09:00:00"
$ journalctl --since "1 hour ago"
$ journalctl --since today
# Xem log từ lần boot này
$ journalctl -b
$ journalctl -b -1 # lần boot trước
$ journalctl --list-boots # liệt kê tất cả boot logs
# Xem kernel messages (tương tự dmesg)
$ journalctl -k
$ dmesg | head -30
[ 0.000000] Linux version 5.14.0-427.13.1.el9_4.x86_64
[ 0.000000] BIOS-provided physical RAM map:
[ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
...
[ 15.234567] EXT4-fs (sda1): mounted filesystem with ordered data mode
Log Files quan trọng trong /var/log/
/var/log/messages — General system messages/var/log/secure — Authentication, sudo, SSH/var/log/httpd/error_log — Apache error log/var/log/audit/audit.log — SELinux AVC denials/var/log/boot.log — Boot process messages/var/log/dnf.log — Package install/remove history4. Network Troubleshooting
Sự cố mạng thường xảy ra theo 4 lớp: physical interface, IP configuration, routing, và DNS/application. Tiếp cận theo thứ tự từ dưới lên (bottom-up) giúp xác định vấn đề nhanh hơn.
# 1. Kiểm tra network interfaces và IP addresses
$ ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP
link/ether 52:54:00:ab:cd:ef brd ff:ff:ff:ff:ff:ff
inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic ens3
# 2. Kiểm tra routing table
$ ip route show
default via 192.168.1.1 dev ens3 proto dhcp metric 100
192.168.1.0/24 dev ens3 proto kernel scope link src 192.168.1.100
# 3. Test connectivity bằng ping
$ ping -c 4 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=116 time=12.3 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=116 time=11.8 ms
--- 8.8.8.8 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss
# 4. Trace route đến đích
$ traceroute google.com
traceroute to google.com (142.250.80.46), 30 hops max, 60 byte packets
1 _gateway (192.168.1.1) 1.234 ms 1.123 ms 1.098 ms
2 10.0.0.1 (10.0.0.1) 5.432 ms 5.321 ms 5.210 ms
3 * * * # packet dropped/filtered
4 142.250.80.46 12.345 ms 12.234 ms 12.123 ms
# 5. Kiểm tra open ports và listening services
$ ss -tlnp
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=987))
LISTEN 0 128 *:80 *:* users:(("httpd",pid=2456))
LISTEN 0 128 *:443 *:* users:(("httpd",pid=2456))
# 6. Kiểm tra DNS resolution
$ dig google.com +short
142.250.80.46
$ nslookup google.com
Server: 192.168.1.1
Address: 192.168.1.1#53
Non-authoritative answer:
Name: google.com
Address: 142.250.80.46
# 7. Kiểm tra NetworkManager
$ nmcli device status
DEVICE TYPE STATE CONNECTION
ens3 ethernet connected ens3
lo loopback unmanaged --
$ nmcli connection show
NAME UUID TYPE DEVICE
ens3 a1b2c3d4-e5f6-7890-abcd-ef1234567890 ethernet ens3
Network Troubleshooting Checklist
- Kiểm tra interface UP/DOWN:
ip link show - Kiểm tra IP address:
ip addr show - Ping gateway:
ip route | grep default, rồi ping địa chỉ đó - Ping IP ngoài:
ping 8.8.8.8(loại trừ DNS issue) - Ping hostname:
ping google.com(test DNS) - Kiểm tra firewall:
firewall-cmd --list-all - Kiểm tra SELinux:
ausearch -m avc -ts recent
5. Software & Package Troubleshooting
Vấn đề với packages thường biểu hiện dưới dạng dependency conflicts, missing libraries, hoặc corrupted installations. DNF và RPM cung cấp các công cụ mạnh để chẩn đoán và sửa chữa.
# Kiểm tra dependency errors khi cài đặt
$ dnf install httpd
Error: Problem: package httpd-2.4.57-5.el9.x86_64 requires mod_http2 >= 1.15.7
- package mod_http2-1.15.7-8.el9.x86_64 conflicts with libnghttp2 < 1.43.0
- cannot install the best candidate for the job
# Giải quyết: cài đặt đầy đủ group liên quan
$ dnf groupinstall "Basic Web Server"
$ dnf install --allowerasing httpd # cho phép thay thế conflicting packages
# Kiểm tra integrity của package đã cài
$ rpm -V httpd
.......T. c /etc/httpd/conf/httpd.conf
SM5....T. /usr/sbin/httpd
# Giải thích output rpm -V:
# S = file Size changed M = Mode/permissions changed
# 5 = MD5 checksum mismatch T = mTime changed
# c = config file (expected to change)
# Cài đặt lại package bị corrupt
$ dnf reinstall httpd
$ rpm --force -ivh /path/to/httpd.rpm # reinstall from local RPM
# Tìm package nào cung cấp một file cụ thể
$ dnf provides /usr/sbin/httpd
httpd-2.4.57-5.el9.x86_64 : Apache HTTP Server
Repo : @System
$ dnf provides "*/libssl.so.3"
# Xem lịch sử cài đặt/xóa
$ dnf history
ID | Command | Date and time | Action | Altered
----+------------------------+------------------+--------+---------
12 | install httpd | 2025-03-10 08:15 | Install | 3
11 | remove vsftpd | 2025-03-09 15:30 | Remove | 1
# Undo một transaction
$ dnf history undo 12
$ dnf history info 12 # xem chi tiết transaction 12
6. Memory & Disk Troubleshooting
Memory diagnostics:
# Kiểm tra RAM usage
$ free -h
total used free buff/cache
Mem: 7.6G 3.2G 1.8G 2.6G
Swap: 2.0G 512M 1.5G
# Top processes dùng nhiều RAM
$ ps aux --sort=-%mem | head -10
$ top # nhấn M để sort by memory
# Check OOM killer (Out of Memory)
$ journalctl -k | grep -i "oom"
$ dmesg | grep -i "out of memory"
# Test RAM hardware
$ memtest86+ # boot từ GRUB
Disk diagnostics:
# Kiểm tra disk space
$ df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda1 xfs 50G 42G 8.0G 84% /
/dev/sda2 xfs 100G 10G 90G 10% /home
# Tìm file/dir chiếm nhiều dung lượng
$ du -sh /* 2>/dev/null | sort -rh | head -10
4.2G /var
2.1G /usr
1.8G /home
# Check filesystem errors (unmount first)
$ fsck /dev/sda1 # RHEL: xfs_repair
$ xfs_repair /dev/sda1
# Disk I/O performance
$ iostat -x 1 5
$ iotop # top for I/O
Lab 21: Systematic Linux Troubleshooting
Thực hành quy trình troubleshooting có hệ thống trên RHEL/Rocky Linux 9.
Mục tiêu Lab
Simulate và troubleshoot một loạt sự cố Linux thực tế: service crash do misconfiguration, network connectivity failure, disk full issue, và package dependency conflict. Thực hành phương pháp chẩn đoán có hệ thống sử dụng systemd, journalctl, ip tools, và dnf.
Simulate và phát hiện Service Failure
Cố tình tạo lỗi cú pháp trong httpd.conf, sau đó chẩn đoán và sửa.
# Đảm bảo httpd đã được cài và đang chạy
$ dnf install -y httpd
$ systemctl start httpd
$ systemctl status httpd
● httpd.service - The Apache HTTP Server
Active: active (running) since ...
# Tạo lỗi cú pháp trong cấu hình
$ echo "InvalidDirective on" >> /etc/httpd/conf/httpd.conf
# Restart để trigger lỗi
$ systemctl restart httpd
Job for httpd.service failed. See 'journalctl -xe' for details.
# Chẩn đoán lỗi bằng journalctl
$ journalctl -u httpd.service -n 20
Mar 10 09:00:01 server httpd[3456]: AH00526: Syntax error on line 210 of /etc/httpd/conf/httpd.conf:
Mar 10 09:00:01 server httpd[3456]: Invalid command 'InvalidDirective'
# Validate config trước khi restart
$ apachectl configtest
AH00526: Syntax error on line 210 of /etc/httpd/conf/httpd.conf:
Invalid command 'InvalidDirective', perhaps misspelled
# Sửa lỗi: xóa dòng cuối
$ sed -i '$ d' /etc/httpd/conf/httpd.conf
$ apachectl configtest
Syntax OK
$ systemctl start httpd
$ systemctl status httpd
● httpd.service - The Apache HTTP Server
Active: active (running)
Phân tích Boot Performance với systemd-analyze
Xác định các service làm chậm quá trình khởi động hệ thống.
# Xem tổng thời gian boot
$ systemd-analyze time
Startup finished in 1.2s (firmware) + 2.1s (loader) + 1.8s (kernel) + 4.2s (initrd) + 15.3s (userspace) = 24.6s
graphical.target reached after 15.1s in userspace
# Liệt kê top 15 service chậm nhất
$ systemd-analyze blame | head -15
5.231s NetworkManager-wait-online.service
3.421s dracut-initqueue.service
2.341s lvm2-monitor.service
1.654s firewalld.service
# Nếu NetworkManager-wait-online quá chậm, có thể disable
$ systemctl disable NetworkManager-wait-online.service
# Chú ý: chỉ disable nếu server không cần network trước khi services khác start
# Kiểm tra các failed units
$ systemctl --failed
UNIT LOAD ACTIVE SUB DESCRIPTION
● cups.service loaded failed failed CUPS Scheduler
# Kiểm tra log chi tiết của failed unit
$ journalctl -u cups.service --since "10 minutes ago"
$ systemctl status cups.service
Network Connectivity Troubleshooting
Chẩn đoán lỗi kết nối mạng theo phương pháp bottom-up.
# Layer 1: Kiểm tra interface state
$ ip link show ens3
2: ens3: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 state DOWN
# NO-CARRIER = cable bị ngắt hoặc switch port vấn đề
$ ip link show ens3
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP
# UP,LOWER_UP = kết nối vật lý OK
# Layer 2: Kiểm tra IP address
$ ip addr show ens3
# Nếu không có IP, thử:
$ nmcli connection up ens3
$ dhclient ens3 # xin DHCP thủ công
# Layer 3: Kiểm tra routing và gateway
$ ip route show
default via 192.168.1.1 dev ens3
$ ping -c 2 192.168.1.1 # ping gateway
# Layer 4: Kiểm tra DNS
$ cat /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
$ dig +short google.com
# Kiểm tra firewall không block traffic
$ firewall-cmd --list-all
$ firewall-cmd --list-services
services: cockpit dhcpv6-client http https ssh
# Kiểm tra SELinux không chặn network
$ getsebool -a | grep httpd_can_network
httpd_can_network_connect --> off
$ setsebool -P httpd_can_network_connect on
Xử lý Disk Full Issue
Chẩn đoán và giải phóng dung lượng đĩa khi filesystem đạt 100%.
# Phát hiện filesystem full
$ df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda1 xfs 20G 19.9G 0 100% /
# 100% = dịch vụ sẽ crash khi cần ghi file!
# Tìm thư mục chiếm nhiều dung lượng nhất
$ du -sh /* 2>/dev/null | sort -rh | head
4.5G /var
3.1G /home
1.2G /usr
$ du -sh /var/* 2>/dev/null | sort -rh | head
3.8G /var/log
400M /var/cache
$ du -sh /var/log/* 2>/dev/null | sort -rh | head
3.5G /var/log/httpd
200M /var/log/audit
# Rotate và clear large log files
$ logrotate -f /etc/logrotate.conf
$ journalctl --vacuum-size=200M # giới hạn journal xuống 200MB
$ journalctl --vacuum-time=2weeks # xóa journal cũ hơn 2 tuần
# Clear DNF cache
$ dnf clean all
$ rm -rf /var/cache/dnf/*
# Clear temp files
$ rm -rf /tmp/*
$ rm -rf /var/tmp/dnf-*
# Kiểm tra lại sau khi dọn dẹp
$ df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda1 xfs 20G 15G 5G 75% /
Boot vào Emergency Mode qua GRUB2
Thực hành khởi động vào emergency target khi hệ thống không boot bình thường.
# Tại màn hình GRUB, nhấn 'e' để edit boot entry
# Tìm dòng 'linux' và thêm vào cuối dòng:
linux /vmlinuz-5.14.0 root=/dev/mapper/rhel-root ro ...
rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap systemd.unit=emergency.target
# Nhấn Ctrl+X để boot
# Hệ thống sẽ dừng ở emergency shell với root filesystem read-only
# Nhập root password khi được hỏi
# Trong emergency shell, remount / thành read-write để chỉnh sửa
$ mount -o remount,rw /
$ mount -a # mount tất cả filesystem từ /etc/fstab
# Sửa vấn đề (ví dụ: sửa fstab sai)
$ vi /etc/fstab
# Kiểm tra syntax fstab
$ findmnt --verify
# Cho phép SELinux relabel khi reboot (nếu cần)
$ touch /.autorelabel
# Reboot bình thường
$ systemctl reboot
SELinux AVC Denial Troubleshooting
Xác định và giải quyết SELinux blocking service operations.
# Triệu chứng: service không start dù config đúng
# Kiểm tra SELinux status
$ getenforce
Enforcing
# Tìm AVC denials gần đây
$ ausearch -m avc -ts recent
type=AVC msg=audit(1710062401.234:567): avc: denied { read } for
pid=3456 comm="httpd" name="custom.conf" dev="sda1" ino=98765
scontext=system_u:system_r:httpd_t:s0
tcontext=system_u:object_r:admin_home_t:s0 tclass=file
# Giải thích: httpd cố đọc file có context admin_home_t, bị từ chối
# Dùng sealert để xem giải pháp đề xuất
$ sealert -a /var/log/audit/audit.log
# Output sẽ đề xuất cách fix, ví dụ:
# Run: chcon -t httpd_sys_content_t /etc/httpd/conf.d/custom.conf
# Or: semanage fcontext -a -t httpd_sys_content_t "/etc/httpd/conf.d(/.*)?"
# Áp dụng fix permanent
$ semanage fcontext -a -t httpd_sys_content_t "/etc/httpd/conf.d/custom.conf"
$ restorecon -v /etc/httpd/conf.d/custom.conf
Relabeled /etc/httpd/conf.d/custom.conf from admin_home_t to httpd_sys_content_t
$ systemctl restart httpd
$ systemctl status httpd
● httpd.service - The Apache HTTP Server
Active: active (running)
Kiểm tra Package Integrity với rpm -V
Xác minh không có binary system nào bị thay đổi trái phép.
# Verify integrity của một package
$ rpm -V sshd
.......T. c /etc/ssh/sshd_config
SM5....T. /usr/sbin/sshd
# Giải thích flags:
# S = file Size mismatch M = Mode (permissions) mismatch
# 5 = MD5 checksum mismatch T = mTime changed
# c = config file (OK to change) . = test passed
# /usr/sbin/sshd bị thay đổi: S=size, M=perms, 5=checksum!
# Đây có thể là dấu hiệu bị compromise
# Verify tất cả packages trên hệ thống
$ rpm -Va 2>/dev/null | grep -v "^....5.*c "
# ^....5.*c = bỏ qua checksum mismatch của config files (bình thường)
# Cài đặt lại package bị nghi ngờ
$ dnf reinstall openssh-server
$ rpm -V openssh-server
# Không có output = tất cả files OK
Tổng kết: Troubleshooting Workflow
Quy trình chuẩn cho mọi sự cố Linux:
Khi Service không start:
systemctl status <service>journalctl -u <service> -n 50- Kiểm tra config syntax
- Kiểm tra SELinux:
ausearch -m avc -ts recent - Kiểm tra firewall:
firewall-cmd --list-all
Khi hệ thống không boot:
- Boot vào emergency target qua GRUB
mount -o remount,rw /- Kiểm tra
/etc/fstabsyntax - Kiểm tra
dmesg | tail -50 - Kiểm tra disk space:
df -h
Câu hỏi ôn tập
Kiểm tra kiến thức troubleshooting Linux của bạn.
Thêm tham số systemd.unit=emergency.target vào cuối dòng linux trong GRUB edit mode. Ví dụ: thêm sau rhgb quiet. Emergency target khởi động hệ thống với filesystem root ở chế độ read-only và không chạy bất kỳ service nào, cung cấp shell tối thiểu để sửa chữa. Sau khi chỉnh sửa, nhấn Ctrl+X hoặc F10 để boot. Thay đổi này chỉ áp dụng cho lần boot đó và không được lưu vĩnh viễn.
Dùng systemctl --failed để liệt kê tất cả units đang bị failed. Để xem log chi tiết, dùng journalctl -u <unit-name> — ví dụ: journalctl -u httpd.service. Thêm flag -n 50 để chỉ xem 50 dòng cuối, hoặc -f để theo dõi real-time. Lệnh systemctl status <unit> cũng hiển thị một số dòng log gần nhất cùng với trạng thái hiện tại.
rescue.target (tương đương runlevel 1): Mount tất cả filesystem, kích hoạt swap, không start networking nhưng cung cấp shell single-user với quyền root. Phù hợp để sửa lỗi khi hệ thống file vẫn accessible.
emergency.target: Khởi động tối thiểu nhất — chỉ mount root filesystem ở chế độ read-only, không mount các filesystem khác, không start bất kỳ service nào. Dùng khi rescue.target cũng không boot được, ví dụ khi /etc/fstab bị lỗi.
systemd-analyze blame liệt kê tất cả systemd units sắp xếp theo thời gian khởi động (activation time) từ chậm nhất đến nhanh nhất. Output hiển thị tên service và thời gian tính bằng giây. Lệnh này rất hữu ích khi hệ thống boot chậm — giúp xác định service "thủ phạm" đang làm chậm quá trình boot. Ví dụ: nếu NetworkManager-wait-online.service mất 30 giây, bạn có thể disable nó nếu không cần thiết. Kết hợp với systemd-analyze time để xem tổng thời gian và systemd-analyze plot để xuất biểu đồ SVG.
Dùng journalctl -p err để chỉ xem error và nghiêm trọng hơn. Hoặc journalctl -p 0..3 để xem từ emergency đến error. Các syslog priority levels:
- 0 = emerg — Hệ thống không thể sử dụng
- 1 = alert — Cần hành động ngay
- 2 = crit — Lỗi nghiêm trọng
- 3 = err — Lỗi thông thường
- 4 = warning — Cảnh báo
- 5 = notice — Điều kiện bình thường nhưng đáng chú ý
- 6 = info — Thông tin
- 7 = debug — Debug messages
Khi IP connectivity hoạt động (ping 8.8.8.8 OK) nhưng hostname resolution thất bại, vấn đề chắc chắn là DNS. Các bước kiểm tra:
- Kiểm tra
cat /etc/resolv.conf— xem có DNS server được configure không - Test DNS query trực tiếp:
dig google.com @8.8.8.8 - Kiểm tra
/etc/nsswitch.conf— xem thứ tự resolution (files, dns) - Kiểm tra NetworkManager DNS:
nmcli connection show <conn> | grep dns - Restart NetworkManager:
systemctl restart NetworkManager
Mỗi ký tự trong output rpm -V đại diện cho một kiểm tra khác nhau, dấu chấm (.) = test pass, ký tự = test fail:
- S = file Size thay đổi so với database
- M = Mode (permissions và file type) thay đổi
- 5 = MD5 checksum không khớp — nội dung file đã bị sửa đổi
- T = mTime (modification time) thay đổi
Output này rất đáng lo ngại — binary /usr/sbin/httpd đã bị thay đổi về size, permissions, và nội dung. Đây có thể là dấu hiệu bị tấn công (compromised binary). Hành động: reinstall package ngay lập tức bằng dnf reinstall httpd và điều tra security incident.
Để giảm kích thước systemd journal:
journalctl --vacuum-size=200M— xóa entries cũ cho đến khi journal < 200MBjournalctl --vacuum-time=2weeks— xóa entries cũ hơn 2 tuần- Để cấu hình vĩnh viễn: edit
/etc/systemd/journald.conf, đặtSystemMaxUse=200M
Để clear DNF cache:
dnf clean all— xóa tất cả cached packages và metadatadnf clean packages— chỉ xóa cached packagesrm -rf /var/cache/dnf/*— xóa trực tiếp
Có hai công cụ chính:
- ausearch:
ausearch -m avc -ts recent— tìm kiếm AVC denial messages trong audit log, cho kết quả thô. Flag-ts recentchỉ lấy entries gần đây. - sealert (từ package
setroubleshoot-server):sealert -a /var/log/audit/audit.log— phân tích AVC denials và đề xuất lệnh cụ thể để fix (ví dụ:semanage fcontext,setsebool, hoặcchcon).
Quy trình: đọc ausearch để xem context bị denied → dùng sealert để hiểu nguyên nhân và nhận giải pháp → áp dụng fix với semanage + restorecon cho permanent fix.
Lệnh ss -tlnp hiển thị tất cả TCP sockets đang listening (chờ kết nối), với thông tin về port và process. Giải thích các flags:
- -t = TCP sockets only (bỏ UDP)
- -l = Listening sockets only (không hiện established connections)
- -n = Numeric — hiện port number thay vì service name (ví dụ: 22 thay vì ssh)
- -p = Process — hiện tên process và PID đang giữ socket
Lệnh này thay thế netstat -tlnp (đã deprecated). Dùng để xác nhận service đang lắng nghe đúng port, hoặc kiểm tra có port bất thường nào không (dấu hiệu malware/backdoor).