Update /etc/hosts

Discussion in 'Server Operation' started by nrtdata, Jun 2, 2021.

  1. nrtdata

    nrtdata New Member

    Hi.
    I create a ubuntu 16.04 server with ispconfig3 following the perfect server guide. Everyting is ok.
    Im working with dynamic ip, and i use dnsexit, i think is the best dynamic dns service. All is working well. I also setup a script to update ispconfig ip's ipupdater.sch.my
    Only problem i have is my /etc/hosts.. in here, my ip is local--> 192.168.1.2 and i need the external ip instead.
    I need to create a bash script and then setup a crontab -e to execute it every 3 minutes.
    If i execute something like this: dig +short myip.opendns.com @resolver1.opendns.com i get my external ip, but now i need to echo to --> /etc/hosts including my fqdn.
    If someone can help...
    Thanks and sorry my english.
     
  2. nrtdata

    nrtdata New Member

    I get almost close with this script
    Code:
    #!/bin/sh
    hostname=$server.domain.tld
    sed -i '/'$hostname'$/ d' /etc/hosts
    ipaddr=$(dig +short myip.opendns.com @resolver1.opendns.com)
    echo "$ipaddr $hostname" >>/etc/hosts
    problem here is hostname. i just get: ".domain.tld" whitout quotes. but no the full fqdn of the server.
    Thanks
     
  3. till

    till Super Moderator Staff Member ISPConfig Developer

    Ubuntu 16.04 is completely outdated. use the current Ubuntu LTS version Ubuntu 20.04 instead.
     
  4. ahrasis

    ahrasis Well-Known Member

    You can already build ISPConfig server using official script nowadays and I concur with @till for using Ubuntu 20.04 instead of the outdated Ubuntu 16.04.

    Many can help with the script provided they know what are you actually up to as it is common to have local ip in /etc/hosts when you are behind a nat router and your ISPConfig server should work fine with it.

    Other thing is dynamic dns service provider is best set at the router level and you can get your server up for production by forwarding all relevant ports to your ISPConfig server.
     
    nrtdata likes this.
  5. nrtdata

    nrtdata New Member

    Thanks for your reply.
    Router is ok, server is on DMZ. I agree with both reply about upgrade to newest distro, but im ok with this one. Everything is working as expected. I also add support to multiple php versions on ispc. from 5.2 to 8.0, as i say, all is working ok.
    I just want to update /etc/hosts file, nothing else.
    Thanks.
     
  6. ahrasis

    ahrasis Well-Known Member

    I am still not clear on why you need the script to change /etc/hosts to replace local ip with public ip since you are behind a nat router.

    So far that I am concern you can manually add public ip with hostname fqdn right after local ip line, and when public ip is changed, get that new public ip and change that specifc line only.

    A more advanced script will need you to describe your plan and purpose for the same but I think you should already get my point above.

    I don't think that this is advisable. Use port forwarding feature of your router is much safer.
     
  7. nrtdata

    nrtdata New Member

    Hi.
    If someone needs a solution for this...
    Code:
    #!/bin/sh
    # Set FQDN
    fqdn=$hostname
    # Find & replace the line hostname(FQDN) the ($) its a linestop (actualy, erease all the content of hosts file)
    sed -i '/'$myHostName'$/ d' /etc/hosts
    # Get external public IP for our FQDN
    ipaddr=$(dig +short myip.opendns.com @resolver1.opendns.com)
    # Set IP and FQDN to -> /etc/hosts
    echo "127.0.0.1        localhost" >> /etc/hosts
    echo "$ipaddr $fqdn    host.domain.tld host" >> /etc/hosts #Replace "host.domain.tld host" with your own fqdn and host
    echo " " >> /etc/hosts
    echo "# The following lines are desirable for IPv6 capable hosts" >>
    
    /etc/hosts
    echo "::1    localhost    ip6-localhost    ip6-loopback" >> /etc/hosts
    echo "ff02::1    ip6-allnodes" >> /etc/hosts
    echo "ff02::2    ip6-allrouters" >> /etc/hosts
    # Now, we need to restart bind9 and postfix
    service bind9 restart
    service postfix restart
    You can execute the code adding it to your crontab.
    Now, a home server with dynamic ip looks like a real dedicated server with dedicated ip. Only thing bad, its the ip pool that my provider assign to me.. these ips are on blacklists so email out are rejected. Everything else, works really really smooth.
     
  8. ahrasis

    ahrasis Well-Known Member

    Basically, my old dynamic IPV4 script at https://ipupdater.sch.my/ that you were using, contains all the needed code but they are in php of course.

    The purpose is mainly to run public dns server function where public IPV4 is needed in them instead of local IPV4, other then web, file and database server function, but it was never meant to directly run mail server function as that won't be possible with dynamic ip.

    In any event, I am glad that you manage to make your bash script to change /etc/hosts but I believe there is a better way to code them and think by time you will find a better way to improve the same.

    I still think, however, it is better not to put a server in DMZ unnecessarily though I think that is the reason why you need to create and run your script on cron job as you mentioned above at the first place.

    In my experience your server may still run fine even if they are not put in DMZ and kept running behind your router firewall via port forwarding.

    Anyway, it may be a matter of preference, so good luck with your setup.
     
    nrtdata likes this.
  9. nrtdata

    nrtdata New Member

    Oh!! you are the developer behind ipupdater.sch? wow man! Thanks you for your work! Really helps me on seting up hosting servers on some offices here in my country! I really apreciate your work. I already remove server from DMZ and setup port forwarding instead. Performance its good, even server load its ok, 0.16 max, with everything working, 3 sites, subdomains as vhost, letsencript, dns, mail, several versions of PHP..
    Again, thanks you for your work!
     
    ahrasis likes this.

Share This Page