If you're running an Ubuntu server, especially the latest 24.04 LTS release, assigning a static IP address is a fundamental step for ensuring reliable network connectivity. Unlike dynamic IPs assigned by DHCP which can change, a static IP remains constant, crucial for servers that need to be consistently accessible for services, websites, or remote management.
For those of us who've spent time managing CentOS or Rocky Linux servers, Ubuntu Server's network configuration might seem a little different at first. Since Ubuntu 18.04, netplan
has been the default network configuration tool. If you're like me and need a quick refresher, or if you're setting up your Ubuntu 24.04 LTS server for the first time, this guide will walk you through setting a static IP address using netplan
.
Step-by-Step Guide to Setting a Static IP
-
Access Your Server and Navigate to Netplan Directory:
First, log in to your Ubuntu Server 24.04 LTS. Open your terminal and navigate to the
netplan
configuration directory:Bashcd /etc/netplan
-
List Netplan Configuration Files:
List the files in this directory. You'll typically find a YAML file, often named something like
50-cloud-init.yaml
. The exact filename might vary.Bashls /etc/netplan/
You should see output similar to:
50-cloud-init.yaml
-
Edit the Netplan Configuration File:
Now, you need to edit this YAML file using a text editor like
nano
orvi
. Important: Make sure you usesudo
as you'll need administrative privileges to modify this file.Bashsudo nano /etc/netplan/50-cloud-init.yaml
Note: If your filename is different from
50-cloud-init.yaml
, replace it in the command above. -
Configure Static IP Settings:
Inside the YAML file, you'll need to modify or add the following configuration. Carefully replace the example IP addresses, gateway, and DNS servers with your network's specific details.
Here's a template you can adapt:
YAMLnetwork: version: 2 renderer: networkd ethernets: ens160: # Replace 'ens160' with your actual interface name (e.g., eth0, enp0s3) dhcp4: no addresses: - 172.16.129.135/24 # Your desired static IP address and subnet mask (e.g., 192.168.1.10/24) routes: - to: default via: 172.16.129.1 # Your network's gateway IP address (e.g., 192.168.1.1) nameservers: addresses: [8.8.8.8, 8.8.4.4] # Your preferred DNS server addresses (e.g., Google Public DNS)
Understanding the Configuration:
network:
The root element for network configuration.version: 2
: Specifies the Netplan configuration version.renderer: networkd
: Indicates thatnetworkd
will be used as the network renderer. This is the standard renderer for Ubuntu Server.ethernets:
: Defines Ethernet interface configurations.ens160:
: Replace this with your actual network interface name. To find your interface name, you can use the commandip link show
orifconfig -a
. Common names includeeth0
,ens3
,ens160
,enp0s3
, etc.dhcp4: no
: Disables DHCP for IPv4 on this interface, ensuring a static IP.addresses:
: Specifies the static IPv4 address and subnet mask in CIDR notation (e.g.,192.168.1.10/24
).172.16.129.135/24
: Example IP address. Change this to your desired static IP./24
represents a subnet mask of255.255.255.0
.
routes:
: Defines network routes.to: default
: Specifies the default route (for all traffic not on the local network).via: 172.16.129.1
: Replace this with your gateway IP address. This is the IP address of your router or the device that connects your network to the internet.
nameservers:
: Defines DNS server settings.addresses: [8.8.8.8, 8.8.4.4]
: Replace these with your preferred DNS server IP addresses.8.8.8.8
and8.8.4.4
are Google Public DNS servers. You can use other public DNS servers or your ISP's DNS servers.
-
Apply the Netplan Configuration:
After saving and closing the configuration file, apply the changes using the following command:
Bashsudo netplan apply
This command will apply the new network configuration.
-
Verify the Static IP Address:
To confirm that your static IP address has been successfully set, use the
ip addr show
command, replacingens160
with your interface name:Baship addr show ens160
Look for the
inet
line within the output for your interface. You should see your newly configured static IP address. For example, you might see output similar to this, confirming the IP172.16.129.135
is set:ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.16.129.135 netmask 255.255.255.0 broadcast 172.16.129.255 inet6 fe80::20c:29ff:fea2:528e prefixlen 64 scopeid 0x20<link> ether 00:0c:29:a2:52:8e txqueuelen 1000 (Ethernet) RX packets 34914 bytes 9756627 (9.7 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 17552 bytes 1901185 (1.9 MB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 device interrupt 44 memory 0x3fe00000-3fe20000
Congratulations! You have successfully configured a static IP address on your Ubuntu Server 24.04 LTS. Your server will now have a consistent IP address, making it reliably accessible for all your server needs.
Troubleshooting Tips:
- Incorrect Interface Name: Double-check that you've used the correct network interface name in the Netplan configuration file.
- YAML Syntax Errors: YAML files are sensitive to spacing and indentation. Ensure your file is correctly formatted. Use spaces for indentation, not tabs. You can use online YAML validators to check for syntax errors.
- Network Connectivity Issues: If you lose network connectivity after applying the configuration, verify your IP address, subnet mask, gateway, and DNS server settings are correct and compatible with your network.
Let me know in the comments if you have any questions or run into any issues! Happy server managing!
0 comments:
Post a Comment