On RHEL 6.5 how to make my own hostname locally resolvable, if my ip address is dynamic and not registered in DNS?

I know, it's possible to add dhcp hook scripts. Is there a similar way to hook the network initscript, so this works with static config too?

| improve this question | | | | |
  • Do you need to use your network IP? What about the loopback address ( – John WH Smith Dec 16 '14 at 16:05

There's a standard custom script /sbin/ifup-local. It is called for each adapter. My script obtains the ip and hostnames and adds them to /etc/hosts

# /sbin/ifup-local
set -e

fn_get_ip() {
ip addr show dev "$1" | sed "s,.* inet \([^/]*\)/.*,\1,;t;d"

fn_aaa() {
ip=`fn_get_ip "$1"`
hostnames=`hostname | sed "s,\([^.]*\)\..*,\1 &,"`

hint='# added by ifup-local'
sed -i "/$hint/d" /etc/hosts
echo "$ip $hostnames $hint" >>/etc/hosts

case $1 in
fn_aaa "$1"
| improve this answer | | | | |

You can use avahi daemon to bind your IP address to some resolvable host name (e.g. linux.local) each time when your IP is changed by DHCP.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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