【问题标题】:Updating Dynamic IP in local DNS server for hostname Resolution更新本地 DNS 服务器中的动态 IP 以进行主机名解析
【发布时间】:2020-12-16 02:16:21
【问题描述】:

我正在使用设备 dev1(hostname:dev1.example.com), dev2 通过路由器连接在同一网络中,

有来自 ISP 的连接。

 When device dev1 got dynamic IP assigned, automatically device hostname and IP should be configured 

在 DNS 服务器中,以便 dev2 可以使用主机名 ping dev1。

When i googled for the same, came across DDNS option. When i gone through it is asking to purchase domain and login with the domain credentials in router configuration.


I want to use my own domain without purchasing and enable DDNS support, is it possible to achieve this.

【问题讨论】:

  • 需要更多关于您在此处尝试执行的操作的信息。您有内部 DNS 服务器吗?主机名可以通过内部 DNS 或外部(公共 DNS)服务器解析。
  • 在网络中,我们希望设备使用主机名进行通信,无论是静态IP还是动态IP。我们可以使用 mDNS 支持解决同样的问题。

标签: linux dns


【解决方案1】:

下面是更新 /etc/hosts 文件中 ip 地址的脚本

#!/bin/sh
# Set the server's hostname
myHostName=$HOSTNAME
# Find and remove the current line with hostname at the end of line in /etc/hosts ($ is for end of line)
sed -i '/'$myHostName'$/ d' /etc/hosts
# Lookup the current IP Address and strip it out of the result
ipaddr=$(/usr/sbin/ip addr show eth0 | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1  -d'/')
#  Add the server's current IP Address and HostName to /etc/hosts
echo "$ipaddr $myHostName" >>/etc/hosts

注意:您必须根据您的情况将 eth0 更改为所需的端口名称

在 crontab 中添加此脚本以每分钟运行一次,因此每当 ip 更改时它会自动更新

    # enter below command
    crontab -e

    # add below entry 
    * * * * *     sh /root/update_dns_entry.sh

请注意,这只是一种解决方法,正确的做法是将您的 mac 地址设置为 dhcp ip 以提供始终相同的 ip,因为您从 ISP 获取 ip 地址,所以您无法控制它

【讨论】:

  • 不应该在我们的设备上更改 /etc/hosts,我们已经使用 mDNS 支持解决了这个问题。
  • 很高兴听到它正在工作,请将它添加到答案中,它将帮助其他人设置
猜你喜欢
  • 1970-01-01
  • 2016-07-05
  • 2021-10-08
  • 1970-01-01
  • 2010-11-21
  • 2014-10-13
  • 2013-06-30
  • 2013-03-25
  • 2020-03-24
相关资源
最近更新 更多