You are on page 1of 12

Linux tips & tricks

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 as well as novice Linux users
will find these tips useful.

Another great source for tips is here: http://www.squidoo.com/linux-tips-and-tricks

History and the shell


The command “history” will show you the past command’s you have used. It will also show you a
number in front of the command. If you type “!number” it will repeat the command from the past.

If you type “!cd” it will append the last command which included cd. Example “cd /home/user”.

Using “Alt and .”. If you press “Alt + .” 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 already 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!

If you use !* instead of !$ it will complete everything after say “vi bla bla2″, so everything after vi.
Removing characters from a command. Say you have mistyped the command, ex rm blaa instead of rm
bla. Press “^a” and it will come back with “rm bla”.

If you go to directories of want to finish command you type press on the tab key to complete the
command or location. Pressing it multiple times will let you cycle through the commands/directories.

Reverse search – if you type “Ctrl + r” you can type in the first few keystrokes of a command to find it
in your history.

env – show user environment variables (in windows its “set”)


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.

cp source target – copy file from source to target ex. cp test /home/me (copy test to /home/me dir)
mv source target – move or rename file

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 original 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


md5sum -c MD5SUM (MD5SUM is file with checksum)
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
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 kim < 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).

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
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.

http://kerneltrap.org/node/14148

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

host domain (ex. host domain.com) – in newer host program shows MX records (Mail Exchanger
records)

update-alternatives –config editor – set default editor under ubuntu/debian systems.


update-alternatives –config – change all your default settings.
blkid – show all ids for disk partitions
Package management
Most Linux distributions use some sort of packagemanagement tools. Some systems use rpm (ex.
RedHat/Fedora and SuSE) or deb’s (ex. Debian and Ubuntu etc.) or even source packages (ex. Gentoo).

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.

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

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.

Getting help!
Man pages – if you type “man command” it will tell you more about the command and the possible
switches. Or you can type either `–help`or `-h` behind the command to learn more about their
operators.
Related command’s in someway can be found by typing “apropos command”.

Gnome
Switching workspaces – Crtl-Alt-Right/Left of you can use your scrollmouse by pointing the mouse
pointer on the workspace switcher and scroll.

Suddenly no sound after installing/deinstalling programs? I experienced this in Ubuntu – open the
volume control and crank up all the sliders.
rehash – after you installed new software. Users who were allready logged in can use this command to
reread the available commands.

compiz – F12 shows you all the programs that you have opened in one screen. its called “scaled” now
in gutsy.
Adding more view ports – general options > desktop size (tab) > “horizontal Virtual Size”. Use slider to
add viewports.
Deskbar applet – Select text and press Alt-F3 -> deskbar applets searches for text.
Fixing problems – like no audacious window but running process. remove or move .config/audacious.

sudo apt-get install ubuntu-restricted-extras


Playing dvds in ubuntu.
cd /usr/share/doc/libdvdread4/
sudo ./install-css.sh

Gaim
crtl – (arrow up/down) scroll though previous posts…
uncheck http connection method when you get connection errors.

gdmflexiserver –xnest -start a nested xserver


Xnest :1 & – start Xnested on display :1
gdm -display :1 – start gdm in screen :1 (nested X server)

Xgl
Shift backspace restarts the current Xgl session – this is very annoying. Add the following line to
you .Xmodmap:
keycode 22 = Backspace Backspace
This fixed the problem for me.

Kernel

make modules && make modules_install && make bzImage

make && make modules && make modules_install

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).

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

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.

Powertools
find – command to find files. Examples: find . -size +1000k -exec rm -f {} \; (find and remove all files
bigger than 1000 kilobytes recursive in this directory and below)
find . -name ‘*.gz’ – find all files with the .gz extension.
find . -ctime -30 -print | xargs tar -cf backup.tar – find all files which changed in the last 30 days and
add them to backup.tar

find . -ctime -1 -type f | xargs tar -rvf backup.tar – all files changed within 24 hours backup to
backup.tar.

