[Webinar] Streamline your web hosting managementRegister Today


assign port number

Posted on 1999-07-23
Medium Priority
Last Modified: 2010-04-20
if i have root privilege, can i assign a port number below 1000 for my server side socket program.  i am not sure how to do it, do i have to modify the /etc/services files or just execute my server side program as a root, so i am able to "bind" a socket with port number below 1000. any input will be greatly appreicated.

Question by:vzhang
  • 3
  • 2

Expert Comment

ID: 1635726
read the manual pages, buddy!

man services includes the first lines:
       services is a plain ASCII file providing a mapping between
       friendly textual names for internet  services,  and  their
       underlying assigned port numbers and protocol types. Every
       networking program should look into this file to  get  the
       port number (and protocol) for its service.  The C library
       routines   getservent(3),   getservbyname(3),   getservby­
       port(3), setservent(3), and endservent(3) support querying
       this file from programs.

so: you may bind to any port you'd like as long as you are root, but you should use services as a "configuration" file, where you take the right port from....

Author Comment

ID: 1635727
thanks gunny for your input!  what happen if i bind a port which is used by other services like "telnet". am i going to have a conflict if i start to run my program. if i am not the root, what is samllest port number am allowed to bind. i run into trouble couple times with binding the socket to the port when i executed the program as user.  it complains that  port is in service.  i just want to choose a port number that will alway allowed me to bind to that port.  

Accepted Solution

gunny051499 earned 90 total points
ID: 1635728
i'm not sure wheater this is defined or not. actualy if you use /etc/services a port conflict should never happen. also it is quite clear that you are going to run into trouble, when installing two services on the same port.

man services, 3th paragraph:
       Port numbers below 1024 (so-called 'low  numbered'  ports)
       can  only  be  bound  to by root (see bind(2), tcp(7), and
       udp(7).)  This is so that clients connecting to  low  num­
       bered ports can trust that the service running on the port
       is the standard implementation, and not  a  rogue  service
       run  by  a  user  of the machine.  Well-known port numbers
       specified by the IANA are normally located  in  this  root
       only space.

if you want to choose a "safe" port (this is an assumption, since as long as your port is not a standard, you could always run into confilct on some other machine) have a look at your existing /etc/services and choose something like port 13193, i'm quite sure not much people are using such things :)

also i realy recommand you to READ the services manualpage!!

Author Comment

ID: 1635729
thanks again gunny, do you know why when i termined my server socket program and try to run it again, it give me the message "bind: Address already in use ", that doesn't make any sense, so each time i terminated my program i have to change the port number and recompile the programs again in order to execute it. any input will be greatly appreciated.  for now, i think i will just change the /etc/services file and choose a port below 1024 and run as root.

Expert Comment

ID: 1635730
'you sure you killed the socket?

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you step-by-step instructions to build your own NTP CentOS server.  The network diagram shows the best practice to setup the NTP server farm for redundancy.  This article also serves as your NTP server documentation.
Virtualization software lets you run different versions of Windows, Ubuntu Linux and other versions of Linux all at the same time, rather than running each one directly from your computer's hard drive.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month9 days, 8 hours left to enroll

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question