Linux Tips and Tricks

1 - I can do better 2 - Jury's out 3 - Pretty darn good 4 - Splendiferous 5 - Awesometastic by 6 people | Log in to rate

Ranked #2,431 in Tech & Geek, #62,315 overall

Linux introduction

Below you will find several tips and tricks I have accumulated over the years which will make your life as a Linux sysadmin hopefully a bit easier. I hope that both experienced aswell as novice Linux users and administrators will find these tips usefull. If you have tips you would like to contribute please send me an email or give me some feedback. I will be calling everything Linux on this page even though Linux is only the kernel and the main programs and utilities are from GNU. So the more accurate name would be a GNU/Linux operating system.


In the link section there are a couple of links to Linux distributions - so if you are new to Linux why not start by downloading it for free today?

Linux filesystem 

Using Linux or Unix based operating systems one of the first differences is a different layout of folders and names. In Windows you have C:\ which marks the partition in which Windows and Programs are installed in the folder "C:\Program Files". Also other drive letters exist like A:\ for the first floppy drive or G: or some other letter for your dvd/cdrom player. In Linux this is different. There are several folders, which can be on one partition or different partitions.



The screenshot above is one I made of Baobap. Its a program which maps your harddrive and gives you information about usage. As you see my /home folder uses the most space. The disk2 is a second harddrive with one partition which I mounted on /disk2. The /usr (see below) is where most programs are installed is only 2,6 G. I have alot of programs installed like openoffice, gimp (a bit like photoshop), games, video players, audio players. So Linux and the programs on Linux do not use up a lot of space. The last time I installed another operating system I needed about the same just for the operating system. Personally I like this because I get to keep alot of space for other stuff like Music or Videos.

Lets look at some common directories:
/

The forward slash is called the root folder and every other subdirectory expands from it. You can mount any directory on this root directory from almost any place. If you work with NFS (Network FileSystem), you can mount a directory from another machine ontop of your filesystem. You won't even notice that you are working on another system. Or you can mount extra storage from another drive.

/home

This is where all the users data is stored on Linux. If you have a user on the system called hank, his home directory would be /home/hank/

/etc

In this folder system configuration files are stored. The
different programs/daemons (daemons are services running on Linux - example an ftp server) have either an separate directory with files or a file where settings are stored.

/var

Storage directory for logs, emails, database data and other data stored by default like crontabs.
ex.. /var/log - storage of all logs, /var/mail etc..

/sbin

Contains system binaries. The essential programs used by administrators to maintain their computer, like hardware configuration programs and maintenance. Examples would be mount, fdisk, mkfs.

/bin

The bin directory contains useful programs for both common user as system adminstrator to work with the system. examples are grep,mkdir,chown,chmod,rmdir and rm. It also contains all the shells like bash. Both the /bin and /sbin directory are essential to the system.

/usr

In this directory there are several sub directories like /usr/bin and /usr/sbin. In these subdirectories applications are installed, like firefox or openoffice.

/lib

Here are all the shared libraries stored. These libraries are needed for essential system stuff - like system32 or system on Windows.

/tmp

This directory contains temporary data. Many programs want to create data to store temporarily. This information can be cleared out on reboot or shutdown. So do not store information here! ;-)

/boot

This contains all you kernels and grub or lilo information. The kernel is basically the program which communicates with your system hardware and other programs.

/dev

Contains all devices on your PC. The interesting thing about all Unix and thus also Linux systems is that everything is a file.

checking diskspace
df -h - checks diskspace.

The dash h means in human format - M for megabytes, G gigabytes etc... If you do not use the dash h , it wil display the numbers in bytes which is not very legible.

Another command for finding out disk usage of directories is du. Try du -sh /home/myusername to find out how much space "myusername" is using.

Gnome and compiz 

Window managers, GUI the cool stuff...

When I first started with Linux it was confusing because it has different Window managers running on top of X windows.

