- Written By: Ajay
- Category: Articles
- Publish Date:
- Views: 1655
Linux is the free, open-source alternative to Microsoft Windows and Mac. The operating system itself can be chopped down entirely to a raw text console, or it can utilize a desktop environment such as Gnome or KDE. This guide will explain how to assign a static IP address on a Linux system through the use of a text console. Although this can usually be accomplished within the system settings of a window manager, different window managers may differ in this process; therefore, by using a simple step-by-step console guide, someone using almost any distribution of Linux can follow this guide. You can assign an IP Address on a Linux Computer, follow the simple steps below.
Switching to root
If you are not already logged in as 'root' (the Linux counterpart of 'Administrator'), open a console program and type 'su' (without quote) and press enter.
Note: *Ubuntu Linux distributions usually have the root password the same as the account created when the operating system was installed.
Enter your root password when prompted, and press enter.
Make a backup of your /etc/network/interfaces file by typing the following in the console: 'cp /etc/network/interfaces /etc/network/interfaces.backup'
Type 'vi /etc/network/interfaces' and press enter. Press 'i' to enter into insert (editing) mode.
Scroll down until you find your network interface card in the file (usually named eth0 for an ethernet connection, or wlan0 or wifi0 for a wifi connection).
Change 'iface eth0 inet dhcp' to 'iface eth0 inet static'.
Add the following lines, substituting the IP address numbers with your desired configuration:
- address 192.168.0.10
- netmask 255.255.255.0
- network 192.168.0.0
- broadcast 192.168.0.255
- gateway 192.168.0.1
- dns-nameservers 22.214.171.124
Save and exit from the file by pressing Escape (to enter vi command mode), then ":wq" and Enter
Type 'ifdown eth0' and press enter.
Type 'ifup eth0' and press enter.
Red Hat or Slackware
The easiest method in Red Hat or Slackware is to type 'netconfig' in a console as the root user. A text-based menu will guide you through its configuration settings.
Use tab to move between fields. Use the spacebar to uncheck or check checkboxes.
When you have input the desired settings, press OK.
To make these settings take effect, type 'service network restart' in the console and press enter (this step is not necessary under Slackware, where the changes are immediate).
Note: There are numerous distributions based on Red Hat Linux (Fedora Core, CentOS, White Box, et cetera); many of them are likely to avail one the same method.
Any Linux system with kernel version 2.4 or newer
This method is slightly more difficult, it involves using the console, but it should work on all modern linux distributions. The tool used is called "ip" and is usually located in the "/sbin/" directory.
First you need to determine the name of the network interface to be used. To list all network interfaces available run "/sbin/ip link". This should print a list of interface names, mac addresses and other info.
Next you assign the IP address using the "addr" subcommand, like this: "/sbin/ip addr add 192.168.0.10/24 dev [INTERFACE_NAME]".
The default gateway is added with the "route" subcommand, like this: "/sbin/ip route add default via [GATEWAY_ADDRESS]".
The last thing is bringing the interface up with the "link" subcommand: "/sbin/ip link set [INTERFACE_NAME] up".
Assuming all given data was correct, network should be up and running. Unfortunately all these settings will be lost after a system restart, so if you want to restore them after startup, append all the above console commands to "/etc/rc.local" - a shell script which is run when the system is initialized.
Dynamic IP Address (DHCP)
Dynamic addresses are obtained automatically and usually do not require any attention from the user. However there are several specific cases:
If the network has not been available all the time while the system was starting, the internet connection may stay disabled even after fixing the network problems. To revive it instantly, run dhclient as root. This will set the dynamic address.
The previous situation may happen if the network is accessed by separate hardware that starts at the same time as your machine. In some cases Linux boots faster than the network router and finds no network at startup. Find dhclient.conf (for instance, /etc/dhcp3/dhclient.cof for Debian) and add/correct the line reboot nn;, where nn is the delay for which the system must wait after booting, for the router to start.
The /etc/resolv.conf file may need to be edited if you can access systems by IP address, but not by domain name. Use the same method as the /etc/network/interfaces file. Don't forget to back up the original copy!
Make a backup copy of the files that you change, so you can revert back or compare versions.
Don't attempt unless you know how networks, IP addresses, and DNS servers work.
Do not stay logged in as root. It is more secure to log in as another user, use the 'su' (switch user) command in the console, and switch back when you are finished. Root access allows anyone with access to it to do anything to your system.
The 'sudo' command (short for superuser do) allows temporary root (system administrator) privileges and is preferable to 'su' in many cases. Read the sudo man page and be glad you did.
Don't forget to make backups! Failing to do so could render you unable to restore previous settings.