Linux приоритет сетевых интерфейсов

How to manage available wireless network priority?

I’d like to move up and down available wireless access points, setting priorities as we can do in Microsoft Windows. I can’t remember to have ever succeeded in this since using network-manager . Maybe something with conf files is possible. I’m not interested in different network managers.

6 Answers 6

Apparently in 2014 NetworkManager team introduced such a feature — to specify priorities for different networks. There is now connection.autoconnect-priority .

This article provides lots of information on how to interface NetworkManager via nmcli .

nmcli -f NAME,UUID,AUTOCONNECT,AUTOCONNECT-PRIORITY c 

you can list known networks and see their default priority to be 0. Now I have run

nmcli connection modify HOME-WIFI connection.autoconnect-priority 10 

to give my home WiFi higher priority. I have the same work WiFi points available at home as well, but need to connect to HOME-WIFI when I am home. That command heavily rewritten /etc/NetworkManager/system-connections/HOME-WIFI which added autoconnect-priority=10 into [connection] section — time will show if it works as it should.

This is visible through KDE 16.04’s Network UI as a «priority» setting. Shame it isn’t available elsewhere. Not even nmtui has it.

Works perfectly in Ubuntu 16.04. Really like this approach since it uses only standard tools. GNOME 3 GUI («Network» in «Control Center») doesn’t give access to priority setting neither. Shame indeed.

From the gnome docs: «autoconnect-priority: The autoconnect priority. If the connection is set to autoconnect, connections with higher priority will be preferred. Defaults to 0. The higher number means higher priority.»

Can WiFi priority be dragged up and down in NetworkManager?

The short answer is “Not yet.” As of mid 2018, Ubuntu has nothing as easy as Microsoft Windows for setting the WiFi priority. However, there are ways to accomplish what you want.

TL;DR? Summary

For command line use nmcli . For GUI, plasma-nm .

nmcli -f autoconnect-priority,name c nmcli c mod "mypreferred" conn.autoconnect-p 10 nmcli c mod "xfinitywifi" conn.autoconnect-p -10 
apt install plasma-nm kde5-nm-connection-editor 

Available alternatives

Here are the currently available ways of setting WiFi priority, along with a short description of why they don’t quite answer the original question.

Command line tools (nmcli)

While this solution is probably the easiest way to do it and it does work with NetworkManager, nmcli does not allow Microsoft Windows-style drag-and-drop reordering. In fact, nmcli (as the name suggests) is a «command line interface» to NetworkManager, which means you’ll need to type commands in the terminal, which can be off-putting for some people. I’ll describe more how to use it below.

Plasma-nm (kde5-nm-connection-editor)

This tool does not allow drag-and-drop reordering, but it does work with NetworkManager. Despite the name «KDE», it works with any desktop environment (I tested it with GNOME). It presents a graphical interface that lets one edit the «autoconnect-priority» of a network. I’ll discuss how to use it below.

Читайте также:  Быстрая виртуальная машина linux

Wifi Radar

This tool does allow moving network priorities up and down, as requested. However, it is not actually integrated with Network Manager; in fact I believe it is a mistake to run both at the same time. Also, despite having a graphical interface, it is not as easy to use as it should be. Another reason to not use Wifi Radar is that it is a Python script that must be run as root, a potential security risk. Also, according to the man page, WiFi Radar is very power consuming and has «probably lots» of bugs.

GNOME network control panel (not even in the running)

I mention this more for completeness as it’s what people would expect to work, but it doesn’t. GNOME, which is Ubuntu’s current default desktop environment, comes with a network control panel that simply cannot show or edit autoconnect priorities at all. (By the way, nmcli and plasma-nm both work fine under GNOME.)

nm-connection-editor: GNOME’s old network control panel

GNOME used to have the ability to set connection priorities, albeit in a clunky way, not drag and drop. The old control panel is yet included in GNOME, but is not accessible by clicking. Instead, run the command nm-connection-editor , then select a WiFi network, click Edit, go to the General tab, and click the — / + buttons next to «connection priority for auto activation».

Emacs/vi

If you’re a hardcore geek, mumble code in your sleep, all you need to do is add the line «autoconnect-priority: 10» to the file /etc/NetworkManager/system-connections/foo . See nm-settings(5).

Full explanation with examples

Both nmcli and kde5-nm-connection-editor can edit individual network autoconnect priorities. Only nmcli can show you a list of all the currently set priorities. Both assume that you know that higher numbers represent higher priorities and that zero is the default. Negative numbers are allowed and work to mark a network as a «last resort» if nothing else is available (See man nm-settings and search for «autoconnect-priority».)

The changes nmcli and plasma-nm make are stored permanently by NetworkManager in /etc/NetworkManager/system-connections/.

How to use nmcli

This is my preferred solution and it already comes with Ubuntu. If you’re familiar with the command line or if you have a naturally linguistic brain, you may find this solution easier than using a mouse. On the other hand, if you want to try a graphical interface first, skip ahead to the next section on KDE’s plasma-nm. For all of the examples below, you’ll need to open a Terminal to type in the commands.

To list current priorities

$ nmcli -f autoconnect-priority,name c 
AUTOCONNECT-PRIORITY NAME 0 Blake5Net 0 Caffe Ubuntu 0 Caffe Ubuntu Guest 0 Fire Hotspot 0 JET & Mishka 0 La Marzocco 0 Le_MX 0 MobileLab 0 xfinitywifi 

Tip: If you have a very long list, you may want to sort them by priority:

nmcli -f autoconnect-priority,name c | tail -n +2 | sort -nr 

How to set a network as preferred

nmcli connection modify "Caffe Ubuntu" connection.autoconnect-priority 10 

Note that you can use any number you want for the priority. A larger number moves the network to the top of the list.

