Set dynamic ip linux

Ubuntu 20.04 Network Configuration

Whether you are a Linux administrator or regular user, you must know the basics of network configuration in your Linux system. it might be helpful when troubleshooting the issues with internal and external connectivity. The basic knowledge involves knowing the interface name, the current IP configuration, and the hostname. Also, you should know how to change the default configurations to the customized settings.

In this article, we will explain how to do basic network configuration in the Ubuntu system. We will use the command-line Terminal for executing the commands. To open the command line Terminal in Ubuntu, use the Ctrl+Alt+T keyboard shortcut.

In this article, we will be covering how to:

Note: We have run the commands and procedure mentioned in this article on the Ubuntu 20.04 system.

View Current IP Address

To view the current IP address of your machine, you can use either of the following commands:

Running either of the above commands will display the IP address information. Note down the name of the interface from the output of the above command.

Set Static IP Address

In the following procedure, we will see how to set up the static IP in a Ubuntu system.

Ubuntu 20.04 uses netplan as a default network manager. The configuration file for the netplan is stored in the /etc/netplan directory. You can find this configuration file listed in the /etc/netplan directory the following command:

The above command will return the name of the configuration file with the .yaml extension, which in my case was 01-network-manager-all.yaml.

Before making any changes to this file, make sure to create a backup copy of it. Use the cp command to do so:

Note: You might have a configuration file with the name other than the 01-network-manager-all.yaml. So make sure you use the right configuration file name in the commands.

You can edit the netplan configuration using any text editor. Here we are using the Nano text editor for this purpose.

Then add the following lines by replacing the interface name, IP address, gateway, and DNS information that fit your networking needs.

network :
version : 2
renderer : NetworkManager
ethernets :
ens33 :
dhcp4 : no
addresses :
— 192.168.72.140/24
gateway4 : 192.168.72.2
nameservers :
addresses : [ 8.8.8.8, 8.8.4.4 ]

Once done, save and close the file.

Now test the new configuration using the following command:

If it validates the configuration, you will receive the configuration accepted message; otherwise, it rolls back to the previous configuration.
Next, run the following command to apply the new configurations.

After this, confirm the IP address of your machine using the following command:

Читайте также:  Astra linux tftp сервер

It should reflect the changes you have made.

Set the Dynamic IP Address

In the following procedure, we will see how to configure the interface to receive a dynamic IP address from DHCP. Edit the netplan configuration file using any text editor. Here we are using the Nano text editor for this purpose.

Then add the following lines by replacing the interface name with your system’s network interface.

Once done, save and close the file.

Now test the new configuration using the following command:

If it validates the configuration, you will receive the configuration accepted message, otherwise, it rolls back to the previous configuration.

Next, run the following command to apply the new configurations.

After this, check the IP address of your machine using the following command:

View Current Hostname

To view the current hostname, you can use either of the following commands:

Change Hostname

There are two different ways to change the hostname of the system. To change the hostname of the system, you must be a root user or a standard user with sudo privileges.

Using the Hostnamectl Command

To change the hostname of the system to a new name, use the following command:

This command will change the hostname of the system to “desktop”.

After that, reboot the system and you will see the new hostname assigned to your system.

Using Hostname Command

Hostname command can also be used to change the hostname of the system. Use the following command to change the hostname of the system:

This command changes the hostname of the system temporarily. To permanently change the hostname, you will need to edit the /etc/hostname and /etc/hosts file.

Use the following command to edit the /etc/hostname file:

Replace the old hostname with the new name, then save and exit the file.

Next, use the following command to edit the /etc/hosts file:

Replace the old hostname with the new name, then save and exit the file.

After that, reboot the system and you will see the new hostname assigned to your system.

Once you are done with basic network configurations, use the ping command to verify the connectivity of your system with other systems on the network and the external network.

That is all the basics you need to know about network configuration in Ubuntu 20.04 system. Note that if you have multiple network interfaces, you will have to perform IP configurations for each interface. I hope you liked the article!

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.

Источник

Static and dynamic IP address configurations for DHCP

IP address configurations are critical, but what is the difference between static and dynamic addressing, and how does DHCP come into play?

Static and dynamic IP configurations for DHCP

IP address configuration is one of the most critical, if simple, settings on your network devices. Workstations, servers, routers, and other components must have properly assigned IP address settings to participate on the network.

Читайте также:  Apc powerchute for linux

This two-part article series covers static and dynamic IP address settings and the configuration of a DHCP server. This article (part one) defines network identities, contrasts static and dynamic configurations, and covers the commands needed to manage the settings. Part two covers the deployment of a DHCP server, DHCP scope configuration, and client-side management of dynamic IP addresses.

Three identities

Network nodes have three identities: Hostname, logical address, and physical address. These three addresses provide different types of connectivity and are used in various ways during network communication.

