To change FreeBSD’s server hostname:
hostname NEWNAME
To make it permanent, surviving reboot:
edit in file /etc/rc.conf
hostname="NEWNAME"
edit in file /etc/hosts
127.0.0.1 NEWNAME
::1 NEWNAME
Here is a SH script to download or fetch: hostname.sh
#!/bin/sh
# fetch http://wp.t66.asia/hostname.sh
host=`uname -n`
read -p "server hostname [$host]: " newhost
if [ -n "$newhost" ]; then
hostname $newhost
echo "CHANGE hostname: $host -> $newhost [`uname -n`]"
echo "EDIT /etc/rc.conf"
sed -i "" 's|hostname="'$host'"|hostname="'$newhost'"|g' /etc/rc.conf
echo "EDIT /etc/hosts"
sed -i "" 's|'$host'|'$newhost'|g' /etc/hosts
cat /etc/rc.conf
cat /etc/hosts
echo New hostname: `uname -n`
else
echo "server hostname unchanged"
fi
That’s it. No reboot is required.