Home / Alt manpages / networks(5)

  • networks(5)
  • File format
  • linux

Define and Check Network Names with /etc/networks

You will add a readable name for a legacy IPv4 network, query it through the system name-service interface, and check the result without restarting a service. The configuration lives in /etc/networks. It is a small file, but its format is strict and its scope is narrower than modern CIDR notation.

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 need to edit 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 that you administer.

Checkpoint: understand what this file 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 a network reachable. The local networks(5) manual identifies route and netstat as consumers, while library lookups such as getnetbyname(3) provide the programming interface behind those names.

The installed source is from Linux man-pages 6.7, packaged here as Debian manpages version 6.7-2. This guide follows that local behaviour. The file is plain ASCII, and fields are 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 it is an optional alias. A trailing .0 for the host part may be omitted. Empty lines are ignored, and # starts a comment that runs to the end of that line.

1. Inspect the existing file

Read the file before editing it. This avoids replacing a locally meaningful name and shows whether another administrator has already documented an exception.

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

On a minimal machine, output may contain only comments or a few entries. For example:

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

Do not assume the presence of a name means that an address is configured. 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, not an individual host address. The manual supports Class A, B and C networks. Partitioned networks such as network/26 and network/28 are not supported by this file, and the slash notation is not a valid replacement for the number field.

For a Class C example, use 192.0.2.0. The equivalent abbreviated form is 192.0.2, because the trailing .0 may be omitted. Keep the full form in shared configuration because it is easier to recognise and less likely to be mistaken for a host address.

Network names may contain printable characters except whitespace and #. In practice, keep them short, descriptive and stable. A name such as docs-net is easier to use in a command than an address, while a name tied to a temporary ticket or machine name will age badly.

3. Add one entry safely

Editing /etc/networks changes system configuration, so this step needs elevated privileges. Make a recoverable backup first, then append the line with an editor. The backup command reads the file and writes a separate copy; it does not alter 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

The second name, documentation, is an alias. It is optional, so a minimal entry could be docs-net 192.0.2.0. Keep comments on separate lines while testing, which makes accidental field errors easier to spot.

Save the file and inspect the changed lines:

tail -n 5 /etc/networks

Expected output includes the new entry. If the editor reports a permission error, leave the file unchanged, check that the path is exactly /etc/networks, and retry with sudoedit. Do not make the file world-writable to get around permissions.

Checkpoint: query the configured name

Use getent to ask the configured name-service switch for the entry. This is an ordinary, read-only command and does not require sudo:

getent networks docs-net
getent networks documentation
getent networks 192.0.2.0

Each successful lookup should print the canonical name followed by the number, for example:

docs-net             192.0.2.0

The alias lookup and the numeric lookup should resolve to the same entry. Spacing in the output is not significant. 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 inspect /etc/nsswitch.conf for a networks: line. A line containing files enables lookups from /etc/networks; a different configuration may consult other sources or omit this file. Do not assume a successful lookup proves that a route exists.

4. Check consumers without changing routes

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

ip route show

This output describes actual routes. It will not necessarily contain docs-net, because the name file does not create one. The legacy tools named by the manual, route and netstat, may be absent on a modern installation or may be provided by a separate package. Their absence does not make the file invalid.

Do not use a name entry as evidence that traffic can reach the network. Confirm reachability separately with the routing, interface and firewall tools appropriate to the machine. 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 described by this file. 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. There is no daemon to restart for the file itself. 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 address.
  • Editing while another change is in progress: if the file has unexpected content or a recent timestamp, stop and coordinate with the administrator responsible for it. Do not overwrite someone else's 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 that it produces no output. If the backup was created solely for this change and you need to restore the previous state, use the backup after reviewing it:

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

The restore changes the system file, so use it only when you have confirmed that the backup is the correct version. Remove the backup later through your normal configuration-management process rather than deleting an unknown file.

Done means

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