Whats X Windows?

What is a Window manager?

A window manager is basically the graphical system run on top of Linux. A bit like in the past you run dos and then started Windows (Windows 2000 and before). But unlike Windows you can run different graphical systems. The most commonly used systems are KDE and Gnome. KDE has a bit more similar look and feel like Windows (my optinion) while Gnome looks a bit more like a Mac.

Why are there different Window managers and not one like Windows?

Its basically the same why are there different cars? Choice! Some people like their Window manager to be small and simple and use not a lot of recources (ex. Blackbox, Fluxbox etc..), others want other features. Linux is not a one size fits all operating system like Windows. If you have an old computer you will still be able to run Linux. There are even complete Linux systems which you can put on your usb stick and run directly from it!

Compiz features

Below is a picure which I made running Compiz on Gnome. Its the expo function which shows all the open workspaces. I have 5 running. In the first I allways put a terminal, second the browser, third evolution mail client, fourth nautlius (windows explorer but then for Gnome).



The filter function or expose like used by Mac. When I press F12 or flick the mouse pointer to the right hand corner it will show all the open programs side by side. By clicking on it i will go to that program in that specific workspace.



Also check out the youtube videos I posted on the bottom to see Compiz in action.

There are a lot more functions within compiz.

Shell Usage 



Most beginning Linux users will feel daunted by the shell/commandline. I advise beginning users to learn a bit about the commandline, because later on you will recognize its power to quickly manage your Linux machine or manage multiple machines with ease. If you have used Dos before you will learn that doing stuff on the commandline under Linux or any other Unix related Operating System (FreeBSD/Solaris) is much much more powerful than you were used to under Dos. Also if you want to try out other operating systems (Solaris, HP-UX, FreeBSD, OpenBSD) later you will find out alot of similarities and it will speed up your knowledge of them.

Every command you enter at the shell gets logged. When entering the command "history" it wil show you the past command's you have used. In front of each command is a number. For example: "1 cd /home". If you type "!1" or instead of 1 the number you choose the shell will repeat the command you issued before.
Another neat trick is that if you type "!cd" it will append the last command which included cd. Example "cd /home/user".

Cycling through previous issued commands.
This is a huge time saver! Instead of entering the same commands over and over again the bash shell lets you repeat commands previously entered.
Start by pressing "Ctrl" + "r" and enter the part of the command, for example "cd". Now the last command containing "cd" will appear!

If you press "Alt"and "." it will cycle through the last part of the command. Another way of doing this is using the "!$" but then it will only use the last part and will not let you cycle through the commands you have allready entered. Give it a try by doing a "ls /home/username" and then either "cd !$" or "cd Alt+.". If you type `cd -` you will go back to you last position. Ex. /home/me to cd /usr, cd - will let you go back to /home/me!

Another way of switching between two locations is by using the "pushd" command.
By entering pushd you will return to the previous directory. Just enter cd /etc and then enter "pushd". You will jump right back from where you came. I never use pushd anymore since I think "cd -" does the same thing and is faster to type.

If you use !* instead of !$ it will complete everything after say "vi bla bla2%u2033, so everything after vi. Removing characters from a command. For example you have mistyped the command rm blaa instead of rm bla. Press "^a" and it will come back with "rm bla".

Basic commands
cp file /home/user/ - copies file to /home/user
mv file /home/user/ - move a file to /home/user directory
Move can also be used to rename a file or directory. Example: mv file file2 renames file to file2.
env - show user environment variables (in windows its "set")

Linking

You can link to a file or directory
ln -s target linkname - ex. ln -s /home/me/test test2 -> link to /home/me/test and call link link2.
-s is symbolic link - removing the link doesn't remove the origibal file - without -s is hardlink - removing the link will also remove the original file!! Be careful.
ln -sd targetdir linkdirname - make symbolic link to directory

Output redirection