find /tmp/ivan/* -mtime +7 -exec /bin/rm -rf {} \; 2>/dev/null 1>&2 (find all files changes 7 days ago
and remove them -> stout to bitbucket) – better find . -mtime +7 -type f | xargs rm 2>&1 /dev/null –
outputs both stout and stdin to /dev/null
find all directories without permissions 0775 and change permissions in 0775 from current directory.
Without the ! it will search for directories with permissions 0775.

find . -type d ! -perm 0775 -exec chmod 0775 {} \;


if you are not sure test it with -ls to show the files/directories
find . -type d ! -perm 0775 -ls {} \;

find broken symlinks (type l in links is for finding symlinks)


for i in `find . -type l`; do [ -e $i ] || echo $i is broken; done
find -H . -maxdepth 1 -type d (find all directories in current place – do not recurse – no links)

wget -H -r (-r is recursive -H means spans hosts – other wise it will only download from the host you
gave)

grep -v “^$” filename – remove empty lines while grepping


grep ‘^[^#]‘ /etc/apt/sources.list – remove comments and empty lines while using grep
grep “fail\|warn\|error” messages – grep on fail/warn/error messages in message log.

sed -e ’s/oldfile/newfile/’ file – replace all oldfile into newfile in file.


sed -i -e ’s/oldfile/newfile/’ file – replaces all oldfile for new file – command above only changes stout.
sed -e ’s/\edgy/ feisty/g’ -i /etc/apt/sources.list example usage…
or
sed -i s/gutsy/hardy/ /etc/apt/sources.list
cut –d “ “ –f1 /etc/httpd/logs/access_log – only hosts from access_log (apache)

awk ‘{split($0,a,”/”); print a[2]}’ – split all occurences of strings with an “/” in them and print out third
array variable.
ps axu | awk ‘{print $11}’ – print all the programnames that are running..

Calculate totals from number of rows (in this case a ms exchange log file).
awk ‘{total +=$15};END {print “Total: “total”"}’ 20060707.log

scp `find . -ctime -1` host: – copy all changed file within the hour to host via scp.

Loops

for loop – ex: replacing all the all oldfiles*

for file in oldfile.*


do
newname=`echo $file | sed -e ’s/oldfile/newfile/’`
mv $file $newname
done

while loop – ex. renaming all files with an ? in the filename to an filename with a dot in it.

find . -name ‘*\?*’ | while read file


do
file2=`echo $file | sed -s ’s/?/./’`
mv “$file” “$file2″
done

parse a file with on each line a variable


exec < file
while read line
do
echo $line | awk ‘{print $1}’
done

cat file | while read line


do
echo $line
done
search and replace in one file:
sed -e ’s/arg1/arg2/g’ file > file.tmp && mv -f file.tmp file

remove all empty lines and lines with comments with sed (alleen tonen niet daadwerkelijk weghalen)
sed ‘/ *#/d; /^ *$/d’

ls *.pdf | head -1 haal de eerste pdf file op uit he ls commando

rename all *.PNG to *.png.


for i in `ls *.PNG`; do base=`basename $i .PNG`; mv $i $base.png ; done

for i in `ls /home`;


do
if [ $i != "lost+found" ]; then
echo /home/$i/
fi
done

for loop
for ((i=0;i<10;i++)); do
echo $i
done

for i in $(seq 1 100) ; do echo $i > /mnt/nbd/$i; done

instead of using:
ls >> file
pwd >> file

use curly braces to execute the commands and then direct output to the file like:
{
ls
pwd
} > file
mv `grep -l “bla” *` dir/

bash sub shells


( ls ; w ; ls ) & – executes stuff in a new sub shell in the background.

which commands do you use most on the commandline?


history|awk ‘{a[$2]++ } END{for(i in a){print a[i] ” ” i}}’|sort -rn|head

longest word typed with left hand :p


grep -i ‘^[qwertasdfgzxcvb]\{12,\}$’ /usr/share/dict/words

Remote synchronization with rsync


rsync -arLuvz /home/user/ pc2:/home/user

The -L switch tells rsync to treat your symlinks as though they were real directories.

Recovery In case of harddisk failure:

fsck.ext3 -b 32768 -y /dev/hdb5


mke2fs -n /dev/hdb5 — shows details filesystem (and superblocks which can be checked with fsck
option -b)
If you cannot read the hardrive at all you can try mke2fs -S . This option writes superblocks and group
descriptors to the partition. Gpart can also be handy to find lost partitions. Read this article on
Linux.com ( http://enterprise.linux.com/enterprise/06/10/11/1911211.shtml?tid=47&tid=89 ).
touch /forcefsck – forces fsck to check root partition on next boot (it will remove this file if sucessful)

Mounting LVM volumes


load modules for lvm first

incase it doent exist like with the gutsy live cdrom – just install apt-get install lvm2 (make sure you
configure your network).
modprobe lvm
modprobe dm-mod

#change the volumes that exist to active


vgchange -ay
#mount the logical partition
mkdir -p /mnt/VolGroup00/LogVol00
mount /dev/VolGroup00/LogVol00 /mnt/VolGroup00/LogVol00

it could be that your lvm volume name is different so change VolGroup00 into whatever it is…
/dev/volumename.

HEAD http://www.google.nl – sends information to webserver and returns webserver information.


runlevels

whois domain.com – get domain info


whois ipaddress – get ipadress ripe information

To change items in the different runlevels you can use these utilities:
chkconfig – Redhat systems
update-rc.d – Debian based systems
service stop apache2 – stop service/daemon apache2
service start apache2 – start apache2
service restart apache2 – restart daemon apache2

See also:
enable services
or
services debian

email
setting up vacation

step 1: create message in “.vacation.msg” in home dir user. example:


From: user@domain.com
Subject: on holiday

Hi!

I’m on vacation from January 1st until the 1st of March 2007.

Regards,

User

step 2: create “.forward” file


“|/usr/bin/vacation -a user@domain.com -a user@domain2.com user”
user
(user is the username and user@domain.com is your email adress and “-a” is the alias email adress)

step 3: initialise for test


vacation -I

step 4: finishing
vacation -I

Turning vacation off is easy just rename the .forward file to .forward.unused.

spamassassin
spamassassin -t < email.txt – test email on spam (for debugging purposes)

Another great anti spam software is bogofilter


merging multile wordlists:
1. dump wordlist.db to text file
bogoutil -d first.db > first.txt etc..
2. merge files and dump to new database without the totals
cat first.txt second.txt | grep -v ‘^\.WORDLIST_VERSION’ | bogoutil -l wordlist.db

vsftpd
vsftpd is a very secure ftp daemon ( http://vsftpd.beasts.org ). How to allow only certain local users to
log in to ftp.

Add these lines to your vsftpd.conf


userlist_enable=YES
userlist_file=/etc/vsftpd.userlist
userlist_deny=NO

Add one user per line in the userlist file.

If you install it for the first time under etch it will allow for anonymous login. Change thefollowing
values:
anonymous_enable=NO # disable anonymous access
local_enable=YES # enable local user access

LDAP
making ldap listening on a certain ip adress. Change the line in /etc/defaults/slapd
SLAPD_SERVICES=”ldap://10.10.10.10:389/ ” -> example.

Samba

smbclient //server/user – log into server as user


nmblookup server – do a netbios lookup for a (samba) host – returns ip address add -S to get the status
of the node

testparm -s – test samba configurationfile


smbtree – same as “network neighborhood” in windows.
mounting samba with cifs
mount -t cifs -o username=username,password=password,_netdev //192.168.44.100/share_name
/path_to/mount_point
In fstab for automatic mounting
//192.168.44.100/share_name /path_to/mount_point cifs
username=server_user,password=server_password,_netdev,uid=client_username,gid=users 0 0

//192.168.44.100/share_name /path_to/mount_point cifs


credentials=/path_to/.creds,_netdev,uid=client_username,gid=users 0 0

In case you get error 6, permission denied. Check if the user is part of the group and the proper
permissions are set on the base directory. Change to 775.

MySQL
my.cnf/ini – if you have problems with insering special characters in MySQL via php – turn off the
option “default-character-set=utf-8″.
max_allowed_packet in my.ini – set this to 32M so that the import doesn’t fail.

SSH
setting up vnc connection via ssh.
create ssh tunnel to remote machine
ssh -f -N -L 5901:localhost:5901 -X username@remotebox.example.com
vncviewer localhost:5901 (open vnc connection)

Tunnel to remote website on ibnside of network – connect to host 192.168.1.100 on port 80. IN
browser type localhost:8080.
ssh -N -f -L 8080:192.168.1.100:80 remoteip

Reverse tunnel (from destinationhost)


ssh -R remoteport:localhost:5901 mycomputer
vncviewer localhost:remoteport will open vncviewer on remote host

ex. ssh – also interesting if firewall blocks certain ports on remote host.

ssh -R 123:localhost:22 mypc


or ssh -R -N 123:localhost:22 mypc
ssh -p 123 localhost – will open ssh session to remote pc iniated earlier with tunnel

opening remote X program


ssh -X remoteserver
after that execute “program” ex. firefox

usage of screen
All commands start with CTRL-A, then another key for the command itself. If you really want to send
a CTRL-A to your application (Like to go to the beginning of the current line in bash for example, hit
CTRL-A twice.)

CTRL-A CTRL-D Detach your current session


“screen -rd” to get back to it

CTRL-A CTRL-C create another “window”


CTRL-A CTRL-N next window
CTRL-A CTRL-P previous window
CTRL-A ” see list of current windows
CTRL-A [ Copy mode… you can see the scrollback buffer with this. Esc to exit
CTRL-A ? Help for further stuff.
CTRL-A 1 jump to first screen CTRL-A 2 (2nd screen etc)

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.

Hardware

The smartmontools is a program to test your harddrives. Below are some examples to test your
harddrive for possible errors:
smartctl -t short /dev/hda – short online test while your system is running.
smartctl -t long /dev/hda – longer better test of your harddrive (you can do this monthly and the above
weekly to test your harddrive integrity).
To view the results enter the following command:
smartctl -l selftest /dev/hda

show only errors on selftest


/usr/sbin/smartctl -q errorsonly -H -l selftest -l error /dev/sdb -d ata

lspci – show pci devices


lshw – show all hardware
lsusb – show all usb devices
lsmod – show all loaded kernel modules
ethtool -i eth0 – show network interfect information (also to change settings)
ifconfig – show all network interfaces plus ipaddresses etc..
dmidecode – show hardwareinformation
lsb_release -a – show distribution information e.g. RedHat/Ubuntu and versionnumber.
all info can also be found in the proc directory eg. cat /proc/cpuinfo (meminfo) etc..

Enable SMART on sata disk


smartctl -s on -d ata /dev/sda

Virtualbox – install guest additions to improve performance. Press Hostkey-f if you are in full screen
mode and use the menu Devices > install guest additions to download and install the latest guest
additions.
Fixes

CTRL-SHIFT-NUMLOCK – incase numpad doesn’t work in Gnome.

http://en.wikibooks.org/wiki/Linux_Guide/Freezes

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).

Kill X
Ctrl-Alt-Backspace
Alt+SysRq+K – if keyboard doesn’t respond.

If remote reboot doesn’t work due to failing harddrive you can reboot through the proc system. If you
enabled CONFIG_MAGIC_SYSRQ with the kernel compilation.

echo 1 > /proc/sys/kernel/sysrq

When you are ready to reboot the machine simply run the following:

echo b > /proc/sysrq-trigger

This does not attempt to unmount or sync filesystems, so it should only be used when absolutely
necessary, but if your drive is already failing then that may not be a concern.

Also, instead of echoing into /proc/sys/kernel/sysrq each time you can activate the magic SysRq key at
system boot time using sysctl, where supported:

echo “kernel.sysrq = 1″ >> /etc/sysctl.conf

If you have tips which are not mentioned here and worth mentioning please drop me a email at kim at
daudretsch.com.

Hacks
wget -O – -q http://ip.boa.nu/ – get your external ip

You might also like