To assign a **static IP address** to a Linux machine (or server) permanently, you need to configure the network settings so that the machine uses the same IP address every time it boots up. The method depends on the network manager used by your Linux distribution, such as **Netplan** (common in Ubuntu 18.04+), **NetworkManager** (used by many desktop distributions), or **`/etc/network/interfaces`** (older or server-focused distributions).
Here are the most common methods based on different Linux setups:
---
### Method 1: **For Ubuntu 18.04+ / Debian 9+ with Netplan (default)**
**Netplan** is the default network configuration tool in modern Ubuntu and Debian systems. To set a static IP, follow these steps:
1. **Edit the Netplan Configuration File**
Netplan configuration files are usually located in `/etc/netplan/`. These files typically have a `.yaml` extension.
Open the appropriate configuration file for your network interface:
```bash
sudo nano /etc/netplan/01-netcfg.yaml
```
You might have a different file name, such as `50-cloud-init.yaml`, depending on your distribution or cloud setup. You can list files in `/etc/netplan/` to find the correct one.
2. **Modify the Network Configuration**
Edit the file to configure a static IP address. For example:
```yaml
network:
version: 2
renderer: networkd # or NetworkManager, depending on your setup
ethernets:
eth0:
dhcp4: no
addresses:
- 192.168.1.100/24 # The static IP address and subnet mask
gateway4: 192.168.1.1 # Your gateway IP (router)
nameservers:
addresses:
- 8.8.8.8 # DNS server (Google DNS in this case)
- 8.8.4.4 # Secondary DNS
```
In the example above:
- `eth0` is the network interface. Replace it with your actual interface name (use `ip link` or `ifconfig` to list interfaces).
- `dhcp4: no` disables DHCP, which means the static IP is set manually.
- `addresses` specifies the static IP address with the `/24` CIDR notation (subnet mask).
- `gateway4` is the gateway (typically your router's IP).
- `nameservers` provides DNS servers (Google DNS servers are used here).
3. **Apply the Changes**
After saving the file, apply the changes with:
```bash
sudo netplan apply
```
4. **Verify the Configuration**
Use `ip a` or `ifconfig` to verify that your static IP address is applied correctly:
```bash
ip a
```
---
### Method 2: **For CentOS/RHEL 7+ or Fedora (NetworkManager)**
**NetworkManager** is the default network management tool for CentOS, Fedora, and many desktop Linux distributions. You can use the `nmcli` tool or directly modify the connection configuration files.
1. **Find Your Network Connection Name**
First, identify the name of your network interface using:
```bash
nmcli device status
```
This will display a list of network interfaces. For example, it might show something like `enp3s0` or `eth0`.
2. **Modify the Connection Settings**
Use `nmcli` to configure a static IP address. Replace `enp3s0` with your interface name:
```bash
sudo nmcli con mod enp3s0 ipv4.addresses 192.168.1.100/24
sudo nmcli con mod enp3s0 ipv4.gateway 192.168.1.1
sudo nmcli con mod enp3s0 ipv4.dns "8.8.8.8 8.8.4.4"
sudo nmcli con mod enp3s0 ipv4.method manual
```
In these commands:
- `ipv4.addresses` sets the static IP address and subnet.
- `ipv4.gateway` sets the gateway.
- `ipv4.dns` sets the DNS servers.
- `ipv4.method manual` specifies that the interface should use a static IP (not DHCP).
3. **Restart the Network Connection**
After making the changes, restart the network connection:
```bash
sudo nmcli con down enp3s0 && sudo nmcli con up enp3s0
```
4. **Verify the Configuration**
Confirm that the static IP is assigned by running:
```bash
ip a
```
---
### Method 3: **For Older Systems or Debian/Ubuntu Server (Using `/etc/network/interfaces`)**
On older systems or some server versions (like Debian or older Ubuntu), network configuration is typically handled through `/etc/network/interfaces`.
1. **Edit `/etc/network/interfaces`**
Open the network configuration file:
```bash
sudo nano /etc/network/interfaces
```
2. **Configure the Static IP**
Add or modify the configuration for your interface (e.g., `eth0`) as follows:
```bash
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
```
In this example:
- `iface eth0 inet static` tells the system to use a static IP.
- `address`, `netmask`, `gateway`, and `dns-nameservers` set the static IP, subnet mask, gateway, and DNS servers.
3. **Restart the Networking Service**
After saving the file, restart the networking service:
```bash
sudo systemctl restart networking
```
4. **Verify the Configuration**
Again, use `ip a` or `ifconfig` to verify the static IP:
```bash
ip a
```
---
### Method 4: **For Arch Linux / Manually Configured Systems (Using `systemd-networkd`)**
If you're using `systemd-networkd` (often used in Arch Linux or minimal setups), you'll configure the static IP with a `.network` configuration file.
1. **Create a Network Configuration File**
Create a new `.network` file in `/etc/systemd/network/`, such as `20-wired.network`.
```bash
sudo nano /etc/systemd/network/20-wired.network
```
2. **Add Static IP Configuration**
Add the following content to the file:
```ini
[Match]
Name=eth0 # Replace with your interface name
[Network]
Address=192.168.1.100/24
Gateway=192.168.1.1
DNS=8.8.8.8
DNS=8.8.4.4
```
3. **Restart `systemd-networkd`**
Enable and restart `systemd-networkd` to apply the changes:
```bash
sudo systemctl restart systemd-networkd
```
4. **Verify the Configuration**
Check the IP address with:
```bash
ip a
```
---
### Conclusion
The method you choose depends on the network manager and Linux distribution you are using. The key takeaway is to configure your system to disable DHCP and manually set the IP, gateway, and DNS servers. This way, the machine will always use the same static IP after reboot.
Let me know if you need further assistance!
Here are the most common methods based on different Linux setups:
---
### Method 1: **For Ubuntu 18.04+ / Debian 9+ with Netplan (default)**
**Netplan** is the default network configuration tool in modern Ubuntu and Debian systems. To set a static IP, follow these steps:
1. **Edit the Netplan Configuration File**
Netplan configuration files are usually located in `/etc/netplan/`. These files typically have a `.yaml` extension.
Open the appropriate configuration file for your network interface:
```bash
sudo nano /etc/netplan/01-netcfg.yaml
```
You might have a different file name, such as `50-cloud-init.yaml`, depending on your distribution or cloud setup. You can list files in `/etc/netplan/` to find the correct one.
2. **Modify the Network Configuration**
Edit the file to configure a static IP address. For example:
```yaml
network:
version: 2
renderer: networkd # or NetworkManager, depending on your setup
ethernets:
eth0:
dhcp4: no
addresses:
- 192.168.1.100/24 # The static IP address and subnet mask
gateway4: 192.168.1.1 # Your gateway IP (router)
nameservers:
addresses:
- 8.8.8.8 # DNS server (Google DNS in this case)
- 8.8.4.4 # Secondary DNS
```
In the example above:
- `eth0` is the network interface. Replace it with your actual interface name (use `ip link` or `ifconfig` to list interfaces).
- `dhcp4: no` disables DHCP, which means the static IP is set manually.
- `addresses` specifies the static IP address with the `/24` CIDR notation (subnet mask).
- `gateway4` is the gateway (typically your router's IP).
- `nameservers` provides DNS servers (Google DNS servers are used here).
3. **Apply the Changes**
After saving the file, apply the changes with:
```bash
sudo netplan apply
```
4. **Verify the Configuration**
Use `ip a` or `ifconfig` to verify that your static IP address is applied correctly:
```bash
ip a
```
---
### Method 2: **For CentOS/RHEL 7+ or Fedora (NetworkManager)**
**NetworkManager** is the default network management tool for CentOS, Fedora, and many desktop Linux distributions. You can use the `nmcli` tool or directly modify the connection configuration files.
1. **Find Your Network Connection Name**
First, identify the name of your network interface using:
```bash
nmcli device status
```
This will display a list of network interfaces. For example, it might show something like `enp3s0` or `eth0`.
2. **Modify the Connection Settings**
Use `nmcli` to configure a static IP address. Replace `enp3s0` with your interface name:
```bash
sudo nmcli con mod enp3s0 ipv4.addresses 192.168.1.100/24
sudo nmcli con mod enp3s0 ipv4.gateway 192.168.1.1
sudo nmcli con mod enp3s0 ipv4.dns "8.8.8.8 8.8.4.4"
sudo nmcli con mod enp3s0 ipv4.method manual
```
In these commands:
- `ipv4.addresses` sets the static IP address and subnet.
- `ipv4.gateway` sets the gateway.
- `ipv4.dns` sets the DNS servers.
- `ipv4.method manual` specifies that the interface should use a static IP (not DHCP).
3. **Restart the Network Connection**
After making the changes, restart the network connection:
```bash
sudo nmcli con down enp3s0 && sudo nmcli con up enp3s0
```
4. **Verify the Configuration**
Confirm that the static IP is assigned by running:
```bash
ip a
```
---
### Method 3: **For Older Systems or Debian/Ubuntu Server (Using `/etc/network/interfaces`)**
On older systems or some server versions (like Debian or older Ubuntu), network configuration is typically handled through `/etc/network/interfaces`.
1. **Edit `/etc/network/interfaces`**
Open the network configuration file:
```bash
sudo nano /etc/network/interfaces
```
2. **Configure the Static IP**
Add or modify the configuration for your interface (e.g., `eth0`) as follows:
```bash
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
```
In this example:
- `iface eth0 inet static` tells the system to use a static IP.
- `address`, `netmask`, `gateway`, and `dns-nameservers` set the static IP, subnet mask, gateway, and DNS servers.
3. **Restart the Networking Service**
After saving the file, restart the networking service:
```bash
sudo systemctl restart networking
```
4. **Verify the Configuration**
Again, use `ip a` or `ifconfig` to verify the static IP:
```bash
ip a
```
---
### Method 4: **For Arch Linux / Manually Configured Systems (Using `systemd-networkd`)**
If you're using `systemd-networkd` (often used in Arch Linux or minimal setups), you'll configure the static IP with a `.network` configuration file.
1. **Create a Network Configuration File**
Create a new `.network` file in `/etc/systemd/network/`, such as `20-wired.network`.
```bash
sudo nano /etc/systemd/network/20-wired.network
```
2. **Add Static IP Configuration**
Add the following content to the file:
```ini
[Match]
Name=eth0 # Replace with your interface name
[Network]
Address=192.168.1.100/24
Gateway=192.168.1.1
DNS=8.8.8.8
DNS=8.8.4.4
```
3. **Restart `systemd-networkd`**
Enable and restart `systemd-networkd` to apply the changes:
```bash
sudo systemctl restart systemd-networkd
```
4. **Verify the Configuration**
Check the IP address with:
```bash
ip a
```
---
### Conclusion
The method you choose depends on the network manager and Linux distribution you are using. The key takeaway is to configure your system to disable DHCP and manually set the IP, gateway, and DNS servers. This way, the machine will always use the same static IP after reboot.
Let me know if you need further assistance!
Comments
Post a Comment