What does “scope” do in ip route and why it is necessary to setup static route in Linux?












0















If I want to replace default dhcp route rules with static ones, I have to add a rule ip route add scope link dev eth0. Or I will get an error: “Nexthop has a invalid gateway”.



Here are my questions:
Q1: What does “scope link” mean in ip route?
Q2: Why it is necessary to change from dhcp rules to static rules?










share|improve this question

























  • What full commands give the error, and what full commands are you using to add the link routes?

    – grawity
    Dec 31 '18 at 15:39













  • @grawity After deleting all rules, I execute “ip route add default 192.168.1.1 proto static dev eth0”

    – user762750
    Dec 31 '18 at 15:41
















0















If I want to replace default dhcp route rules with static ones, I have to add a rule ip route add scope link dev eth0. Or I will get an error: “Nexthop has a invalid gateway”.



Here are my questions:
Q1: What does “scope link” mean in ip route?
Q2: Why it is necessary to change from dhcp rules to static rules?










share|improve this question

























  • What full commands give the error, and what full commands are you using to add the link routes?

    – grawity
    Dec 31 '18 at 15:39













  • @grawity After deleting all rules, I execute “ip route add default 192.168.1.1 proto static dev eth0”

    – user762750
    Dec 31 '18 at 15:41














0












0








0








If I want to replace default dhcp route rules with static ones, I have to add a rule ip route add scope link dev eth0. Or I will get an error: “Nexthop has a invalid gateway”.



Here are my questions:
Q1: What does “scope link” mean in ip route?
Q2: Why it is necessary to change from dhcp rules to static rules?










share|improve this question
















If I want to replace default dhcp route rules with static ones, I have to add a rule ip route add scope link dev eth0. Or I will get an error: “Nexthop has a invalid gateway”.



Here are my questions:
Q1: What does “scope link” mean in ip route?
Q2: Why it is necessary to change from dhcp rules to static rules?







linux networking iproute2 routing-table






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 3:39







user762750

















asked Dec 31 '18 at 15:35









user762750user762750

226




226













  • What full commands give the error, and what full commands are you using to add the link routes?

    – grawity
    Dec 31 '18 at 15:39













  • @grawity After deleting all rules, I execute “ip route add default 192.168.1.1 proto static dev eth0”

    – user762750
    Dec 31 '18 at 15:41



















  • What full commands give the error, and what full commands are you using to add the link routes?

    – grawity
    Dec 31 '18 at 15:39













  • @grawity After deleting all rules, I execute “ip route add default 192.168.1.1 proto static dev eth0”

    – user762750
    Dec 31 '18 at 15:41

















What full commands give the error, and what full commands are you using to add the link routes?

– grawity
Dec 31 '18 at 15:39







What full commands give the error, and what full commands are you using to add the link routes?

– grawity
Dec 31 '18 at 15:39















@grawity After deleting all rules, I execute “ip route add default 192.168.1.1 proto static dev eth0”

– user762750
Dec 31 '18 at 15:41





@grawity After deleting all rules, I execute “ip route add default 192.168.1.1 proto static dev eth0”

– user762750
Dec 31 '18 at 15:41










1 Answer
1






active

oldest

votes


















2














These routes are how you tell the Linux kernel what subnet(s) you're in.



This information isn't stored in a hidden "my current subnet" field when you add an IP address; instead it's always converted to a 'scope link' route with no gateway specified (also called "device route" or "interface route"), and whenever the kernel needs to determine whether some address is directly reachable, it just performs a routing table check.



Normally those routes are added automatically as soon as you configure an IP address – for example, running ip addr add 192.168.1.5/24 assigns the IP address 192.168.1.5 and creates a subnet route for 192.168.1.0/24. So it's not necessary to add these routes in normal usage.



But when you blindly "delete all routes", you also end up deleting these automatically created routes, and the kernel's "is this address in my subnet" checks no longer work. That's why you end up having to re-add the routes manually.



