Linux set ip address command line

How to Change IP Address in Linux

Learn different ways of changing the IP address in Linux. Also learn how to make the changes [ermanent.

As a sysadmin, you’ll often deal with IP address configuration. Changing the IP address is one of the common IP configuration tasks.

While the IP addresses on most systems are configured automatically, you may need to change them manually in some cases.

In this article, you’ll see various ways to change the IP address in Linux. I’ll also focus on Ubuntu network configuration separately.

Before you proceed any further, you should run this command to check your current IP address:

This will also show the interface name which you’ll need while changing the IP address.

Using the ip Command to Set an IP Address

The ip command is available on most Linux distributions.

For setting an IP address, use it like this:

ip addr add [ip_address] dev [interface]

For example, add an IP address to the eth1 interface as:

sudo ip addr add 192.168.56.21/24 dev eth1

You now have two IP addresses: one from the old configuration and one from the new command:

Changing IP address in Linux

As you can see in the above screenshot, after deleting the old one, you are left with only one.

Making an IP Address Permanent

Surprisingly, the IP addresses set by the above method are not going to persist in system reboots.

sudo nano /etc/network/interfaces

If your file read like the below, your IP address will be set by a DHCP client:

auto eth0 iface eth0 inet dhcp

To change the IP address as per our choice, we can modify this file to manually set the IP address. To set the IP address statically, for e.g. as 192.168.56.20, change the above entry to look as:

auto enp0s3 iface enp0s3 inet static address 192.168.56.20 netmask 255.255.255.0 gateway 192.168.40.31

The entries above are self-explanatory for moderate Linux users. To apply the changes, you need to run the command:

$ sudo systemctl restart networking.service

Tip: On RedHat-based systems, the file ‘/etc/sysconfig/networking-scripts/ifcfg-*’ serves the purpose of configuring network interfaces.

Using Netplan for Network configuration (for Ubuntu)

Ubuntu provides a Netplan utility for network configuration.

Let us take an example of configuring an IP address on Ubuntu 20.04. I am using ‘NetworkManager’ as the renderer for the network configuration.

Читайте также:  Sync linux server time ntp server

The current IP address can be checked from the Netplan configuration file. This file is in YAML format and can be created if not present:

sudo nano /etc/netplan/config.yaml

If the IP address is dynamic, you will see the ‘dhcp4’ parameter set to true.

In case, you have a pre-configured static IP, the configuration will look much like this:

--- network: version: 2 renderer: networkd ethernets: eth1: addresses: - 192.168.56.66/24 nameservers: addresses: - 8.8.8.8 routes: - to: default via: 10.0.2.2

To change the IP address, replace the old IP address with a new one. Additionally, you can keep this IP and add one more to the above interface (en01).

Once you have configured the network, pre-test it before applying:

After confirming the changes, the new configuration can be applied as:

Check if the new settings have been applied:

Changing IP address in Ubuntu

One of the advantages of using Netplan is that the network configuration will persist between reboots. Your IP address change is permanent.

Using Graphical Interface to Change the System IP Address (for Desktop Users)

Managing IP addresses with a graphical interface is the easiest one and preferable for new Linux users. On a Ubuntu system, the Network settings contain all the required configurations.

Open ‘Settings’ from the GNOME dashboard and look for the ‘Network’ option:

Choose the setting icon from the active network on your system:

Network settings in Ubuntu

On the new window, select the IPv4 tab and then under the IPv4 method, choose the Manual option. Enter the details for the new IP address:

Change the IP address in Ubuntu

Now restart your connection and check the IP address:

As you can see from the above screenshot, the IP address has now changed.

Wrapping Up

The ip command is suitable for all Linux systems. Netplan is a new way of managing a network and is a very straightforward approach for Ubuntu systems.

I also added steps for the desktop users to help them change the IP address graphically.

Let me know if you have any questions or suggestions.

Источник

NetworkConfigurationCommandLine

You can configure a network interface from the command line. You configure your network client hosts with the command line by using commands to change your current settings or by editing a number of system files.

Find Network Interface Card

When setting up your network you will need to know the network interface cards on your computer. The interface name of cards for different vendors may be different, which is why this step is needed.

This will list the interface names for all NICs on your computer. It will probably include eth0 (hardwired NIC), lo (loopback interface for the localhost), and something for your wireless card (like wifi0, or wlan0).

Configuring Static IP Address For Your Network Card

Configure a Static IP address by editing /etc/network/interfaces. Replace eth0 with your network interface card (see Find Network Interface Card).

sudo nano /etc/network/interfaces
# The primary network interface auto eth0 iface eth0 inet static address 192.168.2.33 gateway 192.168.2.1 netmask 255.255.255.0 network 192.168.2.0 broadcast 192.168.2.255

For these settings to take effect you need to restart your networking services.

sudo /etc/init.d/networking restart

Setting up Second IP Address or Virtual IP Address

If you need to set up a second ip address you need to edit the /etc/network/interfaces.

sudo nano /etc/network/interfaces
auto eth0:1 iface eth0:1 inet static address 192.168.2.33 netmask 255.255.255.0 network x.x.x.x broadcast x.x.x.x gateway x.x.x.x

You need to enter all the details like address,netmask,network,broadcast and gateways values.

Читайте также:  Команда установки пакета linux

For these new settings to take effect you need to restart networking services using the following command

sudo /etc/init.d/networking restart

Setting Your Hostname

The hostname command allows you to directly query, or set, the hostname from the command line.

You can see your current hostname with

To set the hostname directly you can become root and run

When your system boots it will automatically read the hostname from the file /etc/hostname

Setting up DNS

You can add hostname and IP addresses to the file /etc/hosts for static lookups.

To cause your machine to consult with a particular server for name lookups you simply add their addresses to /etc/resolv.conf.

For example a machine which should perform lookups from the DNS server at IP address 192.168.3.2 would have a resolv.conf file looking like this

enter the following details

search myaddress.com nameserver 192.168.3.2

Configuring DHCP Address for Your Network Card

The DHCP address can be configured by editing the following file /etc/network/interfaces. Replace eth0 with your interface card (see Find Network Interface Card).

sudo nano /etc/network/interfaces
# The primary network interface – use DHCP to find our address auto eth0 iface eth0 inet dhcp

Howto Set MTU for a DHCP Connection

Although this is not documented in the manual for interfaces, MTU for a DHCP connected device can be set in the /etc/network/interfaces file. To do so you need to append the ‘pre-up’ command to the ‘iface’ section of the relevent interface.

iface eth0 inet dhcp pre-up /sbin/ip link set $IFACE mtu 1492

The above example sets the MTU for device eth0 to 1492, the usual MTU for a PPPoE ISP connection. This however is only needed if connections seem to hang otherwise (with the default of 1500).

NetworkConfigurationCommandLine (последним исправлял пользователь ip72-213-131-215 2009-08-14 04:14:29)

The material on this wiki is available under a free license, see Copyright / License for details
You can contribute to this wiki, see Wiki Guide for details

Источник

Setting a Static IP in Ubuntu – Linux IP Address Tutorial

Zaira Hira

Zaira Hira

Setting a Static IP in Ubuntu – Linux IP Address Tutorial

In most network configurations, the router DHCP server assigns the IP address dynamically by default. If you want to ensure that your system IP stays the same every time, you can force it to use a static IP.

That’s what we will learn in this article. We will explore two ways to set a static IP in Ubuntu.

Static IP addresses find their use in the following situations:

  • Configuring port forwarding.
  • Configuring your system as a server such as an FTP server, web server, or a media server.

Pre-requisites:

To follow this tutorial you will need the following:

  • Ubuntu installation, preferably with a GUI.
  • sudo rights as we will be modifying system configuration files.
Читайте также:  Linux цветной вывод консоль

How to Set a Static IP Using the Command Line

In this section, we will explore all the steps in detail needed to configure a static IP.

Step 1: Launch the terminal

You can launch the terminal using the shortcut Ctrl+ Shift+t .

Step 2: Note information about the current network

We will need our current network details such as the current assigned IP, subnet mask, and the network adapter name so that we can apply the necessary changes in the configurations.

Use the command below to find details of the available adapters and the respective IP information.

The output will look something like this:

image-14

For my network, the current adapter is eth0 . It could be different for your system

As my current adapter is eth0 , the below details are relevant.

6: eth0: mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:15:5d:df:c3:ad brd ff:ff:ff:ff:ff:ff inet 172.23.199.129/20 brd 172.23.207.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::215:5dff:fedf:c3ad/64 scope link valid_lft forever preferred_lft forever

It is worth noting that the current IP 172.23.199.129 is dynamically assigned. It has 20 bits reserved for the netmask. The broadcast address is 172.23.207.255 .

We can find the subnet mask details using the command below:

Select the output against your adapter and read it carefully.

image-15

Based on the class and subnet mask, the usable host IP range for my network is: 172.23.192.1 — 172.23.207.254 .

Subnetting is a vast topic. For more info on subnetting and your usable IP ranges, check out this article.

Step 3: Make configuration changes

Netplan is the default network management tool for the latest Ubuntu versions. Configuration files for Netplan are written using YAML and end with the extension .yaml .

Note: Be careful about spaces in the configuration file as they are part of the syntax. Without proper indentation, the file won’t be read properly.

ls into the /etc/netplan directory.

If you do not see any files, you can create one. The name could be anything, but by convention, it should start with a number like 01- and end with .yaml . The number sets the priority if you have more than one configuration file.

I’ll create a file named 01-network-manager-all.yaml .

Let’s add these lines to the file. We’ll build the file step by step.

image-17

How to Set a Static IP Using the GUI

It is very easy to set a static IP through the Ubuntu GUI/ Desktop. Here are the steps:

  • Search for settings .
  • Click on either Network or Wi-Fi tab, depending on the interface you would like to modify.
  • To open the interface settings, click on the gear icon next to the interface name.
  • Select “Manual” in the IPV4 tab and enter your static IP address, Netmask and Gateway.
  • Click on the Apply button.

image-16

  • Verify by using the command ip a

image-18

Conclusion

In this article, we covered two methods to set the static IP in Ubuntu. I hope you found the article useful.

What’s your favorite thing you learned from this tutorial? Let me know on Twitter!

You can read my other posts here.

Источник

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