What is localhost? What do the numbers mean? Is there a pattern to how they are used?

116 views
0

Hello,

I’m kind of confused about this.

I’ve noticed some programming software requires you to set up a server to run projects. MySQL runs on localhost:8080, PHP runs on localhost:4000. Are these numbers predetermined and specific to certain applications, or are they decided upon by the user? Can you know which software will use which number based on some kind of pattern?

In: 1

Local host is a host name used to refer to the loopback IP address of the machine its running on also you can use ip 127.0.0.1.

The numbers after the colon are the port numbers and for known services are normally a standard but can be custom if you want. https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers

Edit tried to keep it simple added more info

You know how any machine on a TCP/IP network is supposed to have an IP address that is unique. So when you use a normal IP address, the machine that has that address can be reached from anywhere on the network.

All devices on a TCP/IP network are supposed to have the address we just talked about; as well as an internal address, by convention 127.0.0.1 . This address is guaranteed to not go anywhere outside the machine itself.

So no matter which device you are using, you can always refer to it by the name localhost, and by the IP address 127.0.0.1 .

When you install a server that is supposed to be reached by other machines, you address it by using its regular IP.

But when you set up an internal server of any kind, usually to do development work, you point the client to localhost.

This is all by convention. There is nothing preventing you from using any name and any address you wish. All you have to do is configure your OS and your server software to use that address, and then point any local programs using it to the address you just defined.

It is wise to use the convention, however, because if you select an address that is allocate elsewhere, your machine will never be able to reach the actual “owner” of that address, and will always think it refers to itself.

Likewise ports. There are conventions that you should follow when using them, but there is no mechanism stopping you from using anything else.

EDIT: “any name and address you wish” are an exaggeration. There are limits to what those things can technically be- for instance IP address (in the traditional v4 variant, still mostly in use) are always a sequence of 4 bytes separated by a dot. Because each number is a byte, it can only be something between 0 and 255; and there are other issues as well like network addresses and gateway addresses, but for an ELI5 I think that this is enough.