Ubuntu keeps losing resolv.conf settings? - linux

Every time I reboot my ubuntu server, it loses it's nameserver setting. I have to run:
echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf
each time I reboot for it to work properly again.
I tried editing resolv.conf directly and still doesn't work properly.
Any advice?

Edit below file for making effect on every time when you reboot
This is head section of resolve conf which added in resolve.conf
/etc/resolvconf/resolv.conf.d/head

In Ubuntu 18.04 thats working:
Sudo rm /etc/resolv.conf
sudo echo "nameserver xxx.xxx.xxx.xxx" >> /etc/systemd/resolve/resolv.conf
sudo ln -s /etc/systemd/resolve/resolv.conf /etc/resolv.conf
But I think a better option would be to edit the /etc/network/interfaces file and configure the system correctly, including the dns you need, for example:
auto lo
iface lo inet loopback
auto enp0s3
iface enp0s3 inet dhcp
dns-nameservers 8.8.8.8 8.8.4.4
In this example the first two lines define the local interface, do not touch it, the third line says that when the computer boots up the network card enp0s3 simultaneously (you can find out the name of your network card(s) with the command ifconfig -a), the fourth line tells the enp0s3 card to listen to a dhcp server and take the data needed for its network configuration from there, and the last line tells it which dns you want to use.
If you know your network configuration, it will always be the same or you don't have a dhcp server, the file would be something like this:
auto lo
iface lo inet loopback
auto enp0s3
iface enp0s3 inet static
address 10.10.5.67
netmask 255.255.255.0
gateway 10.10.5.1
broadcast 10.10.5.255
dns-nameservers 8.8.8.8 8.8.4.4
Another possibility is to configure the netplan file, the new default mode to manage the network from ubuntu 17.10 Artful. Here you can see more on the subject, particularly I don't like it but I am aware of its power for complex cases.

You can keep settings by editing a base file.
Install resolvconf:
sudo apt-get install resolvconf
Edit /etc/resolvconf/resolv.conf.d/base:
sudo vi /etc/resolvconf/resolv.conf.d/base
Add your nameserver:
nameserver 8.8.8.8
Start resolvconf:
sudo /etc/init.d/resolvconf start
Check that /etc/resolv.conf contains the following line:
nameserver 8.8.8.8
Then try to restart your server and check /etc/resolv.conf again.

That happens because of resolvconf. As the man page states, it allows other programs to change the DNS resolver configuration. Probably, there is a DHCP server on your network that is providing your host its IP address and the DSN servers.
You can change the DHCP configuration or force the first lines of resolv.confas #sahilKataria suggested. Using your command:
echo "nameserver 8.8.8.8" | sudo tee /etc/resolvconf/resolv.conf.d/head

Edit your netplan file. Example:
sudo vim /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
ethernets:
enp0s31f6:
dhcp4: true # Using DHCP
dhcp4-overrides: # Override DHCP
use-dns: false # Disable DHCP DNS
routes:
- to: x.x.x.x/24
via: x.x.x.x
metric: 100
- to: x.x.x.x/24
via: x.x.x.x
metric: 100
nameservers: # Name Server section
search: [somename.ddns.net] # Set your nameserver search
addresses: [x.x.x.x] # Set your DNS Server
version: 2
When you are done, run:
netplan apply
And make sure that you have this:
$ ls -ltra /etc/resolv.conf
/etc/resolv.conf -> /run/systemd/resolve/stub-resolv.conf
If bind is installed on the same machine:
$ cat /etc/hosts
127.0.1.1 somename.ddns.net somename # example
bind-ipaddress somename.ddns.net somename # example

Related

Remove DNS from device using `nmcli`

