Static IP Address: Difference between revisions

From Variscite Wiki
No edit summary
No edit summary
Line 2: Line 2:


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 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:
<pre>
root@imx7-var-som:~# connmanctl services
*AO Wired                ethernet_0eb31468dcc9_cable
</pre>
 
Wired interfaces will be shown as ethernet_<mac_address>_cable.
 
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 you can run
 
# connmanctl technologies
 
and check for the line that says Powered: True/False.
 
To power the wifi on you can 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>
 
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.
= Using NetworkManager =
 
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.
 
# nmcli con add type ethernet ifname eth0 name static-eth0 ip4 192.168.1.100 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. Once generated it can be incorporated into Yocto image and used on multiple devices.


= Using /etc/network/interfaces =
= Using /etc/network/interfaces =
Line 47: Line 126:
dns=none
dns=none
</pre>
</pre>
= Using connmanctl =
The ConnMan command line tool, connmanctl, can be used create static IP configuration. To create initial configuration the relevant interface should be connected to the network.
To get the list of connections run
# connmanctl services
For example:
<pre>
root@imx7-var-som:~# connmanctl services
*AO Wired                ethernet_0eb31468dcc9_cable
</pre>
Wired Ethernet connections will be shown as ethernet_<mac_address>_cable.
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.
The downside of this method is the use of non-constant parameters (e.g. MAC address) to identify the connection, which makes it difficult to incorporate the configuration into Yocto image and use it on multiple devices.
= Using nmcli =
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.
# nmcli con add type ethernet ifname eth0 name static-eth0 ip4 192.168.1.100 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. Once generated it can be incorporated into Yocto image and used on multiple devices.

Revision as of 17:06, 16 October 2018

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.

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 you can run

# connmanctl technologies

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

To power the wifi on you can 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>

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.

Using NetworkManager

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.

# nmcli con add type ethernet ifname eth0 name static-eth0 ip4 192.168.1.100 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. Once generated it can be incorporated into Yocto image and used on multiple devices.

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 will override the contents of /etc/resolv.conf as it uses dnsmasq tool as DNS proxy. To prevent that, create /etc/NetworkManager/NetworkManager.conf and add the following:

[main]
dns=none