IP-address binding woes in Ubuntu Server 10.04 LTS

If you’re on an Ubuntu Server with multiple IP addresses and you want to bind networking software like sshd or Squid to a dediated network interface (unlike you
might eventually end up with error messages like:

error: Bind to port 22 on 123.234.456.789 failed: Cannot assign requested address.

Which is bad, since you may lock yourself out of the server.

The same may also happen to Squid:

commBind: Cannot bind socket FD 19 to 123.234.456.789:3128: (99) Cannot assign requested address

Both, Squid and sshd, have in common, that they’re upstart-ed from /etc/init. What happens in 10.04 is that ssh or squid are started too early, at least if you configured multiple network interfaces on the server.
And another weird thing is that for every network interface in /etc/network/interfaces the scripts in /etc/network/if-up.d/* are being re-executed when the network is brought up at boot time.
That’s pretty lame if you ask me because if the server has 10 network interfaces, sshd is being started and stopped 10 times during boot.

Anyway, the trick is to tell upstart not to start certain processes until the last network interface on a server is up.
In my case, the last network interface is venet0:8 so the start command in /etc/init/ssh.conf and/or /etc/init/squid.conf has to be modified to:

start on filesystem and net-device-up IFACE=venet0:8

This also prevents the useless restarting of sshd when the network is being started. And again, this only happens if the server has multiple IP addresses and you’re not using the standard bind-’em-all address for processes that are started from /etc/init.