Thứ Ba, 21 tháng 10, 2025

IProute2 Cheat Sheet

iproute2 là bộ công cụ quản lý mạng trên Linux, thay thế cho net-tools, hỗ trợ cấu hình địa chỉ, định tuyến, liên kết, tunnel, v.v.

Ghi chú:

  • $(address) là địa chỉ IP (IPv4/IPv6, dạng thập phân chấm hoặc prefix).
  • $(mask) có thể là subnet mask (255.255.255.0) hoặc độ dài prefix (/24).

Quản lý địa chỉ (Address Management)

LệnhMô tảVí dụ
ip address showHiển thị tất cả địa chỉ IPip -4 addr show (chỉ IPv4)
ip address show $(interface)Hiển thị địa chỉ của giao diện cụ thểip addr show eth0
ip address show upChỉ hiển thị giao diện đang hoạt động
ip address show [dev $(interface)] permanentChỉ hiển thị địa chỉ tĩnhip addr show dev eth0 permanent
ip address show [dev $(interface)] dynamicChỉ hiển thị địa chỉ động (DHCP)ip addr show dev eth0 dynamic
ip address add $(address)/$(mask) dev $(interface)Thêm địa chỉ vào giao diệnip addr add 192.0.2.10/24 dev eth0
ip addr add 2001:db8:1::48/64 dev tun10
ip address add $(address)/$(mask) dev $(interface) label $(label)Thêm địa chỉ với nhãnip addr add 192.0.2.1/24 dev eth0 label eth0:alias
  • Địa chỉ đầu tiên thêm vào là primary (dùng làm nguồn mặc định).
  • Có thể thêm nhiều địa chỉ trên cùng giao diện.

Quản lý định tuyến (Route Management)

LệnhMô tảVí dụ
ip route get $(address)/$(mask)Xem route thực tế kernel dùngip route get 192.0.2.1
ip route show cachedXem bộ đệm route (kernel < 3.6)
ip route add $(address)/$(mask) via $(next_hop)Thêm route qua gatewayip route add 192.0.2.128/25 via 192.0.2.1
ip route add $(address)/$(mask) dev $(interface)Thêm route qua giao diệnip route add 192.0.2.0/25 dev eth0
ip route change $(route_spec)Sửa routeip route change 192.168.2.0/24 via 10.0.0.1
ip route delete $(route_spec)Xóa routeip route delete 192.0.2.0/25 dev eth0
ip route add default via $(address)Thêm route mặc địnhip route add default via 192.168.1.1
ip route add blackhole $(address)/$(mask)Route loại bỏ gói tinip route add blackhole 192.0.2.1/32
ip route add unreachable $(address)/$(mask)Route trả về “unreachable”ip route add unreachable 192.0.2.0/24
ip route add prohibit $(address)/$(mask)Route trả về “prohibited”ip route add prohibit 192.0.2.0/24
ip route add throw $(address)/$(mask)Route dừng xử lý bảngip route add throw 192.0.2.0/24
ip route add $(address)/$(mask) via $(gateway) metric $(number)Thêm route với độ ưu tiênip route add 192.168.2.0/24 via 10.0.1.1 metric 5
ip route add $(address)/$(mask) nexthop via $(gw1) weight $(w1) nexthop via $(gw2) weight $(w2)Multipath routingip route add default nexthop via 192.168.1.1 weight 1 nexthop dev ppp0 weight 10
  • Lưu ý: Route tĩnh sẽ bị xóa nếu giao diện ngừng hoạt động, cần phần mềm như NetworkManager để khôi phục.

Quản lý liên kết (Link Management)

