hoatranlab.io.vn Zalo: 0917516878 Hotline: 0917516878 [email protected]
HoaTranLab Logo HoaTranLab
Linux Bible 11th Edition • Chapter 14

Administering Networking

Quản trị mạng Linux: ip, nmcli, NetworkManager, /etc/hosts, /etc/resolv.conf, kiểm tra kết nối, cấu hình routes và DNS.

Mục tiêu học tập

Kiểm tra Network Interfaces

Dùng ip addr show, ip link, ifconfig để xem thông tin IP, MAC, trạng thái interface, loopback và wireless.

Routing và kết nối

Xem routing table với ip route show, kiểm tra kết nối với ping/traceroute, thêm/xóa route tĩnh.

NetworkManager & nmcli

Cấu hình network với nmcli từ command line, nmtui với giao diện text menu, thiết lập IP tĩnh không cần GUI.

/etc/hosts và /etc/resolv.conf

Cấu hình hostname resolution local, DNS servers trong resolv.conf, hiểu thứ tự tra cứu host trong nsswitch.conf.

DHCP và IP tĩnh

Hiểu cách DHCP cấp IP, subnet mask, gateway, DNS. Cấu hình IP tĩnh cho server environments không có DHCP.

Troubleshooting Network

Sử dụng ping, traceroute, ss, netstat, nmap để chẩn đoán sự cố mạng, kiểm tra ports đang mở, theo dõi kết nối.

Lý thuyết chi tiết

Lệnh ip — Công cụ quản lý mạng hiện đại

Lệnh ip thay thế ifconfig/route/arp cũ. Cú pháp: ip [object] [command]. Các object phổ biến: addr, link, route, neigh.

Terminal — ip command

# Xem tất cả network interfaces

$ ip addr show

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536

inet 127.0.0.1/8 scope host lo

2: eno1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 state DOWN

link/ether f8:b4:6a:a6:93:96 brd ff:ff:ff:ff:ff:ff

3: wlp0s20f3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP

link/ether 06:58:63:3e:af:87 brd ff:ff:ff:ff:ff:ff

inet 192.168.1.24/24 brd 192.168.1.255 scope global dynamic

# Xem chỉ một interface

$ ip addr show wlp0s20f3

# Thêm IP address

$ sudo ip addr add 192.168.1.100/24 dev eno1

# Xóa IP address

$ sudo ip addr del 192.168.1.100/24 dev eno1

# Bật/tắt interface

$ sudo ip link set eno1 up

$ sudo ip link set eno1 down

# Xem với statistics

$ ip -s addr show wlp0s20f3

RX: bytes packets errors dropped

250962570 208402 0 4

Routing: ip route và route

Routing table xác định đường đi của packet. Default route (gateway) chuyển tiếp packet không khớp route nào. Lệnh ip route là công cụ hiện đại, route là legacy.

Terminal — routing

# Xem routing table

$ ip route show

default via 192.168.1.1 dev wlp0s20f3 proto dhcp src 192.168.1.24

172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1

192.168.1.0/24 dev wlp0s20f3 proto kernel scope link src 192.168.1.24

$ route -n

Kernel IP routing table

Destination Gateway Genmask Flags Metric Iface

0.0.0.0 192.168.1.1 0.0.0.0 UG 600 wlp0s20f3

192.168.1.0 0.0.0.0 255.255.255.0 U 600 wlp0s20f3

# Thêm route tĩnh

$ sudo ip route add 192.168.200.0/24 via 192.168.1.199

# Xóa route

$ sudo ip route del 192.168.200.0/24

# Traceroute: theo dõi đường đi packet

$ traceroute google.com

1 192.168.1.1 (192.168.1.1) 1.123 ms

2 10.0.0.1 (10.0.0.1) 5.456 ms

...19 74.125.235.136 169.360 ms

nmcli — NetworkManager Command Line

NetworkManager quản lý network connections trên Linux. nmcli là công cụ command-line đầy đủ tính năng. nmtui cung cấp giao diện text-based cho server không có GUI.

Terminal — nmcli

# Xem tất cả connections

$ nmcli connection show

NAME UUID TYPE DEVICE

joenet-5G 645980f3-166d-... wifi wlp0s20f3

Wired Conn 1 eb28103d-af3b-... ethernet eno1

# Xem devices

$ nmcli device status

DEVICE TYPE STATE CONNECTION

