You
will typically see a file such as 00-installer-config.yaml or 01-netcfg.yaml.
In my case file name is 50-cloud-init.yaml.
Edit the Netplan
Configuration File
sudo
nano /etc/netplan/50-cloud-init.yaml
Replace
the IP address, gateway and DNS as per your required settings and save the
file.
Apply the Netplan
Configuration Settings
sudo
netplan apply
For debugging issues, use:
sudo
netplan --debug apply
Important: YAML is indentation
sensitive. Use spaces instead of tabs because a single indentation mistake will
prevent netplan from loading the configuration.
Configure Static IP Address
on RHEL / CentOS / AlmaLinux / Rocky Linux
Red
Hat based distributions use Network Manager as a primary network management
daemon. You can configure it via nmcli (command line), nmtui (text UI) or by
editing connection files directly.
Method A: Using nmcli
(Recommended for Production)
The
nmcli command is the most reliable and scriptable way to manage network
connections on RHEL based systems.
Step 1: List available
connections:
nmcli
con show
Step 2: Set static IP, gateway and
DNS
This
nmcli command will set the static IP, gateway and DNS on RHEL server. Below is
the output configuration file. 
Output:
Restart the connection:
nmcli
con down ens160 && nmcli con up ens160
Method B: Edit Direct Connection
File
On
RHEL 8/9, connection files are stored at /etc/NetworkManager/system- connections/ens160.nmconnection:
sudo
nano /etc/NetworkManager/system-connections/ens160.nmconnection 
Apply the changes:
sudo
systemctl restart NetworkManager 
Note: On RHEL 9 and Rocky Linux
9 connection profiles are stored under /etc/NetworkManager/system-connections/
as .nmconnection files. Use nmcli command to manage them and avoid editing
these files manually unless necessary.
How to Configure Default
Gateway on Linux Servers
The
gateway allows Linux servers to communicate with external networks and the
internet.
Add a Default Gateway
(Temporary for testing)
sudo
ip route add default via 192.168.1.1 dev ens160 
This
remains till reboot. Use it just for a live testing before committing changes
to a configuration file.
Configure Gateway with
Netplan on Ubuntu
sudo
nano /etc/netplan/50-cloud-init.yaml 
Apply changes:sudo
netplan apply 
Verify Default Route
Configuration
Verify
gateway settings:
ip
route
or
route
-n 
Set Gateway Using NMCLI on
RHEL 9
To configure
the gateway using nmcli, you can modify the connection profile and then
reactivate it.
nmcli
con mod ens160 ipv4.gateway 192.168.1.1 
Apply changes:
nmcli
con up ens160 
How to Configure DNS
Servers on LinuxDNS
configuration is important for production Linux servers because applications depend
on hostname resolution.
Configure DNS Using Netplan
on Ubuntu
To
configure a dns using Netplan
on Ubuntu, edit the Netplan YAML file (usually in /etc/netplan/).
sudo
nano /etc/netplan/50-cloud-init.yaml
Apply changes:
sudo
netplan apply
Configure DNS with NMCLI on
RHEL 9
This
is a most common method used on modern RHEL, Rocky Linux, AlmaLinux and
CentOS to set dns using nmcli command.
nmcli
con mod ens160 ipv4.dns "8.8.8.8 1.1.1.1"
Restart the connection:
nmcli
con up ens160
Update resolv.conf manually
Temporary
DNS configuration:
nano
/etc/resolv.conf
Test DNS Resolution in
Linux
Test
DNS using dig:
dig
google.com
Or
use nslookup:
nslookup
google.com
How to Apply and Verify
Network Changes
After
configuring Linux network settings always verify connectivity.
Restart Networking Services
1. Restart NetworkManager:
systemctl
restart NetworkManager
2. Restart
systemd-networkd
systemctl
restart systemd-networkd
Complete Verification
Checklist
ip
addr show ens33
# 2. Check the default
gateway
ip
route show
# 3. Check DNS
configuration
resolvectl
status
cat
/etc/resolv.conf
# 4. Test gateway
connectivity
ping
-c 4 192.168.1.1
# 5. Test internet/DNS
resolution
ping
-c 4 8.8.8.8
ping -c 4 google.com
# 6. Confirm hostname
hostnamectl
# 7. Check if the correct
interface is UP
ip
link show ens33
Quick
Reference Table
| What to Check |
Command |
| IP Address |
ip addr show |
| Default Gateway |
ip route show |
| DNS Servers |
resolvectl status |
| Hostname |
hostnamectl |
| Interface Status |
ip link show |
| DNS Resolution Test |
dig google.com |
Common Errors and How to
Fix Them
1. Error: "Network unreachable"
after applying static IP address
Cause: Wrong subnet mask or
gateway IP Address
Fix: Double check your network
range. If your network is 192.168.1.0/24, your gateway must also be in the
192.168.1.x range.
2. Error: DNS resolution is
unsuccessful, ping connects to IP address
Reason: DNS servers are
unreachable or improperly configured
Fix: Test DNS manually
dig
@8.8.8.8 google.com
#
Check if systemd-resolved is running
systemctl
status systemd-resolved
3. Error: Netplan apply fails with
YAML error
Cause: Incorrect indentation or
syntax error
Fix: Verify settings in the
YAML file before applying changes
sudo
netplan generate
4. Error: nmcli changes not
persisting after reboot
Cause: connection.autoconnect is
set to no
Fix: sudo nmcli con mod ens33
connection.autoconnect yes
5. Error: hostname reverts back to
previous after reboot
Cause: hostname command was used
instead of hostnamectl
Fix: Always use sudo
hostnamectl set-hostname your-hostname
Best Practices for
Production Linux Server Networking
Production
Linux servers should follow standard networking best practices.
·
Use Static IPs for Production Systems because Static
IP addresses minimize connectivity and infrastructure management issues
·
Document Network Changes
·
Maintain
records of IP addresses, Gateways, DNS servers, VLANs and Hostnames
· Always configure primary and secondary DNS
servers
· Avoid Manual Changes to Auto-Generated Files
· Always use standard configuration tools
· Test Connectivity before Reboot the system
· Verify SSH connectivity before restarting
remote production servers
Security Considerations of
Linux Production Servers
Linux
production server security should include proper network management practices.
· Restrict Unauthorized Network Changes
· Limits sudo access to trusted administrators
only
· Monitor Network Configuration Changes
Track configuration
changes using:
• Git repositories
• Configuration
management tools
• Monitoring systems
• Audit logs
· Use Proper Hostname Naming Conventions
· Use standard names as hostname
Example:
· web-prod-01
· db-prod-01
· proxy-prod-01
Linux process and service management based on proper network configuration. Learn more in our guide on Linux Process Management for Beginners - Complete Guide.
Difference between Netplan
and NetworkManager
A
common question when switching between distros is: which tool should I use?
| Feature |
Netplan |
Network Manager |
| Default On |
Ubuntu 18.04+ |
RHEL, CentOS, Fedora |
| Configuration Format |
YAML files |
INI files or nmcli |
| Backend |
systemd-networkd or NetworkManager |
NetworkManager daemon |
| Best For |
Servers (with networkd) |
Desktops and servers |
| Scripting |
YAML based |
nmcli commands |
| Apply Changes |
netplan apply |
nmcli con up |
Both
are solid choices. On Ubuntu servers, stick with netplan + networkd. On RHEL
based systems use nmcli.
Frequently Asked Questions
(FAQs)
Q1: How to set a static IP
address without rebooting in Linux?
Ans:
Use ip addr add 192.168.1.100/24 dev ens33 and ip route add default via
192.168.1.1 for a live change then update your Netplan or nmcli configuration
for permanent changes.
Q2: How to change the
hostname permanently in Linux?
Ans:
Run sudo hostnamectl set-hostname new-hostname and update the /etc/hosts. The
change immediately takes effect.
Q3: What is the difference
between Netplan and NetworkManager?
Ans:
Netplan is the default on Ubuntu servers and uses YAML config files. Network
Manager is default on RHEL/CentOS and is managed via nmcli. Ultimately both
configure the same kernel networking stack.
Q4: How do I configure DNS
on Ubuntu 22.04 server?
Ans:
Either set DNS in your Netplan YAML under nameservers addresses or configure
/etc/systemd/resolved.conf and restart systemd-resolved.
Q5: How to verify network
settings in Linux?
Ans:
Use ip addr show for IP, ip route show for gateway, resolvectl status for DNS
and hostnamectl for hostname.
Conclusion
Properly
configuring hostname, static IP, gateway and DNS on Linux production servers is
the foundation for stable and secure infrastructure. Whether you are using
Netplan on Ubuntu or nmcli on RHEL, the principles are the same:
• Assign
a static and meaningful IP address
• Set
the correct default gateway for your subnet
• Configure secure DNS servers
• Set a descriptive and permanent hostname
• Always verify before log off
Mastering
these Linux sysadmin skills gives you full control over your server's network
identity and connectivity and prevents the kind of outages that come from
relying on DHCP in production.
If you found this guide helpful, keep visiting seeklinux for more Linux tutorials, server administration guides and troubleshooting tips.
Post a Comment