Static IP Address: Difference between revisions

From Variscite Wiki
No edit summary
No edit summary
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{PageHeader|Configuring Static IP Addresses}} {{DocImage|category1=Yocto|category2=VAR-SOM-MX6}}[[Category: DART-6UL]][[Category: VAR-SOM-MX7]] __toc__
{{PageHeader|Configuring Static IP Addresses}}  
{{DocImage|category1=Yocto|category2=VAR-SOM-MX6}}[[Category: DART-6UL]][[Category: VAR-SOM-MX7]][[Category: DART-MX8M]][[Category:DART-MX8M-MINI]][[Category:VAR-SOM-MX8X]][[Category:VAR-SOM-MX8]]__toc__


It is sometimes desirable to provide static IP configuration for network interface instead of relying on DHCP.  
It is sometimes desirable to provide static IP configuration for network interface instead of relying on DHCP.  
Below we describe several methods to do that.
Below we describe several methods of doing that.


= Using /etc/network/interfaces =
= Using ConnMan =
 
ConnMan is the default Yocto network manager. Network manager is a program for providing detection and configuration for systems to automatically connect to network.
In the examples below we demonstrate how ConnMan command line tool, connmanctl, can be used create static IP configuration.
 
== Configuring wired interfaces ==
 
To get the list of wired interfaces run
 
# connmanctl services
 
For example:
# connmanctl services
*AO Wired                ethernet_0eb31468dcc9_cable
 
Wired interfaces will be shown as ethernet_<mac_address>_cable. Only interfaces physically connected to the network are displayed.
 
To create static IP configuration run
 
# connmanctl config <service> --ipv4 manual <ip address> <netmask> <gateway>
# connmanctl config <service> --nameservers <dns-addr>
 
For example:
# connmanctl config ethernet_0eb31468dcc9_cable --ipv4 manual 192.168.1.100 255.255.255.0 192.168.1.254
# connmanctl config ethernet_0eb31468dcc9_cable --nameservers 8.8.8.8 4.4.4.4
 
The configuration will be saved in /var/lib/connman/ethernet_0eb31468dcc9_cable/settings.


Edit /etc/network/interfaces to add configuration sections for network interfaces. The example below configures eth0
== Configuring wireless interfaces ==


<pre>
To check if wifi is enabled run
auto eth0
    iface eth0 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.254
    dns-nameservers 66.212.63.228 66.212.48.10 
</pre>


Please note that /etc/network/interfaces is read by ifup and ifdown tools that do the actual interface configuration work. In the SystemV init setup they are invoked by /etc/init.d/networking script during boot. If you're using systemd instead of SystemV init, /etc/init.d/networking is not available and you should create your own systemd service file that invokes "ifup -a" during boot and "ifdown -a" during shutdown.
  # connmanctl technologies
   
= Connman interaction with /etc/network/interfaces =


Connman is the default Yocto network manager. Network manager is a service that provides a high-level interface for the configuration of the network interfaces. Connman will override the settings in /etc/network/interfaces unless configured to blacklist certain interfaces. For example, after configuring eth0 and eth1 in /etc/network/interfaces, you should create /etc/connman/main.conf and add the following:
and check for the line that says Powered: True/False.


<pre>
To power the wifi on run
[General]
NetworkInterfaceBlacklist=eth0,eth1
</pre>


= NetworkManager interaction with /etc/network/interfaces =
# connmanctl enable wifi


NetworkMananager is an alternative to connman. Both cannot coexist in the same Yocto image, so the image should be rebuilt after enabling NetworkManager. Interfaces configured via /etc/network/interfaces are automatically ignored by NetworkMananager, so nothing special should be done.
To scan the network connmanctl accepts simple names called technologies. To scan for nearby wifi networks:


= Using connmanctl =
# connmanctl scan wifi
Instead of using /etc/network/interfaces one can use connmanctl tool to configure static IP. To create initial configuration the relevant interface should be connected to the network.


To get the list of connections run
To list the available networks found after a scan run (example output):


  # connmanctl services
  # connmanctl services


