How to set a static IP Address from the Command Line in GNU/Linux using ifconfig and route 5


Assuming you want to make the following changes to the network device eth0

  1. Change the IP to the static value 192.168.1.2
  2. Set the Subnet Mask to 255.255.255.0
  3. Set the Default Gateway for the device to be 192.168.1.1

you can perform these changes using the following two commands


sudo ifconfig eth0 192.168.1.2 netmask 255.255.255.0;
sudo route add default gw 192.168.1.1 eth0;

ifconfig

ifconfig is an application that allows you to configure a network interface.
It is used to configure the kernel-resident network interfaces. and it is used at boot time to set up interfaces as necessary. After that, it is usually only needed when debugging or when system tuning is needed.
If no arguments are given, ifconfig displays the status of the currently active interfaces. If a single interface argument is given, it displays the status of the given interface only; if a single -a argument is given, it displays the status of all interfaces, even those that are down. Otherwise, it configures an interface.

route

route is an application that allows you to show and manipulate the IP routing table. The primary use of route is to set up static routes to specific hosts or networks via an interface after it has been configured with the ifconfig program.
When the add or del options are used, route modifies the routing tables. Without these options, route displays the current contents of the routing tables.

This post is also available in: Greek


Leave a Reply to BobCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

5 thoughts on “How to set a static IP Address from the Command Line in GNU/Linux using ifconfig and route

  • Pingback: CyberWox's Cyber Sec Homelab on Virtual Box • Mr Ashley Ball

  • Siddiq

    Dear Sir,

    Im a beginner of Linux
    i tried to change static IP on ubuntu server 20.04 server in the method of below command, It has changing my IP and get pinging but after restarting NO IP showing on ifconfig. or ip a

    waiting for your valuable reply

    sudo ifconfig eth0 192.168.1.2 netmask 255.255.255.0;
    sudo route add default gw 192.168.1.1 eth0;

    Not saving on eth0

    • Bob

      Good morning,

      These instructions are for older versions of Ubuntu GNU/Linux.
      Ubuntu 20.04LTS does not use by default the command ifconfig.

      Before following these instructions be sure you understand the output of this command:
      ip link;
      It will give you a numbered list of the network interfaces on your server.
      Assuming that the second device (which could have a different name) is ens3 and you want to add a static IP to it, do the following:

      Edit the file /etc/netplan/01-netcfg.yaml using admin rights.
      e.g.
      sudo nano /etc/netplan/01-netcfg.yaml;

      Assuming you did not do any previous changes, it should look similar to this:
      network:

        version: 2
        renderer: networkd
        ethernets:
          ens3:
            dhcp4: yes
      

      You need to change the content for ens3 to look like the following:
      network:

        version: 2
        renderer: networkd
        ethernets:
          ens3:
            dhcp4: no
            addresses:
              - 192.168.1.2/24
            gateway4: 192.168.1.1
            nameservers:
                addresses: [1.1.1.1, 8.8.8.8]
      

      In case you used the nano editor press Ctrl+O and enter to save and then Ctrl+X and enter to exit.
      Finally, execute:
      sudo netplan apply;

  • Pingback: CyberWox's Cyber Sec Homelab on Virtual Box • Mr Ashley Ball

  • Pingback: CyberWox's Cyber Sec Homelab on Virtual Box • Mr Ash