You may consider this article as somewhat of a "part two" of the Command line programs for everyday use in linux article I wrote a few days ago. It's all about going step-by-step to get you, the user, proficient at the command-line and become envy material for your friends. The distribution chosen for this is Ubuntu, but these commands that are about to be exposed will work on any other Linux system you might encounter, and you will be warned when there are exceptions. What you will get is a how-to about how to accomplish various tasks using the command-line. And one of the advantages is that you can use these commands regardless of desktop environment or lack thereof. You are only required to have a minimal Linux knowledge base for this article, so get to your terminals and let's start.
The reasons you might want to go the command-line way can be coercion (your graphics driver started driver decided to stop working all of a sudden) or, better, because you don't want to rely on the distro-specific tools Ubuntu offers. Or you don't have a GUI at all because you want to install Ubuntu server and ... GUIs and servers don't mix that well. You don't want to be in a situation when you're deprived of the graphical UI and you start panicking because you have no idea how to do anything at the command line. This article is here to help you.
Configuring wired and wireless networking
In my experience, that's one of the most common scenarios when the new user starts sweating in front of a terminal: you have to start the system and realize that you have no Internet connection configured. What to do and where to start? The command you're looking for is ifconfig, and of course I recommend reading that manual page. But what you'll read here should suffice to get up and running, unless you have some exotic string-and-tin-can way of connecting to the outside world. First let's see if your network card (we will start with wired networking) is recognized by the system:
# ifconfig -a
You will see at least the lo interface, which stands for local, but you will have to see an ethx entry in order to be able to connect to a wired network. If you don't chances are your Ethernet card isn't supported (yet!) by the Linux kernel or that it needs some kind of firmware to work. Since you're a conscious computer user, you know what kind of card you have, so a Google search like "$card_type Ubuntu Oneiric 11.10" or similar will yield results that will help you know if that card of yours has a chance or not. If not, you can get a USB-connected network card (especially good for laptops) or a PCI-connected one (for desktops/servers/workstations). Now that you have a working Ethernet card, let's see what kind of connection you have and make it work already. Contact your ISP/network administrator/friendly neighbor to see what kind of connection to the Internet you have. We will talk about the most used scenarios.
If you have a DHCP connection (very common nowadays) then you're set sooner than you think. DHCP stands for Dynamic Host Control Protocol and basically means that you get an IP automatically. If you're using Network Manager, which you can check with
nmcli nm, you don't have to do anything. So for the rest of this article we recommend you forget about NM because we're gonna talk about the "old-school" way. The command you need for DHCP in Ubuntu is
# dhclient ethx, but keep in mind that other distros use dhcpcd instead of dhclient. Now when you type the ifconfig command above you should see the ethx interface as being UP and having an IP. You can always test your connectivity by using ping with a remote machine, but I guess you already knew that. To make the changes permanent, edit /etc/network/interfaces (this is Ubuntu/Debian-specific) and make it look like this:
# The loopback network interface
# Do not remove!
auto lo iface lo inet loopback # The primary network interface allow-hotplug eth0 iface eth0 inet dhcp
Now that you have a basic knowledge of network configuration on Linux, here's how to do it when you have fixed IP connection. Here you will use ifconfig, and the command will be like this (again, remember to contact your network provider to get the IP, broadcast and gateway):
# ifconfig eth0 10.0.0.100 netmask 255.255.255.0 # route add default gw 10.0.0.1 eth0
The first line sets eth0's address (eth0 is the first wired Ethernet interface) at 10.0.0.100 with a netmask of 255.255.255.0, and the second sets the default gateway to be 10.0.0.1 . But in the case of fixed IP, you also need to specify the DNS server, which usually isn't necessary when working with DHCP. For that, just open /etc/resolv.conf and add a line like
nameserver x.x.x.x # if there's something wrong with your provider's DNS server, just # use 188.8.131.52, Google's free DNS
To make your changes permanent, go again to /etc/network/interfaces and replace the eth0 section with something like
auto eth0 iface eth0 inet static address 10.0.0.100 network 10.0.0.0 netmask 255.255.255.0 broadcast 10.0.0.255 gateway 10.0.0.1
If you have a PPPoE (Peer-to-Peer Protocol over Ethernet) connection, like many ISPs in the US (and not only) offer (usually if you are at home and you have an ADSL modem installed, you are using PPPoE), Debian-derived distros, so Ubuntu also, offer a simple application called pppoeconf, which will scan for an access concentrator and ask you about your username/password your ISP gave you, and that's the whole deal. Nonetheless, many providers make it a mess for users to configure ADSL, especially on "alternative" operating systems, so if you have problems, I recommend you give them a call.
Things are a little simpler when it comes to wireless networking, with a few gotchas. First, we will assume again that you don't use Network Manager, which usually does a good job in detecting wireless networks to connect to. Second, especially if you own a laptop, use the command above to see if your card is supported. In the world of Wi-Fi cards it's more common for Linux to need some firmware so the card will work. The output of
ifconfig -a should contain a wlan0 interface or similar, so let's take it from there. The equivalent of ifconfig for wired is iwconfig for wireless, but the syntax differs. First, just run iwconfig with no arguments. On my Wi-Fi-less desktop, I see something like this:
lo no wireless extensions. eth0 no wireless extensions.
This is a good way to go if you want to check if your hardware is supported at all. If it is, let's see how to configure it. The output of iwconfig should show you a wireless interface, like ath0 for example (Atheros cards) , and info about the hardware, like ESSID, bitrate, mode, etc. You can scan for wireless routers in range with iwlist:
# iwlist ath0 scan
One example of a successful scan is like this:
ath0 Scan completed : Cell 01 - Address: 00:13:46:1D:BC;0E ESSID:"xxx" Mode: Master Frequency: 2.437 GHz (Channel 6) Quality=49/94 Signal level=-46 dBm Noise level=-95 dBm Encryption key:on Bit Rate:1 Mb/s Bit Rate:2 Mb/s Bit Rate:5 Mb/s Bit Rate:6 Mb/s Bit Rate:9 Mb/s Bit Rate;11 Mb/s Bit Rate;12 Mb/s Bit Rate;18 Mb/s Bit Rate;24 Mb/s Bit Rate;36 Mb/s Bit Rate;48 Mb/s Bit Rate;54 Mb/s Extra bcn_int=100
One thing that must be remembered here is that you can try connecting directly via dhclient, supplying ath0 (or the name of your wireless interface) as an argument. If the scan finds more than one router, use iwconfig (and again, the manual does wonders):
# iwconfig ath0 essid xxx mode $mode key $key
In the world of wireless you can usually forget about fixed IP problems. From that you can easily infer how to configure DHCP with /etc/network/interfaces and your wireless card.
Restarting your network
There will be moments, especially after you changed something in your network config, when you will need to restart the network, meaning you will reinitialize the hardware and apply the new settings. This can be done in two ways: if you are NOT using Network Manager, as in our examples above, just type
# /etc/init.d networking restart
If you are using Network Manager, the command is
# /etc/init.d/network-manager restart
Most of the Linux distributions (some notable exceptions being Gentoo and Arch) have switched to using Grub2 by default, Ubuntu included. However, the default settings aren't the same among Grub2-using distros, so I'll give you some tips and tricks that might help configuring your bootloader to your taste. One of the things I didn't like is the fact that Ubuntu hides the menu by default, unless you're dual booting. The file responsible for Grub2 settings is /etc/default/grub, and the line is
GRUB_HIDDEN_TIMEOUT=0. Simply commenting it will attain the desired goal, as long as you remember to do a
update-grub after any change. Another way to accomplish this is to change the 0 to a positive value that represents seconds, so a splash image will be displayed (no menu though). In that time interval, pressing any key will give you the menu. If the value is empty, the menu will be displayed for a number of seconds equal to the value of GRUB_TIMEOUT. If you want to pass options to the kernel, alter GRUB_CMDLINE_LINUX. Remember that this will be affecting the recovery mode as well. If you need to pass options only to the normal mode line, use GRUB_CMDLINE_LINUX_DEFAULT. Speaking of recovery mode, if you want to disable the display of a recovery mode line for every kernel, thus halving the number of kernel lines Grub2 displays, there is a boolean option, e.g. takes only true or false values, use
Teaser and conclusion
There will be a second part to this article soon, because we have other goodies for you Ubuntu users. In the meantime, don't forget to try, experiment and hack, and tell us what you've come up with. And we hope the results will be more than broken bootloaders, because we know Linux users and especially our readers are smart and always willing to learn more. That's the whole appeal of Linux, isn't it?