For example:
MyNetwork              wifi_dc85de828967_68756773616d_managed_psk
<pre>
OtherNET                wifi_dc85de828967_38303944616e69656c73_managed_psk
root@imx7-var-som:~# connmanctl services
AnotherOne              wifi_dc85de828967_3257495245363836_managed_wep
*AO Wired                ethernet_0eb31468dcc9_cable
FourthNetwork          wifi_dc85de828967_4d7572706879_managed_wep
</pre>
AnOpenNetwork          wifi_dc85de828967_4d6568657272696e_managed_none
 
Every wifi network is identified by a name composed as
 
wifi_<hashlocal>_<hashremote>_managed_<encrption>
 
=== Connecting to an open network ===
 
To connect to an open network, look for wifi networks ending with _managed_none:
 
# connmanctl connect wifi_dc85de828967_4d6568657272696e_managed_none
 
{{note|Network names can be tab-completed}}
 
You should now be connected to the network. Check using ip addr or run
 
# connmanctl state.
 
=== Connecting to a protected network ===
 
Start connmanctl in interactive mode by running
 
# connmanctl
 
Now you need to register the agent to handle user requests. The command is:
 
connmanctl> agent on
 
You now need to connect to one of the protected services.
 
To do this easily, just use tab completion for the wifi_ service.
 
If you were connecting to OtherNET in the example above you would type:
 
connmanctl> connect wifi_dc85de828967_38303944616e69656c73_managed_psk
 
The agent will then ask you to provide any information the daemon needs to complete the connection.
 
The information requested will vary depending on the type of network you are connecting to.
 
The agent will also print additional data about the information it needs as shown in the example below.
 
Agent RequestInput wifi_dc85de828967_38303944616e69656c73_managed_psk
  Passphrase = [ Type=psk, Requirement=mandatory ]
  Passphrase?
 
Provide the information requested, in this example the passphrase, and then type:
 
connmanctl> quit
 
If the information you provided is correct you should now be connected to the protected access point.


Wired Ethernet connections will be shown as ethernet_<mac_address>_cable.
=== Configuring Static IP ===


To create static IP configuration run
To create static IP configuration run


  # connmanctl <service> --ipv4 manual <ip address> <netmask> <gateway>
  # connmanctl config <service> --ipv4 manual <ip address> <netmask> <gateway>
  # connmanctl config <service> --nameservers <dns-addr>  
  # connmanctl config <service> --nameservers <dns-addr>  


For example:
For example:
  # connmanctl ethernet_0eb31468dcc9_cable --ipv4 manual 192.168.1.100 255.255.255.0 192.168.1.254
# connmanctl config wifi_dc85de828967_38303944616e69656c73_managed_psk --ipv4 manual 192.168.1.100 255.255.255.0 192.168.1.254
  # connmanctl config ethernet_0eb31468dcc9_cable --nameservers 66.212.63.228 66.212.48.10
# connmanctl config wifi_dc85de828967_38303944616e69656c73_managed_psk --nameservers 8.8.8.8 4.4.4.4
 
The configuration will be saved in /var/lib/connman/wifi_dc85de828967_38303944616e69656c73_managed_psk/settings.
 
= Using NetworkManager =
 
NetworkManager is an alternative network manager that can be used instead of ConnMann. Both network managers cannot coexist in the same Yocto image, so enabling NetworkManager requires disabling ConnMan and rebuilding the image.
 
In the examples below we use NetworkManager command line tool, nmcli, to create static configurations.
 
== Configuring wired interfaces ==
 
To create static IP configuration run
 
# nmcli con add type ethernet ifname <ethX> con-name <name> ip4 <ipaddr> gw4 <gwaddr>
# nmcli con mod static-eth0 ipv4.dns "<dns1>,<dns2>"
# nmcli con up <name>
 
For example, to create configuration for eth0 run
 
# nmcli con add type ethernet ifname eth0 con-name static-eth0 ip4 192.168.1.100/24 gw4 192.168.1.254
# nmcli con mod static-eth0 ipv4.dns "8.8.8.8,4.4.4.4"
# nmcli con up static-eth0
 
 
The configuration will be saved in /etc/NetworkManager/system-connections/static-eth0.
 
== Configuring wireless interfaces ==
 
To check if WIFI is enabled by NetworkManager run
 
# nmcli dev show wlan0
 
and check the GENERAL.STATE line. If WIFI is enabled, the state is either connected or disconnected. If WIFI is disabled the state is unavailable.
 
To enable WIFI run
 
# nmcli radio wifi on
 
