How To Assign An IP Address On Linux

How To Assign An IP Address On Linux

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.

Debian/Ubuntu/Kubuntu

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 216.10.119.241

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.

Note

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.

Tags:

Raja Rajan Avatar

Help Us Grow

If you like this post, please share it with your friends.

You are free to copy and redistribute this article in any medium or format, as long as you keep the links in the article or provide a link back to this page.

Subscribe to Newsletter




Privacy Settings

Privacy & Cookie Overview

Our website uses cookies to provide you with the best user experience possible. These cookies are stored in your browser and perform essential functions such as recognizing you when you return to our website, as well as helping us to understand which sections of the website you find most useful and engaging.

To learn more, you can read our Privacy & Cookie Policy or reach out through our Contact form.

Strictly Necessary Cookies

Strictly Necessary Cookies must always be enabled to ensure the proper functioning of this website and to allow us to provide you with excellent service. These cookies are also essential for saving your cookie preferences.

Google Adsense

We use Google AdSense to keep this site free by displaying relevant ads. AdSense requires essential cookies that cannot be disabled, but you can manage other cookies. We respect your privacy and provide options to control non-essential cookies.

For more details on how Google handles your data, visit Google's Data Usage Policy. Please review our Privacy Policy for more information on how we protect your data.

AddToAny

We use AddToAny for social sharing. It doesn’t store cookies, ensuring a privacy-friendly experience. AddToAny complies with GDPR and CCPA by default.

For more, see their Privacy Policy.

OneSignal

We use OneSignal to send notifications to users who opt in. OneSignal complies with GDPR and is certified under the EU-US and Swiss-US Privacy Shield frameworks.

For more, see their Privacy Policy.

3rd Party Cookies

This website utilizes third-party cookies, which can enhance your experience and support our ongoing efforts to improve our services.

Google Analytics

We use Google Analytics to collect anonymous data, such as visitor numbers and popular pages, to improve user experience and site performance. Keeping this cookie enabled helps us refine the site based on visitor activity.

For more information, see Google’s Privacy Policy.

Discover more from Prime Inspiration

Subscribe now to keep reading and get access to the full archive.

Continue reading