Debian 13 Tutorial / Configure Static IPv4 and DNS with DHCP Recovery

In the verified Debian 13 ifupdown environment, switch from Dynamic Host Configuration Protocol (DHCP) to static IPv4 by stopping the old configuration before applying the prepared file, then configure DNS through its actual owner. Prepare console access, backups and a tested recovery path before changing a remote connection. This lab checks addresses, routes, name resolution, a new SSH connection and reboot persistence, then restores the original DHCP setup on a dedicated VirtualBox NAT VM.

Confirm the Exact Network Environment

Use the ifupdown environment identified in the network and DNS management lesson. Here enp0s3 uses allow-hotplug and is managed through ifup@enp0s3.service. Initially, dhcpcd supplies its DHCP address and DNS information. NetworkManager, networkd, resolved and resolvconf are not used in this VM.

ip -br address
ip route
cat /etc/network/interfaces
ls -l /etc/resolv.conf
cat /etc/resolv.conf
systemctl cat ifup@.service
systemctl status ifup@enp0s3.service --no-pager

Do not apply the following files directly if your network management tool, interface name, or startup policy is different. Connections managed by NetworkManager or networkd should be handled with the settings of those tools. Environments where resolv.conf is a link from another service also differ from the manual DNS file example in this article.

Make sure you can actually log in to the VirtualBox console and use sudo. Leaving only an SSH session is different from being able to recover via the console. The verification environment was Debian 13.7, ifupdown 0.8.44, VirtualBox 7.2.10.

Choose Addresses, DNS and the NAT Forwarding Target

Item Values for this dedicated VM Selection Criteria
Interface enp0s3 Check actual name using ip output
IPv4 Address and Prefix Length 10.0.2.15/24 Maintain existing address of dedicated NAT and change allocation method to static
Default Gateway 10.0.2.2 Router address of this NAT network
DNS 10.0.2.3 VirtualBox default NAT name lookup function
SSH forwarding 127.0.0.1:22213 → 10.0.2.15:22 Specify the guest address of the existing rule that only connects from the host

An unchanged address does not prove that the guest is using static configuration: DHCP can assign the same address again. This lab changes the assignment method while retaining the existing address in an isolated NAT with one guest. On a working LAN, obtain a non-conflicting address and confirm DHCP reservations/pools, subnet, gateway and internal DNS policy with the administrator. Do not copy these NAT values to another network.

Oracle NAT explanation guides the default address range and port forwarding conditions for fixed guest addresses. In VirtualBox, check the existing SSH rule under VM Settings → Network → Adapter 1 → Advanced → Port Forwarding. Keep Host IP 127.0.0.1, Port 22213, TCP, and Guest Port 22, and specify 10.0.2.15 for the guest IP. Record the original entry before changes and do not add duplicate rules for the same port.

127.0.0.1 of the host is not Debian's own loopback address. Handle the host's forwarding settings and the guest's actual IP configuration as separate tasks. If you choose another fixed IP, the forwarding target must match that value.

Back Up the Files and Prepare a Static Configuration

Run these commands inside Debian. Run the first backup-directory line separately and continue only if it succeeds; stop if any copy fails. Preserve an existing backup by choosing a fresh name and changing every related path consistently.

sudo test ! -e /var/backups/lf-debian13-14 && sudo install -d -m 700 /var/backups/lf-debian13-14
sudo cp -a /etc/network/interfaces /etc/resolv.conf /etc/dhcpcd.conf /var/backups/lf-debian13-14/
sudo cp -a /etc/network/interfaces /var/backups/lf-debian13-14/interfaces.static
sudo nano /var/backups/lf-debian13-14/interfaces.static

From the currently valid file, change only the IPv4 part of enp0s3 from DHCP to static and enter the address and gateway. Keep the source, loopback, and existing IPv6 settings. If there is the same interface definition in a separate included file, duplicate entries must be resolved first. The key full configuration of the file prepared on this minimal VM is as follows.