wlp0s20f3 wifi connected joenet-5G

eno1 ethernet disconnected --

# Tạo connection IP tĩnh

$ sudo nmcli connection add type ethernet con-name "static-eth" ifname eno1 \

ipv4.addresses 192.168.1.100/24 \

ipv4.gateway 192.168.1.1 \

ipv4.dns "8.8.8.8 8.8.4.4" \

ipv4.method manual

# Bật/tắt connection

$ sudo nmcli connection up "static-eth"

$ sudo nmcli connection down "static-eth"

# Sửa connection

$ sudo nmcli connection modify "static-eth" ipv4.dns "1.1.1.1"

# Khởi động lại NetworkManager

$ sudo systemctl restart NetworkManager

/etc/hosts, /etc/resolv.conf và hostname

/etc/hosts + /etc/resolv.conf

# /etc/hosts — local hostname resolution

$ cat /etc/hosts

127.0.0.1 localhost localhost.localdomain

::1 localhost localhost.localdomain

192.168.0.201 node1.example.com node1 joe

192.168.0.202 node2.example.com node2 sally

# /etc/resolv.conf — DNS servers

$ cat /etc/resolv.conf

nameserver 8.8.8.8

nameserver 8.8.4.4

search example.com

# Hostname

$ hostname

spike.example.com

$ dnsdomainname

example.com

$ sudo hostnamectl set-hostname newserver.example.com

# Test DNS resolution

$ nslookup google.com

Server: 8.8.8.8

Name: google.com

Address: 142.250.185.78

$ dig google.com

$ host google.com

Công cụ chẩn đoán mạng

Terminal — ping, ss, netstat

# ping: kiểm tra kết nối

$ ping -c 4 host1

64 bytes from host1 (192.168.1.15): icmp_seq=1 ttl=64 time=0.062 ms

4 packets transmitted, 4 received, 0% packet loss

$ ping6 ::1

# ss: xem connections (thay netstat)

$ ss -tlnp

State Recv-Q Send-Q Local Address:Port

LISTEN 0 128 0.0.0.0:22 users:(("sshd",pid=1234))

LISTEN 0 5 0.0.0.0:80 users:(("httpd",pid=2345))

# -t TCP, -u UDP, -l listening, -n numeric, -p process

$ ss -an | grep :22

# netstat (legacy, cần net-tools)

$ netstat -tlnp

# nmap: scan ports

$ sudo nmap -sS 192.168.1.0/24

$ nmap -p 22,80,443 192.168.1.100

PORT STATE SERVICE

22/tcp open ssh

80/tcp open http

443/tcp closed https

NetworkManager Configuration Files

/etc/NetworkManager/system-connections/wifi.nmconnection (Fedora)

[connection]

id=joenet-5G

uuid=645980f3-166d-446d-8516-1abe9e4c76e5

type=wifi

interface-name=wlp0s20f3

[wifi]

mode=infrastructure

ssid=joenet-5G

[wifi-security]

key-mgmt=wpa-psk

psk=MyPassword

[ipv4]

method=auto

/etc/netplan/... (Ubuntu — YAML format)

network:

version: 2

ethernets:

eno1:

addresses:

- "192.168.100.100/24"

nameservers:

addresses: [8.8.8.8, 8.8.4.4]

routes:

- to: default

via: 192.168.100.1

$ sudo netplan apply

Lab thực hành

Bài lab: Kiểm tra network, cấu hình IP tĩnh với nmcli, thêm host vào /etc/hosts, chẩn đoán sự cố mạng.

1

Kiểm tra network interfaces hiện tại

Terminal

$ ip addr show

$ ip link show

$ nmcli device status

$ nmcli connection show

# Ghi lại tên interface (eth0, eno1, enp2s0...)

2

Kiểm tra kết nối và routing

Terminal

$ ip route show

$ ping -c 3 127.0.0.1

$ ping -c 3 8.8.8.8

$ ping -c 3 google.com

$ traceroute google.com

# Nếu ping 8.8.8.8 ok nhưng google.com không ok → DNS problem

3

Cấu hình IP tĩnh với nmcli

Terminal

# Thay INTERFACE bằng tên interface thực tế

$ sudo nmcli connection add type ethernet \

con-name "server-static" ifname INTERFACE \

ipv4.addresses "192.168.1.200/24" \

ipv4.gateway "192.168.1.1" \

