This article shows how to assign a static IP address to a computer running Linux. This will prevent connection problems or conflicts from arising on the LAN to which you will connect the computer.
Steps
Method 1 of 2: Debian-Based Linux Distributions
Step 1. Find the version of Linux you are using
Debian-based Linux distributions include Ubuntu, Mint, and Raspbian.
Step 2. Open a "Terminal" window
This is the command console present in all Linux distributions comparable to the Windows "Command Prompt" or the "Terminal" window on Mac. Depending on the version of Linux you are using, you may have several methods to open a window "Terminal":
- Press the key combination Ctrl + Alt + T or Ctrl + Alt + F1 (if you are using a Mac replace the Ctrl key with the ⌘ Command key).
- Use the search bar at the top or bottom of the screen (if possible).
- Log in to Menu Linux main to locate and select the "Terminal" app icon.
Step 3. Switch to use the root user
If you are not already logged into the system with the "root" account, type the command su and press the Enter key. At this point, enter the root account password and press the Enter key again.
The Linux "root" user is the equivalent of the administrator account on Windows systems or Mac computers
Step 4. View your computer's current network configuration
Type the ifconfig command in the "Terminal" window and press the Enter key. A list of all network interfaces present in the system will be displayed with their configuration information.
The first item in the list should be the current connection to the LAN. The name of this interface is "eth0" (if you are using an Ethernet cable) or "wifi0" (if you are using a Wi-Fi connection)
Step 5. Find the connection you want to assign a static IP address to
Review the name of the item to edit. This information is listed on the left side of the list that appeared in the previous step.
In most cases you will need to refer to the "eth0" or "wifi0" network interface
Step 6. Change the network IP address
Type the command sudo ifconfig [interface_name] [IP_address] netmask 255.255.255.0 in the "Terminal" window. Make sure to replace the [interface_name] parameter with the name of the network connection you want to assign the static IP to and the [IP_address] parameter with the address to use, then press the Enter key.
For example, to assign the IP address "192.168.2.100" to the Ethernet network interface (named "eth0"), you will need to use this command sudo ifconfig eth0 192.168.0.100 netmask 255.255.255.0
Step 7. Assign the network default gateway
Type the command route add default gw 192.168.1.1 and press the Enter key. The IP address to use is that of the router / modem that manages the network which is normally "192.168.1.1" (if in your case it is different, replace the numerical values given in the command with the address of your router).
Step 8. Add a DNS server
Type the command echo "nameserver 8.8.8.8"> /etc/resolv.conf and press the Enter key.
The example uses Google's primary DNS server, but if you need to use a different one, replace the 8.8.8.8 IP address with that of the DNS service you have chosen to use
Step 9. Verify the new configuration of the network interface under consideration
Run the ifconfig command again, locate the name of the network connection you just changed and verify the new IP address. It should match the IP address you just entered.
Method 2 of 2: RPM-Based Linux Distributions
Step 1. Find out what version of Linux you are using
RPM-based Linux distributions include CentOS, Red Hat, and Fedora.
Step 2. Open a "Terminal" window
This is the command console present in all Linux distributions comparable to the Windows "Command Prompt" or the "Terminal" window on Mac. Depending on the version of Linux you are using, you may have several methods to open a window "Terminal":
- Press the key combination Ctrl + Alt + T or Ctrl + Alt + F1 (if you're using a Mac, replace the Ctrl key with the ⌘ Command key).
- Use the search bar at the top or bottom of the screen (if possible).
- Log in to Menu Linux main to locate and select the "Terminal" app icon.
Step 3. Switch to use the root user
If you are not already logged into the system with the "root" account, type the command su and press the Enter key. At this point, enter the root account password and press the Enter key again.
The Linux "root" user is the equivalent of the administrator account on Windows systems or Mac computers
Step 4. View your computer's current network configuration
Type the command ip a in the "Terminal" window and press the Enter key. A list of all network interfaces present in the system will be displayed with their configuration information.
Step 5. Find the network connection you want to assign a static IP address to
This is usually the Ethernet (wired) or Wi-Fi (wireless) connection. Its current IP address is displayed on the right of the "Terminal" window.
Step 6. Go to the directory where the scripts that manage the network connection are stored
Type the command cd / etc / sysconfig / network-scripts and press the Enter key.
Step 7. View the scripts currently present
Type the command ls and press the Enter key. The name of the current network connection should appear in the upper left of the "Terminal" window.
Step 8. Open the network connection setup script you normally use
Type the command vi ifcfg- [network_name] and press the Enter key. The list of network connection properties will be displayed within the Vi text editor.
For example, if the currently active network connection is called "eno12345678", you will need to type the command vi ifcfg-eno12345678
Step 9. Edit the network configuration
Change the following parameters within the file under consideration:
- BOOTPROTO - replace the dhcp value with none;
- IPV6 addresses - delete any item characterized by the initials IPV6 by moving the text cursor to the left of the letter I and pressing the Canc key;
- ONBOOT - change the value no to the value yes.
Step 10. Enter the new IP address
Press the Enter key to create a new line of text below the entry ONBOOT, then type the code
IPADDR =
enter the IP address to use and press the Enter key.
-
For example, if you want to use the IP address "192.168.2.23", you will need to enter the following code
IPADDR = 192.168.2.23
- and press the Enter key.
Step 11. Add the netmask, default gateway and DNS server information
Follow these instructions:
-
Enter the code
PREFIX = 24
and press the Enter key. At this point you will need to add the netmask as well
NETMASK = 255.255.255.0
-
enter the code
GATEWAY = 192.168.1.1
- and press the Enter key. If the network router / modem you are connecting to uses a different IP address than the one indicated, make the appropriate changes.
Step 12. Save the new network configuration and close the Vi editor
You can use the menu File window or type the command: wq and press the Enter key.