Skip to content
Tutorials Center
My learning

Topics

Lesson 82 of 95 ยท System Administration

Network Services: DHCP, DNS and FTP Servers

Learn what DHCP, DNS and FTP servers do on Linux, how dnsmasq, ISC dhcpd, BIND and vsftpd are configured, and why SFTP is usually the safer choice.

A network of machines needs a handful of background services before anyone can browse a web page. A laptop that joins your Wi-Fi gets an address from a DHCP server. When you type a name, a DNS server turns it into a number. When you want to hand someone a large file, an FTP or SFTP server may deliver it. These three services run on ordinary Linux machines, from a Raspberry Pi at home to the control plane of a data centre, and every system administrator has to know what they do and how they fail.

In this lesson you will learn the job of each service, see the common Linux programs that implement them (dnsmasq, ISC dhcpd, BIND, unbound, vsftpd, OpenSSH's SFTP), read their configuration formats, and use the shell to calculate and check the settings. The sandbox has no network and no root, so the servers themselves are not installed. Every example generates a realistic config file and examines it with Bash, awk and sed, which is also a good way to learn to audit real files.

Safety first. DHCP, DNS and FTP servers answer to a whole network. Never start a DHCP server on a network you do not own, because a second DHCP server hands out wrong addresses and breaks everybody. Test in a virtual network or a lab first.

What each service does

Service Question it answers Port Typical software
DHCP (Dynamic Host Configuration Protocol) "I just joined. What is my address, gateway and DNS server?" UDP 67 (server), 68 (client) dnsmasq, ISC dhcpd, Kea, systemd-networkd
DNS (Domain Name System) "What IP address belongs to www.example.com?" UDP and TCP 53 BIND (named), unbound, dnsmasq, CoreDNS, PowerDNS
FTP (File Transfer Protocol) "Let me upload and download files." TCP 21 plus data ports vsftpd, ProFTPD, Pure-FTPd
SFTP (SSH File Transfer Protocol) The same, over an encrypted SSH connection. TCP 22 OpenSSH (sshd)

The first two work together. A client uses DHCP to learn the address of a DNS server and then uses DNS for every name. Small networks often run both from a single program, dnsmasq, which is why it is the default in many home routers and in tools that create virtual networks.

DHCP: handing out addresses

A new client does not know anything, so it broadcasts. The conversation is called DORA:

  1. Discover: the client shouts "is there a DHCP server?" to the whole network.
  2. Offer: a server proposes an address from its pool.
  3. Request: the client asks for that address (and tells other servers it declined their offers).
  4. Acknowledge: the server confirms and records a lease, an address loaned for a time.

Along with the address, the server sends the subnet mask, the default gateway (router), the DNS servers, and often a search domain and NTP servers. At half the lease time (T1) the client asks to renew; at 7/8 (T2) it tries to rebind with any server; if the lease expires, it must stop using the address.

Configuring DHCP with dnsmasq

dnsmasq's DHCP settings are one line per option. This is a typical file for a small office network:

TEXT
interface=eth0
dhcp-range=192.168.10.50,192.168.10.150,255.255.255.0,12h
dhcp-option=option:router,192.168.10.1
dhcp-option=option:dns-server,192.168.10.2
dhcp-host=aa:bb:cc:00:00:01,printer,192.168.10.20

dhcp-range is the pool (start, end, mask, lease time). dhcp-option sets what clients are told. dhcp-host is a reservation: a device with that MAC address always receives that address, which is the right way to give a printer or a server a stable address without configuring the device itself. Keep reservations outside the dynamic range so they can never collide.

The ISC server (dhcpd) says the same in a block syntax:

TEXT
subnet 192.168.10.0 netmask 255.255.255.0 {
  range 192.168.10.50 192.168.10.150;
  option routers 192.168.10.1;
  option domain-name-servers 192.168.10.2;
  default-lease-time 43200;
  max-lease-time 86400;
}
host printer { hardware ethernet aa:bb:cc:00:00:01; fixed-address 192.168.10.20; }

ISC dhcpd is end-of-life upstream; its successor from the same project is Kea, which uses JSON configuration. The concepts are identical.

Bash Size a DHCP pool and compute lease timers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The two helper functions convert a dotted address to a 32-bit integer and back with shifts and masks, the technique from Arithmetic and Number Bases. With integers you can count the pool and test membership in one line.

On a real server you also check the leases (/var/lib/misc/dnsmasq.leases for dnsmasq, /var/lib/dhcp/dhcpd.leases for ISC), watch the log for DHCPDISCOVER/DHCPOFFER lines, and make sure the firewall allows UDP 67.

DNS: names to numbers

Resolving a name involves several roles. Your program asks the stub resolver in the C library, which reads /etc/nsswitch.conf to decide the order of sources (files dns means /etc/hosts first, then DNS) and sends the question to the recursive resolver named in /etc/resolv.conf. The recursive resolver (your ISP's, a public one, or your own unbound) walks the hierarchy: root servers, then .com, then the domain's own authoritative server, and caches the answer for the record's TTL.

  • Authoritative server: holds the real records for a zone and answers about only that zone. BIND, NSD, PowerDNS and Knot do this.
  • Recursive (caching) resolver: answers for clients by asking others. unbound and BIND can both do it. Run the two roles on separate servers in production.
  • Forwarder: a small cache that sends everything to an upstream resolver. This is dnsmasq's usual job.

Records and zone files

A zone file lists the records for a domain:

TEXT
$TTL 3600
@     IN SOA ns1.example.test. admin.example.test. ( 2026051501 7200 900 1209600 3600 )
      IN NS  ns1.example.test.
      IN MX  10 mail.example.test.
ns1   IN A     192.0.2.2
www   IN A     192.0.2.10
www   IN AAAA  2001:db8::10
mail  IN A     192.0.2.20
blog  IN CNAME www.example.test.
Record Meaning
A / AAAA Name to IPv4 / IPv6 address
CNAME An alias to another name (never combine it with other records on the same name)
MX Mail servers for the domain, with priority (lower wins)
NS Which servers are authoritative
SOA Start of authority: primary server, contact, serial number and refresh timers
PTR Reverse lookup, address to name
TXT Free text, used for SPF, DKIM and domain verification

Two details trip up beginners. The trailing dot in www.example.test. means "absolute name"; without it, the zone name is appended. And you must raise the serial whenever you edit the zone, or secondary servers will not notice the change; the habit is YYYYMMDDnn (date plus a counter).

Bash Audit a zone file with awk

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

On a real system you validate with the tools that ship with BIND: named-checkzone example.test example.test.zone for the file and named-checkconf for the main configuration. You test answers with dig www.example.test @192.0.2.2 (or host, nslookup) and look for the ANSWER section and the status: NOERROR line. A status of NXDOMAIN means the name does not exist; SERVFAIL means the server could not answer.

A minimal BIND zone declaration looks like this:

TEXT
zone "example.test" {
    type primary;
    file "/etc/bind/db.example.test";
    allow-transfer { 192.0.2.3; };   // only the secondary may copy the zone
};

For a LAN, dnsmasq is much simpler: it reads /etc/hosts, answers from it, forwards everything else, and caches. One extra line, address=/printer.lan/192.168.10.20, is a complete DNS record.

FTP: the old file transfer protocol

FTP dates from 1971 and has two quirks that make it hard to run safely.

  • Two connections. The control connection on port 21 carries commands. Each file or listing travels over a separate data connection. In active mode the server connects back to the client, which firewalls and NAT routers block. In passive mode the server opens a port and the client connects to it, so the server needs a configured range of passive ports open in its firewall.
  • No encryption. Classic FTP sends the user name, password and files in clear text. FTPS (FTP over TLS) adds encryption but keeps the complex port behavior.

vsftpd ("very secure FTP daemon") is the usual Linux server. A small configuration:

TEXT
listen=YES
anonymous_enable=NO
local_enable=YES
write_enable=YES
chroot_local_user=YES
ssl_enable=YES
pasv_min_port=40000
pasv_max_port=40100

chroot_local_user=YES jails each user inside their home directory, ssl_enable=YES turns on TLS and the pasv lines choose the passive data range. A server that allows anonymous writes is a classic way to end up hosting somebody else's files.

Bash A tiny vsftpd.conf linter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The linter pulls each setting out with sed -n "s/^key=//p" and compares it to the safe value. The same idea works for sshd_config, nginx.conf and many other files, and it is the heart of the compliance scanners described in System Hardening.

SFTP: the better default

SFTP is not FTP over SSH. It is a separate protocol that runs inside an SSH connection, so you get strong encryption, key-based login, a single TCP port (22) and no passive-port headaches. If sshd already runs, SFTP is already available: sftp user@host, with get, put, ls and cd commands. To give a partner a file-drop account without a shell, add a few lines to /etc/ssh/sshd_config:

TEXT
Match Group sftponly
    ChrootDirectory /srv/sftp/%u
    ForceCommand internal-sftp
    AllowTcpForwarding no

The chroot directory must be owned by root and not writable by the user, with a writable subdirectory inside it. For scripts, scp and rsync -e ssh use the same SSH channel; see SSH and Remote Work. Use plain FTP only when an old device forces you to, and then only inside a trusted network.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the correct order of the four DHCP messages?

    DORA: the client broadcasts Discover, a server answers with Offer, the client sends Request for the offered address, and the server confirms with Acknowledge.
  2. 2. Why should a DHCP reservation (dhcp-host) lie outside the dynamic pool?

    An address in the dynamic range could be leased to another client before the reserved device appears. Keeping them apart avoids conflicts.
  3. 3. You changed a DNS zone but secondary servers still serve old data. What did you most likely forget?

    Secondaries compare the SOA serial and only transfer the zone if the primary's serial is higher.
  4. 4. Which problem makes classic FTP awkward behind firewalls?

    Besides the control port 21, FTP opens data connections. Passive mode needs a configured range of server ports to be reachable.
  5. 5. What is the main security advantage of SFTP over plain FTP?

    SFTP is carried by SSH, so credentials and data are encrypted and key-based login is available. Plain FTP sends everything in clear text.

Summary

  • DHCP gives clients an address, gateway and DNS server through Discover, Offer, Request, Acknowledge; leases are renewed at half their time. Keep static reservations outside the dynamic range.
  • dnsmasq is a small DHCP and DNS forwarder; ISC dhcpd and Kea are full DHCP servers.
  • DNS maps names to records (A, AAAA, CNAME, MX, NS, SOA, PTR, TXT). Authoritative servers (BIND) hold zones; recursive resolvers (unbound) look names up and cache them. Raise the SOA serial after every edit and validate with named-checkzone and dig.
  • FTP uses a control port and separate data connections, and is unencrypted. vsftpd should have anonymous access off, chroot on and TLS enabled.
  • SFTP runs inside SSH (port 22), is encrypted, and is the better choice for file transfer; use ChrootDirectory and internal-sftp for restricted accounts.
  • Config files are plain text, so Bash can audit them: extract settings with sed/awk and compare to safe values.

Next: SELinux and AppArmor