How to control Ubuntu from the command line

Ashton Mills
31 March 2011, 4:39 PM


If the GUI just isn't doing it for you, why not take back control with some of these command-line gems? Our Linux guru Ashton Mills walks you through these powerful commands.


Even a master at Linux desktop-fu can find using the command line difficult. But beyond the basic commands to navigate and manage your system there are, like arcane spells, special incantations that provide new functionality, save you time, or which are just plain interesting. Here's a small selection of command-line gems, with a focus on the desktop and Ubuntu.

Sudo and packages

sudo -s
You've probably read commands to run as root many a time from magazines, and this requires using sudo in front of every command. Alternatively, if you're expecting to run many commands, run sudo -s and you'll be logged in as root.

sudo !!
And for those times you've run a command and then been told you need root privileges, there's no need to type it out again with sudo in front, just run sudo !! and the last command will be repeated with root privileges.

history
An easy way to see past commands is just to run history. You can then select with the mouse and middle-click to paste. Alternatively, combine with grep to search for a command, for example: history |grep make.

apt-add-repository ppa:user/project
Adding new repositories to Ubuntu can be done through the Software Sources dialog, but then you also need to add the appropriate key file to prevent Ubuntu from bugging you about untrusted sources, and you can't copy and paste it. An easier way to add repositories is with the apt-add-repository command and it'll do both for you, for example: sudo add-apt-repository ppa:rye/ubuntuone-extras. You need to know the PPA first of course, which you can browse at https://launchpad.net/ubuntu/+ppas.

dpkg -i
Sometimes you'll find .deb packages built for Ubuntu that aren't in the Ubuntu repositories. Double-clicking on them will install them from the GUI, for the command line use dpkg -i <package>.


You can add new repositories this way, but it's actually quicker at the command line with apt-add-repository.

Process management

ps ax
See the running processes on your system. To look for a particular program, use ps ax |grep program.

pidof program
Alternatively, use pidof to find the process ID (pid) of a program.

kill -9 pid
The kill command ends a single process, but for stubborn ones kill -9 should terminate it. To kill multiple programs at once, or kill one based on name instead of process ID, use killall, for example killall firefox.

xkill
Got a stubborn GUI program that won't close? Run xkill and, when your cursor changes to a target, click on the offending window to zap it and its parent process.

command &
Run a command in the background. Alternatively, you can pause an already running program with Ctrl-Z, returning you to the command prompt. You can then type 'bg' to background it, or 'fg' to bring it back to the foreground.

top
View system resources and processes in real time. Press 'd' to set a quicker delay, and '1' to show load on multiple CPUs. Alternatively, install htop (apt-get install htop) for a prettier, coloured display.


Zap stubborn programs with xkill.

Disk and filesystem

cd ~
Quickest way to change to your home directory.

vdir --color
An alternative to ls -la for browsing files and directories.

lsof
See all the files opened by a process. Use with grep to search by name, or use the -p and -u flags to search by PID and user respectively. You can even show net connections by protocol with -i.  

du -h -x --maxdepth=1
Display disk usage for the current directly and below only, in human-readable format. Add | sort -nr to then sort the result by size.

df -h
Display an overview of free space across all disks in human-readable format.

hdparm -Tt /dev/sda
A basic but quick gauge of hard drive performance.

mount | column -t
Get a clean overview of all mounted filesystems (local, network, USB connected and so on).

Other commands

dmesg
See the kernel log from bootup and any subsequent kernel messages. Use with | less to browse or |grep to search for particular keywords (for example, to see driver messages for hardware).

nano
The default text editor for the command line in Ubuntu. This is very handy for editing config files.

service
Control the starting, stopping and management of services. For example, service samba restart. Run on its own with a service to see available options.

netstat -tupln
See the services listening on your system. Use netstat -tupn to see current connections to your machine.

uname -r
Get your running kernel version. Helpful when checking which kernel is booted.

smbtree
Get a visual overview of shares on a Windows network.

shutdown -h now
Shut down your machine. Alternatively, use reboot to reboot. Sudo or root access assumed.

Tricks

The power of Linux really shines when you combine commands. This is just a sample of some of the more interesting tricks you can do at the command line.

Make a PDF out of a command's manual page (which can be much easier to read): man -t command | ps2pdf - filename.pdf


Make it easier to read PDFs out of manual pages.

Force a disk check at the next boot, rather than waiting for every 30 reboots (sudo -s first): >/forcefsck

The presence of 'forcefsck' in the root of a filesystem will tell Ext2/3/4 to check, the use of '>' creates an empty file. The file will be removed automatically.

Back up your hard disk, to a remote machine, over the network, securely, and compress it on the fly: dd bs=1M if=/dev/sda | gzip | ssh user@remote.PC.IP.Address 'dd of=drive_backup.gz'

Substituting /dev/sda for another disk if necessary.

Check your battery life on the laptop: grep -F capacity: /proc/acpi/battery/BAT0/info

Watch the interrupts on your system in realtime: watch -n.1 'cat /proc/interrupts'

Mount a CD-ROM .ISO image file onto a local directory (be sure to mkdir the directory first): mount -o loop CDROM.ISO /mnt/directory

