1

This might sound a little strange, but I try to achieve to set up an ip address permanently in Ubuntu using the command line, but not an editor.

My idea is to provision servers and just type in the ip address once and the script takes care of this.

I just can't imagine there is nothing out there that writes

ifconfig eth1 10.1.1.1 netmask 255.255.255.0 up
into the /etc/network/interfaces file.
David
  • 159
  • 8
  • 1
    Sorry, are you saying that you understand how the `interfaces` file controls IP address assignations, but you want a command that both sets the address and updates that file in a single operation? – MadHatter Dec 16 '13 at 14:37
  • Exactly! I totally understand how to configure a network interface with an editor and have done this hundreds of times. Now I try to figure out how to do this from the command line permanently. – David Dec 16 '13 at 14:42
  • 2
    `man echo` would be a good place to start for figuring out how to insert text from a script into a file. – Jenny D Dec 16 '13 at 14:44
  • 1
    I'm afraid this wouldn't be the first place in UNIX that the way to do something now, and the way to ensure it's done in future, are separate (eg, `chkconfig foo on` doesn't also do `service foo start`; `iptables -A foo -j bar` doesn't also update `/etc/sysconfig/iptables`, and so on). – MadHatter Dec 16 '13 at 14:46
  • I am aware of doing that and able to do that, I thought there might be a tool out there maybe that takes care exactly of this. – David Dec 16 '13 at 14:46

2 Answers2

6

You can easily write a script which :

  • Reads IP adress from command line
  • Updates /etc/network/interfaces accordingly
  • restart the network

A much better idea is to use software configuration management : Tools like puppet have many advantages over home-made scripts.

1

Will a redirect from the command line work?

echo -e "auto eth0\niface eth0\ninet static\naddress 10.1.1.1\nnetmask 255.255.255.0\n" > /etc/network/interfaces
MadHatter
  • 79,770
  • 20
  • 184
  • 232
Lego
  • 465
  • 4
  • 7
  • 12
  • Won't help much if `eth0` already appears in that file. – MadHatter Dec 16 '13 at 15:44
  • You are over-writing the /etc/network/interfaces, but you haven't included a stanza for the loopback adapter. This will almost certainly result in a very broken system. – Zoredache Dec 16 '13 at 20:15