ls > file - pipe output of command ls to file
ls >> file - append output to existing file
ls 2> file - only pipe errors to file of command ls
ls 2>&1 file - output stdout and stderror to file or "ls &> file" or ls > /dev/null 2>&1 - output stderr & stdout to bitbucket.
mail my@mail.com < file - mail command takes input from file and sends it to receiver
wc < file > other_file -> wordcount takes input from file and pipes output to other_file
output can also be piped to screen for example a different console/terminal. try this "cal > /dev/pts/2" this will output a calender in the terminal no. 2. (ssh host "cal > /dev/pts/1" - paste calender on remote machine).
reset - lets you clear the terminal (for ex. when its garbled).

Usage of su and sudo
To change to root you can use the "su" command. You become the user root. BEWARE: root has unlimited access to the system. Certain commands can destroy your system, like rm -rf /

The sudo command is more to get temporary root privileges with certain commands.

sudo command - run command as root - ex. sudo apt-get update

sudo -i - opens a root shell
sudo passwd -l root - disable root passwd (lock)
sudo passwd -u root - unlock root passwd (if passwd was set before locking)
sudo passwd root - enable root passwd
sudo -s - opens a new shell and allows you to work like root without having to use sudo for each command.

dd if=/dev/hda of=bla.iso - make an iso file from your dvd or cd
dd if=/dev/zero of=10MB bs=1024 count=10240 - make 10MB file.
Signals
kill -HUP pid - restart process with processid or kill -HUP `cat /var/run/process.pid`
export GREP_OPTIONS='--color=auto' - shows match in red color
ls /home/{user1,user2} - show content of both user1 and user2 homefir

script filename - lets you record commands till you press Ctrl-D or type exit.

mounting directories and servers

The "mount" command shows you which devices are mounted in Linux. Example:
/dev/sda1 on /boot type ext3 (rw)
The first partition on the first SATA harddrive is mounted on the directory boot. The filesystem is ext3 and it is readable and writable.

If you want to unmount a device which is currently in use you can find out about the processes which are using this device by issuing a "lsof /mountpoint". You can also issue the command lsof -n | grep "mountpoint" to achieve the same result. If a disk refuses to be umounted issue the command umount -l "mountpoint".

mount -o loop iso /mountpoint - mounts the iso at mountpoint (you can check its content)

set noatime,nodiratime in fstab inclreases filesystem performance.
vi /etc/fstab and add these two values after default option.
mount -o remount / to remount filesystem.
The atime,nodiratime keeps a record each time the file or dir was accessed - in most cases this is not necessary.

Usefull commands

find . -type f | xargs tar -zcf files.tar.gz
Find all files in current and subdirectories and compress them all in archive. The xargs argument takes input from the find command and passes all the files through the tar command.

grep -i "fail\|warn\|error" messages
Find all messages which contain fail,warn,error in the messages file. I usually use the "*" to search all the logs for possible errors. Also you could use the zgrep command to search through packed archives.

mv `grep -l "linux" *` dir/
move all the files containing the word linux to folder dir. The quotes are backticks not single quotes and cause the command to be executed.

Use curly braces to execute the commands and then direct output to the file like:
{
ls
pwd
} > file

Networking 

Computer networking is something Linux was born for. Linux has its roots as a server operating system, and is used a lot for various networking services like:


  • Nameservers (e.g. converting ip addresses into domainnames and back)

  • Mailservers (Receiving and sending mail)

  • Webservers (Apache is the biggest webserver application in the world and runs on Linux - also on other Operating Systems)

  • LDAP (Directory services)

  • Routing/Firewall systems

  • Fileserver functionality to both Windows (Samba) and Unix based systems (NFS)

  • Proxy server



Useful commands
ethtool -i eth0 - show network interfect information (also to change settings)
ifconfig - show all network interfaces plus ipaddresses etc..
route -n - show all routes.
route add default gw 192.168.0.1 - add a route to the gateway

