Static IP Address

From Variscite Wiki
Revision as of 09:19, 18 October 2018 by Admin (talk | contribs)
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 ConnMann

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:

root@imx7-var-som:~# 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 <service> --ipv4 manual <ip address> <netmask> <gateway>
# connmanctl config <service> --nameservers <dns-addr> 

For example:

 # connmanctl 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 <service> --ipv4 manual <ip address> <netmask> <gateway>
# connmanctl config <service> --nameservers <dns-addr> 

For example:

 # connmanctl 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> name <name> ip4 <ipaddr> gw4 <gwaddr>
# nmcli con mod static-eth0 ipv4.dns "<dns1>,<dns2>"

For example, to create configuration for eth0 run

# nmcli con add type ethernet ifname eth0 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"


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