LệnhMô tảVí dụ
ip link showHiển thị tất cả liên kếtip link show
ip link show dev $(interface)Hiển thị liên kết cụ thểip link show dev eth0
ip link set dev $(interface) [up|down]Bật/tắt liên kếtip link set dev eth0 up
ip link set dev $(interface) alias "$(description)"Đặt biệt danhip link set dev eth0 alias "LAN interface"
ip link set dev $(old_name) name $(new_name)Đổi tên giao diệnip link set dev eth0 name lan (phải tắt trước)
ip link set dev $(interface) address $(address)Thay đổi địa chỉ MACip link set dev eth0 address 22:ce:e0:99:63:6f
ip link set dev $(interface) mtu $(value)Thay đổi MTUip link set dev tun0 mtu 1480
ip link delete dev $(interface)Xóa liên kếtip link delete dev tun0
ip link set $(interface) multicast [on|off]Bật/tắt multicastip link set eth0 multicast on
ip link set $(interface) arp [on|off]Bật/tắt ARPip link set eth0 arp off
ip link add name $(vlan_name) link $(parent) type vlan id $(tag)Tạo VLANip link add name vlan10 link eth0 type vlan id 10
ip link add $(interface) type ifbTạo giao diện IFBip link add ifb10 type ifb
ip link add name $(veth1) type veth peer name $(veth2)Tạo cặp vethip link add name veth-host type veth peer name veth-guest
  • IFB: Dùng để chuyển hướng lưu lượng với tc.
  • veth: Tạo ở trạng thái UP, không cần bật thủ công.

Quản lý nhóm liên kết (Link Group Management)

LệnhMô tảVí dụ
ip link set dev $(interface) group $(number)Thêm giao diện vào nhómip link set dev eth0 group 42
ip link set dev $(interface) group 0Xóa giao diện khỏi nhómip link set dev eth0 group 0
echo "$(number) $(name)" > /etc/iproute2/groupGán tên tượng trưng cho nhómecho "10 customer-vlans" > /etc/iproute2/group
ip link set group $(number) $(operation)Thực hiện thao tác trên nhómip link set group 42 down
ip link show group $(number)Xem thông tin nhómip link show group 42
  • Tên nhóm có thể thay thế số trong lệnh.

Quản lý thiết bị Tun/Tap (Tun and Tap Devices)

LệnhMô tảVí dụ
ip tuntap add dev $(interface) mode $(mode) piTạo tun/tap với định dạng gói PIip tuntap add dev tun1 mode tun pi
ip tuntap add dev $(interface) mode $(mode) one_queueTạo tun/tap bỏ qua flow controlip tuntap add dev tun1 mode tun one_queue
ip tuntap del dev $(interface)Xóa tun/tapip tuntap del dev tun0
  • Tun/Tap: Mô phỏng thiết bị mạng cho chương trình userspace.

Quản lý bảng Neighbor (ARP/NDP Tables)

LệnhMô tảVí dụ
ip neighbor showXem bảng neighborip neigh show
ip neighbor show dev $(interface)Xem neighbor của giao diệnip neigh show dev eth0
ip neighbor flush dev $(interface)Xóa bảng neighbor của giao diệnip neigh flush dev eth1
ip neighbor>ip neighbor add $(address) lladdr $(mac) dev $(interface)Thêm bản ghi neighborip neigh add 192.0.2.1 lladdr 22:ce:e0:99:63:6f dev eth0
ip neighbor delete $(address) lladdr $(mac) dev $(interface)Xóa bản ghi neighborip neigh delete 192.0.2.1 lladdr 22:ce:e0:99:63:6f dev eth0
  • Hỗ trợ cả “neighbour” (UK spelling).

Quản lý tunnel (Tunnel Management)