I want to remove the DNS currently associated to the device and add a new one using nmcli
So, if I do nmcli device show eth0 I can see
IP4.DNS[1]: 10.0.2.2
If I do sudo nmcli device modify eth0 ipv4.dns "8.8.8.8"
then I can see
IP4.DNS[1]: 10.0.2.2
IP4.DNS[2]: 8.8.8.8
but I want to remove the first one. How can I do it? If i try sudo nmcli device modify eth0 ipv4.dns "" then the second one (8.8.8.8) is removed but the first one is still there.
My final goal is to set ONLY 8.8.8.8 (for example...)
EDIT:
I am a bit confused between connection and device.
For example, let's say that I had 10.0.2,2 and I had 8.8.8.8 using one of these two commands:
nmcli connection modify netplan-eth0 ipv4.dns 8.8.8.8
nmcli device modify eth0 ipv4.dns 8.8.8.8
Because it seems there is a device and then there is a connection bound to it, so I can modify the dns using one of these two commands.
Now, I can see:
Using nmcli device show I have in the result
IP4.DNS[1]: 10.0.2.2
IP4.DNS[2]: 8.8.8.8
but using nmcli connection show netplan-eth0 I can see only
ipv4.dns: 8.8.8.8
So, my problem now is that I can easily modify the only dns in the connection, that is 8.8.8.8 using one of the followin command:
nmcli connection modify netplan-eth0 -ipv4.dns 8.8.8.8
nmcli device modify eth0 -ipv4.dns 8.8.8.8
BUT, I don't know how to remove the 10.0.2.2 that is showing only in the device but not in the connection.
BTW I did not set manually 10.0.2.2, I suppose it was taken through dhcp. And for some motivation this dns is bound only to the device but not to the connection.
With these details the problem should be more clear :)
For the general way to modify the dns, look at the answer of missconfigured.
About the problem of the dns get through netplan or dhcp, I could remove them using the following command:
nmcli device modify eth0 ipv4.ignore-auto-dns yes
After that, I was able to remove the 10.0.2.2 dns.
To the Answer,
The below command is able to remove the default DNS, but after restart NetworkManager, the default DNS comes back. How to make it permanent? Thanks.
nmcli device modify eth0 ipv4.ignore-auto-dns yes

Check NetworkManager is DHCP or Static with command line Ubuntu

How can I check the GUI network setting is set DHCP or Static with command line? for the active and connected interface in Ubuntu 18.04
I want one line command like grep give me static/dhcp or true/false
Can use ip command to check interface you're interested in.
E.g. to check the interface eth0:
if ip -6 addr show eth0 | grep -q dynamic; then
echo "Uses DHCP addressing"
else
echo "Uses static addressing"
fi
-6 option is for checking IPv6 interface. You can use -4 for IPv4.
nmcli -f ipv4.method con show
If the output is auto, then it is DHCP.
If the output is manual, then it is static.
or
cat /etc/network/interfaces
DHCP enabled
auto eth0
iface eth0 inet dhcp

Pass IP-address to cloud-init metadata

I am searching for a way to pass a ip-address to cloud-init metadata. So when my qcow boots, it does not have to wait for 120 - 180 seconds to boot.
Currently I've created a workaround by adding IP-address information to the userdata part of cloud-init. The con is, it does take some time because cloud-init userdata is only executed after booting the VM.
echo -e "
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
\taddress $address
\tnetmask $netmask
\tgateway $gateway
\tdns-nameservers $dnsnameservers
" > /etc/network/interfaces
ifdown eth0; ifup eth0
Currently to set the hostname in cloud-init metadata, I've already obtained this part:
cat > $config_dir/meta-data <<-EOF
instance-id: $uuid
hostname: $hostname
local-hostname: $hostname
EOF
But I need something more solid, more concrete because the cloud-init documentation is very vague
EDIT: I've seen that it is possible because Openstack can do it.
It is possible by using the following format:
network-interfaces: |
auto lo
iface lo inet loopback
iface eth0 inet static
address xxx.xxx.xxx.xxx (static ip)
netmask xxx.xxx.xxx.xxx
gateway xxx.xxx.xxx.xxx (gateway ip, usually ip address of router)
You basically write out the configuration you want your interfaces to have as an interfaces file would look as long as you have 'network-interfaces: |'
Hope that answers your question!

LXC Container as VPS

