What is the loopback device and how do I use it?

885

The loopback device is a special, virtual network interface that your computer uses to communicate with itself. It is used mainly for diagnostics and troubleshooting, and to connect to servers running on the local machine.

The Purpose of Loopback

When a network interface is disconnected--for example, when an Ethernet port is unplugged or Wi-Fi is turned off or not associated with an access point--no communication on that interface is possible, not even communication between your computer and itself. The loopback interface does not represent any actual hardware, but exists so applications running on your computer can always connect to servers on the same machine.

This is important for troubleshooting (it can be compared to looking in a mirror). The loopback device is sometimes explained as purely a diagnostic tool. But it is also helpful when a server offering a resource you need is running on your own machine.

For example, if you run a web server, you have all your web documents and could examine them file by file. You may be able to load the files in your browser too, though with server-side active content, it won't work the way it does when someone accesses it normally.

So if you want to experience the same site others do, the best course is usually to connect to your own server. The loopback interface facilitates that.

Addresses on Loopback

For IPv4, the loopback interface is assigned all the IPs in the 127.0.0.0/8 address block. That is, 127.0.0.1 through 127.255.255.254 all represent your computer. For most purposes, though, it is only necessary to use one IP address, and that is 127.0.0.1. This IP has the hostname of localhost mapped to it.

Thus, to log in as bob via SSH to the SSH server running on your own machine, you would use:

ssh bob@localhost

Like other network adapters, the loopback device shows up in the output of ifconfig. Its name is lo.

ek@Del:~$ ifconfig lo
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:50121 errors:0 dropped:0 overruns:0 frame:0
          TX packets:50121 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:4381349 (4.3 MB)  TX bytes:4381349 (4.3 MB)

An Example: CUPS

One common, production (i.e., not just diagnostic) use of localhost on Ubuntu is to perform advanced printer configuration. In a web browser, go to:

http://localhost:631

CUPS runs a web server on port 631, and this can be used to configure printing, regardless of what GUI you are running (or even if you are not running a GUI at all).

Screenshot showing CUPS in a web browser

If you try connecting to http://127.0.0.1:631, this will work too. However, if you try to connect to http://127.0.0.2, it will not. All the 127.*.*.* addresses identify your computer on the loopback interface, but a server program can decide to bind just to a specific IP address.

A Notable Difference from Windows

If you come from a Windows background, you might expect loopback to itself be a synonym of localhost (and thus to be able to ping loopback, connect to servers on loopback, and so forth). That behavior is peculiar to Windows.

Other Meanings of "Loopback"

The general concept of loopback is a mechanism through which a message or signal ends up (or loops) back to where it started.

So there are a few other ways loopback is use in Ubuntu that should not be confused with the loopback device in networking.

Loop Mounts

To mount a disk image in Ubuntu, you could run:

sudo mount -o loop image.iso /media/label

This is usually called a loop device (and not a loopback device), but the term loopback file interface is occasionally used.

This has nothing to do with the loopback device in networking.

Sound

Pulseaudio and other sound systems provide a mechanism to "connect" line-in to line-out, so that audio input is echoed back to your speakers/headphones. Pulseaudio's loopback module facilitates this.

Here, it is correct to use the term loopback, but like loop mounts, this also has nothing to do with the loopback device in networking. (And nothing to do with loop mounts, either.)

Further Reading

Share:
885

Related videos on Youtube

Naman
Author by

Naman

Updated on September 18, 2022