Читайте также:  Remove installed software linux

How to set a network as a last resort

nmcli connection modify "xfinitywifi" connection.autoconnect-priority -10 

Negative priority values are lower than the default of 0, which means they will be tried last, if no other known WiFi network can be found. Note that, due to a bug in some versions of nmcli , you may see negative numbers listed as huge positive numbers like 4294967286. Don’t worry about this as it will still work fine.

How to use plasma-nm

Plasma-nm, KDE’s NetworkManager client, lets you change a network’s priority through several clicks in a graphical interface. You cannot drag-and-drop, instead you must specify a number. As with nmcli , higher numbers are higher priority. Unfortunately, you cannot see a list of what other priorities you’ve set in the past so you may have to guess at a number. Personally, I just use «10» for preferred networks and «-10» for last resort networks and I don’t differentiate between them beyond that.

You don’t need to install all of KDE just to get plasma-nm. (On my machine, running GNOME, the full KDE package would have required downloading over 600MB; in contrast, plasma-nm was only 8MB). I installed plasma-nm from the command line like so:

sudo apt install plasma-nm 

and I ran it by typing this:

(Well, technically, I didn’t type all that. I used TAB to autocomplete the typing for me).

If you don’t use KDE, the first time you run plasma-nm, it will ask you about creating a «wallet» to store credentials. I just hit «Cancel» to the question and it worked fine for me.

Then it will show you a list of connections. Double-click on the connection you want to edit. That will bring up the «Connection Editor» panel. Choose the «General Configuration» tab. At the bottom, you’ll see a box labeled «Priority». Change it from 0 to 10 (preferred) or -10 (last resort). Click «OK» to save the changes.

Connection Editor panel of plasma-nm

Since changes are permanently stored with NetworkManager, once you’re finished adjusting priorities, you don’t have to keep plasma-nm installed if you don’t want to. ( sudo apt remove plasma-nm )

Источник

How to change network interfaces’ priority

I have 3 network interfaces: lo , new0 and new1 . I use new1 to access the internet. My computer went nuts a few days ago and began to try to access the internet on lo . Doing ifconfig lo down causes packets to go via new0 . But I want it to use new1 . I can use ssh , download torrents and use other protocols, but http and https are not working; I just get instant «Server not found» without loading time in browser. How can have the system prefer new1 over new0 and lo ? I’m not able to change my metric , both new1 and new0 are stuck at 1.

2 Answers 2

Prioritising interfaces for general traffic is done by manipulating the routing metrics.

Each route has associated parameters such as hop-counts and bandwidths. See netstat -nr and the » metric » option in the man-page for route command.

For very simple control of metric use ifmetric tool:

sudo apt-get update sudo apt-get install ifmetric 

Once installed use it to change metric value for interface, refer this http://manpages.ubuntu.com/manpages/vivid/man8/ifmetric.8.html

Читайте также:  What to do after installing kali linux

This worked for on Ubuntu 16.04, but it took me some time to figure out exact commands. The first thing to do is finding interface you want to update and metric value to set. To do so run route command and note Iface and Metric column values. (netstat doesn’t show metric value, metric from ifconfig is not what you are looking for). To de-prioritize interface (e.g. ens33) you need, pick metric value a bit larger than other interfaces. Let’s say it is 110. Now run ifmetric as follows: sudo ifmetric ens33 110

Metric can change on several place. Best working for me is configuration in etc/network/interface

auto lo iface lo inet loopback auto new0 iface new0 inet static address xxx.xxx.xxx.xxx netmask xxx.xxx.xxx.xxx up route add default gw aaa.aaa.aaa.aaa metric 10 down route del default gw aaa.aaa.aaa.aaa auto new1 iface new1 inet static address yyy.yyy.yyy.yyy netmask yyy.yyy.yyy.yyy up route add default gw bbb.bbb.bbb.bbb down route del default gw bbb.bbb.bbb.bbb 

On this way when interface new0 go up will bring route to gateway aaa.aaa.aaa.aaa with metric 10. Interface new1 bring up route to bbb.bbb.bbb.bbb with lower metric and traffic will be routed on that interface.

If you do sudo ifdown new1 on unplug cable from card route will be deleted and route to aaa.aaa.aaa.aaa will be new best route for traffic.

When again brin int new1 up route will again be added and bbb.bbb.bbb.bbb will again be your best route.

Route priority is not the same like route based on destination port.

But I give you example to route traffic for ssh, port 22 and you can implement for port you need for

Let’s start from the iptable. Here we shall take ssh as the service for routing. Use mangle table of iptable for modifying the ssh packets.

 sudo iptables -t mangle -A OUTPUT -p tcp –dport 22 -j MARK –set-mark 0×1 

We are marking all packets with destination port 22 as ’0×1′ .

Now save and restart iptables .

service iptables save service iptables restart 

To delete this entry from iptables we can use -D instead of -A .

Next, create a new IP route table in /etc/iproute2/rt_table by just giving an entry

Write rule for ssh packets.

ip rule add fwmark 0×1 lookup sshtable 

Add route at new table sshtable . Here we shall use ’192.168.1.1′ as the gateway for ssh . Address ’192.168.1.1′, in this example will be gateway address for new2 All other traffic will go through the default gateway aka new1 , which can be seen by IP route show command.

We copy all entries except default gateway entry from main table.

 sudo ip route show table main | grep -Ev ^default | while read ROUTE ; do ip route add table sshtable $ROUTE; done 

Add default gateway entry for ssh packets to table sshtable

 sudo ip route add default via 192.168.1.1 table sshtable 

Use ip route show table sshtable to show all routes at sshtable .

Repeat everything for imap on port 465 or hhtp on port 80 or https on port 443 .

Источник

Оцените статью
Adblock
detector