Ubuntu / Debian Tips & Tricks

Installing X windows

apt-get install x-window-system-core multi-aterm
dpkg-reconfigure xserver-xorg

Setting up Apache Server

apt-get install apache2 php5 libapache2-mod-php5
a2enmod userdir
echo "<? phpinfo(); ?>" > /home/tmtong/public_html/testphp.php
chmod 755 /home/tmtong/public_html/testphp.php
# go to http://localhost/~tmtong/testphp.php

# To Install SQL Lite
apt-get install sqlite php5-sqlite

#In /etc/php5/apache2/php.ini, add in
extension=sqlite.so

Setting up Dynamic DNS

I use no-ip, although I know there are several alternatives out there. You can simply apply for a free account online, then you do
apt-get install no-ip
# to configure
no-ip -C 
# enter your email, password for no-ip
/etc/init.d/no-ip start

In your /etc/inittab, you will see a line like
id:5:initdefault: 

which tells you what your default run level. In this case, my run level is 5, so make sure you see S20no-ip in your /etc/rc5.d

Setting up Swap File

You can definitely set up a swap partition, but I just don't really like an extra partition on my hardrive, so I use a swap file instead. You first need to create a blank file. How big should the file be depends on how much memory you use. I never use more than 512 MB of virtual memory, and I have 256 MB RAM on my computer, so I usually create a swap file of 512 MB - 256 MB = 256 MB,
# creating a 256 MB swap file
dd if=/dev/zero of=/swapfile bs=1024 count=262144
# format the swap file
mkswap /swapfile
# activate the swap file
swapon /swapfile
# edit /etc/fstab to have
/swapfile swap swap defaults 0 0

Setting up Fstab

/dev/hda1 / reiserfs defaults,noatime,notail 0 0
/dev/cdrom /media/cdrom iso9660,udf noauto,users,exec,ro 0 0
/dev/sda1 /mnt/sda1 auto noauto,user,umask=0007,exec,rw 0 0
/dev/hdb1 /mnt/hdb1 ntfs auto,users,umask=000 0 0

SSH Server

apt-get install ssh

Apparent, ssh is the soul of the computer. If people can get in, you are in deep trouble. You should really spend some time harden your sshd.
# pretty important, people like to bruteforce login a lot!
# login into root by ssh to a user account, then su. 
# That way it requires two passwords to get through
PermitRootLogin no
# To allow X forwarding
X11Forwarding yes
# restart
/etc/init.d/ssh restart
# try it
ssh root@localhost

apt-get install denyhosts
touch /etc/hosts.deny 
touch /etc/hosts.allow

nano /etc/denyhosts.conf 
BLOCK_SERVICE = ALL
DENY_THRESHOLD_INVALID = 1
DENY_THRESHOLD_VALID = 6
nano /etc/ssh/sshd_config
AllowUsers yourusername
MaxAuthTries 2
MaxStartups 3:50:10

Read Asian Font

# To read Simplifed chinese 
apt-get install ttf-arphic-gbsn00lp ttf-arphic-gkai00mp
# To Read Traditional Chinese
apt-get install ttf-arphic-bkai00mp ttf-arphic-bsmi00lp 
# To Read Japanese
apt-get install ttf-sazanami-gothic ttf-sazanami-mincho ttf-mikachan
# To Read Korean 
apt-get install ttf-unfonts ttf-alee
fc-cache -f -v
# Fonts may be rendered correctly in firefox, but for konqueror, you may
# see some traditional chinese words are missing. People say this is a
# bug in kde font subsitution
# For Konqueror, I go Settings -> Fonts -> Standard Font -> AR PL KaitiM Big5
# edit /etc/fonts/local.conf
# Rearrange, so that AR PL kaitiM Big5 is the first entry. 

Setting Local Time

# setting timezone
ln -sf /usr/share/zoneinfo/US/Pacific /etc/localtime
# Just set hardware clock to local time, to avoid trouble when you boot to mswindows
# go to /etc/default/rcS, make sure
UTC=no
# if you change it, run
/etc/init.d/hwclock.sh start
# To time sync with a server
apt-get install ntpdate
ntpdate ucla.edu
# run date to see what happen