or if you need to disable it you can run
# nmcli radio wifi off
 
If WIFI is enabled you can get the list of available APs by running
# nmcli dev wifi list
 
  SSID                          MODE    CHAN  RATE      SIGNAL  BARS  SECURITY 
  MyNetwork                    Infra  149  54 Mbit/s  100    ****  WPA1 WPA2
  OtherNetwork                  Infra  11    54 Mbit/s  75      ***  WPA2   
  AnotherNetwork                Infra  6    54 Mbit/s  75      ***  WPA2     
  FourthNetwork                Infra  64    54 Mbit/s  60      ***  WPA2     
  OpenNetwork                  Infra  3    54 Mbit/s  37      **    -- 
 
To connect to an open WIFI network run
# nmcli dev wifi connect <SSID>
 
To connect to a protected WIFI network run
# nmcli dev wifi connect <SSID> password <password>
 
To check connection status run
 
# nmcli dev show wlan0
 
=== Configuring static IP ===
 
By default WIFI connection will use DHCP. To switch to static IP configuration run the following commands:
 
# nmcli con mod <SSID> ipv4.method manual ipv4.addr <ipaddr>/<netmask> ipv4.gateway <gwaddr> ipv4.dns "<dns1>,<dns2>"
# nmcli con down <SSID>
# nmcli con up <SSID>
 
 
For example
 
# nmcli con mod MyNetwork ipv4.method manual ipv4.addr "192.168.1.100/24" ipv4.gateway 192.168.1.254 ipv4.dns "8.8.8.8,4.4.4.4"
# nmcli con down MyNetwork
# nmcli con up MyNetwork
 
The configuration will be saved in /etc/NetworkManager/system-connections/<SSID>.
 
= Using /etc/network/interfaces =
 
This is a legacy method with various limitations and we do not recommend using it, especially in combination with network managers.
The configuration file /etc/network/interfaces is used by ifup and ifdown tools to perform network interface configuration. In the SystemV init setup ifup and ifdown are invoked by /etc/init.d/networking boot script. In systemd setup /etc/init.d/networking is not available and you should create your own systemd service file that invokes "ifup -a" on startup and "ifdown -a" at shutdown.


The configuration will be saved in /var/lib/connman/ethernet_0eb31468dcc9_cable/settings.
If you configure all network interfaces via /etc/network/interfaces, it is better to completely disable the network manager service.
 
The example below configures eth0
 
<pre>
auto eth0
    iface eth0 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.254
    dns-nameservers 8.8.8.8 4.4.4.4
    dns-search foo.org bar.com
</pre>


The downside of this method is the use of non-constant parameters (e.g. MAC address) to identify the connection, which makes it impossible to incorporate the configuration into Yocto image and use it on multiple devices.
The DNS settings in /etc/network/interfaces rely on the availability of /sbin/resolvconf tool to update /etc/resolv.conf configuration file. If /sbin/resolvconf is not present, rebuild Yocto image after adding the following line to conf/local.conf


IMAGE_INSTALL_append = " resolvconf"


= Using nmcli =
Configuring WIFI via /etc/network/interfaces is not recommended.


The NetworkManager command line tool, nmcli, can also be used create static IP configuration. In the example below a static configuration is created for interface eth0.
== Interaction with ConnMan  ==


ConnMan will override the settings in /etc/network/interfaces unless configured to ignore relevant interfaces. For example, after configuring eth0 and eth1 in /etc/network/interfaces, you should create /etc/connman/main.conf and add the following:


# nmcli con add type ethernet ifname eth0 name static-eth0 ip4 192.168.1.100 gw4 192.168.1.254
<pre>
# nmcli con mod static-eth0 ipv4.dns "66.212.63.228,66.212.48.10"
[General]
NetworkInterfaceBlacklist=eth0,eth1
</pre>
 
ConnMan will also override the contents of /etc/resolv.conf as it includes its own DNS proxy. To prevent that, start ConnMan with the options "-r" or "--nodnsproxy"


== Interaction with NetworkManager ==


The configuration will be saved in /etc/NetworkManager/system-connections/static-eth0. Once generated it can be incorporated into Yocto image and used on multiple devices.
NetworkManager may override the contents of /etc/resolv.conf. To prevent that, create /etc/NetworkManager/NetworkManager.conf and add the following:
<pre>
[main]
dns=none
</pre>

