Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How do I change the IP address of an interface from a script?

I should say that I don't want to use ifconfig and also I don't want to edit the /etc/network/interfaces file manually. I need a script.

share|improve this question

1 Answer 1

If you are using ifup to configure the interface then you can define multiple so-called logical interface definitions for the interface and switch among them using ifup and ifdown. For example, you can define "iface home" and "iface work" in /etc/network/interfaces and then bring up eth0 either with "ifup eth0=home" or "ifup eth0=eth0-work". See ifup(8). Example of doing this from a script follows.

#!/bin/sh
ifdown eth0
ifup eth0=home

If you are using NetworkManager to configure the interface then you can define multiple so-called connections and switch between them using the nmcli command.

#!/bin/sh
nmcli con down id  'Wired connection work'
nmcli con up id  'Wired connection home'

If you are using neither ifupdown nor NetworkManager and you don't want to use ifconfig then you can use the ip utility. See ip(8).

#!/bin/sh
ip link set dev eth0 ...
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.