Changing your MAC address

ifconfig eth0 hw ether 00:50:56:01:02:02

Setting up your Ethernet Card

# for DHCP, in your /etc/network/interfaces
iface eth0 inet dhcp
# run dhclient eth0 afterwards
# for static ip, in your /etc/network/interfaces
iface eth0 inet static
address 192.168.0.160
netmask 255.255.255.0
gateway 192.168.0.1

Profiling your Program

g++ -pg -o blah blah.c
./blah
gprof ./blah gmon.out > report

Read Powerpoint File

# You also need to install the font to see anything in powerpoint
apt-get install wine msttcorefonts
Download Microsoft Powerpoint viewer from Microsoft for Free

Flash Plugin

apt-get install flashplugin-nonfree
update-flashplugin

Java and Java plugin

# no more fakeroot crap
apt-get install sun-java5-jre sun-java5-jdk 

Media Player

# add this to your /etc/apt/sources.list
deb http://apt.mepis.org/6.0/ mepis main
apt-get install mplayer-686 kplayer
# go to http://www2.mplayerhq.hu/MPlayer/releases/codecs/all-something.tar.bz2
# wget http://www2.mplayerhq.hu/MPlayer/releases/codecs/all-20060611.tar.bz2
extract it to /usr/lib/win32
# go to /etc/mplayer/mplayer.conf
zoom = yes
framedrop = yes
# play your video by
kplayer file.avi

That Annoying Locale Problem: 'perl: warning: Falling back to the standard locale ("C").'

It is always pain in the ass. Searching on the web, can't find anything, but some not-so-helpful comment, like dpkg-reconfigure locale ... This is what I do and that works for me ... not sure if it works on yours ...
apt-get install locale localepurge
;; choose only en_US.UTF-8
locale-gen en_US.UTF-8

echo "export LANGUAGE=en_US.UTF-8" >> /etc/bash.bashrc
echo "export LC_ALL=en_US.UTF-8" >> /etc/bash.bashrc
echo "export LC_CTYPE=en_US.UTF-8" >> /etc/bash.bashrc
echo "export LANG=en_US.UTF-8" >> /etc/bash.bashrc
echo "export LC_MESSAGES=en_US.UTF-8" >> /etc/bash.bashrc
echo "export OUTPUT_CHARSET=iso-8859-1" >> /etc/bash.bashrc

bash
;; test it out
apt-get install nano
apt-get remove nano
apt-get install nano

Fixing Annoying Backspace and Delete

Fixing stupid delete key on bash shell can be very annoying, and this is what I do

1) You need to have an idea what your backspace and delete key are. 
You can do it by pressing 
control v, then backspace 
control v, then delete
that will output the character representing your key. 

2) If your .bashrc or .bash_profile has the environment variable 
TERM set, you need to remove it. 
Open up a text editor. If possible, remove a line like
export TERM=xterm # or something else

3) Re-Login
echo $TERM # this will be your term
Why I am doing all these? That's because your environment change 
if you login from different consoles. (ssh from windows, local login, 
login from some sun terminal, etc ...) that's
why your keys sometimes work and sometimes doesn't

4) Let's say you found TERM=vt100, 
key for backspace is ^H, key for delete is ^?.
Open up your .bashrc
if [ "$TERM" == "vt100" ]; then # subsitude vt100 with your TERM
  # BACKSPACE
  # your key could be \C-h \C-? \M-[3~ \C-d
  bind '"\C-h": backward-delete-char' # subsitude \C-h with backspace key
  stty erase ^H # subsitude ^H with your backspace key
  # DEL
  bind '"\C-?": delete-char' # subsitude with your delete key here
fi

5) source ~/.bashrc
6) See if it works. Add more cases to handle different console, if needed

Running Windows Inside Linux

qemu-img create xp.img 10G
;; to install
qemu -boot d -hda ./xp.img -localtime -cdrom ./winxp.iso
;; to boot
qemu -boot c -hda ./xp.img -localtime -user-net

Round Corners in Fluxbox

