متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 70Network · Linux

How to add a network route

A server with two networks (public and private), a branch network behind a VPN gateway, a backup network — the default gateway is not always the way. A static route says: "for 10.20.0.0/16, use gateway 10.0.1.254". This how-to checks which way a packet goes now, adds a route for a test, makes it permanent with NetworkManager (RHEL) and netplan (Ubuntu), and fixes the usual errors.

  • 5 الدروس
  • 4 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

70.1

The quick answer

ip route get 10.20.0.5
ip route add 10.20.0.0/16 via 10.0.1.254 dev ens224
nmcli connection modify ens224 +ipv4.routes "10.20.0.0/16 10.0.1.254"
nmcli connection up ens224
ip route

ip route add is for a quick test (lost at reboot); nmcli … +ipv4.routes makes it permanent.

الأوامر في هذا الدرس

70.2

Step by step

  1. Which way does a packet go now?
ip route get 10.20.0.5
10.20.0.5 via 192.0.2.1 dev ens192 src 192.0.2.10 uid 1000
ip route
default via 192.0.2.1 dev ens192 proto static metric 100
10.0.1.0/24 dev ens224 proto kernel scope link src 10.0.1.11 metric 101
192.0.2.0/24 dev ens192 proto kernel scope link src 192.0.2.10 metric 100

Here 10.20.0.5 goes out of the public interface — wrong.

  1. Test with a temporary route (gone after reboot or network restart):
ip route add 10.20.0.0/16 via 10.0.1.254 dev ens224
ip route get 10.20.0.5
10.20.0.5 via 10.0.1.254 dev ens224 src 10.0.1.11
ping -c 3 10.20.0.5
ip route del 10.20.0.0/16

The gateway (via) must be in a network the server is directly connected to (here 10.0.1.0/24).

  1. Make it permanent — RHEL / NetworkManager:
nmcli connection modify ens224 +ipv4.routes "10.20.0.0/16 10.0.1.254"
nmcli connection up ens224
nmcli -g ipv4.routes connection show ens224
10.20.0.0/16 10.0.1.254

Remove it again with -ipv4.routes "10.20.0.0/16 10.0.1.254". A metric: "10.20.0.0/16 10.0.1.254 50".

  1. Make it permanent — Ubuntu / netplan:
/etc/netplan/60-static.yaml (part)
    ens224:
      addresses: [10.0.1.11/24]
      routes:
        - to: 10.20.0.0/16
          via: 10.0.1.254
netplan try
  1. Change the default gateway (careful on a remote server — use the console or netplan try):
nmcli connection modify ens192 ipv4.gateway 192.0.2.254
nmcli connection up ens192
  1. In the cloud (AWS, Azure, GCP) routes between subnets are set in the VPC/VNet route tables, not on the server — a route on the server alone is often not enough.

70.3

Check that it worked

ip route get 10.20.0.5
tracepath -n 10.20.0.5

The route uses the right gateway and interface, the target answers, and after a reboot ip route still shows it.

70.4

Common problems

ProblemCauseFix
RTNETLINK answers: Network is unreachableThe gateway is not in a directly connected network.Use a gateway in one of the server's own subnets.
RTNETLINK answers: File existsThe route (or one with the same target) exists.ip route — delete or replace it (ip route replace …).
The route is gone after rebootIt was added with ip route add only.Add it with nmcli or netplan.
Ping goes out, answers never come backThe other side has no route back (asymmetric routing), or rp_filter drops it.Add the return route on the other side/gateway.
Two default routesBoth interfaces got a gateway (DHCP).ipv4.never-default yes on the private connection.
Works on the server, not for other subnets in the cloudCloud route tables / security rules.Add the route in the VPC/VNet route table too.

الأوامر في هذا الدرس

70.5

Go further

1How do you see which gateway and interface a packet to 10.20.0.5 will use?
2Why is a route added with ip route add lost after a reboot, and how do you keep it?

It lives only in the running kernel; add it to the connection with nmcli … +ipv4.routes (or netplan).

كل الأوامر في هذا الفصل