Is it possible to put a real IP (not in the 127.x.x.x) range on a loopback device?
Nothing forbids to do it.
# ifconfig lo:1 10.0.0.1/8 # ifconfig lo:1 lo:1 Link encap:Local Loopback inet addr:10.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:65536 Metric:1 # ping -c 1 10.0.0.1 PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data. 64 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=0.025 ms --- 10.0.0.1 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.025/0.025/0.025/0.000 ms
For this address to persist after a reboot on Ubuntu 16.04, you can modify your
/etc/network/interfaces file with these
auto lo lo:1 iface lo inet loopback iface lo:1 inet static address 10.0.0.1 network 10.0.0.0 netmask 255.0.0.0
On the current Linux kernel with the
ip utility it is quite simple:
ip addr add 10.0.1.8 dev lo
This can be useful when you have a service that binds a port on an interface, and want to run a different program on the same port and network. I use it to enable both
dnsmasq to co-exist on the same server.
If you are using
/etc/network/interfaces to configure your interfaces, then update the
lo stanza to include:
up ip addr add 10.0.1.8 dev lo
As an alternative to using
lo:0, you can also use
dummy interfaces in Linux as in:
ifconfig dummy0 10.0.0.1/32 ifconfig dummy1 10.1.1.1/24
In addition to others answers:
- I do not recommend changing the usual/official loopback interface address as a lot of functionality depends on it;
- however, you can have/create several loopback/dummy interfaces - either lo:0 to lo:255 or dummyX interfaces;
- it has to be taken into account that lo:0 to lo:255 are aliases, whilst dummyX are full interfaces;
- moreover, one of the usual tactics in Linux for creating virtual IPs over BGP or OSPF is assigning them to loopback/dummy interfaces AND making pathways to them via routing;
- again, some daemons have problems with announcing addresses in aliases (for instance, quagga ) - so dummyX interfaces are advised in those cases;
- I would stress that without routing in the infrastructure, such addresses are only known/capable of being used in the server in question;
- assigning a private/public address to a loopback interface, without the proper routing can be a temporary low computing cost measure to blacklist communications temporarily with an IP address/network.
For more details, see for instance a BIND anycast setup tutorial done with Quagga/BIRD.
routing clues here: OSPF: Migrating Quagga to BIRD
P.S. Linux by default only creates dummy0 and dummy1 and has to be instructed to create a bigger number of dummy interfaces.
Yes, but that doesn't mean it's a good idea. If you use an IP that is ever going to be accessed by your system, any data that it tries to send there will be redirected to the local system instead, which can cause all kinds of odd networking issues. This means in particular that you can't safely use anything outside of the following ranges:
127.0.0.0/8 192.0.2.0/24 198.51.100.0/24 203.0.113.0/24 240.0.0.0/4
With the possible exception of any of the following dependent on how your other network interfaces are configured:
10.0.0.0/8 192.168.0.0/16 172.16.0.0/12 169.254.0.0/16
This is a case where RFC 1925, section 2, item 3 applies.