Revision as of 11:34, 8 September 2019

Configuring Static IP Addresses

It is sometimes desirable to provide static IP configuration for network interface instead of relying on DHCP. Below we describe several methods of doing that.

Using ConnMan

ConnMan is the default Yocto network manager. Network manager is a program for providing detection and configuration for systems to automatically connect to network. In the examples below we demonstrate how ConnMan command line tool, connmanctl, can be used create static IP configuration.

Configuring wired interfaces

To get the list of wired interfaces run

# connmanctl services

For example:

# connmanctl services
*AO Wired                ethernet_0eb31468dcc9_cable

Wired interfaces will be shown as ethernet_<mac_address>_cable. Only interfaces physically connected to the network are displayed.

To create static IP configuration run

# connmanctl config <service> --ipv4 manual <ip address> <netmask> <gateway>
# connmanctl config <service> --nameservers <dns-addr> 

For example:

# connmanctl config ethernet_0eb31468dcc9_cable --ipv4 manual 192.168.1.100 255.255.255.0 192.168.1.254
# connmanctl config ethernet_0eb31468dcc9_cable --nameservers 8.8.8.8 4.4.4.4 

The configuration will be saved in /var/lib/connman/ethernet_0eb31468dcc9_cable/settings.

Configuring wireless interfaces

To check if wifi is enabled run

# connmanctl technologies

and check for the line that says Powered: True/False.

To power the wifi on run

# connmanctl enable wifi

To scan the network connmanctl accepts simple names called technologies. To scan for nearby wifi networks:

# connmanctl scan wifi

To list the available networks found after a scan run (example output):

# connmanctl services
MyNetwork               wifi_dc85de828967_68756773616d_managed_psk
OtherNET                wifi_dc85de828967_38303944616e69656c73_managed_psk
AnotherOne              wifi_dc85de828967_3257495245363836_managed_wep
FourthNetwork           wifi_dc85de828967_4d7572706879_managed_wep
AnOpenNetwork           wifi_dc85de828967_4d6568657272696e_managed_none

Every wifi network is identified by a name composed as

wifi_<hashlocal>_<hashremote>_managed_<encrption>

Connecting to an open network

To connect to an open network, look for wifi networks ending with _managed_none:

# connmanctl connect wifi_dc85de828967_4d6568657272696e_managed_none


Network names can be tab-completed

You should now be connected to the network. Check using ip addr or run

# connmanctl state.

Connecting to a protected network

Start connmanctl in interactive mode by running

# connmanctl

Now you need to register the agent to handle user requests. The command is:

connmanctl> agent on

You now need to connect to one of the protected services.

To do this easily, just use tab completion for the wifi_ service.

If you were connecting to OtherNET in the example above you would type:

connmanctl> connect wifi_dc85de828967_38303944616e69656c73_managed_psk

The agent will then ask you to provide any information the daemon needs to complete the connection.

The information requested will vary depending on the type of network you are connecting to.

The agent will also print additional data about the information it needs as shown in the example below.

Agent RequestInput wifi_dc85de828967_38303944616e69656c73_managed_psk
  Passphrase = [ Type=psk, Requirement=mandatory ]
  Passphrase?

Provide the information requested, in this example the passphrase, and then type:

connmanctl> quit

If the information you provided is correct you should now be connected to the protected access point.

Configuring Static IP

To create static IP configuration run

# connmanctl config <service> --ipv4 manual <ip address> <netmask> <gateway>
# connmanctl config <service> --nameservers <dns-addr> 

For example:

# connmanctl config wifi_dc85de828967_38303944616e69656c73_managed_psk --ipv4 manual 192.168.1.100 255.255.255.0 192.168.1.254
# connmanctl config wifi_dc85de828967_38303944616e69656c73_managed_psk --nameservers 8.8.8.8 4.4.4.4 

The configuration will be saved in /var/lib/connman/wifi_dc85de828967_38303944616e69656c73_managed_psk/settings.

Using NetworkManager

NetworkManager is an alternative network manager that can be used instead of ConnMann. Both network managers cannot coexist in the same Yocto image, so enabling NetworkManager requires disabling ConnMan and rebuilding the image.

In the examples below we use NetworkManager command line tool, nmcli, to create static configurations.

Configuring wired interfaces

To create static IP configuration run

