Linux

Use the command line to gather your networking information in Linux

Jack Wallen takes a break from his usual rants and raves to offer up some new-to-Linux-user help. This time around it's all about finding networking information on your Linux box. Learn how to find your IP address, hostname, DNS addresses, and gateway address here.

I have been asked, by numerous readers, to intersperse my regular rants and raves with some beginner technical posts. So from this point on you will find the occasional article targeted for new users -- this one being one of them.

You're new to Linux. You've installed a distribution that seems to be working out just perfectly for you. But you've come up against a small hitch - you need to know your networking information. For whatever reason you need to know your IP Address, your hostname, your DNS addresses, and your gateway address. How do you find this information? You could go to your desktop environments' networking GUI tool, but that would depend upon the distribution and the desktop. So let's take a more universal approach to this task - the command line.

Although many newbies fear the command line, the tasks you will see below are a very simple way of getting to know both the command line AND your networking configuration.

IP address

The first bit of information we want to find out is the IP address of our machine. If you've used Windows enough, you know the command ipconfig will give you this bit of information (and more). For Linux, the command is ifconfig. If you issue the command without arguments you will see listed all addresses associated with all networking interfaces. A typical entry will look like:

eth0 Link encap:Ethernet  HWaddr 00:30:1b:81:d3:f7

inet addr:192.168.1.108  Bcast:192.168.1.255  Mask:255.255.255.0

inet6 addr: fe80::230:1bff:fe81:d3f7/64 Scope:Link

UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

RX packets:1610700 errors:0 dropped:0 overruns:0 frame:0

TX packets:1185599 errors:5 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:1790159713 (1.7 GB)  TX bytes:160110456 (160.1 MB)

Interrupt:32 Base address:0x2000

As you can see, the address you are looking for is the inet addr and, in this case, would be 192.168.1.108.

Hostname

The next piece of information is the hostname. If you notice in your terminal window (the window you are running your commands in) you will see that your bash prompt looks like:

jlwallen@ubuntu:~$

You can see a portion of the hostname there (in this case, "ubuntu"). To get the full hostname you would issue the command hostname. On my machine it's ubuntu.wallen.local.

DNS information

To find your current DNS addresses, issue the command less /etc/resolv.conf which will reveal something like:

# Generated by NetworkManager

nameserver 74.128.19.102

nameserver 74.128.17.114

Gateway

Figure A

Now we come to the gateway address. The command used to find your gateway, netstat, is a very powerful command that can print network connections, routing tables, interface statistics, NAT connections, and multicast memberships. For the purposes of this article we are going to issue the command using the n and r switches like so:

netstat -nr
Which will output something like you see in Figure A. As you can see there are basically three addresses listed but only one address has an associated gateway. That solo gateway address listed in the gateway address for your machine.

Wrap it up

There you go. You have now used the command line and done so to gather up your basic networking information for your new Linux box. Pretty simple stuff eh? We'll continue on with new-user-friendly articles now and then and eventually those new users will be drilling down deeper and deeper into the Linux operating system.

About

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website getjackd.net.

17 comments
eric.van.rheenen
eric.van.rheenen

I think this is usefull info for a new administrator. However i mis the 'ethtool' command. Output is like this: # ethtool eth0 Settings for eth0: Supported ports: [ TP ] Supported link modes: 10baseT/Half 10baseT/Full 100baseT/Half 100baseT/Full 1000baseT/Full Supports auto-negotiation: Yes Advertised link modes: 10baseT/Half 10baseT/Full 100baseT/Half 100baseT/Full 1000baseT/Full Advertised auto-negotiation: Yes Speed: 1000Mb/s Duplex: Full Port: Twisted Pair PHYAD: 1 Transceiver: internal Auto-negotiation: on Supports Wake-on: g Wake-on: g Link detected: yes

Juanita Marquez
Juanita Marquez

for dropping crumbs for us Linux n00bs. I look forward to more posts like this.

pgit
pgit

For whatever reason you need to know your IP Address, your hostname, your DNS addresses, and your gateway address. I'm at a loss as to what a newbie would come up against such that they need this network info. Probably get more eyes on an article like this if you posed the situation. Newbies are going to find themselves in situations, looking for answers. For instance a lot of people jump into various forums complaining they have no internet access. They have an IP, they can ping other hosts on their LAN, but no browser will bring up anything off the net. Now there's where this data would come in handy. Most often it's a DNS problem. Could be proxy related. Could even be IPv6 gumming up the works. Another scenario; where's my windows share? I'm on the internet but I can't see my windows multimedia server. First thing is look at the IPs of the hosts and try to access one another with them rather than host or netbios name... Getting OpenDNS set up... Getting your shiny new Linux laptop integrated with various LAN segments at work... Real world reasons for knowing something. Works every time. ;) EDITED: I see quote tags don't work here.