Setting networkcard speed
ethtool -s eth0 speed 10|100|1000
ethtool eth0 shows you the capabilities of the card.
sudo netstat -plutn - open listening ports

whois domain.com - get domain info
whois ipaddress - get ipadress ripe information
host www.domain.com - show ip info on www.domain.com. On Ubuntu if you do a host domaim.com it will also show you the mailserver records for this domain if any. If you do host ipadress it will return the corresponding full domainname.

DHCP

dhclient3 -1 eth0 - renew ip adrress on eth0 (change eth0 into whatever your interface is)
dhclient -r eth0 - release ip adress
ifdown eth0 - bring interface eth0 down
ifup eth0 - bring eth0 up - if it is configured as dhcp interface it will automatically restart dhcp settings.

Daemons on Linux 

Services running on Linux

Daemons has nothing to do with the word "demons" but everything with services running on Linux. An good example would be a fileserver service or a mailserver. In this chapter I will tell you something about the basic services running on Linux.

Cron and Atd

Both the cron the atd are daemons for executing commands/programs at certain intervals. The atd daemon is used for single events while the cron daemon is used for repeated events (like updating the locate database). Cron is really useful to automate certain tasks. For example mailserver statistics to be send at a certain time to administrator, clean up tasks etc...

Usage cron.
crontab -e - edit the cronfile from the current user
crontab -l - list the cronfile.

Stanza
There are 5 fields for time and one field for the command that has to run. All fields are seperated by a space.

1. Minute (00 - 59)
2. Hour (00 - 23)
3. Day of the month (01 - 31)
4. Month (01 - 12)
5. Day of the week. (0 - 6) (0 is sunday, or 7)
6. the command or script that needs to be run. (ex /home/me/myscript.sh)

Minute Hour Day of the month Month Day of the week command
00 04 01 * * ls -> this will run every first of the month at 4 in the morning the command ls.

Usage at
atq - view all current commands in queue
at "time" - start new command ex. at 14:00 (start new at 14:00)
afther that enter the command(s) you wish to be executed end with Ctrl-D.

Packages 

Everything about packagemanagement

Most Linux distributions use some sort of package management to handle the installation of new programs as well as the updates of the whole system.

Contrary to most claims that the installation of programs under Linux is hard, its actually really easy with the current package managers to install new software or manage existing software.

With Windows software you have to visit websites for each program installed to see if there are updates or if you are lucky it will notify you with a special program running (Windows update/Logitec/Adobe etc..). This is easier in Linux. For example both Fedora and Ubuntu will notify you for each package installed that there are updates. With a few clicks your whole system will be updated to the latest version!

Explanation of the differences in package systems.

There are three mayor systems:
rpm based - ex. RedHat/SuSE/Fedora/OpenSuse/Mandrake and others.
deb based - ex. Debian/Ubuntu and others.
source based - Gentoo/Slackware package management

For beginning Linux users I would recommend to try out both Fedora and Ubuntu which use the two most used package systems. A good way to try them out would be to use vmware/virtualbox under Windows and create a virtual machine. Or you could install it on a spare machine or spare harddrive so that you can test it and you do not need to worry about your Windows installation. Learn the differences and similarities between package management.

Graphical installation of programs

Updating with the update manager in Ubuntu:
Go to system > administration and select update manager. If there are updates you will usually see an icon in the top right corner mentioning updates. See screenshot of update manager with updates. Click on the thumbnail to view the full size image.



As you can see its only one button you have to click to update the whole system!

Installing new programs with Synaptic.
Screenshot of synaptic. Click on thumbnail to view full size image.


Another great way to install new programs is the add/remove application under Applications.



Shell installation of programs.

RPM based systems
rpm -qa - gives you a complete list of all the installed packages.
rpm -Uvh packages - upgrades an package. The v part in the command means that the output should be verbose.

yum package management
yum is the commandline packagement for fedora and other rpm based systems.
yum update - update all packages in one command