(The kernel needs to perform this check during ip route add because route gateways (nexthops) must be directly reachable on the same L2 connection – they cannot be behind another gateway. That is, the gateway must be in your subnet.



Route scopes are a generic mechanism to express this restriction: the new route's nexthop needs to be reachable through an existing route with a lower scope. In other words, you must go through a local host (link scope) before you can reach a remote host (global scope).)






share|improve this answer


























  • Thank you for solving my doubts, but I have another question: why this “link scope” command is not needed when I configure systemd-networkd with static IP address?

    – user762750
    Jan 1 at 4:06











  • The kernel automatically creates these routes when you add an IP address. So the command is never needed unless you first add an address and then delete all routes.

    – grawity
    Jan 1 at 12:04











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1389292%2fwhat-does-scope-do-in-ip-route-and-why-it-is-necessary-to-setup-static-route-i%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














These routes are how you tell the Linux kernel what subnet(s) you're in.



This information isn't stored in a hidden "my current subnet" field when you add an IP address; instead it's always converted to a 'scope link' route with no gateway specified (also called "device route" or "interface route"), and whenever the kernel needs to determine whether some address is directly reachable, it just performs a routing table check.



Normally those routes are added automatically as soon as you configure an IP address – for example, running ip addr add 192.168.1.5/24 assigns the IP address 192.168.1.5 and creates a subnet route for 192.168.1.0/24. So it's not necessary to add these routes in normal usage.



But when you blindly "delete all routes", you also end up deleting these automatically created routes, and the kernel's "is this address in my subnet" checks no longer work. That's why you end up having to re-add the routes manually.



(The kernel needs to perform this check during ip route add because route gateways (nexthops) must be directly reachable on the same L2 connection – they cannot be behind another gateway. That is, the gateway must be in your subnet.



Route scopes are a generic mechanism to express this restriction: the new route's nexthop needs to be reachable through an existing route with a lower scope. In other words, you must go through a local host (link scope) before you can reach a remote host (global scope).)






share|improve this answer


























  • Thank you for solving my doubts, but I have another question: why this “link scope” command is not needed when I configure systemd-networkd with static IP address?

    – user762750
    Jan 1 at 4:06











  • The kernel automatically creates these routes when you add an IP address. So the command is never needed unless you first add an address and then delete all routes.

    – grawity
    Jan 1 at 12:04
















2














These routes are how you tell the Linux kernel what subnet(s) you're in.



This information isn't stored in a hidden "my current subnet" field when you add an IP address; instead it's always converted to a 'scope link' route with no gateway specified (also called "device route" or "interface route"), and whenever the kernel needs to determine whether some address is directly reachable, it just performs a routing table check.



Normally those routes are added automatically as soon as you configure an IP address – for example, running ip addr add 192.168.1.5/24 assigns the IP address 192.168.1.5 and creates a subnet route for 192.168.1.0/24. So it's not necessary to add these routes in normal usage.



But when you blindly "delete all routes", you also end up deleting these automatically created routes, and the kernel's "is this address in my subnet" checks no longer work. That's why you end up having to re-add the routes manually.



(The kernel needs to perform this check during ip route add because route gateways (nexthops) must be directly reachable on the same L2 connection – they cannot be behind another gateway. That is, the gateway must be in your subnet.



Route scopes are a generic mechanism to express this restriction: the new route's nexthop needs to be reachable through an existing route with a lower scope. In other words, you must go through a local host (link scope) before you can reach a remote host (global scope).)






share|improve this answer


























  • Thank you for solving my doubts, but I have another question: why this “link scope” command is not needed when I configure systemd-networkd with static IP address?

    – user762750
    Jan 1 at 4:06











  • The kernel automatically creates these routes when you add an IP address. So the command is never needed unless you first add an address and then delete all routes.

    – grawity
    Jan 1 at 12:04














2












2








2







These routes are how you tell the Linux kernel what subnet(s) you're in.



This information isn't stored in a hidden "my current subnet" field when you add an IP address; instead it's always converted to a 'scope link' route with no gateway specified (also called "device route" or "interface route"), and whenever the kernel needs to determine whether some address is directly reachable, it just performs a routing table check.



Normally those routes are added automatically as soon as you configure an IP address – for example, running ip addr add 192.168.1.5/24 assigns the IP address 192.168.1.5 and creates a subnet route for 192.168.1.0/24. So it's not necessary to add these routes in normal usage.



But when you blindly "delete all routes", you also end up deleting these automatically created routes, and the kernel's "is this address in my subnet" checks no longer work. That's why you end up having to re-add the routes manually.



(The kernel needs to perform this check during ip route add because route gateways (nexthops) must be directly reachable on the same L2 connection – they cannot be behind another gateway. That is, the gateway must be in your subnet.



Route scopes are a generic mechanism to express this restriction: the new route's nexthop needs to be reachable through an existing route with a lower scope. In other words, you must go through a local host (link scope) before you can reach a remote host (global scope).)






share|improve this answer















These routes are how you tell the Linux kernel what subnet(s) you're in.



This information isn't stored in a hidden "my current subnet" field when you add an IP address; instead it's always converted to a 'scope link' route with no gateway specified (also called "device route" or "interface route"), and whenever the kernel needs to determine whether some address is directly reachable, it just performs a routing table check.



Normally those routes are added automatically as soon as you configure an IP address – for example, running ip addr add 192.168.1.5/24 assigns the IP address 192.168.1.5 and creates a subnet route for 192.168.1.0/24. So it's not necessary to add these routes in normal usage.



But when you blindly "delete all routes", you also end up deleting these automatically created routes, and the kernel's "is this address in my subnet" checks no longer work. That's why you end up having to re-add the routes manually.



(The kernel needs to perform this check during ip route add because route gateways (nexthops) must be directly reachable on the same L2 connection – they cannot be behind another gateway. That is, the gateway must be in your subnet.



Route scopes are a generic mechanism to express this restriction: the new route's nexthop needs to be reachable through an existing route with a lower scope. In other words, you must go through a local host (link scope) before you can reach a remote host (global scope).)







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 31 '18 at 16:21

























answered Dec 31 '18 at 16:06









grawitygrawity

236k37498553




236k37498553













  • Thank you for solving my doubts, but I have another question: why this “link scope” command is not needed when I configure systemd-networkd with static IP address?

    – user762750
    Jan 1 at 4:06











  • The kernel automatically creates these routes when you add an IP address. So the command is never needed unless you first add an address and then delete all routes.

    – grawity
    Jan 1 at 12:04



















  • Thank you for solving my doubts, but I have another question: why this “link scope” command is not needed when I configure systemd-networkd with static IP address?

    – user762750
    Jan 1 at 4:06











  • The kernel automatically creates these routes when you add an IP address. So the command is never needed unless you first add an address and then delete all routes.

    – grawity
    Jan 1 at 12:04

















Thank you for solving my doubts, but I have another question: why this “link scope” command is not needed when I configure systemd-networkd with static IP address?

– user762750
Jan 1 at 4:06





Thank you for solving my doubts, but I have another question: why this “link scope” command is not needed when I configure systemd-networkd with static IP address?

– user762750
Jan 1 at 4:06













The kernel automatically creates these routes when you add an IP address. So the command is never needed unless you first add an address and then delete all routes.

– grawity
Jan 1 at 12:04





The kernel automatically creates these routes when you add an IP address. So the command is never needed unless you first add an address and then delete all routes.

– grawity
Jan 1 at 12:04


















draft saved

draft discarded




















































Thanks for contributing an answer to Super User!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1389292%2fwhat-does-scope-do-in-ip-route-and-why-it-is-necessary-to-setup-static-route-i%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Plaza Victoria

In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...