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ệnh |
Mô tả |
Ví dụ |
ip address show |
Hiển thị tất cả địa chỉ IP |
ip -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 up |
Chỉ hiển thị giao diện đang hoạt động |
– |
ip address show [dev $(interface)] permanent |
Chỉ hiển thị địa chỉ tĩnh |
ip addr show dev eth0 permanent |
ip address show [dev $(interface)] dynamic |
Chỉ 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ện |
ip 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ãn |
ip 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ệnh |
Mô tả |
Ví dụ |
ip route get $(address)/$(mask) |
Xem route thực tế kernel dùng |
ip route get 192.0.2.1 |
ip route show cached |
Xem bộ đệm route (kernel < 3.6) |
– |
ip route add $(address)/$(mask) via $(next_hop) |
Thêm route qua gateway |
ip 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ện |
ip route add 192.0.2.0/25 dev eth0 |
ip route change $(route_spec) |
Sửa route |
ip route change 192.168.2.0/24 via 10.0.0.1 |
ip route delete $(route_spec) |
Xóa route |
ip route delete 192.0.2.0/25 dev eth0 |
ip route add default via $(address) |
Thêm route mặc định |
ip route add default via 192.168.1.1 |
ip route add blackhole $(address)/$(mask) |
Route loại bỏ gói tin |
ip 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ảng |
ip route add throw 192.0.2.0/24 |
ip route add $(address)/$(mask) via $(gateway) metric $(number) |
Thêm route với độ ưu tiên |
ip 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 routing |
ip 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ệnh |
Mô tả |
Ví dụ |
ip link show |
Hiển thị tất cả liên kết |
ip 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ết |
ip link set dev eth0 up |
ip link set dev $(interface) alias "$(description)" |
Đặt biệt danh |
ip link set dev eth0 alias "LAN interface" |
ip link set dev $(old_name) name $(new_name) |
Đổi tên giao diện |
ip link set dev eth0 name lan (phải tắt trước) |
ip link set dev $(interface) address $(address) |
Thay đổi địa chỉ MAC |
ip link set dev eth0 address 22:ce:e0:99:63:6f |
ip link set dev $(interface) mtu $(value) |
Thay đổi MTU |
ip link set dev tun0 mtu 1480 |
ip link delete dev $(interface) |
Xóa liên kết |
ip link delete dev tun0 |
ip link set $(interface) multicast [on|off] |
Bật/tắt multicast |
ip link set eth0 multicast on |
ip link set $(interface) arp [on|off] |
Bật/tắt ARP |
ip link set eth0 arp off |
ip link add name $(vlan_name) link $(parent) type vlan id $(tag) |
Tạo VLAN |
ip link add name vlan10 link eth0 type vlan id 10 |
ip link add $(interface) type ifb |
Tạo giao diện IFB |
ip link add ifb10 type ifb |
ip link add name $(veth1) type veth peer name $(veth2) |
Tạo cặp veth |
ip 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ệnh |
Mô tả |
Ví dụ |
ip link set dev $(interface) group $(number) |
Thêm giao diện vào nhóm |
ip link set dev eth0 group 42 |
ip link set dev $(interface) group 0 |
Xóa giao diện khỏi nhóm |
ip link set dev eth0 group 0 |
echo "$(number) $(name)" > /etc/iproute2/group |
Gán tên tượng trưng cho nhóm |
echo "10 customer-vlans" > /etc/iproute2/group |
ip link set group $(number) $(operation) |
Thực hiện thao tác trên nhóm |
ip link set group 42 down |
ip link show group $(number) |
Xem thông tin nhóm |
ip 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ệnh |
Mô tả |
Ví dụ |
ip tuntap add dev $(interface) mode $(mode) pi |
Tạo tun/tap với định dạng gói PI |
ip tuntap add dev tun1 mode tun pi |
ip tuntap add dev $(interface) mode $(mode) one_queue |
Tạo tun/tap bỏ qua flow control |
ip tuntap add dev tun1 mode tun one_queue |
ip tuntap del dev $(interface) |
Xóa tun/tap |
ip 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ệnh |
Mô tả |
Ví dụ |
ip neighbor show |
Xem bảng neighbor |
ip neigh show |
ip neighbor show dev $(interface) |
Xem neighbor của giao diện |
ip neigh show dev eth0 |
ip neighbor flush dev $(interface) |
Xóa bảng neighbor của giao diện |
ip neigh flush dev eth1 |
ip neighbor>ip neighbor add $(address) lladdr $(mac) dev $(interface) |
Thêm bản ghi neighbor |
ip 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 neighbor |
ip 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ệnh |
Mô 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 GRE |
ip 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óa |
ip 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 IPv6 |
ip -6 tunnel add gre0 mode ip6gre local 2001:db8::1 remote 2001:db8::2 |
ip tunnel del $(interface) |
Xóa tunnel |
ip tunnel del gre0 |
ip tunnel change $(interface) $(options) |
Sửa tunnel |
ip tunnel change tun0 remote 203.0.113.89 key 23456 |
ip tunnel show |
Xem thông tin tunnel |
ip 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ệnh |
Mô 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 UDP |
ip 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 tunnel |
ip l2tp add session tunnel_id 1 session_id 1 |
ip l2tp show session |
Xem thông tin session |
ip 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ệnh |
Mô tả |
Ví dụ |
ip route add $(route_options) table $(table_id) |
Tạo route trong bảng tùy chỉnh |
ip route add 192.0.2.0/27 via 203.0.113.1 table 10 |
ip route show table $(table_id) |
Xem route trong bảng |
ip route show table 100 |
ip rule add from $(source_network) $(action) |
Rule theo mạng nguồn |
ip rule add from 192.0.2.0/24 lookup 10 |
ip rule add to $(dest_network) $(action) |
Rule theo mạng đích |
ip rule add to 192.0.2.0/24 blackhole |
ip rule add tos $(tos_value) $(action) |
Rule theo ToS |
ip rule add tos 10 lookup 110 |
ip rule add fwmark $(mark) $(action) |
Rule theo firewall mark |
ip rule add fwmark 0x11 lookup 100 |
ip rule add iif $(interface) $(action) |
Rule theo giao diện vào |
ip rule add iif eth0 lookup 10 |
ip rule add oif $(interface) $(action) |
Rule theo giao diện ra |
ip rule add oif eth0 lookup 10 |
ip rule add $(options) $(action) priority $(value) |
Rule với độ ưu tiên |
ip rule add from 192.0.2.0/24 lookup 10 priority 20 |
ip rule show |
Xem tất cả rule |
ip -6 rule show |
ip rule del $(options) $(action) |
Xóa rule |
ip rule del from 192.0.2.0/24 lookup 10 |
ip rule flush |
Xóa tất cả rule |
ip -6 rule flush |
- Bảng mặc định:
local
(255), main
(254), default
(253).
Quản lý cấu hình sysctl (Netconf)
Lệnh |
Mô tả |
Ví dụ |
ip netconf show |
Xem 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ện |
ip netconf show dev eth0 |
Quản lý không gian mạng (Network Namespace)
Lệnh |
Mô tả |
Ví dụ |
ip netns add $(namespace) |
Tạo namespace |
ip netns add ns1 |
ip netns list |
Liệt kê namespace |
– |
ip netns delete $(namespace) |
Xóa namespace |
ip netns delete ns1 |
ip netns exec $(namespace) $(command) |
Chạy lệnh trong namespace |
ip netns exec ns1 ip addr |
ip netns pids $(namespace) |
Liệt kê PID trong namespace |
ip netns pids ns1 |
ip netns identify $(pid) |
Xác định namespace của PID |
ip netns identify 1234 |
ip link set $(interface) netns $(namespace) |
Gán giao diện vào namespace |
ip 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 namespace |
ip link add name veth0 type veth peer name veth1; ip link set veth1 netns ns1 |
ip netns monitor |
Theo 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ệnh |
Mô 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 VXLAN |
ip link add vxlan0 type vxlan id 100 group 239.1.1.1 dev eth0 |
Quản lý Multicast
Lệnh |
Mô tả |
Ví dụ |
ip maddress show |
Xem nhóm multicast |
ip maddr show dev eth0 |
ip maddress add $(mac_addr) dev $(interface) |
Thêm địa chỉ multicast |
ip maddr add 01:00:5e:00:00:ab dev eth0 |
ip mroute show |
Xem 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ệnh |
Mô tả |
Ví dụ |
ip monitor |
Theo 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ừ rtmon |
ip monitor route file /var/log/rtmon.log |
rtmon [-family inet|inet6] $(event_type) file $(log_file) |
Ghi log nhị phân |
rtmon -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.
Tải xuống cheat sheet