Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


How to set custom dns server within OpenVZ container (Ubuntu 14.04)?
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

How to set custom dns server within OpenVZ container (Ubuntu 14.04)?

I am using wable and their system automatically reverts the nameservers to 8.8.8.8 on system restart. Changing it in /etc/resolv.conf will work for sometime, but it doesn't persist ofc. Tried following these suggestions here:

http://unix.stackexchange.com/questions/128220/how-do-i-set-my-dns-on-ubuntu-14-04

But the network config within an ovz container seem to be drastically different.. Anyone have any experience with this?

Comments

  • teknolaizteknolaiz Member
    edited October 2015

    Open /etc/rc.local and add these lines to the end:

    echo "nameserver ip" > /etc/resolv.conf
    echo "nameserver ip2" >> /etc/resolv.conf
    

    If you use more DNS server just add another "echo "nameserver ip3" >> /etc/resolv.conf" line. On every boot/reboot the system will change the DNS servers to the one you specified in rc.local and they should stay so all the time.

    I have a few OpenVZ and DNS servers never changed magically by themselves ones changed once I changed them manually. Only on reboot/boot. And the solution above will change them on reboot/boot to the one you want.

    Thanked by 1smallet
  • You can just set chattr +i on /etc/resolv.conf after you set your DNS.

    Thanked by 1smallet
  • @Kupol said:
    You can just set chattr +i on /etc/resolv.conf after you set your DNS.

    Umm, what does that actually do? Sry, kind of a linux noob...

  • teknolaizteknolaiz Member
    edited October 2015

    @smallet The command "chattr +i" sets a immutable file attribute for /etc/resolv.conf. That means that "a file with the i attribute cannot be modified. It cannot be deleted or renamed, no link can be created to this file and no data can be written to the file. When set, prevents, even the superuser, from erasing or changing the contents of the file."

  • smalletsmallet Member
    edited October 2015

    Oh, nice. But does feel kind of hacky. Had to do: apt-get install e2fsprogs before I could use the command though. So I just use chattr -i to remove the 'lock' right?

    Thanks for the help. I saw the same suggestion in stackexchange, but didn't understand how that would help me. Didn't know what an immutable flag was.

  • @smallet Yes, chattr syntax is a bit like the chmod syntax. + adds the attribute and - removes it. The tool needs e2fsprogs in order to set all the advanced and additional filesystem attributes.

    If you don't really want to use that way you can try my solution. The system will simply override the changes made by OpenVZ when you reboot your VPS, back to the DNS servers you want. No need to install anything or use any other software.

  • smallet said: But the network config within an ovz container seem to be drastically different.. Anyone have any experience with this?

    Networking is controlled by the host node, you can kludge it with /etc/rc.local or you could ask the provider to vzctl set CTID --nameserver x.x.x.x --nameserver x.x.x.x --save

  • Kupol said: You can just set chattr +i

    Doesn't that only work on filesystems mounted with extended attributes? Debian and Ubuntu both discourage the usage of extended attributes and will not have them on by default. As it will never be fully supported under Debian I would be careful using them.

    smallet said: automatically reverts the nameservers

    You could use something like dnsmasq as a local caching DNS server. You could then override the system DNS servers at will.

    I personally use SaltStack to maintain a consistent state (RamNode changes the hostname automatically).

  • Yeah, I needed this for using dnsmasq (Though installing it doesn't make it the system default dns resolver). I am gonna go with /etc/rc.local solution then.

  • @Silvenga said:
    Doesn't that only work on filesystems mounted with extended attributes? Debian and Ubuntu both discourage the usage of extended attributes and will not have them on by default. As it will never be fully supported under Debian I would be careful using them.

    It appears that user_xattr is actually one of the default mount options on any ext4 file system in Debian:

    root@blah:/etc# lsattr
    ----i--------e-- ./resolv.conf
    root@blah:/etc# dumpe2fs /dev/mapper/vg-root | grep attr
    dumpe2fs 1.42.12 (29-Aug-2014)
    Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
    Default mount options:    user_xattr acl
    

    The reason it's set to immutable on my VPS'es is due to the DNSCrypt autoinstall script, which also contains the same command that @Kupol mentioned.

    Thanked by 1Silvenga
  • Tsuyo said: appears that user_xattr is actually one of the default mount options on any ext4 file system in Debian:

    When did they do that? I do remember them defaulting to disabled because of the inconsistent compatibility of the userland tools.

Sign In or Register to comment.