daboochmeister
daboochmeister

Jack, would it make sense to show the equivalent way to get the same info via the system applets/GUIs? That would have two benefits -- it would help the new users who will never really use the command line, but do want a technical understanding; and it'll avoid snarky comments from people who don't seem willing to understand that having multiple ways to do things is good, and the command line is an efficient alternative. Understood that would be a challenge, because the different desktop environments use slightly different GUIs (just like Windows and MacOS use different GUIs, and didn't choose to standardize). You could probably get away with just providing a list of links for the major desktop envs/distros.

Justin James
Justin James

From the "10 Things" article (http://blogs.techrepublic.com.com/10things/?p=1861) recently: " I challenge Linux users to see how long they can perform their day-to-day tasks without the command line. You will be surprised at how long you can go. Oh sure, the command line is much more efficient than its GUI alternatives. But if you don?t want to use the multitude of commands Linux offers, you don?t HAVE to use them. If you can?t go an entire release cycle of a distribution without having to use the command line, you haven?t explored the GUI much. Get out! Explore. Get to know your surroundings." From this article: "How do you find this information? You could go to your desktop environments? networking GUI tool, but that would depend upon the distribution and the desktop. So let?s take a more universal approach to this task - the command line." The second quote would lead me to think that learning the CLI is a lot more important than the first makes it sounds like. J.Ja

rbees
rbees

it doesn't read the data off my broadcom wireless card. I have to use ndiswrapper for it. That may be why

Jellimonsta
Jellimonsta

I find ethtool is a good *nix tool. It is a shame Windows drivers for NIC are not uniform so as to make functionality like this possible. :(

htaylor
htaylor

It was a great informative post. I also appreciate how you contrasted the windows command equivalent with the Linux command. A person mentioned earlier why would a new Linux user need that information? Just because we are new to Linux, does mean that we are new to computers. We would need that information for the same reason we do in Windows, MacOs, etc: setting up network shares, forwarding services, checking to insure our router is handing out IP addressed properly, to insure that DNS is working, etc et al

Madsmaddad
Madsmaddad

And different Distributions have different software tools for doing things, And even those software tools can change with upgrades. So we cannot have a convenient chart showing the CLI command against the GUI program. Comment 2- The hardest part of getting windows shares to work from linux for me has been configuring Comodo Firewall on the windows box.

JackOfAllTech
JackOfAllTech

In all the years I've monitored Midrange systems, I've never seen a server with a GUI. Using Telnet or PuTTY gets me to the CLI where I can do anything as fast as I can type on hardware that would make a GUI crawl.

CharlieSpencer
CharlieSpencer

I don't think the post describes a 'day-to-day' task, at least not for a newbie user who isn't trying to learn tech support.

pgit
pgit

You are explicitly naming the device, eg ethtool -i wlan0? Works for me.

tim.stephens
tim.stephens

I think we may have lost the trees for the forest...Once upon a time when hardware was scarce and expensive the cmd line was king. Now hardware is cheap and powerful. This means we can have GUI. Both have their place and it is nice to be able to use both. eg create a single user from the cmd line compared to the GUI...conversly check a networking issue using the GUI or run netstat. I rest my case.

CharlieSpencer
CharlieSpencer

administered by an untrained newbie. At least, not one in production.

Justin James
Justin James

If you look at the intro to this article, that's who it is aimed at, someone who just put their first Linux desktop together. Personally, I agree on the server side of things, but it's not like that anymore in Linux land. The last few Linux installs I did had a GUI by default, and I had to search to figure out how to change that. :( One reason I stick to FreeBSD for my non-Windows servers! J.Ja

Justin James
Justin James

... "You?re new to Linux. You?ve installed a distribution that seems to be working out just perfectly for you." I agree 100% with you, but the intro to the article says it's aimed at the new Linux user which made it a headscratcher for me in light of the article from a few weeks ago. The funny thing is, this is a task which is almost always better done via CLI anyways, even in Windows. It's faster and gives more details, usually. J.Ja

Madsmaddad
Madsmaddad

Isn't it CTRL/ALT/F2 through F6 to get a non-gui login session? Depending on the Distribution F1 or F7 (and I may be wrong on that, but I'm on XP at the mo.) gives you the GUI session

Editor's Picks