DEB based systems
dpkg --get-selections give you a complete list with all installed (and uninstalled) packages.

# dump packages to list and reinstall from list
dpkg --get-selections > installed_packages.txt

To re-install the packages from the list
# dpkg --set-selections < installed_packages.txt
# apt-get dselect-upgrade

apt-get install packagename - installs packagename
apt-get update - updates package database
apt-get upgrade - upgrades all installed packages
apt-get clean - cleans deb's which were downloaded

Update every package from the commandline in one line in Ubuntu/Debian with sudo:
sudo apt-get update && sudo apt-get upgrade && sudo apt-get clean

apt-file search /usr/bin/vi - searches for file in package even if it uninstalled
dpkg -S /usr/bin/file - searches for file in packages on local system
dpkg -l - list all packages installed/uninstalled on system

removing packages when getting errors..
dpkg --purge --force-all package_name
dpkg --remove --force-all package_name

apt-cache search packagename - search for package in repositories.

Cleaning up your debian based systeem
I use both `deborphan` and `debfoster` to clean up the system of unwanted packages. The deborphan package searches for orphaned packages - packages with no dependencies on others. Like old libraries. Allways check that you are sure that you want to remove these packages. Debfoster also looks for packages which can be removed but it makes a run through all the packages and askes you if you would like to remove these.

You can combine commands to orphaned packages.
sudo apt-get remove --purge $(deborpan)

Fixes 

Solutions for problems encountered

If you run into trouble try one of these fixes below.

If Gnome or another window manager freezes you can use the following keys combo to terminate the window manager and return to the login screen Ctrl+Alt+backspace. Another way to kill the X session is by using Alt-PrintScreen-K.
You can also drop to the shell by using the following combo's Ctrl+Alt+F1 - or any F key till F6). If you use Ctrl-Alt-F7 you will return to your active X session.
You can also use the Alt+left or right arrow keys to switch between consoles. If you have ssh running on the machine you could also try to remotely login onto the machine and then kill the X session or other processes.

Linux can be rebooted using the Ctrl-Alt-Del keys. You can disable this setting if you want to. If you have a really complete freeze of your system and the last combo doesn't help you can try to use the following key combo:

When Linux freezes and Crtl-Alt-Del doesn't work do the following:
While holding down Alt-PrintScreen press: REISUB. (Raising Elephants Is Utterly Boring - or busier spelled backwards).
This works everytime! Use this instead of a hard reset (using the reset button on your pc). I allways avoid using the reset button since it may cause filesystem problems due to the fact that the harddisk is still writing.

Great programs under Linux 

Try out these wonderful programs under Linux

  • Audacious - if you are used to Winamp under Windows you will love Audacious. It has a similar layout and loads of features which you can add with plugins.
  • VLC - videolan player is also available under Linux. It plays everything and just rocks.
  • Transmission - bittorrent client. Small, fast and beautiful.
  • Gringotts - still keeping your passwords in different places and in text files? Store it in Gringotts encrypted. Choose your encryption method and just place all your passwords within this file. With a password you can unlock this file.
  • K3B - Burn cds/dvs etc.. under Linux. Burning cds couldn' t get any easier with K3B. K3B has the best features of any burning program I have found under Linux. If you want something simple and no frills you can also try out Brasero under Gnome.
  • The Gimp - bring out the artist in yourself. The Girp is a great program to edit pictures or do other graphical things.
  • Evolution - outlook like mail program.
  • Inkscape - vector grahics program.
  • OpenOffice.org - like the Windows version. Open and edit Word documents, Excell, Powerpoint, Access databases.
  • Firefox - just like under Windows you can use firefox to surf the web.
  • Pidgin - a messenger. Communicate with msn/yahoo/google messengers.
  • emesene - communicate with msn users.

Getting Help 

1. The man pages. The man command is an abbreviation for "manual". If you enter "man command" it will give you more information about the command and possible switches.