;; edit you style file (perhaps it is in /usr/share/fluxbox/styles
menu.roundCorners: TopRight TopLeft BottomRight BottomLeft

Changing Resolution 'Dynamically'

Very nice to have in the menu, for example ... Dont need to edit XF86 Config, like I also does

;; look at what your monitor and graphics card capable of
xrandr
;; to change resolution to 1024x768
xrandr -s 1024x768 -r 60
;; to change resoultion to 800x600
xrandr -s 800x600 -r 72

PlayStation Emulator

apt-get install  psemu-input-omnijoy pcsx
cp pcsx.txt pcsx
chmod 666 /dev/input/ts0
;; for your keyboard
;; choose omnijoy
;; use /dev/input/ts0 as your device
;; enable keyboard support
;; click on keybutton on screen,
;; then press the correspond keyboard key
;; for graphics, I use
;; 800x600, 8:Scale2x stretching, 0: No dithering
;; use FPS limit, Autodetect FPS/Fram Skipping limit
;; use Frame Skipping Framerate:200 FPS

Running Windows App Using Wine

apt-get install wine 
;; Set it up, add your cdrom
winecfg
;; Play your StarCraft!
wine drive_c/StarCraft/Starcraft.exe

Mounting A Remote Filesystem Via SSH

I would use this rather than stupid NFS!
# Option 1
apt-get install shfs-source shfs-utils
module-assistant prepare
module-assistant build shfs # it has a progress bar to build! how sophisticated!
module-assistant install shfs
shfsmount tmtong@panther.cs.ucla.edu ./panther # wow this is so cool!

# Option 2
apt-get install sshfs
mknod -m 666 /dev/fuse c 10 229
chmod 4755 /usr/bin/fusermount
chmod o+rw /dev/fuse
sshfs tmtong@panther.cs.ucla.edu: ./panther

Forget Root Password or Taking Over a Machine ...

If your computer is CD Bootable, simply use a live CD and mount the drive, su to the account, and passwd.
mkdir /mnt/hda1
mount /dev/hda1 /mnt/hda1
chroot /mnt/hda1
passwd

Alternatively, if you use grub, press e, at the end of the kernel line add init=/bin/sh. For example
kernel /boot/vmlinuz root=/dev/hda1 ro quiet splash init=/bin/sh
# then do passwd when it is done

Prelinking

When a program starts up, it needs to find its dynamic libraries every single time it runs. What prelinking does is it does it once, write it to the executable, dramatically reduce the start up time for large programs.
apt-get install prelink
prelink -amR
# consider putting it in your cron tab

Crontab / Scheduling

Sometimes, you want to run a program every once and a while. Linux provides crontab for you.
apt-get install cron
# set your editor to whatever you like
export VISUAL=nano
crontab -e 
# minutes (0-59) hour (0-23) dom (1-31) month (1-12) dow (0-6) command
# for example, every sunday 5am it will run prelink
0 5 * * 0 prelink -amR
# exit the program

Killing / Slaugthering Process

Do you ever want to kill a process, but you do not know its process id? Then you look it up with ps ... Kind of teadious ... I have two solutions for you
# Solution 1 with KDE
apt-get install ksysguard
# go to /etc/inittab
# change ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r now to
ca:12345:ctrlaltdel:/usr/bin/ksysguard --showprocesses
# reboot
# you press ctrl + alt + del, graphical tasklist show up, kill whatever you like

# Solution 2 with Bash
In your .bashrc, you put
function stab() {
  kill -9 `ps ax | grep $1 | awk '{print $1}'`
}
# quit your editor
. ~/.bashrc
# if you want to kill konqueror, you do
stab konqueror

IRC / BitchX

Commands are really channel specific, but generally, it is very similar to the following
apt-get install bitchx
# find a irc server on the web and channel

# bitchx username server
bitchx hairyass irc.irchighway.net
# login with your username
# register your username
/msg NickServ REGISTER ircpassword email
# check your email for your confirmation code
/msg NickServ confirm jUxb9ec27
# next time you login, you do 
/msg NickServ IDENTIFY ircpassword
# join channel
/j #channelname
# downloading, for example
/msg Barazoku xdcc send #x
# download status will be list on the lower right corner
# your file will be at $HOME/dcc btw
# to quit
/quit

Firewall with Firehol

This is how to setup a firewall
apt-get install firehol

# Enable all your services and interfaces you need
# run this to automatically generate a config file or a template
firehol-wizard helpme >/etc/firehol/firehol.conf

# look at /etc/firehol/firehol.conf
# take out all the comments, disable anything that you dont need
# this is what I have (a standalone server, with no computer behing me)
# I have ssh, asterisk, http running on my computer
# I allow people on my network to ping, sip, ssh, http me
# I allow people on internet to ssh, http, sip me

interface eth0 interface1 src "128.195.100.0/24"

        server ICMP accept
        server sip accept
        server ssh accept
        server http accept

        client all accept

interface eth0 interface2 src not "${UNROUTABLE_IPS} 128.195.100.0/24"

        policy drop

        # server iax2 accept
        # server ICMP accept
        server sip accept
        server ssh accept
        server http accept

        client all accept

# nano /etc/default/firehol
# change no to yes

# make sure it runs when startup
ln -s ../init.d/firehol S92firehol
# restart servies
/etc/init.d/firehol restart

# go to this web site to port scan your computer

View What Your Firewall Blocks

apt-get install fwanalog
# open your browser as root 
# go to /var/log/fwanalog/today.html

# you will see a beautiful report!

Finding Fastest Apt Mirror

netselect-apt and apt-spy only works on debian, it doesn't work on ubuntu, so this is a script to test mirror speed The bigger the number is, the fastest the mirror is

Enabling MP3 Playerback

apt-get install gstreamer0.10-fluendo-mp3 rhythmbox

Burn CD

# somehow they don't have all the dependency installed
apt-get install k3b cdrecord cdrdao dvd+rw-tools
k3bsetup

Watch VCD

# do not mount the cdrom
# chmod 666 /dev/hdd 666
# for the first track
mplayer vcd://1 -cdrom-device /dev/hdd
# for the second track
mplayer vcd://2 -cdrom-device /dev/hdd

VNC via SSH Tunnel

apt-get install vnc4server
server> vnc4server :0

# If you are using Windows SSH Client, go to Preference, Tunnel Setting
# Set Listen and Dest port as 5900

# If you are using linux client
ssh -X -L 5900:localhost:5900 servername

# On Client Side, fire up vncviewer, use server as
localhost :0

Install Ubuntu From Scratch

No matter what the installer is, I still prefer installing from scratch using debootstrap. That way I can have my own lean customized system with my own packages and configurations. The following are what I like about it.
1) I just don't like to have junk on my system that I never use. I use fluxbox or wmaker, so no default full blown desktop for me.
2) It is faster, since you install less packages, and you can configure and install package simultaneouly. By the time it is done, you have a configured system running
3) Whenever there is a new release, I can ideally use the same old kubuntu live cd. I don't need to download another isos, burn it.
4) It works for me! That's what it matters, don't need to depend on installer
Here is the Makefile. Feel free to customize it.

