Network setup

Karl Auer kauer at biplane.com.au
Tue Jan 5 05:04:45 UTC 2016


On Mon, 2016-01-04 at 21:31 -0200, Thiago Farina wrote:
> It is not that I like it. I'm just trying to show that the process for
> setting a static ip could be made simpler. On Mac you can, with **one
> command line**. Can you do the same on Ubuntu?

Not one line, but two. It dependd on what the optional "gateway" in
networksetup does. If it just sets up the link route for the configured
address, then it's one line in Linux too. If it sets up a default route,
you need two lines in Linux. Easy enough to pack in a script if you do
it often. And BTW, you can statically configure interfaces using the
NetworkManager GUI if you want. "Edit connections" and follow the
bouncing ball.

Assuming you do want to do it outside NetworkManager, there's a
preparatory step - telling Networkmanager *not* to manage the interface.

Add these lines to /etc/network/interfaces:

    auto eth0
    iface eth0 manual

Then restart Networkanager. These two steps only have to be done once -
NetworkManager will henceforth *not* manage that interface and you can
do whatever you like with it, free of NetworkManager's "interference".

I *think* you can also use the NetworkManager GUI to stop it managing an
interface. "Edit connections", then the "General" tab, and uncheck
"Automatically connect to this network when it is available". I have
never used this method myself. You might also be able to use nmcli, but
I've never tried that.

In these examples I've left my wlan0 interface visible; it's being
managed by Networkmanager. eth0 is not.

After telling NetworkManager not to manage the interface (here eth0),
you can configure it like this:

    ip addr add 192.168.100.100/24 dev eth0

That adds the address to the interface, and sets up a link route for it.
This is how the routing table looks afterwards:

kauer at karl:~$ ip route
default via 192.168.1.1 dev wlan0  proto static 
192.168.1.0/24 dev wlan0  proto kernel  scope link  src 192.168.1.196
metric 9 
192.168.100.0/24 dev eth0  proto kernel  scope link  src
192.168.100.100 

Notice that the previous default route is unchanged. If you want the
default route to point over the interface as well, it's this command
(assuming the gateway is 192.168.100.1 of course):

    ip route change default via 192.168.100.1

Afterwards, the routing table looks like this:

kauer at karl:~$ ip route
default via 192.168.100.1 dev eth0 
192.168.1.0/24 dev wlan0  proto kernel  scope link  src 192.168.1.196
metric 9 
192.168.100.0/24 dev eth0  proto kernel  scope link  src
192.168.100.100 

So no, you don't get "one command" to do it. But the few commands you
need are simple and obvious, and very easy to script. ip even has a
"-b" (or "--batch") option that reads ip subcommands from a specified
file.

Here's a sample, written off the top of my head and completely
untested ;-) that sets up the interface and optionally sets up a default
route.

#!/bin/sh
NETWORK="$1"
INTERFACE="$2"
GATEWAY="$3"

if [ -z "$2" ] ; then
   echo "Usage: $0 <network> <interface> [<gateway>]"
   echo "Provide network in \"address/prefix\" format.
fi

ip addr add "$NETWORK" dev "$INTERFACE"

if [ ! -z "$GATEWAY" ] ; then
   ip route change default via "$GATEWAY"
fi

You could call this script "networksetup" :-)

Regards, K.

PS: All of the commands above that change addresses or routes need to be
done as root or using sudo.

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Karl Auer (kauer at biplane.com.au)
http://www.biplane.com.au/kauer
http://twitter.com/kauer389

GPG fingerprint: E00D 64ED 9C6A 8605 21E0 0ED0 EE64 2BEE CBCB C38B
Old fingerprint: 3C41 82BE A9E7 99A1 B931 5AE7 7638 0147 2C3C 2AC4






More information about the ubuntu-users mailing list