Great Linux resources

  • Hostname — descriptive, easy to remember names for the benefit of humans
  • IP address — logical address to uniquely identify a network node, primarily used by routers
  • MAC address — physical address encoded on the network interface card (NIC), used mainly by switches

Hostnames are configured when the OS is installed, and MAC addresses are hard-coded on NICs. Sysadmins typically configure IP address information on servers, workstations, portable systems, and network devices.

I’ll cover the two primary ways that IP address information is provided to the nodes: Static and dynamic configurations.

Static and dynamic configurations:

  • Static — manually configured by sysadmins
  • Dynamic — automatically leased by clients from a Dynamic Host Configuration Protocol (DHCP) server

The standard settings are IP addresses, subnet masks, default gateways, and nameservers.

Static configuration

NetworkManager primarily handles network configuration. NetworkManager can be used in a GUI, TUI, or CLI environment.

The nmcli process to set a static IP configuration is to create a connection profile and then set the values desired. Red Hat has documentation here.

Here is an example of creating a network connection named home-network with an IP address of 192.168.2.200/24, a default gateway of 192.168.2.1, and a name server of 8.8.8.8:

# nmcli connection add con-name home-network ifname enp7s0 type ethernet # nmcli connection modify home-network ipv4.gateway 192.168.2.1 # nmcli connection modify home-network ipv4.addresses 192.168.2.200/24 # nmcli connection modify home-network ipv4.dns 8.8.8.8 # nmcli connection up home-network

The GUI configuration can be accomplished by selecting the Manual button and then filling in the blanks with the appropriate information.

GUI Network Manager screen with static IP info

Recall that you can make no typographical errors when configuring IP addresses, and duplicate addresses will cause network connection problems.

Why static configurations?

Static IP addresses do not change unless the administrator actively reconfigures them. This is an important fact when it comes to servers because most client computers need to be able to find servers consistently.

For example, an NFS file server hosting department directories needs to keep the same IP address over time as configuration files such as a client computer’s /etc/fstab file may use the IP address for connectivity.

Other network nodes also may need an unchanging network identity. Appliance devices such as firewalls or proxies, print servers, name resolution servers, web servers, and virtually all other infrastructure devices need a consistent identity. Sysadmins will almost always configure these systems with static IP address information.

Читайте также:  Linux mint сменить тему

Tracking IPs

It is essential to track your statically assigned IP addresses. Depending on the size of your environment, this might be so simple as a text document or a spreadsheet, all the way up to specialized software that integrates with directory services and DHCP. I find it’s best to at least track IP address (and subnet mask), MAC address (not essential), hostname, role on the network (justifies why the devices have a static IP), and any additional notes.

spreadsheet tracking IP addresses, MAC addresses, hostnames, etc

Dynamic configurations

The devices that require a static IP configuration are a relatively small percentage of your network. Most network devices are end-user systems such as workstations, laptops, phones, tablets, and other transient devices. In addition, these systems do not usually host network services that need to be discoverable by other computers.

IP address configurations are unforgiving when it comes to duplicates and typos. In addition, static IP address settings are fairly time-consuming. Finally, IP address settings tend to be temporary, especially with the advent of portable devices like laptops, phones, and tablets. To save time and reduce the chances of a mistake, dynamic IP address allocation is preferable for these kinds of nodes.

Linux systems are configured as DHCP clients by using NetworkManager.

Here is an example of adding a network connection profile configured to lease an IP address from DHCP:

# nmcli connection add con-name home-network ifname enp7s0 type ethernet

By not specifying an address NetworkManager assumes the DHCP client role.

Here is a screenshot of a dynamic IP address configuration from the GUI:

Network Manager GUI and static IP configuration

Cloud services

The dhclient command

The dhclient command is also used to manage dynamic IP address configurations. However, in RHEL 8, network configurations, including DHCP, are handled by NetworkManager. Older RHEL versions rely on dhclient , as do some other distributions.

The ip route command displays lease information.

The second article in this series goes over the dhclient command in more detail.

Wrap up

IP address settings are crucial to network communications. Values such as the IP address, subnet mask, default gateway, and nameservers can be manually managed, but sysadmins must be very careful not to make any mistakes. Static settings don’t change unless the administrator reconfigures them, so they are essential for servers whose services are made available across the network.

Dynamic IP configurations are far more convenient for systems that don’t host network services, such as end-user devices. Furthermore, many of these devices enter and leave the network regularly, and it would be very time-consuming to set IP values each time manually. Instead, a DHCP server is used to host a pool of available addresses that client systems can lease.

Understanding the difference between static and dynamic IP addresses is straightforward but essential for administrators. As a general rule, servers and network devices utilize static, unchanging IPs, while client devices rely on dynamically allocated IP configurations.

Источник

Оцените статью
Adblock
detector