I've been looking into LXC containers and I was wondering as to whether or not it is possible to use an LXC container like an ordinary VPS?
What I mean is;
How do I assign an external IP address to an LXC container?
How do I ssh into an LXC container directly?
I'm quite new to LXC containers so please let me know if there are any other differences I should be aware of.
lxc-create -t download -n cn_name
lxc-start -n cn_name -d
lxc-attach -n cn_name
then in container cn_name install openssh server so you can use ssh then reboot it or restart ssh service.
To make any container "services" available to the world configure port forwarding from the host to the container.
For instance if you had a web server in a container, to forward port 80 from the host ip 192.168.1.1 to a container with ip 10.0.3.1 you can use the iptables rule below.
iptables -t nat -I PREROUTING -i eth0 -p TCP -d 191.168.1.1/32 --dport 80 -j DNAT --to-destination 10.0.3.1:80
now the web server on port 80 of the container will be available via port 80 of the host OS.
It sounds like what you want is to bridge the host NIC to the container. In that case, the first thing you need to do is create a bridge. Do this by first ensuring bridge-utils is installed on the system, then open /etc/networking/interfaces for editing and change this:
auto eth0
iface eth0 inet dhcp
to this:
auto br0
iface br0 inet dhcp
bridge-interfaces eth0
bridge-ports eth0
up ifconfig eth0 up
iface eth0 inet manual
If your NIC is not named eth0, you should replace eth0 with whatever your NIC is named (mine is named enp5s0). Once you've made the change, you can start the bridge by issuing the command
sudo ifup br0
Assuming all went well, you should maintain internet access and even your ssh session should stay online during the process. I recommend you have physical access to the host because messing up the above steps could block the host from internet access. You can verify your setup is correct by running ifconfig and checking that br0 has an assigned IP address while eth0 does not.
Once that's all set up, open up /etc/lxc/default.conf and change
lxc.network.link = lxcbr0
to
lxc.network.link = br0
And that's it. Any containers that you launch will automatically bridge to eth0 and will effectively exist on the same LAN as the host. At this point, you can install ssh if it's not already and ssh into the container using its newly assigned IP address.
"Converting eth0 to br0 and getting all your LXC or LXD onto your LAN"

Dynamic Switching of WiFi using a shell program in Raspberry Pi / Ubuntu / Debian

How can I check in a shell script, whether the system is connected to WiFi-1 and if not connected, connect to WiFi-2? I tried using wpa_supplicant and interfaces configurations, but not working as expected. I followed this tutorial
If there is any other way of doing this, please let me know.
/etc/network/interfaces file
auto eth0
iface eth0 inet6 manual
pre-up ip link set dev eth0 up || true
wpa-iface eth0
wpa-driver wired
wpa-conf /etc/wpa_supplicant/wpa_supplicant_wired.conf
pre-down /etc/netplug/netplug eth0 out >/dev/null 2>&1 || true
post-down ip link set dev eth0 down || true
auto wlan0
iface wlan0 inet manual
pre-up ip link set dev wlan0 up || true
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
wpa-driver wext
pre-down /etc/netplug/netplug wlan0 out >/dev/null 2>&1 || true
post-down ip link set dev wlan0 down || true
# open is specific (IPv6 only) so I just stop dhcp via netplug
iface open inet manual
up /etc/netplug/netplug wlan0 out >/dev/null 2>&1 || true
iface default inet manual
up /etc/netplug/netplug wlan0 in >/dev/null 2>&1 || true
down /etc/netplug/netplug wlan0 out >/dev/null 2>&1 || true
I think you may have already found the solution by now. Anyway as a workaround to do this you can install Network manager (supports all debian distributions including ubuntu/raspbian/etc)
sudo apt-get install network-manager
And then add required network configuration to /etc/NetworkManager/system-connections folder.
Sample configuration for a wifi network connection as follows:
[connection]
id=milanWiFi
uuid=56439211-1067-4334-b199-b73ceed32d83
type=802-11-wireless
[802-11-wireless]
ssid=milanWiFi
mode=infrastructure
mac-address=84:3A:4B:9B:CF:66
security=802-11-wireless-security
[802-11-wireless-security]
key-mgmt=wpa-psk
auth-alg=open
psk=milan123
[ipv4]
method=auto
[ipv6]
method=auto
Here, id is an unique name for your connection and you can obtain uuid by calling,
uuidgen command in the console.
For you to get the available wifi connections, you can use
sudo iw dev wlan0 scan
and there you can see the relevant details about the connections. What you have to do is to grab the details out of it and create the configuration file.

Resources