LệnhMô tảVí dụ
ip tunnel add $(interface) mode ipip local $(local_addr) remote $(remote_addr)Tạo tunnel IPIP (IPv4 trong IPv4)ip tunnel add tun0 mode ipip local 192.168.1.1 remote 203.0.113.1
ip tunnel add $(interface) mode sit local $(local_addr) remote $(remote_addr)Tạo tunnel SIT (IPv6 trong IPv4)ip tunnel add tun0 mode sit local 192.168.1.1 remote 203.0.113.1
ip -6 tunnel add $(interface) mode ip6ip6 local $(local_addr) remote $(remote_addr)Tạo tunnel IP6IP6 (IPv6 trong IPv6)ip -6 tunnel add tun0 mode ip6ip6 local 2001:db8::1 remote 2001:db8::2
ip tunnel add $(interface) mode ipip6 local $(local_addr) remote $(remote_addr)Tạo tunnel IPIP6 (IPv4 trong IPv6)ip tunnel add tun0 mode ipip6 local 2001:db8::1 remote 2001:db8::2
ip link add $(interface) type gretap local $(local_addr) remote $(remote_addr)Tạo tunnel gretap (Ethernet qua GRE)ip link add gretap0 type gretap local 192.168.1.1 remote 203.0.113.1
ip tunnel add $(interface) mode gre local $(local_addr) remote $(remote_addr)Tạo tunnel GREip tunnel add gre0 mode gre local 192.168.1.1 remote 203.0.113.1
ip tunnel add $(interface) mode gre local $(local_addr) remote $(remote_addr) key $(key)Tạo GRE với khóaip tunnel add gre0 mode gre local 192.168.1.1 remote 203.0.113.1 key 1234
ip -6 tunnel add $(interface) mode ip6gre local $(local_addr) remote $(remote_addr)Tạo GRE qua IPv6ip -6 tunnel add gre0 mode ip6gre local 2001:db8::1 remote 2001:db8::2
ip tunnel del $(interface)Xóa tunnelip tunnel del gre0
ip tunnel change $(interface) $(options)Sửa tunnelip tunnel change tun0 remote 203.0.113.89 key 23456
ip tunnel showXem thông tin tunnelip tunnel show tun0
  • Tunnel tạo ở trạng thái DOWN, cần dùng ip link set $(interface) up để bật.

Quản lý L2TPv3 Pseudowire

LệnhMô tảVí dụ
ip l2tp add tunnel tunnel_id $(local_id) peer_tunnel_id $(remote_id) encap udp udp_sport $(sport) udp_dport $(dport) local $(local_addr) remote $(remote_addr)Tạo tunnel L2TPv3 qua UDPip l2tp add tunnel tunnel_id 1 peer_tunnel_id 2 encap udp udp_sport 1701 udp_dport 1701 local 192.168.1.1 remote 203.0.113.1
ip l2tp add session tunnel_id $(tunnel_id) session_id $(session_id)Thêm session vào tunnelip l2tp add session tunnel_id 1 session_id 1
ip l2tp show sessionXem thông tin sessionip l2tp show session session_id 1 tunnel_id 1
  • Mỗi tunnel có thể chứa nhiều session, mỗi session gắn với giao diện l2tpethX.

Quản lý định tuyến theo chính sách (Policy-Based Routing)

LệnhMô tảVí dụ
ip route add $(route_options) table $(table_id)Tạo route trong bảng tùy chỉnhip route add 192.0.2.0/27 via 203.0.113.1 table 10
ip route show table $(table_id)Xem route trong bảngip route show table 100
ip rule add from $(source_network) $(action)Rule theo mạng nguồnip rule add from 192.0.2.0/24 lookup 10
ip rule add to $(dest_network) $(action)Rule theo mạng đíchip rule add to 192.0.2.0/24 blackhole
ip rule add tos $(tos_value) $(action)Rule theo ToSip rule add tos 10 lookup 110
ip rule add fwmark $(mark) $(action)Rule theo firewall markip rule add fwmark 0x11 lookup 100
ip rule add iif $(interface) $(action)Rule theo giao diện vàoip rule add iif eth0 lookup 10
ip rule add oif $(interface) $(action)Rule theo giao diện raip rule add oif eth0 lookup 10
ip rule add $(options) $(action) priority $(value)Rule với độ ưu tiênip rule add from 192.0.2.0/24 lookup 10 priority 20
ip rule showXem tất cả ruleip -6 rule show
ip rule del $(options) $(action)Xóa ruleip rule del from 192.0.2.0/24 lookup 10
ip rule flushXóa tất cả ruleip -6 rule flush
  • Bảng mặc địnhlocal (255), main (254), default (253).