source /etc/network/interfaces.d/*

auto lo
iface lo inet loopback

allow-hotplug enp0s3
iface enp0s3 inet static
    address 10.0.2.15/24
    gateway 10.0.2.2
iface enp0s3 inet6 auto

This is an IPv4 static configuration corresponding to the interfaces manual, and /24 is the network prefix length. Do not specify the same default route on other interfaces. This is an example that maintains existing automatic configuration, not one that disables IPv6.

sudo ifup --no-act --force -i /var/backups/lf-debian13-14/interfaces.static enp0s3

In the --no-act output, verify that the prepared address and default route are correct. This is not a command to test actual connection or DNS success. Current interfaces have not been brought down yet, so existing SSH connections are maintained.

Prepare and Verify the Recovery Script

The allow-hotplug interface of this VM keeps the DHCP process running with the existing ifup@ service. If you only call ifup within a temporary systemd service, the child DHCP process may also terminate when the temporary service ends. Recovery must be entrusted to the existing interface service. Next, open the new file.

sudo nano /var/backups/lf-debian13-14/restore.sh

The full content is as follows. Align the interface name and path with your verified configuration.

set -eu
ifdown --force enp0s3 || true
cp -a /var/backups/lf-debian13-14/interfaces /etc/network/interfaces
cp -a /var/backups/lf-debian13-14/resolv.conf /etc/resolv.conf
systemctl restart ifup@enp0s3.service
printf 'Original DHCP and DNS ownership restored\n'

This is an emergency recovery script that restores the saved configuration even if ifdown fails. To succeed in recovery, you must check the last service start and the actual address, process, and DNS. Do not apply ifup@ as is on servers that use the auto policy or other startup services.

During practice, initially, ifup was executed directly within a temporary service, which caused the DHCP to stop together. After modifying it as above, the scheduled recovery was re-verified. This is why the connection is not judged to be restored just by the exit code of the recovery command.

Write the Apply Script and Check DNS Ownership

Next, run sudo nano /var/backups/lf-debian13-14/apply.sh to save the entire content below. Release of DHCP is first performed in a state where the previous DHCP configuration is read, and then the static files are moved to their actual locations.

set -eu
ifdown enp0s3
cp -a /var/backups/lf-debian13-14/interfaces.static /etc/network/interfaces
ifup enp0s3
if pgrep -x dhcpcd > /dev/null; then
  printf 'A DHCP management process remains. Check DNS ownership before continuing.\n'
  exit 1
fi
printf 'nameserver 10.0.2.3\n' > /etc/resolv.conf
ip -br address show enp0s3
ip route
getent ahostsv4 deb.debian.org

Manual writing of resolv.conf applies only to regular files like this VM and only when the DHCP client has terminated without any other DNS manager. You can refer to dhcpcd manual for the way dhcpcd creates DNS files. Do not use methods that forcibly block other managers by fixing the file with write-protection attributes.

Simply writing dns-nameservers in interfaces does not mean that DNS will be applied in all installations. Like the resolvconf integration documentation, hooks and management configurations that handle that option are necessary. Environments using resolved, NetworkManager, or resolvconf set DNS through their respective management paths. For this exercise, no additional DNS managers will be installed.

Schedule Recovery Before Applying the Change

Check script permissions and syntax before scheduling recovery, and stop on any failure. Use the rollback scheduling line separately and confirm that lf-net14-rollback.timer is active and waiting with sudo systemctl status lf-net14-rollback.timer --no-pager before running the apply line. If a unit with either name already exists, inspect it rather than reusing it blindly. Choose a longer interval before applying if five minutes will not be enough for your checks.

sudo chmod 700 /var/backups/lf-debian13-14/restore.sh /var/backups/lf-debian13-14/apply.sh
sudo bash -n /var/backups/lf-debian13-14/restore.sh
sudo bash -n /var/backups/lf-debian13-14/apply.sh
sudo systemd-run --unit=lf-net14-rollback --on-active=5m /bin/bash /var/backups/lf-debian13-14/restore.sh
sudo systemd-run --unit=lf-net14-apply --collect /bin/bash /var/backups/lf-debian13-14/apply.sh

Use temporary services and timers as described in the systemd-run manual. Although the applied command is separated from the SSH shell to continue the task even if the connection is lost, the service creation message itself does not indicate that the setup is complete. Keep the console as the connection may briefly drop.

The recovery timer is not a permanent schedule that persists beyond rebooting. First, verify in the current boot, and if its normal operation cannot be confirmed, do not try to resolve it by rebooting; use scheduled recovery and the console.

Verify Addresses, Routes, DNS, SSH and Reboot Persistence

Check the following on the console or a reconnected session. Immediately after applying, there may be a brief period when the link state is settling, so compare the logs with the actual final state.

ip -br address show enp0s3
ip route
cat /etc/resolv.conf
pgrep -a dhcpcd
getent ahostsv4 deb.debian.org
sudo journalctl -u lf-net14-apply.service --no-pager -n 30

In this environment, 10.0.2.15/24 and 10.0.2.2 as the default route, and nameserver 10.0.2.3 should be confirmed, and dhcpcd should no longer be running. The address in the name resolution result varies at runtime, so do not memorize a single address for comparison. Name resolution success and the normal response of a specific web service are separate matters.

Also check a new connection in the host's new terminal using the existing key via SSH. If the port forwarding path is the same, use ssh -p 22213 lfstudent@127.0.0.1, but adjust your key and account settings. Since the server has not been reinstalled, do not ignore host key warnings or randomly delete known_hosts.

Cancel the scheduled recovery only when the address, route, DNS, and new connection are all correct.

sudo systemctl stop lf-net14-rollback.timer
sudo systemctl reboot

Save necessary tasks and secure the console before rebooting. After booting, repeat the same checks and new SSH connection. On the actual VM, fixed IPv4, default route, and manual DNS were maintained, and IPv6 autoconfiguration, active SSH, and zero failed services were confirmed.

Restore DHCP and Verify Recovery

To finish the fixed configuration practice, run the prepared recovery script either from the console or as a standalone service. Use only one of the methods below. If there have been other normal changes after the backup, check the differences first.

sudo systemd-run --unit=lf-net14-restore --collect /bin/bash /var/backups/lf-debian13-14/restore.sh

To run it directly from the console, use sudo bash /var/backups/lf-debian13-14/restore.sh. Do not change the part that keeps the existing interface service on DHCP to a standalone ifup execution. As when applying, check new connections and the final status.

ip -br address show enp0s3
ip route
pgrep -a dhcpcd
head -n 6 /etc/resolv.conf
systemctl status ifup@enp0s3.service --no-pager
getent ahostsv4 deb.debian.org
sudo cmp /etc/network/interfaces /var/backups/lf-debian13-14/interfaces

The corrected recovery script was tested through a shorter, 45-second scheduled timer in the original lab. DHCP addressing and routes, dhcpcd-generated DNS, restored interface configuration and a new SSH connection were verified afterward. The five-minute example gives more time for reader checks. A renewed DHCP lease can supply updated DNS information, so resolv.conf need not remain byte-for-byte identical to its old text.

The host's port forwarding is also reverted to the original guest IP value recorded. This practice restored the previously empty guest IP according to the default DHCP behavior. Ultimately, you need to check the restoration of guest settings and host forwarding separately to accurately determine connection issues.