What Is /etc/services in Linux/Unix?

The Linux Services Files Stores Well-Known Ports

/etc/services in linux

UNIX operating systems store what's called a services file at /etc/services. It stores information about numerous services that client applications might use on the computer. Within the file is the service name, port number and protocol it uses, and any applicable aliases. 

The port numbers are mapped to specific services much like the hosts file on Windows computers map a hostname to an IP address.

However, the UNIX operating system's services file does not include IP addresses but instead information like whether the service is TCP or UDP and what common names it might go by.

A simple text editor can be used to edit the /etc/services file, like Vim or Kate.

Example of a UNIX Services File

On UNIX, the key role of the configuration file /etc/services is so that programs can do a getportbyname() sockets call in their code in order to understand what port they should use. For example, a POP3 email daemon would do a getportbyname (POP3) in order to retrieve the number 110 that POP3 runs on. 

The idea is that if all POP3 daemons use getportbyname(), then no matter what POP3 daemon you run, you can always reconfigure its port number by editing /etc/services. 

Note: It's unreliable to use the services file in order to figure out what port numbers mean. If you want to find out what ports programs are using, you should instead use the program lsof to find out exactly which ports are bound to which processes.

If running lsof is not appropriate, then you should research the ports in a more generic reference.

All services files follow the same syntax of:

name port/protocol aliases comments

However, an alias and comment for each database entry is not necessary, as you can see in this example services file:

$ cat /etc/services
#
# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
#ident "@(#)services 1.34 08/11/19 SMI"
#
# Network services, Internet style
#
tcpmux 1/tcp
echo 7/tcp
echo 7/udp
discard 9/tcp sink null
discard 9/udp sink null
systat 11/tcp users
daytime 13/tcp
daytime 13/udp
netstat 15/tcp
chargen 19/tcp ttytst source
chargen 19/udp ttytst source
ftp-data 20/tcp
ftp 21/tcp
ssh 22/tcp # Secure Shell
telnet 23/tcp
smtp 25/tcp mail
time 37/tcp timserver
time 37/udp timserver
name 42/udp nameserver
whois 43/tcp nicname # usually to sri-nic
swat 901/tcp # Samba Web Adm.Tool
servicetag 6481/udp
servicetag 6481/tcp
snmpd 161/udp snmp # SMA snmp daemon
$