CentOS 를 설치하면 /etc/hosts 에는 localhost 만 등록되어 있다.
DHCP로 IP를 받고 hostname 을 받은 경우, 자동으로 hosts 파일을 업데이트 할 수 있다.
#!/bin/sh
# for Ubuntu
# /etc/dhcp/dhclient-exit-hooks.d/sethostname
# for CentOS
# /etc/dhcp/dhclient.d/sethostname
# for debugging:
echo "sethostname BEGIN"
export
set -x
echo new_ip_address=$new_ip_address
echo new_host_name=$new_host_name
echo new_domain_name=$new_domain_name
oldhostname=$(hostname -s)
if [ $oldhostname != $new_host_name ]; then
# Rename Host
echo $new_host_name > /etc/hostname
hostname -F /etc/hostname
fi
# Update /etc/hosts if needed
TMPHOSTS=/etc/hosts.dhcp.new
if ! grep "$new_ip_address $new_host_name.$new_domain_name $new_host_name" /etc/hosts; then
# Remove the 127.0.1.1 put there by the debian installer
grep -v '127\.0\.1\.1 ' < /etc/hosts > $TMPHOSTS
# Add the our new ip address and name
echo "$new_ip_address $new_host_name.$new_domain_name $new_host_name" >> $TMPHOSTS
mv $TMPHOSTS /etc/hosts
fi
echo "sethostname END"