在Ubuntu上配置bind9 DNS服务器,解析CentOS虚拟机主机名
Got it, let's walk through how to get this set up step by step so your clients can access the CentOS web server via http://ixchariot-server/. Here's what we need to tackle:
First, log into your CentOS VM and set its hostname permanently:
sudo hostnamectl set-hostname ixchariot-server
Verify the change with hostname—it should output ixchariot-server.
Next, update the /etc/hosts file to map the VM's IP to its new hostname (this helps local resolution on the VM itself):
sudo nano /etc/hosts
Add this line at the bottom:
172.29.15.7 ixchariot-server ixchariot-server.localdomain
Save and exit (Ctrl+O, Enter, Ctrl+X). You can log out and back in to see the hostname update in your shell prompt.
Since your Ubuntu machine acts as both a DHCP server and a DNS resolver for clients, we need to make sure it can map ixchariot-server to the CentOS VM's IP, and that it shares this mapping with clients.
2.1 Update Ubuntu's Local Hosts File
First, add the mapping to Ubuntu's /etc/hosts so the host itself can resolve the name:
sudo nano /etc/hosts
Add this line:
172.29.15.7 ixchariot-server
Save and exit. Test it with ping ixchariot-server—you should see responses from 172.29.15.7.
2.2 Configure DHCP to Serve the Correct DNS & Domain Mapping
Assuming you're using dnsmasq (a common combined DHCP/DNS tool on Ubuntu) for your DHCP server:
- Open the dnsmasq config file:
sudo nano /etc/dnsmasq.conf - Add this line to force all clients using your DHCP to resolve
ixchariot-serverto the VM's IP:address=/ixchariot-server/172.29.15.7 - Also make sure your DHCP config tells clients to use the Ubuntu host as their DNS server. Look for a line like:
If it's not there, add it.dhcp-option=option:dns-server,172.29.15.1 - Restart dnsmasq to apply changes:
sudo systemctl restart dnsmasq
If you're using isc-dhcp-server instead:
- Update your DHCP config (usually
/etc/dhcp/dhcpd.conf) to set the DNS server to172.29.15.1:option domain-name-servers 172.29.15.1; - Then, on Ubuntu, set up a local DNS server (like
dnsmasqorbind9) that includes theixchariot-server->172.29.15.7mapping.
Since your clients are on the 172.29.14.x subnet and the CentOS VM is on 172.29.15.x, you need to enable IP forwarding on the Ubuntu host to let traffic pass between the two:
- Enable forwarding temporarily:
sudo echo 1 > /proc/sys/net/ipv4/ip_forward - Make it permanent by editing
/etc/sysctl.conf:
Find the linesudo nano /etc/sysctl.confnet.ipv4.ip_forward=0and change it to:net.ipv4.ip_forward=1 - Apply the change:
sudo sysctl -p
- On a client machine (e.g.,
172.29.14.1), verify it's using172.29.15.1as its DNS server (check withnslookuporipconfig/ifconfig). - Ping
ixchariot-serverfrom the client—you should get responses from172.29.15.7. - Finally, open a browser and navigate to
http://ixchariot-server/—you should see your CentOS web server's content.
内容的提问来源于stack exchange,提问作者SamJ