Quản lý cấu hình sysctl (Netconf)

LệnhMô tảVí dụ
ip netconf showXem cấu hình sysctl tất cả giao diện
ip netconf show dev $(interface)Xem cấu hình sysctl của giao diệnip netconf show dev eth0

Quản lý không gian mạng (Network Namespace)

LệnhMô tảVí dụ
ip netns add $(namespace)Tạo namespaceip netns add ns1
ip netns listLiệt kê namespace
ip netns delete $(namespace)Xóa namespaceip netns delete ns1
ip netns exec $(namespace) $(command)Chạy lệnh trong namespaceip netns exec ns1 ip addr
ip netns pids $(namespace)Liệt kê PID trong namespaceip netns pids ns1
ip netns identify $(pid)Xác định namespace của PIDip netns identify 1234
ip link set $(interface) netns $(namespace)Gán giao diện vào namespaceip link set eth0 netns ns1
ip link add name $(veth1) type veth peer name $(veth2); ip link set $(veth2) netns $(namespace)Kết nối namespaceip link add name veth0 type veth peer name veth1; ip link set veth1 netns ns1
ip netns monitorTheo dõi sự kiện namespace
  • Namespace cô lập stack mạng, dùng cho bảo mật hoặc quản lý VM.

Quản lý VXLAN

LệnhMô tảVí dụ
ip link add $(interface) type vxlan id $(vni) [group $(mcast_addr) | remote $(remote_addr)] dev $(phys_interface)Tạo liên kết VXLANip link add vxlan0 type vxlan id 100 group 239.1.1.1 dev eth0

Quản lý Multicast

LệnhMô tảVí dụ
ip maddress showXem nhóm multicastip maddr show dev eth0
ip maddress add $(mac_addr) dev $(interface)Thêm địa chỉ multicastip maddr add 01:00:5e:00:00:ab dev eth0
ip mroute showXem route multicast
  • Chủ yếu dùng để debug, không thêm route thủ công.

Theo dõi sự kiện mạng (Network Event Monitoring)

LệnhMô tảVí dụ
ip monitorTheo dõi tất cả sự kiện
ip monitor $(event_type)Theo dõi sự kiện cụ thể (link, address, route, mroute, neigh)ip monitor route
ip monitor $(event_type) file $(log_file)Đọc log từ rtmonip monitor route file /var/log/rtmon.log
rtmon [-family inet|inet6] $(event_type) file $(log_file)Ghi log nhị phânrtmon -family inet route file /var/log/rtmon.log

Mẹo sử dụng

  • IPv4/IPv6: Thêm -4 hoặc -6 vào lệnh để lọc (ví dụ: ip -6 addr show).
  • Debug: Dùng ip -s link để xem thống kê chi tiết.
  • Tạm thời: Các thay đổi không lưu vĩnh viễn trừ khi thêm vào cấu hình hệ thống.
