If I made a mistake when I am trying to connect a Linux computer to the Internet for installing a package or fetching a file, I always search for the topic in the engines. One search for setting the IP, one for setting the route, and other things…

You can ask why I don’t use a network manager for this problem. You are totally right, there are lots of network managers out there. When I sit down in front of a computer of which distribution is totally alien to me, I would like to rely on simple commands like ip to establish the internet connection.

Another question you may ask is why I am not using ifconfig. I would like to point that it is deprecated, in newer versions of the distributions you may not find it or you need to install another package (net-tools, I guess) for it.

The last question, in fact, it is the first question you may ask is why I am not installing all the packages in the first place and configuring a stable internet connection and then use it. Well, Least Privilege Principle holding me back not to install all packages and not to connect a machine to the Internet if it is not necessary all the time.

In fact, a mirror/cache proxy should be created for the packages which also checked on my side for security. Unfortunately, I don’t have enough budget (money, time, equipment, etc.), so for this kind of issue and I accept the risk.

I think enough talking. Let’s dive into the thing.

Preconditions:

  • No systemd services or applications handles the network.
  • No DHCP client is running.
  • No network manager is running.
  • No application is writing to /etc/resolv.conf.




Table of Contents:


Introduction

Before diving into how to use ip command, I would like to give some introduction regarding the command:

  1. The strings coming after ip command, can be objects or commands are completed. For example:

    $ ip address
    $ ip addr
    $ ip a
    

    or

    $ ip route
    $ ip ro
    $ ip r
    

    are all same command.

  2. The general format of ip command is:

     ip [ OPTIONS ] OBJECT { COMMAND | help }
    

    The following sentence is copied from man page because I cannot describe better:

    If no command is given, some default command is assumed. Usually it is list or, if the objects of this class cannot be listed, help.

    For example:

     $ ip address show
     $ ip address
    

    are all same.

  3. -c parameter can be used to make the output colorful. Nevertheless, I decided not to use this parameter in the post, because I don’t want to confuse the people and I want to keep the steps as plain as possible.

     $ ip -c a
    

    Color

  4. I use 192.168.137.1 as the gateway and 192.168.137.0 as the network in my case. As you know, the gateway IP is NAT device IP address by default if you share the connection in Windows. I assume that the network we are in is behind a NAT / Router device and it can work as a DNS for us.

Setting the IP

  1. First of all, you need to determine the interface you will use to connect the internet. You can use ip address or ip link command to list all the interfaces available on the computer. I have used ip address command to list the interfaces:

     ip a
    

    NIC No IP

    My interface is enp0s3. As you see, no IP is assigned to the interface and its state is down.

  2. We can set an IP with the following command:

     ip address add 192.168.137.2/24 dev enp0s3
    

    NIC Set IP

  3. At this stage, your interface may or may not be down. Let’s check my interface if it is down:

     ip a
    

    NIC Down

  4. If the interface is down, there should be no rule for the network, which the interface is configured to be in:

     ip route
    

    No Route

  5. Let’s make the interface up:

     ip link set dev enp0s3 up
    

    NIC Down

  6. After making the interface up, addresses should be like below:

     ip a
    

    NIC UP IP

  7. And the routes:

     ip r
    

    NIC UP Route

Setting the Default Gateway

  1. You need to be sure that you can access the gateway before setting an IP for the gateway. For this, we are using ping command:

     $ ping 192.168.137.1
    

    Ping Gateway

  2. After being sure if the gateway is accessible, we can set the default gateway with the following command:

     ip route add default via 192.168.137.1
    


    Set Gateway

  3. Configuring the gateway makes you access the Internet with only IP addresses:

     ping 8.8.8.8
    

    Ping Google DNS

Setting the DNS

  1. If your gateway is NAT, as you know NAT device also acts like a DNS server/proxy and the DNS configuration on the NAT device is used. So it is totally safe using your gateway as a DNS if it is a NAT device or you can set any DNS IP directly:

     echo “nameserver 192.168.137.1” > /etc/resolv.conf
    

    or you can edit the resolv.conf by yourself with your favorite editor. I am using nano for this task:

     nano /etc/resolv.conf
    

    nano /etc/resolv.conf

  2. I write the DNS IP as below:


    /etc/resolv.conf

  3. At this point, you should be able to connect the Internet and can query the IP of the domain:

     ping google.com
    

    Ping Google

  4. If the ICMP is filtered by the firewall or disabled, you may not run the ping command successfully. The alternative is to use HTTP protocol:

     curl google.com
    

    Curl Google

Setting the Proxy

In fact, I am planning to write a separate post regarding setting the proxy because the applications honor a different kinds of settings and for each application one may need to configure different files or settings. For now, I am listing the basic proxy variables you should set for the proxy:

export http_proxy=http://<proxy ip>:<proxy port>/
export https_proxy=$http_proxy
export ftp_proxy=$http_proxy
export rsync_proxy=$http_proxy
export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"

If you are in a corporate network, the no_proxy variable may be different, and most probably longer. I know it is not necessary to point that you should use your proxy ip and port in place of <proxy ip> <proxy port>, but still, I want to do that 😄.

References