Using Adesklets

An alternatives to gdesklets, not too many dependencies, but of course, not too many desklets to choose from, compared to gdesklets.
apt-get install adesklets 
# go to http://adesklets.sourceforge.net/desklets.html
# I recommend weather, doityourself, calender
# download a desklet from the web site, extract it, run the python script, control c
# it will register the desklet in your .adesklets, 
# invoke the past desklets in your .adesklets
adesklets &
# look at ~/.adesklets for further configuration

Keeping Track of Software Install

I tried deborphan and debfoster, but that is not really what I want. Here is a simple script to keep track of the apt-get install.
#!/bin/bash
# aptinstall.sh

# Run this script like
# aptinstall.sh nano

# change this line to your own directory
DIR=/home/tmtong/conf 

apt-get --force-yes -y install $@ 2>&1 | tee $DIR/install.tmp
echo "" >> $DIR/install.tmp
echo "=====================================================" >> $DIR/install.tmp
echo "" >> $DIR/install.tmp
echo ""
echo -n "Logging Installation Down ... "
[ ! -f $DIR/install.log ] && touch $DIR/install.log
cat $DIR/install.log >> $DIR/install.tmp
mv $DIR/install.tmp $DIR/install.log
echo "Done"

Setting Up CVS Respository

Everyone knows what cvs is, right?
# Note that path need to be absolute
cvs -d $HOME/.cvs init
# on your local machine
export CVSROOT="/home/username/.cvs
# on other machine
export CVSROOT=":ext:username@server:/home/username/.cvs"