# nmcli con add type ethernet ifname <ethX> con-name <name> ip4 <ipaddr> gw4 <gwaddr>
# nmcli con mod static-eth0 ipv4.dns "<dns1>,<dns2>"
# nmcli con up <name> 

For example, to create configuration for eth0 run

# nmcli con add type ethernet ifname eth0 con-name static-eth0 ip4 192.168.1.100/24 gw4 192.168.1.254
# nmcli con mod static-eth0 ipv4.dns "8.8.8.8,4.4.4.4"
# nmcli con up static-eth0


The configuration will be saved in /etc/NetworkManager/system-connections/static-eth0.

Configuring wireless interfaces

To check if WIFI is enabled by NetworkManager run

# nmcli dev show wlan0

and check the GENERAL.STATE line. If WIFI is enabled, the state is either connected or disconnected. If WIFI is disabled the state is unavailable.

To enable WIFI run

# nmcli radio wifi on

or if you need to disable it you can run

# nmcli radio wifi off

If WIFI is enabled you can get the list of available APs by running

# nmcli dev wifi list
  SSID                          MODE    CHAN  RATE       SIGNAL  BARS  SECURITY  
  MyNetwork                     Infra   149   54 Mbit/s  100     ****  WPA1 WPA2 
  OtherNetwork                  Infra   11    54 Mbit/s  75      ***   WPA2     
  AnotherNetwork                Infra   6     54 Mbit/s  75      ***   WPA2       
  FourthNetwork                 Infra   64    54 Mbit/s  60      ***   WPA2      
  OpenNetwork                   Infra   3     54 Mbit/s  37      **    --  

To connect to an open WIFI network run

# nmcli dev wifi connect <SSID>

To connect to a protected WIFI network run

# nmcli dev wifi connect <SSID> password <password>

To check connection status run

# nmcli dev show wlan0

Configuring static IP

By default WIFI connection will use DHCP. To switch to static IP configuration run the following commands:

# nmcli con mod <SSID> ipv4.method manual ipv4.addr <ipaddr>/<netmask> ipv4.gateway <gwaddr> ipv4.dns "<dns1>,<dns2>"
# nmcli con down <SSID>
# nmcli con up <SSID>


For example

# nmcli con mod MyNetwork ipv4.method manual ipv4.addr "192.168.1.100/24" ipv4.gateway 192.168.1.254 ipv4.dns "8.8.8.8,4.4.4.4"
# nmcli con down MyNetwork 
# nmcli con up MyNetwork

The configuration will be saved in /etc/NetworkManager/system-connections/<SSID>.

Using /etc/network/interfaces

This is a legacy method with various limitations and we do not recommend using it, especially in combination with network managers. The configuration file /etc/network/interfaces is used by ifup and ifdown tools to perform network interface configuration. In the SystemV init setup ifup and ifdown are invoked by /etc/init.d/networking boot script. In systemd setup /etc/init.d/networking is not available and you should create your own systemd service file that invokes "ifup -a" on startup and "ifdown -a" at shutdown.

If you configure all network interfaces via /etc/network/interfaces, it is better to completely disable the network manager service.

The example below configures eth0

auto eth0
    iface eth0 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.254
    dns-nameservers 8.8.8.8 4.4.4.4
    dns-search foo.org bar.com

The DNS settings in /etc/network/interfaces rely on the availability of /sbin/resolvconf tool to update /etc/resolv.conf configuration file. If /sbin/resolvconf is not present, rebuild Yocto image after adding the following line to conf/local.conf

IMAGE_INSTALL_append = " resolvconf"

Configuring WIFI via /etc/network/interfaces is not recommended.

Interaction with ConnMan

ConnMan will override the settings in /etc/network/interfaces unless configured to ignore relevant interfaces. For example, after configuring eth0 and eth1 in /etc/network/interfaces, you should create /etc/connman/main.conf and add the following:

[General]
NetworkInterfaceBlacklist=eth0,eth1

ConnMan will also override the contents of /etc/resolv.conf as it includes its own DNS proxy. To prevent that, start ConnMan with the options "-r" or "--nodnsproxy"

Interaction with NetworkManager

NetworkManager may override the contents of /etc/resolv.conf. To prevent that, create /etc/NetworkManager/NetworkManager.conf and add the following:

[main]
dns=none