HOWTO-configure-network-on-ubuntu
· 2 min read
Set netplan configuration
Set Dynamic IP address
for inter in $(ls /sys/class/net); do
if [[ $inter != 'lo' ]]; then
cat << EOF > /etc/netplan/99-$inter.yaml
network:
ethernets:
$inter:
dhcp4: true
version: 2
renderer: networkd
EOF
ip link set dev $inter up
fi
done
Set Static IP address
network:
version: 2
renderer: networkd
ethernets:
eth0: # Replace with your interface name
dhcp4: false # Disable DHCP for IPv4
addresses:
- 192.168.1.100/24 # Static IP address with subnet mask
routes:
- to: 0.0.0.0/0 # Default route (default gateway)
via: 192.168.1.1 # Replace with your gateway IP
nameservers:
addresses:
- 8.8.8.8 # Primary DNS server (Google DNS)
- 8.8.4.4 # Secondary DNS server
Apply configurations
netplan apply
Check IP status
ip a
Set network config on boot
-
combine script as a shell
/etc/netplan/gen_netplan_config.sh
#!/bin/env bash
umask 377
for inter in $(ls /sys/class/net); do
if [[ $inter != 'lo' ]] && [[ ! -e /etc/netplan/99-$inter.yaml ]]; then
cat << EOF > /etc/netplan/99-$inter.yaml
network:
ethernets:
$inter:
dhcp4: true
version: 2
renderer: networkd
EOF
ip link set dev $inter up
fi
done
netplan apply -
write
/usr/lib/systemd/system/wait-netplan-dhcp.service
[^3][Unit]
Description=Generate DHCP networking DHCP demo for netplan
Before=network-online.target
[Service]
ExecStart=/etc/netplan/gen_netplan_config.sh
[Install]
WantedBy=multi-user.target -
start and enable
systemctl start wait-netplan-dhcp
systemctl enable wait-netplan-dhcp