=============================
Website không chứa bất kỳ quảng cáo nào, mọi đóng góp để duy trì phát triển cho website (donation) xin vui lòng gửi về STK 90.2142.8888 - Ngân hàng Vietcombank Thăng Long - TRAN VAN BINH
=============================
Nếu bạn không muốn bị AI thay thế và tiết kiệm 3-5 NĂM trên con đường trở thành DBA chuyên nghiệp hay làm chủ Database thì hãy đăng ký ngay KHOÁ HỌC ORACLE DATABASE A-Z ENTERPRISE, được Coaching trực tiếp từ tôi với toàn bộ bí kíp thực chiến, thủ tục, quy trình của gần 20 năm kinh nghiệm (mà bạn sẽ KHÔNG THỂ tìm kiếm trên Internet/Google) từ đó giúp bạn dễ dàng quản trị mọi hệ thống Core tại Việt Nam và trên thế giới, đỗ OCP.
- CÁCH ĐĂNG KÝ: Gõ (.) hoặc để lại số điện thoại hoặc inbox https://m.me/tranvanbinh.vn hoặc Hotline/Zalo 090.29.12.888
- Chi tiết tham khảo:
https://bit.ly/oaz_w
=============================
2 khóa học online qua video giúp bạn nhanh chóng có những kiến thức nền tảng về Linux, Oracle, học mọi nơi, chỉ cần có Internet/4G:
- Oracle cơ bản: https://bit.ly/admin_1200
- Linux: https://bit.ly/linux_1200
=============================
KẾT NỐI VỚI CHUYÊN GIA TRẦN VĂN BÌNH:
📧 Mail: binhoracle@gmail.com
☎️ Mobile/Zalo: 0902912888
👨 Facebook: https://www.facebook.com/BinhOracleMaster
👨 Inbox Messenger: https://m.me/101036604657441 (profile)
👨 Fanpage: https://www.facebook.com/tranvanbinh.vn
👨 Inbox Fanpage: https://m.me/tranvanbinh.vn
👨👩 Group FB: https://www.facebook.com/groups/DBAVietNam
👨 Website: https://www.tranvanbinh.vn
👨 Blogger: https://tranvanbinhmaster.blogspot.com
🎬 Youtube: https://www.youtube.com/@binhguru
👨 Tiktok: https://www.tiktok.com/@binhguru
👨 Linkin: https://www.linkedin.com/in/binhoracle
👨 Twitter: https://twitter.com/binhguru
👨 Podcast: https://www.podbean.com/pu/pbblog-eskre-5f82d6
👨 Địa chỉ: Tòa nhà Sun Square - 21 Lê Đức Thọ - Phường Mỹ Đình 1 - Quận Nam Từ Liêm - TP.Hà Nội

=============================
cơ sở dữ liệu, cơ sở dữ liệu quốc gia, database, AI, trí tuệ nhân tạo, artificial intelligence, machine learning, deep learning, LLM, ChatGPT, DeepSeek, Grok, oracle tutorial, học oracle database, Tự học Oracle, Tài liệu Oracle 12c tiếng Việt, Hướng dẫn sử dụng Oracle Database, Oracle SQL cơ bản, Oracle SQL là gì, Khóa học Oracle Hà Nội, Học chứng chỉ Oracle ở đầu, Khóa học Oracle online,sql tutorial, khóa học pl/sql tutorial, học dba, học dba ở việt nam, khóa học dba, khóa học dba sql, tài liệu học dba oracle, Khóa học Oracle online, học oracle sql, học oracle ở đâu tphcm, học oracle bắt đầu từ đâu, học oracle ở hà nội, oracle database tutorial, oracle database 12c, oracle database là gì, oracle database 11g, oracle download, oracle database 19c/21c/23c/23ai, oracle dba tutorial, oracle tunning, sql tunning , oracle 12c, oracle multitenant, Container Databases (CDB), Pluggable Databases (PDB), oracle cloud, oracle security, oracle fga, audit_trail,oracle RAC, ASM, oracle dataguard, oracle goldengate, mview, oracle exadata, oracle oca, oracle ocp, oracle ocm , oracle weblogic, postgresql tutorial, mysql tutorial, mariadb tutorial, ms sql server tutorial, nosql, mongodb tutorial, oci, cloud, middleware tutorial, docker, k8s, micro service, hoc solaris tutorial, hoc linux tutorial, hoc aix tutorial, unix tutorial, securecrt, xshell, mobaxterm, putty

ĐỌC NHIỀU

Trần Văn Bình - Oracle Database Master