Score:0

How do I set a static IP in Ubuntu 20.04.3 LTS?

pk flag

I'm trying to set up a lab computer as a ssh server following this guide. One of the steps is setting up a static IP address. So, I was glad to find this answer. Following it, I created /etc/netplan/50-cloud-init.yaml file, pasted

network:
    ethernets:
        enp0s3:
            addresses: [desired_ip_address/24]
            gateway4: my_router_ip_address
            dhcp4: no
            nameservers:
              addresses: [1.1.1.1,8.8.8.8]
            optional: true
    version: 2

and run

sudo ip addr flush my_ethernet_num
sudo systemctl restart networking.service

where my_ethernet_num == enp9s0. Buuuut, after this ip a showed no ip address for the Ethernet:

svyatoslav@svyatoslav-desktop ~> ip a                                    (base) 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp9s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether a8:a1:59:4b:e8:bb brd ff:ff:ff:ff:ff:ff
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:f3:38:2f:33 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever

and sudo lshw -C network yielded *-network DISABLED. Then I tried to use that guide (section 'Static IP Address Assignment'). I created /etc/netplan/99_config.yaml, pasted the necessary code, run sudo netplan apply. Nothing changed except that I STOPPED SEEING wired connections in network settings... I tried the answer from here, and the 'Wired' section returned. But I can't change anything (for example, I can't pick 'Manual' option) screenshot.

So, the questions are: how can I return the ability of changing it (in order to follow one more guide), or is there any other solution that will succeed in setting static ip address?...

--EDIT from 12/05/2021--

Here are all of my .yaml files showed by ls -al /etc/netplan:

01-network-manager-all.yaml:

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager

50-cloud-init.yaml:

network:
  ethernets:
    enp0s3:
      adresses: [192.168.0.116/24]
      gateway: 93.175.20.231
      dhcp4: no
      nameservers:
        addresses: [1.1.1.1,8.8.8.8]
      optional: true
  version: 2

99_config.yaml:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      addresses:
        - 192.168.0.116/24
      gateway4: 192.168.0.1
      nameservers:
        search: [mydomain, otherdomain]
        addresses: [192.168.0.1, 8.8.8.8]
raj avatar
cn flag
raj
Probably you should delete/revert all changes that you have made, to get back to the original configuration. Then just use "Manual" option from the GUI. No need to mess with configuration files manually.
TopCoder2000 avatar
pk flag
@raj, thank you for your comment! If I only knew how to revert all the changes... By the way, after rebooting, I suddenly found out that I can make changes to the settings! So, I'll try the the GUI option and write about results :)
heynnema avatar
ru flag
Why does your .yaml use enp0s3, when your interface is enp9s0? Are you using a Server or Desktop installation?
TopCoder2000 avatar
pk flag
@heynnema, because it was in the answer for ['How do I set a static IP in Ubuntu?'](https://askubuntu.com/questions/766131/how-do-i-set-a-static-ip-in-ubuntu/767693#767693). As we can see, enp0s25 is used in the question, but then enp0s3 is used in the answer, so I thought that it always must be enp0s25. > Are you using a Server or Desktop installation? I would like to use a Desktop installation, but I don't know concretely which one I was using... Is there any difference? I tried to follow Desktop installation but I could get confused.
heynnema avatar
ru flag
@TopCoder2000 Your network can't possibly be working with enp0s3 in the .yaml file. It needs to be enp9s0. But if you're using a Desktop installation, then your .yaml is all wrong anyway. A Desktop installation has a GUI, a Server installation is CLI only. Also, your self-accepted answer doesn't really make a lot of sense.
heynnema avatar
ru flag
@TopCoder2000 How many .yaml files do you have in /etc/netplan? Show me `ls -al /etc/netplan`.
TopCoder2000 avatar
pk flag
Thank you for you willingness to help, @heynnema! > Also, your self-accepted answer doesn't really make a lot of sense Why? When should I accept the answer then? > How many .yaml files do you have in /etc/netplan? Here is the output `total 28 drwxr-xr-x 2 root root 4096 ноя 29 11:45 ./ drwxr-xr-x 153 root root 12288 дек 5 14:15 ../ -rw-r--r-- 1 root root 104 авг 5 2019 01-network-manager-all.yaml -rw-r--r-- 1 root root 204 ноя 29 11:45 50-cloud-init.yaml -rw-r--r-- 1 root root 239 ноя 29 11:07 99_config.yaml` P. S. how do I make a new line in comments?..
heynnema avatar
ru flag
@TopCoder2000 Ah! You have 3 .yaml files... and I'll bet they're conflicting. Edit your question and show me ALL .yaml files, WITHOUT redactions, and then I can come up with an answer for you. You can't make a new line in comments... that's why data like that belongs as an edit to your question. Your answer, below, doesn't answer your question and solve your problem... that's why I said it really makes no sense.
TopCoder2000 avatar
pk flag
@heynnema, has edited :) Where is it easily explained what netplan is and what .yaml files do? I want to have a superficial understanding.
heynnema avatar
ru flag
@TopCoder2000 Status please...
Score:0
ru flag

Pick ONE of the following two configurations...


Server installation with static IP...

Delete /etc/netplan/01-network-manager-all.yaml

Delete /etc/netplan/50-cloud-init.yaml

Edit /etc/netplan/99_config.yaml to look EXACTLY like this...

Note: regarding 192.168.0.116, make sure this address is outside of the DHCP range set in your router, and is not already used elsewhere.

Note: For DNS nameservers, settle on servers from one source... Google, Cloudflare, OpenDNS, etc. (3 max).

network:
  version: 2
  renderer: networkd
  ethernets:
    enp9s0:
      addresses: [192.168.0.116/24]
      gateway4: 192.168.0.1
      nameservers:
        addresses: [192.168.0.1, 8.8.8.8]
      optional: true

sudo netplan generate

sudo netplan apply

reboot


Desktop installation with static IP...

Delete /etc/netplan/50-cloud-init.yaml

Delete /etc/netplan/99_config.yaml

Edit /etc/netplan/01-network-manager-all.yaml to look EXACTLY like this...

Note: Use the NetworkManager GUI to setup your static IP for "Wired Connection".

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager

sudo netplan generate

sudo netplan apply

reboot


Note: See https://netplan.io/examples/ for examples and design info about netplan.

TopCoder2000 avatar
pk flag
Thank you for your answer and netplan examples! Now `ip a` shows correct output. But how do I setup a static IP with NetworkManager GUI?
heynnema avatar
ru flag
@TopCoder2000 Firstly, you can't use both parts of my answer at the same time. It's part 1 or part 2. If you're using part 2, then my answer says *"Use the NetworkManager GUI to setup your static IP for "Wired Connection""*. See the IPv4 tab. Click on the manual button. Fill in the address, mask, gateway, and DNS servers, and turn off DNS Auto.
TopCoder2000 avatar
pk flag
yes, I remember! I'll accept it as soon as I don't have any questions :) Yes, I'm using the second option. But you also wrote yesterday that 'Static IPs should be set to be outside of the DHCP range set in the router'. But how do I know that set? For example, the second answer from [here](https://www.quora.com/How-can-I-find-the-number-of-available-IP-addresses-in-my-DHCP) says that we have to check DHCP server configuration...
heynnema avatar
ru flag
@TopCoder2000 You must log into the admin page of your router and find the DHCP server settings. For your computer's static IP, you'll need to pick an address outside of the DHCP server's range of addresses.
TopCoder2000 avatar
pk flag
Ah, good! Thanks!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.