linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Lessons
  • How it works
  • Simulator
  • Knowledge base
  • Interview prep
Index
Categories
All entries
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
home/linux/kb/Networking: L4 and above/udp-basics

kb/network-l4 ── Networking: L4 and above ── beginner

UDP: User Datagram Protocol

UDP delivers datagrams without establishing a connection, without retransmits, and without ordering guarantees. Header is 8 bytes. Use it for DNS, DHCP, QUIC, VoIP, and any case where latency matters more than reliability.

view as markdownaka: udp, udp-protocol, datagram

What UDP does

port plus a checksum: that is the entirety of UDP. Unlike [[tcp-handshake|TCP]], UDP opens no connection, assigns no sequence numbers, and acknowledges no delivery. It takes your data, wraps it in a datagram with src/dst ports, and sends it into the [[ipv4-addressing|IP]] network.

UDP header: 8 bytes

 0      7 8     15 16    23 24    31
+--------+--------+--------+--------+
|     src port    |     dst port    |
+--------+--------+--------+--------+
|     length      |    checksum     |
+--------+--------+--------+--------+
|              data ...              |
  • src/dst port - 16 bits, 0-65535 (same as TCP)
  • length - length of the UDP header plus payload in bytes
  • checksum - 16-bit sum over the header and payload (optional in IPv4, required in IPv6)

Compare with TCP, where the header is at least 20 bytes plus options: UDP is 2.5x smaller.

When to choose UDP

ScenarioWhy UDP
DNS query (one query, one reply)TCP handshake costs 1.5xRTT overhead, which is unnecessary here
DHCP (broadcast, new client)TCP does not work over broadcast
VoIP / video callslosing 1-2 packets is better than a retransmit delay
NTP (time sync)retransmits break accuracy
QUIC (HTTP/3)delivery control lives in user space, not the kernel
Streaming (RTP)dropping a frame is simpler than waiting for retransmit
Games (real-time)current state matters more than a stale retransmit

When you cannot use UDP

  • Large data where order matters (HTTP/1-2, SSH, databases): use TCP instead.
  • Application-level reliability: either you write your own ack/retry on top of UDP (as QUIC does), or UDP is the wrong choice.

Packet size and fragmentation

The theoretical maximum UDP payload is 65,507 bytes (65535 minus the 20-byte IP header minus the 8-byte UDP header). But Ethernet MTU is 1500, and a UDP datagram larger than the MTU triggers IP-level fragmentation. That is bad:

  • If even one fragment is lost, the entire datagram is dropped.
  • Many firewalls drop fragments by default.
  • MTU along the path may be less than 1500 (tunnels, VPN).

For this reason, UDP applications keep the payload under 1472 bytes (1500 minus IP minus UDP). DNS is historically limited to 512 bytes over UDP; for larger replies it switches to TCP, or EDNS0 extends the limit to 4096.

UDP socket on Linux

python
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # UDP = SOCK_DGRAM
s.bind(("0.0.0.0", 5353))
data, addr = s.recvfrom(4096)  # blocking read of one datagram
s.sendto(b"pong", addr)        # reply to the specific client

No listen()/accept()/connect(): just bind() and recvfrom().

What tcpdump shows

IP 10.0.0.1.55321 > 8.8.8.8.53: UDP, length 32
IP 8.8.8.8.53 > 10.0.0.1.55321: UDP, length 64

No SYN/ACK flags: just two independent datagrams. From a single tcpdump packet you cannot tell whether they are related (there is no state).

Notes on conntrack

Even though UDP has no state, [[conntrack|netfilter conntrack]] still creates a pseudo-connection keyed on the 5-tuple (src-ip, src-port, dst-ip, dst-port, proto=UDP) and holds it for 30 seconds after the last packet. This lets [[nat|NAT]] work and allows the reply packet to match RELATED,ESTABLISHED.

When things go wrong

  • No response - UDP reports nothing. If the port is closed, the kernel may send an ICMP unreachable, but that is best-effort.
  • Out-of-order delivery - UDP does not sort packets. Your application must handle ordering with its own sequence numbers.
  • Duplicate packets - they can appear. Again, the application is responsible.
  • Large packet, timeout - a fragment was probably lost; send smaller datagrams.

§ команды

bash
ss -unlp

List all UDP sockets in LISTEN state: -u UDP, -n numeric, -l listening, -p processes

bash
tcpdump -i any -nn 'udp port 53' -c 5

Capture the first 5 DNS requests and replies: pure UDP traffic

bash
nc -u 8.8.8.8 53

Open a UDP socket with netcat to send raw bytes manually

bash
echo 'ping' | nc -u -w1 1.2.3.4 5353

Send one UDP datagram and wait one second for a reply (-w1)

bash
iperf3 -u -c host -b 100M -t 10

Measure UDP throughput by generating 100 Mbit/s for 10 seconds

§ см. также

  • tcp-handshakeTCP three-way handshakeTCP connection opens with three packets: SYN from the client, SYN-ACK from the server, ACK from the client. After that the connection is Established and data transfer can begin.
  • portPort: How Multiple Services Share One IPA 16-bit number (0-65535) that identifies the **destination process** on a host. IP says which host; port says which process. 80 is HTTP, 443 is HTTPS, 22 is SSH.
  • coapCoAP: REST for Constrained Devices over UDPCoAP is REST over UDP for low-power IoT devices. 4-byte header, GET/POST/PUT/DELETE, response codes like HTTP. Observe for notifications. DTLS for security. Used in LwM2M, Thread.
  • cmd-digdig: DNS queries with full detaildig queries DNS. Ask for any record type from any server. +short gives compact output. +trace follows resolution from the root. +dnssec shows DNSSEC signatures. It replaces nslookup for debugging.
  • cmd-iperf3iperf3: measuring bandwidth`iperf3` measures TCP/UDP throughput between two endpoints. Run a server on one host and a client on the other. Use it for network testing, not in production.
  • quic-http3QUIC: Modern Transport over UDPQUIC is a transport over UDP. TLS 1.3 is built in (1 RTT, 0-RTT for resume). Multiplexing without head-of-line blocking. Connection migration (Wi-Fi to 4G without drop). HTTP/3 = HTTP semantics over QUIC.
Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies