Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am curious as to what a port number does. Are there specific port numbers for specific things?

I know there is port 80 and I heard of 8000 in passing. I don't know why they are called that and what it actually means.

Could I have a web page that is say example.com:10? I say 10 as a random number. Does it matter what you call a port? Where do these numbers come from? How would I go about setting up a development environment with a port number and why would I want to do that? During development I see all sorts of port numbers based on who they are. I always wondered where the numbers come from and how they configure the server to make it work.

share|improve this question

migrated from stackoverflow.com Aug 18 '10 at 1:08

This question came from our site for professional and enthusiast programmers.

add comment

5 Answers

Think of your network interface as a giant loading dock (like a UPS hub or something). Each one of those cargo doors has a number. By default, trucks carrying certain types of cargo use certain loading docks. By having multiple loading docks, the hub can handle more than one truck at a time and helps to keep the cargo types segregated.

Now that you have that visualization, port numbers on a computer are similar. You have TCP and UDP with TCP having a port range of 1-65535. That is where those numbers come from. Now, some of these ports are reserved for common traffic types. Example, 80 is for HTTP, 443 for HTTP-SSL, 21 for FTP, 25 for SMTP, 110 for POP3. There are a lot more of course, but I hope that this helps with the general idea. Now, you can make use of non-reserved ports for things you want to do, but I wouldn't use a low port like 10. If you want to host your site on an alternate port, choose 8080 or 8443 (alternate HTTP-SSL port), some number that is not reserved or is well-known alternate port for HTTP traffic.

Alternate ports are used because you can only have one service listening on a specific port at one time. Example, you can't have Apache and IIS both listen on port 80 for web request on the same machine. This is because those services are distinct and they will not be able to tell who was supposed to get what traffic. By using different ports, those services know that the traffic on that port was meant for them.

To set this up, your end service (web server, email, VPN, whatever) needs to know what port to listen on (which cargo bay to go stand beside and wait for stuff to come in or go out). Also, your firewall will need to have an exception (your security guards for your hub check to see if you are allowed to go to certain places) for that port as well.

List of common ports in use

share|improve this answer
    
"As for development, you might need to be able to specify the port number to not clash with other stuff, but functionality should not change." Can't you specify a number of virtual hosts to not clash with your stuff? That way you could say dev1.local is stuff in /www/html and dev2.local is stuff in /www/fun. Can't you even specify in the virtual hosts what port they use too? –  Keith Aug 14 '10 at 17:17
    
I ask because I see john is working in his envirnment using dev.local:8081 and bill is using dev.local:8082. I don't understand why they use different port numbers rather than just changing the nameserver (dev.local) to something different and use 80 so then you don't have to type a port number. –  Keith Aug 14 '10 at 17:17
    
I would say its a matter of preference I suppose. But typically, your right you could use host headers as well and keep everything on port 80 or 8081 or whatever. perhaps there are other requirements at play here regarding the host headers. –  Tommy Aug 14 '10 at 21:06
add comment

Port numbers don't 'do' anything specifically. They are one of the ways a connection is uniquely identified. Each TCP connection is defined by four things: the IP addresses of each machine, and a port number on each machine. Every IP packet that comes in contains these four pieces of information, allowing the network layer to determine what should be done with that packet. For example, a packet arrives with a destination port of 80, and the system knows Apache is listening on port 80, so it hands it off to the webserver (I am glossing over some details).

For a client to make a connection to your service, it needs to know your IP and which port your service is listening on. Because your IP and port uniquely define your end of any incoming connection, only one service may listen on a given port at any one time. If you had nothing else using port 10, you could definitely host your webserver on it, and all clients would need to specify the :10 at the end of your hostname/IP. There's nothing magic about port 80 or 443, except that they are the standard ports defined for HTTP and HTTPS (so clients will use those ports if the user doesn't specify one).

When a client (1.2.3.4) connects to a web server (11.12.13.14) on the default http port, it picks a currently unused port on its side, for example 5307, and establishes a connection. Now the connection is uniquely identified - 1.2.3.4:5307 <-> 11.12.13.14:80