# import a project, on the project directory
project> cvs import project blah foo
# cd ..; rm -rf project
# cvs co project

# update
project> cvs update
# commit
project> cvs commit file.c
# add file
project> cvs add file.c  

Programs I use

wmaker - I just really like the interfaces, okay fast, but get a little tired of it
# I tried openbox, blackbox, fluxbox is the best! nice transparency, taskbar, systray
fluxbox, idesk, root-tail, adesklets
octave - free matlab in linux
checkinstall - to remove manually installed program cleanly 
asterisk - setting up voip server at home
gcc g++ autoconf automake make cvs - basic compilers ...
ntpdate - Sync my Computer Clock
multi-aterm - transpart tabbed aterm 
qemu - x86 emulator
wine - WinAPI on linux
vnc4server - vnc server (don't user tightvncserver, it doesn support opengl)
;; for KDE
kpdf kate kword kspread kview krita kghostview 
konqueror ksnapshot k3b kplayer kopete kolourpaint
mozplugger - for pdf inside firefox


Old Stuff ...

+ To setup Samba Server
Apt power!
apt-get install samba samba-common smbclient
During installation it will ask you if you want to create password database. Say yes
Also use the secure connection option

For some reason samba have all directories read only. I guess it is a good practice
smb.conf change writeable to yes

You need to setup account for each individual in your linux box
smbpasswd tmtong

Somehow windows screw things up
Uninstall file sharing in network of control panel
Restart

Go to your ssh clients in windows, have port forward Outgoing and port 139
ssh to the server from your window machine
Put this in the window browser to see if it works
\\localhost\tmtong

+ To setup Mysql
Apt power!
apt-get install mysql-common mysql-server

Create database for user
mysql
create database tmtong
grant all privileges on tmtong.* to tmtong@localhost identified by "test";

+ To setup virtual host

NameVirtualHost *

<VirtualHost *>
    DocumentRoot /var/www
</VirtualHost>

<VirtualHost *>
    DocumentRoot /home/tmtong/public_html       
    ServerName soysauce.servebeer.com                 
</VirtualHost>


+ To know the CPU temperature
Apt Power!
apt-get install lm-sensors
sensors-detect 

Reboot
Show the temperature by typing
sensors 
  
+ To have ACPI working
Make sure kernel and BIOS support it.
apt-get install acpid


+ Install Java with Apt in Debian!
Include contrib to apt source. We will change back when we are done

apt-get update

apt-get install java-package
Remember what you installed before
Donwload j2sdk from sun web site
Make a deb package from the donwload binary as a normal user 
fakeroot make-jpkg ./j2sdk-1_4_2_07-linux-i586.bin

Then 
dpkg -i sun-j2sdk1.4_1.4.2+06_i386.deb

Remove all the junk we installed before
apt-get --purge remove ...
any junk you installed before

For firefox java plugin
cd /usr/lib/mozilla-firefox/plugins
ln -s /usr/lib/j2sdk1.4-sun/jre/plugin/i386/ns610-gcc32/libjavaplugin_oji.so .
Path can vary!

+ This fonts for emacs works well for me

Emacs.Font: fontset-standard
Emacs.Fontset-0: -*-fixed-medium-r-normal--14-*-*-*-*-*-fontset-14
Emacs.Fontset-1: -*-fixed-medium-r-normal--16-*-*-*-*-*-fontset-16
Emacs.Fontset-2: -*-fixed-medium-r-normal--24-*-*-*-*-*-fontset-24
Emacs.Fontset-3: -*-fixed-medium-r-normal--18-*-*-*-*-*-fontset-18
Emacs.Fontset-4: -*-fixed-medium-r-normal--26-*-*-*-*-*-fontset-26

+ Install Latex
apt-get install tetex-extra tetex-bin



+ To login from local to remote without password
In debian, ssh is openssh
local> ssh-keygen -t dsa
local> scp $HOME/.ssh/identity.pub remote:
remote> append to .ssh/authorized_keys2

+ Remove Risky Packages
apt-get remove --purge telnetd ftpd fingerd nfs-common nfs-server portmap

+ To allow certain people run root program
apt-get install super
In /etc/super.tab
:define admin myusername hisusername
uisp	'/usr/local/bin/uisp'	uid=root $admin




+ To recursively search for files that contain a specific word
find . -exec grep wordiwantosearch {} \; -print

+ Install Java COMM RXTX in Debian
Instruction dated May 2005
1) First install java SDK as shown above.
Let say your java is in /usr/lib/j2sdk1.4-sun. It depends on your version, 
and where you install it. I will call it $JAVA 

