Linux is a free and open source alternative to Windows and Mac OS. This operating system can work in the form of a raw text console, or in a graphical environment such as GNOME and KDE. This guide will explain how to assign an IP address on a Linux system via the console. This guide assumes that you know the basics of using Linux, and how networks, IP addresses, and DNS servers work.
Step
Method 1 of 5: Using a "Root" Account
Step 1. If you are not logged into the system as 'root' (a kind of Administrator account in Linux), open a console and enter "su"
Press "Enter".
Step 2. Note:
Ubuntu Linux distributions usually have the same "root" password as the account password that was created when the operating system was installed.
Step 3. Enter the "root" account password when prompted, and press "Enter"
Method 2 of 5: Debian/Ubuntu/Kubuntu
Step 1. Make a copy of the /etc/network/interfaces file by writing the following command in the console:
'cp /etc/network/interfaces /etc/network/interfaces.backup'
Step 2. Type 'vi /etc/network/interfaces', then press "Enter"
Press 'i' to enter editing mode.
Step 3. Scroll until you find the network card in the file
The network card is usually named eth0 for an Ethernet network card, or wlan0/wifi0 for a wireless network.
Step 4. Change 'iface eth0 inet dhcp' to 'iface eth0 inet static'
Step 5. Add the following lines to the file
Replace the IP address with the IP address you want: address 192.168.0.10netmask 255.255.255.0network 192.168.0.0broadcast 192.168.0.255gateway 192.168.0.1dns-nameservers 216.10.119.241
Step 6. Save the file and exit the editor
Press "Esc" to enter vi command mode, then write ":wq". Press "Enter".
Step 7. Enter 'ifdown eth0' and press "Enter"
Step 8. Enter 'ifup eth0' and press "Enter"
Method 3 of 5: Red Hat or Slackware
Step 1. The easiest way to set the IP on Red Hat or Slackware is to type "netconfig" on older distribution versions, or "ifconfig /netstat" on new distribution versions
Run the command in the console as "root". The text-based menu will help you set the required configuration.
Step 2. Write the following command in console: nano /etc/sysconfig/network-scripts/ifcfg-eth0. Use "Tab" to move between columns, and use spaces to check or uncheck checkboxes.
Step 3. Once you have entered the desired settings, close the settings window
Step 4. To apply the rule, enter the command 'service network restart' into the console, then press "Enter"
This step isn't necessary if you're using Slackware -- your changes will be applied automatically. Note: There are various distributions based on Red Hat Linux, such as Fedora Core, CentOS, White Box, etc. Most of these operating systems may have the same IP configuration command.
Method 4 of 5: Any Linux System with Kernel 2.4 and Up
The following method is a little more complicated. You'll still need a console, but the steps should work for any modern Linux distribution. The configuration program used is "ip", and is located in the "/sbin/" folder.
Step 1. Know the name of the network interface to use
To display all available network interfaces, run the command "/sbin/ip link". A list of interface names, MAC addresses, and other information will appear on the screen.
Step 2. Apply the IP address with the "addr" subcommand, such as "/sbin/ip addr add 192.168.0.10/24 dev [INTERFACE_NAME]"
-
The default access point is added with the "route" subcommand, such as "/sbin/ip route add default via [GATEWAY_ADDRESS]".
Step 3. Activate the network interface with the "link" subcommand:
"/sbin/ip link set [INTERFACE_NAME] up". If all the data you entered is correct, the network will run normally. Unfortunately, all network settings will be lost if you restart the system, so if you want to restore the settings after the system has started, add all of the above commands to "/etc/rc.local" -- a file that runs when the system starts.
Method 5 of 5: Dynamic IP Address/DHCP
Dynamic IP addresses are generated automatically and generally do not require user attention, but there are specific cases to be aware of:
Step 1. If the network is not active when the system starts up, the internet connection may not be available even after the network problem is solved
To restore the internet connection immediately, run the command "dhclient" as root. The dynamic address will also be reset.
Step 2. The same situation may also occur if the network is accessed by hardware that starts with your computer
In some cases, Linux starts up faster than the network "router" and doesn't find the network on startup. Find dhclient.conf (eg /etc/dhcp3/dhclient.conf for Debian) and add/fix the reboot line nn;. Replace nn with the waiting time interval for the "router" activation process after the system is active.
Tips
- The 'sudo' command (short for "superuser do") allows temporary administrator access and is more suitable than 'su' in most cases. Read the sudo guide by typing "man sudo," and be grateful that you did.
- The /etc/resolv.conf file may need to be edited if you want to access the system by domain name instead of IP address. Use methods like editing the /etc/network/interfaces file, and don't forget to back up the originals!
Warning
- Don't forget to back up your files so you can restore the original settings.
- Don't try the steps above, unless you understand how networks, IP addresses, and DNS servers work.
- Don't keep logging in as root. We recommend that you log in as another user, use the su command in the console, and then return to that user account when you're done. Root access allows other users with access to your computer to do anything to your system.