Hostname is a critical system identifier that helps distinguish devices in a network. Proper configuration ensures seamless communication, service identification, and log management. Below are key methods to manage hostname in Debian:
Use any of these commands to check the current hostname:
hostnamectl status (shows detailed info including static/dynamic hostname)hostname (returns the short hostname)cat /etc/hostname (displays the persistent hostname stored in the file).For immediate but non-persistent changes (resets after reboot), use:
sudo hostname new-hostname
This updates the runtime hostname but does not modify configuration files.
To make the change permanent across reboots, update the following files:
/etc/hostnameEdit the file using a text editor (e.g., nano):
sudo nano /etc/hostname
Replace the existing hostname with the new one (e.g., my-debian-server) and save the file. This file stores the persistent hostname.
/etc/hostsModify the /etc/hosts file to map the new hostname to the loopback IP (127.0.0.1 or 127.0.1.1 for Debian):
sudo nano /etc/hosts
Find the line starting with 127.0.1.1 (or 127.0.0.1) and replace the old hostname with the new one. Example:
127.0.0.1 localhost
127.0.1.1 my-debian-server
Save the file. This ensures the system resolves the hostname to the correct IP locally.
To apply changes without rebooting, restart the systemd-hostnamed service:
sudo systemctl restart systemd-hostnamed
hostnamectl for Permanent ChangeThe hostnamectl command can also set a permanent hostname directly:
sudo hostnamectl set-hostname new-hostname
This updates both /etc/hostname and /etc/hosts automatically. No additional steps are required.
Network setup in Debian involves configuring IP addresses, gateways, and DNS servers. The method depends on your Debian version and preferred network manager.
/etc/network/interfacesThis is the classic approach for Debian versions prior to 10 (Stretch) and remains valid for later versions (with ifupdown2).
sudo cp /etc/network/interfaces /etc/network/interfaces.bak
sudo nano /etc/network/interfaces
eth0 with your actual interface name, e.g., ens33):
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4
auto eth0
iface eth0 inet dhcp
sudo systemctl restart networking
ping -c 4 google.com
```.
netplan (Debian 10+)Debian 10 (Buster) and later use netplan for network configuration.
/etc/netplan/01-netcfg.yaml):sudo nano /etc/netplan/01-netcfg.yaml
eth0 with your interface name):network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
addresses: [192.168.1.100/24]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
For DHCP, set dhcp4: yes and remove the addresses, gateway4, and nameservers sections.sudo netplan apply
For desktop environments (e.g., GNOME, KDE), NetworkManager provides a graphical interface and CLI tools.
nmcli connection show
Wired connection 1 with your connection name):sudo nmcli connection modify "Wired connection 1" ipv4.method manual ipv4.addresses 192.168.1.100/24 ipv4.gateway 192.168.1.1 ipv4.dns "8.8.8.8,8.8.4.4"
sudo nmcli connection down "Wired connection 1" && sudo nmcli connection up "Wired connection 1"
/etc/hosts or a DNS server) for other devices to access it.