What is the use of /etc/services file
– The /etc/services file is used by applications to translate human-readable service names into port numbers when connecting to a machine across a network. The file will typically include the service name, port/protocol, any aliases, and comments.
– A protocol being listed in the /etc/services file does not mean that the port is blocked or unblocked by a firewall or a security mechanism.
– The default entries should not be modified in /etc/services file but new entries can be added as per the requirement.
– The file is also a good resource to see which services use which port numbers and the protocols that are specified to work.
Here a sample /etc/services file from CentOS 7 system.
To get more information on “/etc/service”, check the man page of “services”:
# man services
/etc/services syntax and guidelines
1) You can use the same service name more than once if it refers to a different protocol.
MyService1 48643/tcp # MyService1 MyService1 48643/udp # MyService1
However if you attempt to use the same service name with different port numbers, such as:
MyService1 48643/tcp # MyService1 MyService1 48700/tcp # MyService1
getservbyname(“MyService1”, “tcp”) would only return the first one, and thus the second would serve no purpose.
2) You should not list the same port/proto combo multiple times, because only the first would be returned by getservbyport. For example:
MyService1 48643/tcp # MyService1 MyService2 48643/tcp # MyService2
getservbyport(htons(48643), “tcp”) only returns MyService1.
3) You should not use duplicate aliases, since you can search by alias with getservbyname. For example:
MyService1 48643/tcp MyService # MyService1 MyService2 48643/tcp MyService # MyService2
getservbyname(“MyService1”, “tcp”) and getservbyname(“MyService2”, “tcp”) will both return a struct servent* with “MyService” in the s_aliases element, but getservbyname(“MyService”, “tcp”) will only return the first entry.