Home / Alt manpages / networks(5)

  • networks(5)
  • File format
  • linux

Define and Check Network Names with /etc/networks

/etc/networks maps a plain name to an IPv4 network number for old tools like route and netstat, and its format is stricter than it looks. You add a readable name for a legacy network, query it through the system name-service interface, and check the result without restarting a single service.

Allow about 10 minutes for one entry and its checks. You need a shell account, read access to the file, and sudo only if you are editing the system copy. The examples use the private documentation network 192.0.2.0, replace it with a real Class A, B or C network you administer.

Before You Start: What This File Actually Does

/etc/networks maps a symbolic network name to an IPv4 network number. It is not an interface configuration file: adding a line does not assign an address, create a route, change firewall rules or make anything reachable. The local networks(5) manual names route and netstat as consumers, while library calls such as getnetbyname(3) sit behind those names in code.

The installed source here is Linux man-pages 6.7, packaged as Debian manpages 6.7-2. The file is plain ASCII with fields separated by spaces or tabs:

name number alias1 alias2

The first field is the name, the second is the official network number in numbers-and-dots notation, and anything after that is an optional alias. A trailing .0 on the host part can be dropped. Empty lines are ignored, and # starts a comment to the end of the line.

1. Inspect the Existing File

Read the file before you touch it. It stops you overwriting a name someone else already relies on, and shows whether another administrator documented an exception already.

sed -n '1,120p' /etc/networks

On a minimal machine the output might be just comments, or a handful of entries, for example:

# symbolic names for networks, see networks(5) for more information
link-local 169.254.0.0

A name being present does not mean an address is configured anywhere. This file is a name database, not a source of interface state.

2. Choose a Supported Network Number

Use the network portion of an IPv4 address, never a single host address. The manual only supports Class A, B and C networks: partitioned networks such as network/26 or network/28 are not supported here, and slash notation is not a valid stand-in for the number field.

For a Class C example, use 192.0.2.0. The abbreviated form 192.0.2 works too, since the trailing .0 can be dropped, but keep the full form in shared configuration, it is easier to recognise and less likely to be mistaken for a host address.

Network names can use any printable character except whitespace and #. In practice keep them short, descriptive and stable: a name such as docs-net is easier to type than an address, while one tied to a ticket number or a machine name will age badly.

3. Add One Entry Safely

Editing /etc/networks changes system configuration, so this step needs elevated privileges. Take a backup first, the backup command only reads the file and writes a copy, it changes nothing about networking:

sudo cp -p /etc/networks /etc/networks.backup
sudoedit /etc/networks

Add this line on its own:

docs-net 192.0.2.0 documentation

documentation is an optional alias, so a minimal entry could just be docs-net 192.0.2.0. Keep comments on their own lines while you are testing, it makes a stray field error easier to spot.

Save the file and check what changed:

tail -n 5 /etc/networks

You want the new entry to show up. If the editor throws a permission error, leave the file alone, double-check the path is exactly /etc/networks, and retry with sudoedit. Never make the file world-writable just to get past a permission error.

4. Query the Configured Name

Ask the configured name-service switch for the entry with getent. This is read-only and needs no sudo:

getent networks docs-net
getent networks documentation
getent networks 192.0.2.0

A successful lookup prints the canonical name followed by the number, for example:

docs-net             192.0.2.0

The alias lookup and the numeric lookup should both resolve to the same entry, and spacing in the output means nothing. Check the exit status when scripting:

if getent networks docs-net >/dev/null; then
    echo 'network name is available'
else
    echo 'network name was not found' >&2
    exit 1
fi

If the command prints nothing, check spelling, field separation and the file path first. Then look at /etc/nsswitch.conf for a networks: line: one containing files enables lookups from /etc/networks, a different configuration may use other sources or skip this file entirely. A successful lookup is never proof a route exists.

5. Check Consumers Without Touching Routes

Some older networking tools accept network names. If they are installed, check their help or query output before pointing them at a production route table. For a read-only sanity check, list the kernel's actual route table and compare it with the name you added:

ip route show

This describes actual routes, and it will not necessarily contain docs-net, because the name file never created one. The legacy tools the manual names, route and netstat, may be missing on a modern install or shipped in a separate package; their absence does not make the file invalid.

A name entry is never evidence that traffic can reach the network. Confirm reachability separately with routing, interface and firewall tools. Keep that distinction clear in monitoring and documentation: name resolution answers "what is this network called", not "can I connect to it".

Common Traps

  • Using CIDR notation. 192.0.2.0/24 is not the number format this file expects, use a supported numbers-and-dots value instead.
  • Entering a host address. 192.0.2.17 identifies a host, not the Class C network, use 192.0.2.0.
  • Expecting a reload. This file is read by lookup functions and utilities directly, there is no daemon to restart. Just re-run getent after saving.
  • Confusing aliases with extra networks. Text after the number gives alternate names for the same network, it does not add another one.
  • Editing mid-change. If the file has unexpected content or a recent timestamp, stop and coordinate with whoever else is touching it rather than overwriting their update.

Undo and Removal

If the entry is wrong, edit the file with sudoedit and remove only the affected line and its aliases, then repeat the getent networks docs-net check and confirm it produces nothing. If you kept the backup solely for this change and need to restore it, review it first:

sudo diff -u /etc/networks.backup /etc/networks
sudo cp -p /etc/networks.backup /etc/networks

The restore changes the system file, so only use it once you have confirmed the backup is the right version. Clean up the backup afterwards through your normal configuration-management process rather than deleting a file you are not sure about.

Done means

  • Entry well-formed. It uses a symbolic name, a supported IPv4 network number, and optional aliases separated by spaces or tabs.
  • Lookup confirmed. getent networks docs-net returns the expected canonical name and number.
  • Scope understood. You have not mistaken the lookup for a route, interface, firewall rule or reachability test.
  • Reversible. Any edit can be undone by removing the line or restoring the reviewed backup.