2) Get the latext binary for linux from rxtx.org
untar it, you will see lib-something-parallel.so and lib-something-serial.so
cp those to $JAVA/jre/lib/i386/
you will also see RXTXcomm.jar
cp that to $JAVA/jre/lib/ext

3) Get Java Comm from sun
http://java.sun.com/products/javacomm/downloads/index.html
Download Version 2.0 for Microsoft Windows and Solaris/x86
Get the Solaris x86 version
you will see comm.jar 
cp that to $JAVA/jre/lib/ext

4) echo "Driver=gnu.io.RXTXCommDriver" > $JAVA/jre/lib/javax.comm.properties
5) Test out your installation
Go to the untar javacomm directory
inside it, there should be a samples/Simple
javac *.java

See if there is error. If there is no error, installation is finished!

+ To capture screen
xwd > screen.xwd
convert screen.xwd screen.jpg (require imagemagic)

+ Menus

There is a way to automatically update menu in all windown manager 
in debian, after new application is installed

apt-get install menu
update-menus

but I prefer to have my own, for simplicity




+ Garbbage Collection / Checking Memory Leak in C and C++
apt-get install libgc-dev

In your C File put, 
#include "gc.h"
#include "stdio.h"
#include "stdlib.h"
#include "leak_detector.h"

GC_find_leak = 1; // in the main function
malloc / free whatever as usual
call CHECK_LEAKS(); // when you want it to check the memory leak is

Compile it with -I/usr/include/gc -lgc


+ Ads in Firefox
Firefox has already block some of the ads, but these tools will block everything including
those embedding in the web page. 

Adblock, that's the extension that do block the ad
FilterSet.G.Updater. This is the regular expression updater that adblock uses

+ Control firefox by wiggling your cursor
All in one gestures
Use your right mouse button and draw a line to the left for going backward

+ General Tricks for blocking advertisement
Put the pop up server address to /etc/hosts
like 
127.0.0.1 annoying.server.com

And you will not see any ad from that server ever again. It work on all browsers on the computer


+ Kate

Although I do not use kde, you can edit a file through, ssh, ftp as if it is in your own fs
When you are asked to open a file, you type
fish://username@server.com/file
You can change whatever and save. That's very convenient
I heard it also works on any kde application. Maybe I should start using kde now. 

It can also hide a function too which is pretty cool, when you program. 

+ Xplanet

apt-get install xplanet
xplanet -radius 65 -utclabel -radius 65 -origin sun &

It can be used as a cool desktop background. 
It will update itself according to the time of the day
I always find myself staring at australia somehow. 



+ GJDB

A GDB debugger for java and it works well in emacs

;; it does places its files in some weird place, so do install it to your 
home directory, otherwise emacs cannot find it

./configure --prefix=$HOME/software
make
make install

Put this in your .emacs file
(load "$HOME/software/lib/emacs/lisp/gjdb.el")

and this in your .bashrc
export PATH=${PATH}:$HOME/software/bin

Restart emacs



+ Play Flac with beep media player
apt-get install xmms-flac
cd /usr/lib/bmp/Input/
ln -s /usr/lib/xmms/Input/libxmms-flac.la .
ln -s /usr/lib/xmms/Input/libxmms-flac.so .

+ Debian on USB Sticks!
Go Here For my Tutorial



Written by Terence Tong
Statistics:
Go back to Home