Shutdown a Windows machine over the network (assumes you have an admin account): net rpc shutdown -I Windows.PC.IP.Address -U username%password

You can substitute 'shutdown' with 'shutdown -r' to reboot it instead.

Download an entire website, including internal links, images and all: wget -r -p -l0 -e robots=off -U mozilla http://www.website.com

Find out your external IP address: curl icanhazip.com

The Matrix in your console: tr -c "[:digit:]" " " < /dev/urandom | dd cbs=$COLUMNS conv=unblock | GREP_COLOR="1;32" grep --color "[^ ]"


Matrix in your terminal. Because you can.

And for the finale, just because you can: watch an ASCII version of Star Wars (yes, the movie!) via telnet: telnet towel.blinkenlights.nl


Post your comment



Comments

RSS feed Email alert

Me In Oz (User):

Why not go back to DOS if you want all scripted commands.
In the 21st century, why would any one (other than programmers) want to dabble in this unnecessary tomfoolery.

31 March 2011, 5:15 PM (1 year ago)report abuse Send to a friend reply

petert (Cornerstone member):

Quoting Me In Oz:
why would any one (other than programmers) want to dabble in this unnecessary tomfoolery.


Maybe they want to do it because they want to do it. You may as well as, "Why climb a mountain when you can use a helicopter?" and any one of myriad other inane questions. If people want to do it, then let them. They're not hurting anyone.

31 March 2011, 6:11 PM (1 year ago)report abuse Send to a friend reply

phar (New user):

A GUI cannot necessarily be equated advanced computing. It has done a great deal to make things accessible to more users in an intuitive way but the ability to control the system from the command line means that these smaller programs can be combined programatically do more useful things without reinventing the wheel.

The last few items are admittedly silly but the rest of the article demonstrates the serious power and usefulness of the Unix philosophy. The magician has revealed some of the magic that goes on behind the GUI.

When you learn about some of this magic you begin to realize that some things can be done way faster than with the GUI. Yes this is also about the freedom to choose either the GUI or the command line. If someone wants to use a GUI for a task I have no problems with that. I often find some things easier to do with a GUI as well. If you are administering many machines you often need this type of "tomfoolery" to get things done efficiently.

DOS was never this good.

31 March 2011, 11:12 PM (1 year ago)report abuse Send to a friend reply

petert (Cornerstone member):

If I understand things correctly, in much of Linux (and possibly other OSes) a GUI of an application effectively sits on time of a command line. That being the case, it implies that everything that an application can do is catered-for in the GUI, which might not always be the case; hence, the need to be able to access the command line. Is that correct?

31 March 2011, 11:42 PM (1 year ago)report abuse Send to a friend reply

phar (New user):

I am not sure if "on top of the command line" the quite the right way to look at it. They are both programs with a particular user interface running on the operating system. Usually each type of program can execute other programs whether they are GUI or command line oriented. The command line is a program you use to execute other programs using text commands.

You are right the GUI has to expose the thing that someone want to do otherwise the command line might be the only way to do whatever it may be. That can often be the case in Linux because of the rich command line history (thank you gnu) but it is much less common with modern distributions where you (most of the time) have the choice to use a GUI or command line to perform a task. These days it is often more of personal preference.

I have to confess, I also often use the command line in Windows (all versions). Sometimes it is quicker/faster/easier. Perhaps that's my DOS heritage showing. But there are many things I prefer to use a GUI for.

A quick note about the "Unix Philosophy". It is key to understanding why there is this command line tendency in Linux and to a significant extent at deeper levels of other operating systems as well. Take a quick peek at the Wikipedia entry (http://en.wikipedia.org/wiki/Unix_philosophy)

01 April 2011, 12:38 AM (1 year ago)report abuse Send to a friend reply

Me In Oz (User):

Quoting phar:
If you are administering many machines you often need this type of "tomfoolery" to get things done efficiently.

So I'll add system administrators to my list of boffins who need this 'tomfoolery' .................. But the other 98% of computer users don't give a toss.




01 April 2011, 10:55 AM (1 year ago)report abuse Send to a friend reply

ss-rotel (Senior Forumologist):

Quoting Me In Oz:
why would any one (other than programmers) want to dabble in this unnecessary tomfoolery


AHAHAHA.... ok... i guess you're obviously a windows GUI only user.

What do you need to do to get a list of all the files on the C: drive using a mouse?
Move mouse to start, click start, click computer, double click on the C: drive, and if you haven't once already, click on the "yes i wanna see the files"
OR
you type "DIR C:"

How many clicks does it take you to get to your network settings, to say, set a static IP? in windows 7/vista?
it's like 5in vista, and 3-4 in win 7.
OR
ncpl.cpl

Move a mouse is slower then typing the command, (Well it IS if you can type 80-100 WPM i guess).

i mean, i use DISKPART, (there's a tut in here that i wrote for a girl that needed help with formating a drive), were you can do exactly the same thing using Diskmanager, but it takes longer, and you dont get ALL the options.

Also, REally... if you dont already know 1/2 of the above commands, you really shouldn't be playing with linux

01 April 2011, 12:51 PM (1 year ago)report abuse Send to a friend reply

anonymous user Anonymous user