2. Help switch a more short version of the man page.
Or you can type either `--help`or `-h` behind the command to learn more about their operators.

3. Finding related info. Related command's in someway can be found by typing "apropos command".

4. The internet - the biggest library in the world! Just use your favorite search engine and type in the issue you are experiencing. you can also look into newsgroups. there are special groups about certain software (Linux or deamons like the Apache webserver).

5. Irc. enter a channel and ask your question. be polite if people don't answer straightaway.

6. Books - eventhough you can find almost everything on the internet its nice to have some books about Linux. I can really recommend "Running Linux" from O'reilly. It covers a lot to get you started and get to know Linux basics. This was the first book I bought about Linux.

7. Forums - if you are an Ubuntu user join the ubuntuforum.org and start asking questions or answer them. If you use fedora join fedoraforum.org.

Linux is build on contribution. Beginning Linux users will find a huge community dedicated on improving Linux. Why don't you join us make Linux better. Start by joining a forum or usergroup. Report bugs you found or wishes you have.

Starter Tip List 

Tips for starting and other Linux users
  • Install Linux in a virtual machine to try it out! Lots of people are afraid of messing up their system and their settings. With a virtual machine you install a operating system within a file. The O.S. thinks its installed on a proper harddisk and processor but most is just emulated. This way you can run Linux under Windows to try it out. If you crash and burn you can start over again.

    You can download it for free
    http://www.vmware.com
    http://www.virtualbox.org

    Vmware also offers premade virtual machines - just download one and start ubuntu, fedora or any other Linux distribution.
  • Join the forum of your Linux of choice. Each distribution has an own forum and other resources to help you out. You can also join a local user group in your area.

    Linux is all about collaboration.
  • Missing certain software under Linux? With Wine you can install Windows software under Linux. Check the wine database on how well it runs under Linux. http://appdb.winehq.org Another option is visualization. Like point one but then the other way around.
    Have you checked for "alternatives" for your program under Linux?
    You can also write to the programs maker or company and ask if they will port it to Linux.

Great Stuff on Amazon 

Running Linux

Amazon Price: $32.97 (as of 11/23/2009) Buy Now

Essential System Administration, Third Edition

Amazon Price: $34.62 (as of 11/23/2009) Buy Now

sed & awk (2nd Edition)

Amazon Price: $23.07 (as of 11/23/2009) Buy Now

UNIX CD Bookshelf, 3.0

Amazon Price: (as of 11/23/2009) Buy Now

Linux in a Nutshell, 5th Edition

Amazon Price: (as of 11/23/2009) Buy Now

Links 

The best resources

Linux.com
Linux.com features articles, news and tutorials about Linux.
Slashdot - News for nerds, stuff that matters
Slashdot the latest news about Linux and other stuff in the world of IT.
OSNews is Exploring the Future of Computing
OSNews.com informs you about the latest news on a vast range of operating systems, from the well-known mainstream OSes, down to small embedded (but also very interesting technically) ones.
Debian Administration - Debian Administration Resources
Tips for a Debian GNU/Linux System Administrator.
Ubuntu Forums
A help and support forum for Ubuntu Linux.
Fedora Project
Fedora Linux distribution - the community version of RedHat Linux
Ubuntu Home Page | Ubuntu
Ubuntu Linux distribution
OpenSuse Linux
OpenSuse Linux Homepage - the community version of SuSE Linux
Documentation for Ubuntu 8.04
Documentation for Ubuntu
Debian -- The Universal Operating System
Debian GNU/Linux - rock solid. I use it for all my servers and they just keep running.
Xenon Systems
Xenon Systems - provides Linux and Open Source support for small and medium businesses in the Netherlands.

Linux vids 

Seeing is believing

curated content from YouTube

Feedback 

submit

by BookNow

Hello world. This is my bio. I can edit it later! (more)

Explore related pages

Create a Lens!