If the same client makes a second connection at the same time, it needs another unused port number (5308 this time), because the other three pieces of the connection are constant - the service's listening port and both IP addresses are already set. This allows a single client to have multiple distinguishable connections to the same service. Another client could also connect using 5307, and it would be distinguishable from the first by the different IP address.

share|improve this answer
add comment

Ports exist to distinguish different services on the same IP. So the same machine might have email (SMTP: 25) and a web server (HTTP: 80).

Port numbers under 1000 are usually reserved for well-known stuff like those I just mentioned. Pick numbers above that up to around 65565 (the power of 2) for your own stuff.

Also, note there's no reason your web server must run on the well-known port. It's just where everyone will be looking for it.

These days, due to Port Address translation, your router actually translates your internal traffic to an unused port on your internet IP. That's why you see a bunch of different numbers. So, for instance, your web client would ask google.com:80 for pages. But the source would be from your IP, on an unused port. Like this:

google.com:80 <=talks to=> 88.88.88.88:4502 (Whatever your IP is, and port is whatever your NAT gateway decided to use)

The combination of IP/port is called a socket, by the way. As for development, you might need to be able to specify the port number to not clash with other stuff, but functionality should not change.

Lastly, if you're going through a firewall, you need to have the port open.

share|improve this answer
add comment

Tommy's loading dock analogy is an excellent one.

A service can listen at only one port (and yes, listen(2) is the name of the system call where a service binds to a port), and a port can have at most one service listening at it. If you want to listen on two ports (say running an HTTP server on ports 80 and 8080), then you have to start up two processes, one for each port.

Port numbers aren't reserved in any sense -- you're allowed to have a mail server listening on port 80 and a web server on port 25, but it'd be pretty confusing for everyone. But ports are associated with services in two ways:

  1. There are 'well-known numbers' for services (and the list is looked after by the Internet Assigned Numbers Authority). These aren't reservations as such, but they're effective 'keep-clear' markers. These numbers can also be found in the file /etc/services on unix machines.
  2. A protocol such as HTTP or SMTP will typically, in its RFC, declare a default port for the protocol – for HTTP this is port 80, and for SMTP port 25. All this means is that an HTTP client, for example, will try connecting to port 80 on a remote machine, unless it's told otherwise.

At least on Unix, port numbers below 1024 are privileged, in that only processes running as root can bind to them. This isn't a restriction imposed by TCP, but a unix-specific thing (it was originally intended as a very lightweight security measure).

The upshot of all this is that if, for example, you wanted to have several web servers on a machine (or of any other service), you can, as long as you start them up whilst telling them to listen on different ports. You might do that with a command-line option when you start the service, or an entry in a configuration file.

You might have a web service on port 80 (root would have to start that), plus some other HTTP-based services on port 8080, or 9000. The first would be addressable as http://example.org/, the other as http://example.org:8080/. Because port 80 is the documented default for HTTP, your web browser (or whatever client you were using) would automatically pick port 80 in the first case.

There's no significance to those numbers 8080 and 9000, by the way – they're merely the 'traditional' unprivileged port numbers to use for alternate web services; because they're unprivileged, any non-root user can start up a service listening there, as long as there's no service there already.

share|improve this answer
add comment

Lets say you have a server and it's IP address is XXX.XXX.XXX.XXX. You can connect to your server to do a variety of things. You always connect to it using the same IP address, but the port number you use is how the server determines what you are trying to do.

If you want to view the webpages on your server, you'd connect to the server using port 80: XXX.XXX.XXX.XXX:80. When the server accepts your connection on port 80 it understands that you want to view a webpage.

If you want to connect to the FTP program running on the server, you connect to port 21: XXX.XXX.XXX.XXX:21. When the server accepts your connection on port 21 it understands that you want to use the FTP.

Same situation goes for SSH/SFTP (port 22), HTTPS (port 443), DNS requests (port 53), etc... There are thousands of standardized port numbers for different applications/services.

The ports are basically just a way for a computer to determine what type of request is being made to it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.