ipv4.dns "8.8.8.8 1.1.1.1" \

ipv4.method manual

$ sudo nmcli connection up "server-static"

$ ip addr show INTERFACE

inet 192.168.1.200/24 brd 192.168.1.255 scope global INTERFACE

4

Cấu hình /etc/hosts cho local resolution

Terminal

$ sudo vim /etc/hosts

# Thêm các dòng:

192.168.1.10 webserver web01

192.168.1.20 dbserver db01

$ ping web01

PING webserver (192.168.1.10): 56 bytes of data.

$ ssh db01

5

Xem ports đang mở với ss

Terminal

$ ss -tlnp

State Recv-Q Send-Q Local Address:Port

LISTEN 0 128 0.0.0.0:22 users:(("sshd"))

$ ss -ulnp

# Port 22 = SSH, 80 = HTTP, 443 = HTTPS

# 3306 = MySQL, 5432 = PostgreSQL

$ sudo nmap -sS localhost

6

Test DNS và kiểm tra hostname

Terminal

$ hostname

myserver.example.com

$ hostnamectl

Static hostname: myserver.example.com

$ nslookup google.com 8.8.8.8

Server: 8.8.8.8

Name: google.com

Address: 142.250.185.78

$ dig +short google.com

142.250.185.78

Câu hỏi ôn tập

1

Lệnh nào xem IP address của tất cả network interfaces trên Linux hiện đại?

ip addr show

Legacy: ifconfig (cần cài net-tools, deprecated trên nhiều distro). Xem cụ thể một interface: ip addr show wlp0s20f3

2

Làm thế nào để cấu hình IP tĩnh 192.168.1.100 cho interface eno1 bằng nmcli?

sudo nmcli connection modify "Wired connection 1" ipv4.addresses "192.168.1.100/24" ipv4.gateway "192.168.1.1" ipv4.dns "8.8.8.8" ipv4.method manual && sudo nmcli connection up "Wired connection 1"
3

File /etc/hosts dùng để làm gì? Cho ví dụ nội dung file.

/etc/hosts cho phép ánh xạ hostname thành IP address locally mà không cần DNS server. Được tra cứu trước DNS (theo /etc/nsswitch.conf). Ví dụ:
192.168.0.201 node1.example.com node1
192.168.0.202 node2.example.com node2

4

Lệnh nào xem kernel routing table? Giải thích ý nghĩa của default route.

ip route show hoặc route -n
Default route (0.0.0.0/0 via GATEWAY): là "cửa thoát" cho mọi packet không khớp route cụ thể. Ví dụ: default via 192.168.1.1 dev wlp0s20f3 — mọi traffic ra Internet đều qua router 192.168.1.1.

5

Khi ping IP thành công nhưng ping hostname thất bại, nguyên nhân là gì?

Đây là vấn đề DNS resolution. Kết nối mạng OK (ping IP thành công) nhưng hệ thống không thể chuyển hostname thành IP. Kiểm tra: cat /etc/resolv.conf (DNS server đúng không?), nslookup hostname (DNS trả lời gì?), cat /etc/hosts (có entry local không?).

6

ss -tlnp hiển thị gì và các flags có nghĩa là gì?

Hiển thị các socket đang listening (chờ kết nối).
-t: TCP sockets, -u: UDP sockets
-l: listening sockets
-n: numeric (không resolve tên)
-p: process info (cần root)
Ví dụ output: LISTEN 0.0.0.0:22 users:(("sshd",pid=1234)) — sshd đang lắng nghe port 22 trên mọi IP.

7

NetworkManager lưu configuration files ở đâu trên Fedora và Ubuntu?

Fedora/RHEL: /etc/NetworkManager/system-connections/ — định dạng .ini-style (.nmconnection)
Ubuntu: /etc/netplan/ — định dạng YAML, áp dụng bằng netplan apply
Cả hai đều được NetworkManager quản lý. Không khuyến nghị sửa trực tiếp — dùng nmcli hoặc nmtui để tránh lỗi.

8

Lệnh nào thêm route tĩnh đến network 10.20.0.0/16 qua gateway 192.168.1.254?

sudo ip route add 10.20.0.0/16 via 192.168.1.254

Lưu ý: route này là tạm thời, mất sau reboot. Để vĩnh viễn, thêm vào NetworkManager connection: nmcli connection modify "conn-name" +ipv4.routes "10.20.0.0/16 192.168.1.254"