Comments

  • Naman
    Naman almost 2 years

    I am trying to solve INTEGER1 problem on spoj. My approach is very simple. It first calculates x(x^i=n) for all power of from 2 to 63. It then removes all the duplicates and then finally adds up the powers. But its is giving me wrong answer on spoj. I have tried it on Ideone and my machine for many use cases but it is giving me correct result.

    #include<stdio.h>
    #include<math.h>
    int main()
    {
        unsigned long long int a,b,result;
        unsigned long long int power[65],temp;
        int i,j;
    
        while(1)
        {
            scanf("%lld",&a);
            scanf("%lld",&b);
            if(a==0)
                break;
            result=0;
            power[0]=0;
            power[1]=b-a+1;
    
            a--;
            for(i=2;i<64;i++)
            {
                power[i]=floor(pow((long double)b,(long double)1/i));
                while(pow((power[i]-1),(long double)i)>=b)
                {
                    power[i]--;
                }
                while(pow((power[i]+1),(long double)i)<=b)
                {
                    power[i]++;
                }
    
                temp=floor(pow((long double)a,(long double)1/i));
                while(pow((temp-1),(long double)i)>=a)
                {
                    temp--;
                }
                while(pow((temp+1),(long double)i)<=a)
                {
                    temp++;
                }
                power[i]-=temp;
            }
            for(i=63;i>=1;i--)
            {
                for(j=i*2;j<64;j=j+i)
                {
                    power[i]-=power[j];
                }
            }
            for(i=1;i<64;i++)
            {
                result+=i*power[i];
            }
            printf("%lld\n",result);
        }
    
        return 0;
    }
    

    Please help me out.

    • Deduplicator
      Deduplicator almost 10 years
      Did you read any of the comments on the challenge? Please also read What Every Computer Scientist Should Know About Floating-Point Arithmetic
    • Naman
      Naman almost 10 years
      @Deduplicator I read but I thought I could use it once for calculating root. But thanks a lot for providing me this useful link. Does it mean that I have to calculate the nth root myself? If so can you please give me link for that also?
    • deviantfan
      deviantfan almost 10 years
    • Naman
      Naman almost 10 years
      @deviantfan I had a look at it. But I have edited my current code to correct the precision error still it's giving me Wrong Answer. Is it because it is overflowing with my new changes? Can you please take a look at the edited code in question?
    • Daniel Kamil Kozar
      Daniel Kamil Kozar almost 10 years
      @Naman : you are losing precision due to the usage of floating-point arithmetic. This challenge probably requires you to implement some kind of custom arbitrary-size arithmetic ("big number" arithmetic).
    • Naman
      Naman almost 10 years
      @DanielKamilKozar I agree. Can you suggest some method to find integer kth root of a number. I think Newton's method might take a lot time to converge for big numbers like(10^18)
    • Daniel Kamil Kozar
      Daniel Kamil Kozar almost 10 years
      @Naman : Sorry, I have no idea about numerical algorithms.
    • Naman
      Naman almost 10 years
      @DanielKamilKozar No problem and thanks a lot for the help.
    • Deduplicator
      Deduplicator almost 10 years
      @DanielKamilKozar: Please re-read. long long is plenty big enough for the task.
    • Penguino
      Penguino almost 10 years
      If you want to follow the path of finding the kth root the simplest way might be to a) calculate root r using floating point, b) truncate the root to an integer R, c) using 64 bit integers calculate R^k to confirm you aren't being fooled by floating point precision limits.
    • M.M
      M.M almost 10 years
      %llu should be used for scanf with unsigned long long.
    • Naman
      Naman almost 10 years
      @Penguino I am using the same method but for part c) 64bit integer calculation might overflow. For example, N=10^18 and k=1/59 then N^k=2 but to verify it I need to calculate (2-1)^59 and (2+1)^59 and for second part 64bit int will overflow. Is there any way to cope with it without using arrays?
    • Admin
      Admin about 7 years
      Isn't it two separate question in 1?
  • Naman
    Naman almost 10 years
    I think precision error will come with this method also. Should I use nearest integer or floor for the output of this function?
  • Naman
    Naman almost 10 years
    Finally I got AC. I used the same pow function to get first estimate of root and then corrected precision error with INT64 calculation. Earlier I was not checking for overflows. SO now my solution checks for overflows with first double calculation then does INT64 calculation. Thanks a lot for your help.
  • rock321987
    rock321987 almost 10 years
    @Naman:- congrats..if it helped you can accept it..i don't think it will overflow
  • rock321987
    rock321987 almost 10 years
    i had edited my answer..and i don't find a case of overflow..if you find any please point
  • JellicleCat
    JellicleCat about 9 years
    Why does 127.0.0.0/8 map to 127.0.0.1 through 127.255.255.254 instead of 127.0.0.0 through 127.255.255.255? (Sorry if this is perceived as a highjack.)
  • Gabriel Samfira
    Gabriel Samfira almost 9 years
    He mentioned only usable host addresses, and excluded network and broadcast addresses. You are correct in saying that it should be 127.0.0.0-127.255.255.255, but the first and last have other purposes.
  • Pacerier
    Pacerier over 8 years
    @Eliah, Must localhost be pointing to 127.0.0.1? Or could it also point to another loopback address e.g. 127.0.0.2?
  • MAChitgarha
    MAChitgarha almost 6 years
    @GabrielSamfira, so why when I try to connect to those two IP addresses using SSH (e.g. by running ssh 127.0.0.0), it will say "Network is unreachable" instead of "Connection refused"?
  • Gabriel Samfira
    Gabriel Samfira almost 6 years
    @MAChitgarha The first IP address in a subnet is the network identification address, and the last IP address is the broadcast address. Neither of these can be assigned to a host by default. The broadcast address is used to address every host in a network, and the network address is used to ID the network itself. Please see: en.wikipedia.org/wiki/IP_address
  • Nathan Long
    Nathan Long over 3 years
    I think another common use of loopback is for reverse proxies. Eg, you run an http (non-SSL) web service on a server on loopback and it isn't directly visible to the outside world. But you also run Nginx on the same machine. Nginx can reach the app via loopback, and Nginx listens for https connections from the outside world, forwarding the http request handling to the app.