Source: RESIN BLOG

Balena Blog How to configure a static IP address with resin.io

Recently a number of users have asked us how to set static IP addresses on resin.io devices with use cases ranging from a strict firewall which blocks traffic from all but whitelisted IP addresses to setting a stable IP address for easy SSH access on the local network.This post explains how to do this step-by-step.ConfigurationWe use connman to manage network connections. This runs as a service in the host OS and on boot we set its configuration based on the contents of config.json, located on a FAT partition of the SD card (or eMMC storage for applicable devices.)In order to configure a static IP address you need to adjust the contents of the this file to add the appropriate connman setting. While you can mount the partition remotely via web terminal or a custom configured SSH connection and adjust it that way, it is far easier to do this in advance of provisioning your device.The guide below assumes you are adjusting the file prior to provisioning your device, however if you do this remotely the process remains exactly the same, though the device will require a reboot to pick up the new settings.NOTE: As always we're constantly working on improving our service, and are actively working on adding the ability to configure the network via our API, which will make the process even easier.GuideDownload a resin.io image or grab an already provisioned SD card.Mount the FAT partitions of the image either directly from the image file, or by burning the SD card and mounting it on your computer. The configuration volume is labelled resin-conf:Open config.json. The file will look something like:{"applicationId":"1234","applicationName":"foobar","deviceType":"raspberry-pi2","apiKey":"0123456789abcdefghijklmnopqrstuv","userId":"102420484096","username":"jack_frost","apiEndpoint":"https://api.resin.io","listenPort":"48484","mixpanelToken":"0123456789abcdefghijklmnopoqrstu","pubnubPublishKey":"xxx-x-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","pubnubSubscribeKey":"xxx-x-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","registryEndpoint":"registry.resin.io","vpnEndpoint":"vpn.resin.io","vpnPort":"443","appUpdatePollInterval":"60000","wifiSsid":"foobar","wifiKey":"bazblah","files":{"network/settings":"[global]\nOfflineMode=false\n\n[WiFi]\nEnable=true\nTethering=false\n\n[Wired]\nEnable=true\nTethering=false\n\n[Bluetooth]\nEnable=true\nTethering=false","network/network.config":"[service_home_ethernet]\nType = ethernet\nNameservers = 8.8.8.8,8.8.4.4\n\n[service_home_wifi]\nType = wifi\nName = foobar\nPassphrase = bazblah\nNameservers = 8.8.8.8,8.8.4.4"},"uuid":"0123456789abcdefghijklmnopoqrstuvwxyz0123456789abcdefghijklmno","registered_at":1445953960566,"deviceId":12345678901234}As this is JSON encoded it is somewhat unfriendly, and we're working on making the process a lot less painful, but for now we need to dig inside!The network/network.config key/value pair contains connman configuration settings and is where we need to set the static IP. Taking the example above, the value we're interested in is:"[service_home_ethernet]\nType = ethernet\nNameservers = 8.8.8.8,8.8.4.4\n\n[service_home_wifi]\nType = wifi\nName = foobar\nPassphrase = bazblah\nNameservers = 8.8.8.8,8.8.4.4"Locate the ethernet or wifi connection you'd like to add a static IP address to - you'll need to add the setting to the end of the appropriate section, in this example this would be immediately after the Nameservers setting for either [service_home_ethernet] if you're setting for ethernet, or [service_home_wifi] if you're setting for wifi.The format required to set a static IP address is:IPv4 = <static ip>/<netmask>/<gateway> If you aren't sure about netmask, 255.255.255.0 is almost always the right way to go. Follow these instructions to determine your gateway IP.For a static IP of 192.168.0.2, a netmask of 255.255.255.0 and a gateway of 192.168.0.1, we'd set:IPv4 = 192.168.0.2/255.255.255.0/192.168.0.1 Adding this to the wifi configuration of our example above yields:"[service_home_ethernet]\nType = ethernet\nNameservers = 8.8.8.8,8.8.4.4\n\n[service_home_wifi]\nType = wifi\nName = foobar\nPassphrase = bazblah\nNameservers = 8.8.8.8,8.8.4.4\nIPv4 = 192.168.0.2/255.255.255.0/192.168.0.1"Important: Don't forget to keep this JSON encoded and especially pay attention to \n newline escape characters. A missing newline could break the configuration here.You're done! If you've mounted the image locally, write it to your SD card, or if you're editing the file in the SD card directly, simply eject it and boot your device with it as you would normally.AdvancedIf you're curious about advanced connman configuration check out their full documentation, you can configure whatever you like by adjusting the appropriate fields in the same key/value pair.As I mentioned above, we are in the process of exposing the network configuration via the API so if you have any ideas or requests regarding the implementation of this feature please don't hesitate to reach out on our support service (the question mark lurking in the bottom right of your dashboard), our community forum or via email at support@resin.io!Happy hacking!

Read full article »
Est. Annual Revenue
$100K-5.0M
Est. Employees
1-25
Alexandros Marinos's photo - Co-Founder & CEO of Balena

Co-Founder & CEO

Alexandros Marinos

CEO Approval Rating

89/100

Read more