- Просмотр и построение таблиц маршрутизации в Linux
- Что такое маршрутизация
- Как посмотреть таблицу маршрутизации
- Команда route
- Команда netstat
- Построение таблицы маршрутизации
- Route
- Ключи
- Основные опции (command)
- Обозначения
- Опции для указания вводных данных
- IP Route
- Основные опции (command)
- Обозначения
- Опции для указания вводных данных
- Примеры статической маршрутизации
- Составление нового маршрута
- Изменение локальной сети
- Изменение адреса тоннеля
- Изменение провайдера
- What is routing table in linux
- 4.8.1. Routing Table Entries (Routes)
- 4.8.2. The Local Routing Table
- 4.8.3. The Main Routing Table
Просмотр и построение таблиц маршрутизации в Linux
Содержание:
Что такое маршрутизация
Вся цифровая информация передаётся по сети в виде пакетов данных. По пути от отправителя к адресату они проходят через цепочку промежуточных устройств – маршрутизаторов (роутеров) и/или соответственно настроенных компьютеров.
Маршрутизация – это процесс определения пути (сетевого маршрута) для установки соединения между хост-устройствами. Этот путь настраивается как внутри локального устройства, так и на маршрутизаторе.
Построение сетевого маршрута происходит на основе информации из таблиц маршрутизации. Для их формирования применяются протоколы маршрутизации или инструкции сетевого администратора.
Каждая таблица содержит ряд параметров, позволяющих правильно идентифицировать и читать сетевой маршрут. Таблица содержит минимум 5 разделов:
- Destination (Target). IP-адрес сети назначения – конечной цели для передаваемых данных.
- Netmask (Genmask). Маска сети.
- Gateway. IP-адрес шлюза, через который можно добраться до цели.
- Interface. Адрес сетевого интерфейса, по которому доступен шлюз.
- Metric. Числовой показатель, задающий предпочтительность маршрута.
Опционально в таблице также может содержаться следующая информация:
- адрес отправителя (source);
- размер TCP-окна (window);
- максимальная величина пакета (MSS) и типы записей.
Как посмотреть таблицу маршрутизации
Таблицу маршрутизации в Linux (например, в популярных серверных ОС типа Ubuntu или CentOS) можно посмотреть с помощью нескольких команд.
Команда route
Программа используется для настройки параметров статической маршрутизации. Просмотр таблицы можно осуществить с помощью команды:
Команда netstat
Утилита используется для сбора информации о состоянии сетевых соединений. Вывести таблицу можно с помощью команды:
Построение таблицы маршрутизации
Существует несколько основных утилит для настройки таблицы маршрутизации (добавления, обновления, удаления старых и новых маршрутов):
- Route. Устаревшая утилита, входящая в состав пакета net-tools. Служит для отображения таблицы маршрутизации и построения статических маршрутов.
- IP Route. Обновленный инструмент, призванный заменить Route. Имеет большую функциональность, по сравнению со своим предшественником.
Оба инструмента могут использоваться для выполнения аналогичных задач. Далее будет рассмотрен синтаксис каждого в пределе основных возможностей.
Route
Команда имеет следующий вид:
Ключи
- -f – очистка таблиц от записей всех шлюзов.
- -p – сохранение маршрута в качестве постоянного при использовании ADD. По умолчанию все маршруты временные и после перезагрузки системы сбрасываются.
Основные опции (command)
- add – добавление маршрута.
- del – удаление маршрута.
- replace – замена маршрута.
- change – изменение или настройка параметров маршрута.
Обозначения
- [destination] – адрес сети назначения.
- [MASKnetmask] – маска подсети.
- [gateway] – адрес шлюза.
- [METRICmetric] – числовой показатель, задающий предпочтительность маршрута (используется в том случае, если устройство является маршрутизатором).
- [IFinterface] – сетевой интерфейс.
Опции для указания вводных данных
- -net – целевая сеть.
- -host – целевой хост.
- gw – шлюз (Gateway).
- dev – сетевой интерфейс.
- netmask – маска подсети.
- metric – метрика.
IP Route
Команда имеет следующий вид:
Основные опции (command)
- add – добавление маршрута.
- del – удаление маршрута.
- replace – замена маршрута.
- change – изменение или настройка параметров маршрута.
Обозначения
- [destination] – адрес сети назначения.
- [MASKnetmask] – маска подсети.
- [gateway] – адрес шлюза.
- [METRICmetric] – числовой показатель, задающий предпочтительность маршрута (используется в том случае, если устройство является маршрутизатором).
- [IFinterface] – сетевой интерфейс.
Опции для указания вводных данных
- via – используется в значении «через» для указания шлюза.
- dev – сетевой интерфейс.
- netmask – маска подсети.
- metric – метрика.
Примеры статической маршрутизации
Составление нового маршрута
Можно представить два офиса: A и B. В каждом стоят маршрутизаторы на Linux, которые соединены между собой IP-IP туннелем.
Маршрутизатор A имеет IP-адрес — 192.168.1.1, а маршрутизатор B — 192.168.1.2.
Чтобы подключение к локальной сеть маршрутизатора A стало возможным из локальной сети маршрутизатора B и наоборот, нужно прописать на маршрутизаторе B:
Будет произведена установка шлюза «192.168.1.1» для сети «172.16.10.0/24».
Также необходимо прописать на маршрутизаторе A обратный маршрут в локальную сеть маршрутизатора B:
Изменение локальной сети
В случае изменения локальной сети маршрутизатора B, необходимо удалить старую запись:
А после добавить новый маршрут на маршрутизаторе А:
Изменение адреса тоннеля
Если на маршрутизаторе B изменится IP-адрес туннеля, то следует также актуализировать адрес шлюза на маршрутизаторе А:
После выполнения команды адрес шлюза для подсети «172.16.10.0/24» будет изменён.
Изменение провайдера
Чтобы перенаправить трафик через другого провайдера («ISP2»), следует изменить маршрут «по умолчанию» («default»):
Начни экономить на хостинге сейчас — 14 дней бесплатно!
Источник
What is routing table in linux
Linux kernel 2.2 and 2.4 support multiple routing tables [22] . Beyond the two commonly used routing tables (the local and main routing tables), the kernel supports up to 252 additional routing tables.
The multiple routing table system provides a flexible infrastructure on top of which to implement policy routing. By allowing multiple traditional routing tables (keyed primarily to destination address) to be combined with the routing policy database (RPDB) (keyed primarily to source address), the kernel supports a well-known and well-understood interface while simultaneously expanding and extending its routing capabilities. Each routing table still operates in the traditional and expected fashion. Linux simply allows you to choose from a number of routing tables, and to traverse routing tables in a user-definable sequence until a matching route is found.
Any given routing table can contain an arbitrary number of entries, each of which is keyed on the following characteristics (cf. Table 4.1, “Keys used for hash table lookups during route selection”)
destination address; a network or host address (primary key)
tos; Type of Service
For practical purposes, this means that (even) a single routing table can contain multiple routes to the same destination if the ToS differs on each route or if the route applies to a different interface [23] .
The ip route and ip rule commands have built in support for the special tables main and local . Any other routing tables can be referred to by number or an administratively maintained mapping file, /etc/iproute2/rt_tables .
The format of this file is extraordinarily simple. Each line represents one mapping of an arbitrary string to an integer. Comments are allowed.
Example 4.6. Typical content of /etc/iproute2/rt_tables
The routing table manipulated by the conventional route command is the main routing table. Additionally, the use of both ip address and ifconfig will cause the kernel to alter the local routing table (and usually the main routing table). For further documentation on how to manipulate the other routing tables, see the command description of ip route .
4.8.1. Routing Table Entries (Routes)
Each routing table can contain an arbitrary number of route entries. Aside from the local routing table, which is maintained by the kernel, and the main routing table which is partially maintained by the kernel, all routing tables are controlled by the administrator or routing software. All routes on a machine can be changed or removed [25] .
Each of the following route types is available for use with the ip route command. Each route type causes a particular sort of behaviour, which is identified in the textual description. Compare the route types described below with the rule types available for use in the RPDB.
A unicast route is the most common route in routing tables. This is a typical route to a destination network address, which describes the path to the destination. Even complex routes, such as nexthop routes are considered unicast routes. If no route type is specified on the command line, the route is assumed to be a unicast route.
Example 4.7. unicast route types
This route type is used for link layer devices (such as Ethernet cards) which support the notion of a broadcast address. This route type is used only in the local routing table [26] and is typically handled by the kernel.
Example 4.8. broadcast route types
The kernel will add entries into the local routing table when IP addresses are added to an interface. This means that the IPs are locally hosted IPs [27] .
Example 4.9. local route types
This route entry is added by the kernel in the local routing table, when the user attempts to configure stateless NAT. See Section 5.3, “Stateless NAT with iproute2 ” for a fuller discussion of network address translation in general. [28] .
Example 4.10. nat route types
When a request for a routing decision returns a destination with an unreachable route type, an ICMP unreachable is generated and returned to the source address.
Example 4.11. unreachable route types
When a request for a routing decision returns a destination with a prohibit route type, the kernel generates an ICMP prohibited to return to the source address.
Example 4.12. prohibit route types
A packet matching a route with the route type blackhole is discarded. No ICMP is sent and no packet is forwarded.
Example 4.13. blackhole route types
The throw route type is a convenient route type which causes a route lookup in a routing table to fail, returning the routing selection process to the RPDB. This is useful when there are additional routing tables. Note that there is an implicit throw if no default route exists in a routing table, so the route created by the first command in the example is superfluous, although legal.
Example 4.14. throw route types
The power of these route types when combined with the routing policy database can hardly be understated. All of these route types can be used without the RPDB, although the throw route doesn’t make much sense outside of a multiple routing table installation.
4.8.2. The Local Routing Table
The local routing table is maintained by the kernel. Normally, the local routing table should not be manipulated, but it is available for viewing. In Example D.12, “Viewing the local routing table with ip route show table local ”, you’ll see two of the common uses of the local routing table. The first common use is the specification of broadcast address, necessary only for link layers which support broadcast addressing. The second common type of entry in a local routing table is a route to a locally hosted IP.
The route types found in the local routing table are local , nat and broadcast . These route types are not relevant in other routing tables, and other route types cannot be used in the local routing table.
If the the machine has several IP addresses on one Ethernet interface, there will be a route to each locally hosted IP in the local routing table. This is a normal side effect of bringing up an IP address on an interface under linux. Maintenance of the broadcast and local routes in the local routing table can only be done by the kernel.
Example 4.15. Kernel maintenance of the local routing table
Note in Example 4.15, “Kernel maintenance of the local routing table”, that the kernel adds not only the route for the locally connected network in the main routing table, but also the three required special addresses in the local routing table. Any IP addresses which are locally hosted on the box will have local entries in the local table. The network address and broadcast address are both entered as broadcast type addresses on the interface to which they have been bound. Conceptually, there is significance to the distinction between a network and broadcast address, but practically, they are treated analogously, by other networking gear as well as the linux kernel.
There is one other type of route which commonly ends up in the local routing table. When using iproute2 NAT, there will be entries in the local routing table for each network address translation. Refer to Example D.21, “Creating a NAT route for a single IP with ip route add nat ” and Example D.22, “Creating a NAT route for an entire network with ip route add nat ” for example output.
4.8.3. The Main Routing Table
The main routing table is the routing table most people think of when considering a linux routing table. When no table is specified to an ip route command, the kernel assumes the main routing table. The route command only manipulates the main routing table.
Similarly to the local table, the main table is populated automatically by the kernel when new interfaces are brought up with IP addresses. Consult the main routing table before and after ip address add 192.168.254.254/24 brd + dev eth1 in Example 4.15, “Kernel maintenance of the local routing table” for a concrete example of this kernel behaviour. Also, visit this summary of side effects of interface definition and activation with ifconfig or ip address .
[22] The kernel must be compiled with the option CONFIG_IP_MULTIPLE_TABLES=y . This is common in vendor and stock kernels, both 2.2 and 2.4.
[23] If somebody has used scope or oif as additional keys in a routing table, and has an example, I’d love to see it, for possible inclusion in this documentation.
[24] Can anybody describe to me what is in table 0? It looks almost like an aggregation of the routing entries in routing tables 254 and 255.
[25] Once again, I recommend caution when altering the local routing table. Removing local route types from the local routing table can break networking in strange and wonderful ways.
[26] OK, I’m not absolutely sure you can’t use the broadcast route in other routing tables, but I believe you can’t. Testing forthcoming.
[27] Ibid. I’m not sure that local route types can be used in any routing table other than the local routing table. Testing forthcoming.
[28] Ibid. nat route types might be ineffectual outside the local routing table. Testing forthcoming.
Источник