I'm trying to connect to internet using static IP, but when I enter sudo netplan apply command, the console returns the following error:
Invalid YAML at //etc/netplan/01-netcfg.yaml line 8 column 5: did not find expected key.How to fix this? The content of my 01-netcfg.yaml looks like:
network:
version: 2
renderer: networkd
ethernets:
enp0s3: dhcp4: no dhcp6: no addresses: [10.0.2.15/64] gateway4: 10.0.2.0 nameservers: addresses: [8.8.8.8,8.8.4.4] 1 4 Answers
So, with regards to the specific error you're getting, you've failed basic YAML syntax and indentation, which is one of the reasons netplan is complaining. YAML is extremely indentation-oriented for how it interprets commands, config arguments, etc. You need to have proper indentation for YAML configs to be parsed. Surprise, you aren't doing that at all, and breaking YAML syntax. That's why netplan is complaining because you aren't providing proper YAML.
But even more importantly than the YAML syntax which will make Netplan work, you have some critical failures with your network configuration, and you really need to fix these regardless. These failures are, specifically:
10.0.2.15/64is not a valid IPv4 CIDR range. The acceptable CIDR ranges are between/0(for all IPv4) and/32(for a single address) for IPv4. Most networks are/24for the CIDR range (with this IP range, that would encompass10.0.2.1 - 10.0.2.255as usable address space, though I would assume the.1is the Gateway and the.255is the Broadcast, but that might differ in your network so double check all the values!). The proper CIDR range is needed so the system knows what its netmask and reachable IP space is.10.0.2.0isn't a proper gateway address. The.0address is not a usable address in IP subnetting, as it's usually held by the network prefix alone. And since your netowrk config attempts to do a/24but horrendously fails, this gateway should probably be10.0.2.1- but again, double check these values with your network admin first.
So, fixing your indentation and fixing the network to assume it's a /24 based on the attempted IP and gateway you specified, your YAML should look like the below, with the proper indentations. And compared to your existing one, you really need to learn how indentation works.
network: version: 2 ethernets: enp0s3: dhcp4: no dhcp6: no addresses: [10.0.2.15/24] gateway4: 10.0.2.1 nameservers: addresses: [8.8.8.8, 8.8.4.4] 6 This one worked for me:
network: ethernets: enp2s0: addresses: - 192.168.0.2/24 dhcp4: false gateway4: 192.168.0.1 nameservers: addresses: - 192.168.0.1 - 8.8.8.8 search: - workgroup version: 2
then followed these commands:
sudo netplan generate
sudo netplan apply
Hope it does for you too.
1I tried to configure netplan, using guides like the one above and the following:
How to configure static IP address on Ubuntu 18.04 Bionic Beaver Linux
... but I found it easier to remove nplan completely and do something like this and the solution that works straight away, gets the job done with the least hassle is always going to be the one I use, rather than something new, which is more convoluted and annoying.
When I edited the .yaml file and executed: $ netplan try it failed.
In my case I could solve it leaving the space that had the original file: This is on Ubuntu Server 18.04.1 LST. Backup the original file before edit it. Good luck.