Thursday, December 18, 2008

Knowing your hardware

In linux, there are a few ways that you can know you hardware details without opening the chassis of your machine. Below are a few ways that I know and hopefully can help linux users out there;

  1. Refer to the /proc directory. This directory contains a few files that can give you information about your hardware such as memory (meminfo), processor (cpuinfo), partitions (partition) and many more
  2. Use "lspci" command. This is a command to list all pci devices connected to your machine
  3. Use "lshw" command. This command will list out all hardware installed on your system. Available in ubuntu
  4. Use "kudzu -p" command. This is redhat/centos hardware probing and installing tool. Use "kudzu -p" to display all the hardware connected to the system
  5. Use "dmidecode" command. This is a tool for dumping bios information into human readable form
  6. Run "lsusb" to list out all usb devices. Thanks to KwangErn Liew for the suggestion in the comment.
If anyone have any tool that I didn't list out, please feel free to drop out a comment

Thursday, November 27, 2008

Strace - powerful troubleshooting tool

Strace is a tool in linux used for tracing system call and signals. This is very useful for tracking the error occurred when a program is run and we do not have any clue where to start troubleshooting. The usage of strace is very simple. It will record the system calls that happen during the execution of a particular program.

To use strace (below is an example if you want to trace the system call when you run ls):
# strace ls

To use strace and print the output to file
# strace -o outputfile ls

After that, you can analyze the outputfile, to see where is the error that fails the program

Wednesday, November 26, 2008

Fedora 10

Fedora 10, is available now. Download the livecd iso here. For those who are upgrading from fedora 9, check out the guide here. For more info, go here. Enjoy

Tuesday, November 25, 2008

Using gnu screen

Screen is a full-screen window manager that multiplexes a physical terminal between several processes. In short, you can have a few virtual terminal by using only one physical terminal. Screen is useful when you are accessing servers remotely, or running jobs on the background. When you want to run jobs on the background, it is very useful to run it inside one screen so that you can detached it, and simply log out without worry. Below are a few useful command to be used with screen to get you started.

To start using screen
# screen

To start using screen with sessionname
# screen -S sessionname

To list out all available screen
# screen -ls

To attach to a detached screen
# screen -r

To attach to a detached screen with certain pid
# screen -r pidnumber

To attach to a non detached screen session (multi display mode). Using this mode, you can make other people see what you are doing on your screen
# screen -x

When inside screen:

To kill current screen
ctrl-a k

To detach current screen
ctrl-a d

You cannot use the scroll function when inside screen. To scroll, you have to use screen's copy mode, then you can scroll up or down using your arrow key, pageup and pageup. To exit copy mode, press 'Esc'. To enter copy mode:
ctrl-a [
or
ctrl-a Esc

To monitor terminal for activity (start or stop)
ctrl-a M

To monitor terminal for 30-seconds silence (start or stop)
ctrl-a _

Give name to the terminal
ctrl-a shift-a

View all opened screen (interactive)
ctrl-a "

View all opened screen (non-interactive)
ctrl-a w

Move to the next screen
ctrl-a n

Move to the previous screen
ctrl-a p

Move to screen number
ctrl-a N where N is the number of the screen

Lock screen
ctrl-a x

Kill all windows and terminate screen
ctrl-a \

View help for screen
ctrl-a ?

Thursday, November 20, 2008

sos, machine information collection tool

A few weeks back, I have encountered a problem on one of my redhat server. Since the server is licensed, I send an email to redhat customer support through redhat network in search for the solution. The first thing that redhat reply to me was to run a command named 'sosreport'. What is sosreport?

According to the man page:
Sosreport (formerly known as sysreport) generates a compressed tarball of debugging information for the system it is run on that can be sent to technical support reps that will give them a more complete view of the overall system status.

Sosreport belongs to the sos package:

# whereis sosreport
sosreport: /usr/sbin/sosreport
# rpm -qf /usr/sbin/sosreport
sos-1.7-9.2.el5_2.2
# rpm -qi sos
Name : sos Relocations: (not relocatable)
Version : 1.7 Vendor: Red Hat, Inc.
Release : 9.2.el5_2.2 Build Date: Thu 17 Jul 2008 11:50:34 PM MYT
Install Date: Fri 17 Oct 2008 12:27:17 PM MYT Build Host: js20-bc2-10.build.redhat.com
Group : Development/Libraries Source RPM: sos-1.7-9.2.el5_2.2.src.rpm
Size : 421400 License: GPL
Signature : DSA/SHA1, Thu 28 Aug 2008 08:02:35 PM MYT, Key ID 5326810137017186
Packager : Red Hat, Inc.
URL : http://sos.108.redhat.com/
Summary : System Support Tools
Description :
SOS is a set of tools that gathers information about system
hardware and configuration. The information can then be used for
diagnostic purposes and debugging. Sos is commonly used to help
support technicians and developers.

To use sosreport:

  1. type 'sosreport' as root
  2. answer a few questions
  3. wait for a while
  4. check your sosreport output (bz2) at /tmp
  5. You can use the sosreport output to troubleshoot remote machine or to ask help from remote technical support personnel
That's all folks :)

Sunday, November 2, 2008

Ubuntu 8.10 Intrepid Ibex

Ubuntu 8.10 is available for download now. Download your own copy now. For those who currently using ubuntu 8.04, check out the upgrading instructions here :)

Saturday, November 1, 2008

Listing the uncommented

When working with configuration files, we are usually dealing with hundreds of lines, sometimes thousands of lines. There is an easy way for us to list only the uncommented lines. This is useful to check whether the setting that we have done is correct.

To show only the uncommented line for file.conf (this is for the configuration files that use # as comment) :

  • $ grep -v ^# file.conf | grep -v ^$
  1. "grep -v ^#" means list out everything that do not start with #
  2. "grep -v ^$" means list out everything that do not start with blank space

Wednesday, October 15, 2008

Openoffice 3.0

Openoffice 3.0 is now available for download.
From the openoffice.org website:

"Apologies - our website is struggling to cope with the unprecedented
demand for the new release 3.0 of OpenOffice.org. The technical teams are
trying to come up with a solution.

Thank you for your patience."

That means so many people in the world is using openoffice that openoffice.org have to increase their servers capacity ;)

Thursday, September 25, 2008

Using find to do operations on multiple files

I learned this technique from my sifoo when he had to change a bunch of html files to unix using dos2unix command. I found that this is very useful and I would like to share it :)

To find files with the extension .html in the current folder and run command dos2unix to all of them:

  • $ find . -type f -name *html -exec dos2unix ’{}’ \;
To see more use of find, refer to find manpage:
  • $ man find

Thursday, September 11, 2008

Send email with attachment from terminal

To be able to do as the above mentioned, a tool named mutt is needed. Mutt is a mail user agent (MUA) and a very excellent one in my opinion. To install mutt:

  • # yum install mutt
To send email, you can use this commands (Choose whichever you like):
  • # echo "your messages" | mail -s "your subject" johndoe@yahoo.com
    • where -s is for subject and johndoe@yahoo.com is your recipient name
  • # echo "your messages" | mutt -s "your subject" johndoe@yahoo.com
    • where -s is for subject and johndoe@yahoo.com is your recipient name
To send email with attachment
  • # echo "your messages" | mutt -s "your subject" -a /path/to/attachment johndoe@yahoo.com
    • where -s is for subject, johndoe@yahoo.com is the recipient name and /path/to/atachment is the path to attachment file
Hope this will be helpful...:)

Tuesday, September 9, 2008

Ubuntu forgotten password

What to do when you forgot your password for your ubuntu machine?? Here are some simple steps on how to change the password using single user mode.

  1. Reboot the machine
  2. When grub is loading, press 'Esc'
  3. Choose 'Ubuntu kernel...........(recovery mode)'
    • Press 'e' to edit the kernel parameter
    • Append 'single init=/bin/bash' to the kernel parameter
  4. Press 'b' to boot from that particular kernel
  5. You will enter single user mode
  6. Your hard drive will be in read-only mode. Remount it in read-write mode
    • # mount -o remount,rw /dev/sda1
  7. Change your passwd
    • # passwd user
  8. Reboot your machine
  9. Access your machine using your new password
Congratulations, you just changed you user password using single user mode

Friday, August 29, 2008

Resizing your xen DomU using LVM

To resize the disk space of a xen DomU that is using Logical Volume Manager(LVM) is very easy. Below is step by step on how to do the resizing process.

  1. Create a new image with the size that you require. Just give any meaningful name to the image. In this case I will use the name extended.img
    • # dd if=/dev/zero of=extended.img bs=1 count=1 seek=20G conv=notrunc
  2. Add the new image to the configuration file of your DomU. In this example, the name of the domU is xen0
    • # vi /etc/xen/xen0
    • Add these line to it
      • disk = [ 'tap:aio:/path/to/xen/xen0.img,xvda,w','file:/path/to/xen/extended.img,xvdb,w' ]
    • Save
  3. Start your domU
    • # xm create xen0
  4. Access your domU
    • # xm console xen0
  5. Once inside, check whether the new image is detected
    • # fdisk -lu
  6. After confirm that your new hard disk image is detected, it is time we have to work on the lvm
    • Create new physical volume (PV) using the new hard disk image
      • # pvcreate -v /dev/xvdb
    • Check that you have successfully added the PV
      • # pvdisplay
    • Extend your existing volume group (VG) to include the new PV
      • # vgextend -v VolGroup00 /dev/xvdb
    • Check that you have successfully add the PV into the VG
      • # vgdisplay
    • Extend your logical volume (LV)
      • # lvextend -L +20G -v /dev/VolGroup00/LogVol00
    • Check that the extension has been added
      • # lvdisplay
    • If all the steps are successfully done, you have to resize the / partition
      • # resize2fs /dev/mapper/VolGroup00-LogVol00
  7. You are done. Check your new hard disk space :)
    • # df -lh

Monday, August 25, 2008

NFS quick howto for centos 5

To use nfs successfully, you have to configure the server and the client. In this example, the client is 192.168.0.3 and the server is 192.168.0.1. The folder to be shared is /home/sharing, and to be mounted to /mnt on the client

On the server

  1. Make directory that you want to use.
    • # mkdir /home/sharing
  2. Edit /etc/exports, insert the client machine's ip
    • # vi /etc/exports
      • Add this line:
        • /home/sharing 192.168.0.3/255.255.255.255(rw,sync)
      • Save
  3. Edit /etc/hosts.allow
    • # vi /etc/hosts.allow
      • Add this line:
        • portmap: 192.168.0.0/255.255.255.0
      • Save
  4. Start nfs and portmap
    • # /etc/init.d /nfs start
    • # /etc/init.d/portmap start
On the client
  1. Start portmap
    • # /etc/init.d/portmap start
  2. Mount the nfs folder
    • # mount 192.168.0.1:/home/sharing /mnt
  3. Check /var/log/messages for any error that might occur
    • # tailf /var/log/messages
  4. Use mount to check if the folder is mounted properly
    • # mount
      • This should be the output:
        • 192.168.0.1:/home/sharing on /mnt type nfs (rw,addr=192.168.0.1)
  5. Edit /etc/fstab to mount the shared folder on boot
    • # vi /etc/fstab
      • Add this line
        • 192.168.0.1:/mnt/sdb1/backup /mnt nfs rw,hard,intr 0 0
      • Save
You can use 'man exports' to see the options available for /etc/exports

Wednesday, August 20, 2008

Installing backuppc in centos 5

I have been given the task of setting up one backuppc server, and below are the steps on how I did it :)

Server setup

  1. Download the backuppc 3.1.0 srpm from dev.centos.org
  2. Rebuild the srpm using rpmbuild. If the command is not there in your pc, install it first using 'yum install rpm-build'
    • # rpmbuild --rebuild backuppc-3.1.0-1.el5.centos.src.rpm
  3. Get your rpm file at /usr/src/redhat/RPMS/i386 and install it. Install all the perl module needed using yum
    • # yum install perl-Compress-Zlib perl-Archive-Zip perl-File-RsyncP perl-XML-RSS httpd
    • # rpm -Uvh backuppc-3.1.0-1%{dist}.i386.rpm
  4. User backuppc will be created upon installation. Change apache user to backuppc.
    • # vi /etc/httpd/conf/httpd.conf
    • Change 'User apache' to 'User backuppc'
    • Save
  5. Edit file /etc/httpd/conf.d/backuppc.conf
    • # vi /etc/httpd/conf.d/backuppc.conf
    • change 'Allow from 127.0.0.1' to 'Allow from all'
    • Save
  6. Create password for cgi-bin admin user
    • # htpasswd -c /var/lib/backuppc/passwd/htpasswd admin
  7. Edit backuppc config file
    • # vi /etc/BackupPC/config.pl
    • Find and change accordingly
      • $Conf{ServerHost} = 'localhost';
      • $Conf{SplitPath} = '/usr/bin/split';
        $Conf{CatPath} = '/bin/cat';
        $Conf{GzipPath} = '/bin/gzip';
        $Conf{Bzip2Path} = '/usr/bin/bzip2';
      • $Conf{BackupPCUser} = 'backuppc';
      • $Conf{TopDir} = '/var/lib/backuppc';
        $Conf{ConfDir} = '/etc/BackupPC';
        $Conf{LogDir} = '/var/log/BackupPC';
        $Conf{InstallDir} = '/usr';
        $Conf{CgiDir} = '/usr/share/backuppc/cgi-bin';
      • $Conf{ServerInitdPath} = '/etc/init.d/backuppc';
        $Conf{ServerInitdStartCmd} = '$sshPath -q -x -l root $serverHost$serverInitdPath start';
      • $Conf{SshPath} = '/usr/bin/ssh';
      • $Conf{NmbLookupPath} = '/usr/bin/nmblookup';
      • $Conf{PingPath} = '/bin/ping';
      • $Conf{CgiAdminUsers} = 'admin';
    • Save
  8. Grant passwordless sudo for user backuppc to run /bin/gtar and /bin/tar
    • # visudo
    • Add these entries
      • Defaults !lecture # to disable lecture
      • backuppc ALL=NOPASSWD:/bin/gtar,/bin/tar # enable user backuppc to run /bin/tar and /bin/gtar without authentication.
    • Comment this entry
      • #Defaults requiretty
    • Save
  9. Restart apache and backuppc service
    • # /etc/init.d/http restart
    • # /etc/init.d/backuppc restart
  10. Open your browser and point it to 'http://backuppc_server_ip/backuppc' and you should see the backuppc web interface
  11. After this, you have to do almost all the configuration through the web interface. To test, you can run localhost backup first. You have to create the host, fill up all the setting and you are ready to go. Record the host and ip in /etc/hosts.
Client setup
  1. Create new user
    • # useradd backupuser
    • # passwd backupuser
  2. Grant passwordless sudo for user backupuser
    • # visudo
    • Add these entries
      • Defaults !lecture # to disable lecture
      • backupuser ALL=NOPASSWD:/bin/gtar,/bin/tar # enable user backuppc to run /bin/tar and /bin/gtar without authentication.
    • Comment this entry
      • #Defaults requiretty
    • Save
  3. From the server using backuppc user, create ssh public key
    • # su -s /bin/bash backuppc
    • $ mkdir .ssh
    • $ chown backuppc.backuppc .ssh
    • $ chmod 700 .ssh
    • $ ssh-keygen -t rsa
    • $ ssh-copy-id -i .ssh/id_rsa.pub backupuser@client
  4. To make sure that the 3rd step is a success, try to ssh to backupuser@client using backuppc user from the server. If no password is asked, then you are ready.
    • # su -s /bin/bash backuppc
    • $ ssh backupuser@client
  5. You can start entering the client to the list of host and start backing up :)
Update: Precompiled rpm can be downloaded here

Monday, August 18, 2008

Saving website on local machine

Sometimes you found a website that is very interesting, but you just do not have enough time to read it on that particular time. You wish you could save it so that you can view it offline without connecting to the internet. This can be done using wget;

$ wget -m -k -K -E http://www.tldp.org/HOWTO/LVM-HOWTO/index.html

where -m for mirror, -k for convert the links so that it will be suitable for local viewing, -K for backup converted files and -E for adding html extension to the files downloaded. This is the result of the above command;

$ ls
www.tldp.org

Use any web browser to view the files offline by opening the .html file inside the above folder

Tuesday, July 22, 2008

Monitoring hard disk with smartmontools

Monitoring your hard disk health is a very important thing. You do not want to wake up one day, turn on your computer and suddenly your hard disk has crash and all your valuable data has gone with the wind. At that time crying would not get your data back. Like some people always say, prevention is better than cure. Apart from backing up your data regularly, monitoring the health of your hard disk is an essential task. It is to make sure any symptoms of bad sector or any failure can be detected earlier and steps to take care of it can be done sooner. One of the tool that can be used to do the job mentioned before is smartmontools. According to yum description, smartmontools are "Tools for monitoring SMART capable hard disks".

To install smartmontools on fedora:
# yum install smartmontools

Make sure your hard disk is smart capable
# smartctl -i /dev/sda
smartctl version 5.37 [i386-redhat-linux-gnu] Copyright (C) 2002-6 Bruce Allen
Home page is http://smartmontools.sourceforge.net/

=== START OF INFORMATION SECTION ===
Model Family: Western Digital Caviar SE (Serial ATA) family
Device Model: WDC WD800JD-60LSA5
Serial Number: WD-WMAM9MA75547
Firmware Version: 10.01E03
User Capacity: 80,026,361,856 bytes
Device is: In smartctl database [for details use: -P show]
ATA Version is: 7
ATA Standard is: Exact ATA specification draft version not indicated
Local Time is: Tue Jul 22 10:05:31 2008 MYT
SMART support is: Available - device has SMART capability.
SMART support is: Enabled

Smart support is available for this hard disk and enabled

To monitor your hard disk health
# smartctl -H /dev/sda
smartctl version 5.37 [i386-redhat-linux-gnu] Copyright (C) 2002-6 Bruce Allen
Home page is http://smartmontools.sourceforge.net/

=== START OF READ SMART DATA SECTION ===
SMART overall-health self-assessment test result: PASSED

To run test on your hard disk
# smartctl -t short /dev/sda

To see the selftest logs of smartctl
# smartctl -l selftest /dev/sda

See all options for smartctl
# smartctl -h

Manual for smartctl
# man smartctl

Wednesday, July 16, 2008

mtr, another network diagnostic tool

mtr is a network diagnostic tool that combine both traceroute and ping in one easy to use tool. As mtr starts, it investigates the network connection between the host mtr runs on and HOSTNAME. by sending packets with
purposely low TTLs. It continues to send packets with low TTL, noting the response time of the intervening routers. This allows mtr to print the response percentage and response times of the internet route to HOSTNAME. The good thing about mtr is, it will run until we ask it to quit by pressing 'q'. That means we will have a live traceroute and ping that will keep updating until we ask it to stop.

To use mtr, just type:
$ mtr HOSTNAME

example:
$ mtr www.google.com

It will display the a few statistics about ping and packets, enough for us to do some basic network diagnostics work.

Thursday, June 19, 2008

Allowing user to run root privileged commands

To allow normal user to run root privileged commands, you have to use sudo. Sudo allows a user to run commands as superuser or another user. To set your user to be able to use sudo to act as superuser, a number of steps have to be done.

  1. Login as superuser(root)
  2. Edit sudoers file using visudo
    • # visudo
    • Uncomment the below line and save. visudo use vi as text editor, so to save just press 'Esc' and then ':wq'
      • %wheel ALL=(ALL) ALL
  3. Add your user to the group wheel(You can use any name for the group as long as you add it to the sudoers file). As example, we will use 'foo' as our username
    • # usermod -G wheel foo
  4. To make all the superuser's environment variable available to the user, edit /home/foo/.bash_profile
    • # vi /home/foo/.bash_profile
    • Add the following lines, append if the line already exist.
      • PATH=$PATH:/sbin:/usr/sbin
      • export PATH
    • Save the file
    • To activate the changes, run
      • # . .bash_profile
  5. Now, you can use superuser environment variables, but without tab completion feature. To enable tab completion feature, edit /home/foo/.bashrc
    • # vi /home/foo/.bashrc
    • Add the following line
      • complete -cf sudo
    • Save the file
    • To activate the changes, run
      • # . .bashrc
  6. Logout and login back. Now you can use sudo to execute root privileged commands, you inherited the root environment variables and you can use tab completion while using sudo

Wednesday, June 18, 2008

Using sftp to transfer file through network

There are a lot of ways on how to transfer files through network in linux and open source. One of the solution is to use sftp a.k.a. secure file transfer protocol. The reason this sftp is different from the original ftp is, sftp will do all its operation over encrypted ssh transport. This make sure that your file is safely transferred through network. To use sftp, you can just run command
$ sftp user@servername
For example,
$ sftp foo@server.name or
$ sftp foo@192.168.0.1

To use sftp efficiently, a few important commands one need to know, as listed below:

  1. To get help on commands available.
    • sftp> help
    • sftp> ?
  2. The commands are generally divided into 2 groups: the commands that can be used to manipulate localhost and the commands that can be used to manipulate remote host. The commands that start with 'l' are specially for locahost only. Example, to list all directory listing on localhost:
    • sftp> lls
  3. To list directories on remote host, use:
    • sftp> ls
  4. The most important command, how to upload file to the remotehost
    • sftp> put /local/path /remote/path
  5. To download file from remotehost
    • sftp> get /remote/path /local/path
To get more information, use the first step to generate help page where list of commands and how to use it are shown.

Wednesday, June 4, 2008

Displaying message of the day (motd)

When you have logged to your machine through ssh, this is what you will always see after each successful access:

Last login: Tue Jun 3 13:17:35 2008 from 10.20.20.171
[user@server ~]$

You can have additional message displayed like this, by using message of teh day (motd):

Last login: Wed Jun 4 14:59:13 2008 from 10.20.20.241
This is a my server!!!!!!
[user@server ~]$

Here are the steps to do it:

  1. Open /etc/motd using your favorite text editor. I will use vi
    • vi /etc/motd
  2. Append your message to the file and save
    • This is a my server!!!!!!
  3. Then, when this will be displayed when you access your machine again
    • Last login: Wed Jun 4 14:59:13 2008 from 10.20.20.241
      This is a my server!!!!!!
      [user@server ~]$
Have fun....:)

Tuesday, June 3, 2008

Shell scripting built-in variables

When doing scripting in shell, like bash, there are a few built-in variables that we can use to optimize our script. Below are a few useful ones:

  • $$ = The PID number of the process executing the shell.
  • $? = Exit status variable.
  • $0 = The name of the command you used to call a program.
  • $1 = The first argument on the command line.
  • $2 = The second argument on the command line.
  • $n = The nth argument on the command line. n = 0-9
  • $* = All the arguments on the command line.
  • $# = The number of command line arguments.
Hope this can help

Creating banner for ssh server

A banner for ssh server is a few phrase that will come out the time you want to access a server through ssh. By default, this feature is turned off. To turned it on:

  1. Login as 'root'
  2. Create your banner file first. In this example, i will create banner file named /home/banner
    • # vi /home/banner
    • Insert your banner message to the file. I will insert 'Welcome to my pc'
  3. After you have finish with the banner file, open /etc/sshd_config
    • # vi /etc/sshd_config
    • Uncomment or add the following line
      • Banner /home/banner
  4. Restart ssh server
    • # /etc/init.d/sshd restart
  5. When you login, this will be displayed
    • # ssh pingu@10.20.20.171
      Welcome to my pc
      pingu@10.20.20.171's password:

Monday, May 26, 2008

The mystery of quotes

In linux environment, there are 3 types of quotes as far as i know. Each of the quotes bring different meaning and usage.

  1. ' a.k.a. single quotes - Everything wrapped in this quote won't be changed (Strong quotes)
  2. " a.k.a. double quotes - Quotes that doesn't expand meta-characters like "*" or "?," but does expand variables and does command substitution (Weaker quotes)
  3. ` a.k.a. back quotes - To execute command
Examples of quotes usage (top lines are commands and the output are displayed below the commands):

Example of using back quotes within single quotes. Nothing is changed.
$ echo 'Today is `date`'
Today is `date`

Example of using back quotes within double quotes. The `date` command will be executed
$ echo "Today is `date`"
Today is Mon May 26 09:42:50 MYT 2008

Wednesday, May 14, 2008

Fedora 9 is here

Fedora 9 is already available. Get your copy here. A brief introduction to fedora:

" Fedora is a Linux-based operating system that showcases the latest in free and open source software. Fedora is always free for anyone to use, modify, and distribute. It is built by people across the globe who work together as a community: the Fedora Project. The Fedora Project is open and anyone is welcome to join.

The Fedora Project is out front for you, leading the advancement of free, open software and content. "

Tuesday, May 13, 2008

Replacing words in vi

To replace word in vi, the below steps can be used(replace OLD with NEW). Please make sure you are in command(normal) mode:

  1. to replace first occurrence of OLD to NEW on current line
    • :s/OLD/NEW
  2. to replace all occurrence of OLD to NEW on current line
    • :s/OLD/NEW/g
  3. to replace all occurrence of OLD to NEW between two line numbers (# are the line numbers)
    • :#,#s/OLD/NEW/g
  4. to replace every occurrence of OLD to NEW on current file
    • :%s/OLD/NEW/g

Sunday, May 11, 2008

Creating ssh reverse tunnel

Imagine you are out of the office, but you have an important document that you have to get from your personal computer in your office. Unfortunately your computer is protected behind a firewall, making it impossible to access. But you have a server that you can access and your personal computer also can access this server. This is where ssh reverse tunnel come into action. For easy explanation, we will call your current computer as current, your server as middle and your personal computer at the office as target.

Pre-condition for ssh reverse tunnel

  1. The current computer that you have can connect to port 12000 (or any other) on the middle server.
  2. The middle is running an ssh daemon willing to do port-forwarding (enabled by default in OpenSSH) and the GatewayPorts feature is enabled
  3. You can open an ssh connection from target to the middle in advance and leave it open.
  4. The SSH daemon is running on target on port 22. In fact the port can be arbitrary and the daemon does not have to allow port forwarding. You can even establish your own (not root) ssh daemon.
Below are the steps:
  1. Create a tunnel from middle to target and leave it open when you are still at the office. You cn also ask your colleague at the office to do this. The below command will open port 12000 on middle for listening and forward all request on port 12000 on middle to port 22 of target
    • user@target $ ssh -R 12000:localhost:22 middleuser@middle
  2. Now you can access to port 12000 on middle from current and you will be forwarded to port 22 on target
    • user@current $ ssh targetuser@middle -p 12000
  3. If somehow you cannot access, access middle first, then connect to port 12000 of localhost
    • user@current $ ssh middleuser@middle
    • user@middle $ ssh targetuser@localhost -p 12000
  4. You are now in the target server

Friday, April 25, 2008

Ubuntu 8.04 is already available

Ubuntu 8.04 is already available!!! Get your own copy at ubuntu.com

Friday, April 11, 2008

Setting up samba with password protection

To easily share your files to linux and windows clients, samba is still the preferred choice. In this guide I will show how to setup a samba server on centos 5 machine, that can be accessed only by certain people protected by password.

  1. Install samba on the server
    • # yum install samba
  2. Create the group that all the samba users will be contained in, for example 'samba'
    • # groupadd samba
  3. Create samba users and add it to the above group, which is in this example is 'samba'. Below is the example to create a user named 'user1' and add it to group 'samba'. Set the password for user1
    • # useradd user1 -g samba
    • # passwd user1
  4. Create the directory to be shared. In this example, i will use /home/shared. Change the ownership to root and group ownership to the 'samba' group. Change permission so that only user and group can read write and execute
    • # mkdir /home/shared
    • # chown -R root.samba /home/shared
    • # chmod -R 775 /home/shared
  5. Below is a simple setting of samba
    • [global] workgroup = samba
      server string = Samba Server
      security = user [shared_folder]
      comment = Sharing place
      path = /home/shared
      public = no
      writable = yes
      printable = no
      write list = @samba
      create mask = 0755
      force create mode = 0755
      directory mask = 0775
      force directory mode = 0775
    • What the above setting does basically is to setup /home/shared as samba shared directory but can only be accessed by user from group samba
  6. Add user/users to samba
    • # smbpasswd -a user1
  7. Start smb service, restart if it has already been started
    • # /etc/init.d/smb start
  8. 'user1' can now access the samba server using address 'smb://samba_server_ip_address/shared_folder' at any nautilus address bar. For windows client, you can see at your 'My Network Places' and find a workgroup named 'samba'

Friday, April 4, 2008

'Watch'ing your commands running

Sometimes when you run a command, you need to see the progress of the command, yet you do not want to keep refreshing the command by repeating it a few times. There is a useful command in linux that can do the job of refreshing for you which is watch. According to the watch man page: "watch - execute a program periodically, showing output fullscreen". By default watch will refresh the command in the interval of 2 seconds but this can be changed according to your needs.
For example, you want to see the memory usage of your computer every 2 seconds:

$ watch free -m

This kind of output will show:


(Click on the image for clearer view)

To set the interval(15 seconds) which watch will refresh, use -n option:

$ watch -n 15 free -m

The output:


(Click on the image for clearer view)

To see the differences of each refresh session, use -d. Watch will highlight the changes that happening on that particular moment:

$ watch -d free -m


(Click on image for clearer view)

To exit from watch, just use your trusty Ctrl-c

Tuesday, March 25, 2008

Using tab in vi, vim

The new version of vi and vim supports tab function. Make sure your vi and vim version is 7.0 and above to do this. To open new tab, run :tabnew or :tabe in normal mode. T open a file in new tab, use :tabnew filename or :tabe filename. To move between tab, use ctrl+pgup and ctrl+pgdown. You can also use gt to and gT to move between tabs. To move to specific tab number use igt where i is the tab number

Picture shows tabs in vi

Monday, March 24, 2008

Boosting openoffice's performance

Some of you people out there that just started using OpenOffice(OO) complaints that this office suite is a bit slow and not suitable for old and low end computers. Here are a few tips on boosting your OO performance.

  1. Disable java environment
    • Tools -> Options -> OpenOffice.org -> java
    • Uncheck 'Use a java runtime environment'
  2. Add memory capacity of OO
    • Tools -> Options -> OpenOffice.org -> Memory
    • Under 'Graphics Cache', increase the 'Use for OpenOffice.org' memory. Increase accordingly depending on your system physical memory. For example, you can put it to 128 MB if your physical memory is 512MB
    • Increase also the 'Memory for object' value
    • For easier start of OO application, check the 'enable systray Quickstarter'. It will put one shortcut for all OO application at the right hand corner of your desktop

Wednesday, March 19, 2008

MAMPU of Malaysia migrates to openoffice

MAMPU, the Malaysian Administrative Modernisation and Management Planning Unit, will start adopting the Open Document Format (ODF) starting from 1st April 2008. They are also planning to use uninstall Microsoft Office suite from their computers by the end of this year. The main objective is to reduce cost in obtaining the proprietary software such as Microsoft Office.
The migration process will be assisted by Malaysian Government Open Source Competency Centre(OSCC) in the form of training all the staffs and new users of OpenOffice to smoothen the migration. This is a great news for the open source community. For further details, refer here

Tuesday, March 11, 2008

Redirecting stdout and stderr

Typing commands in shell will result in two output streams namely stdout and stderr. Stdout is the normal output stream while stderr is the output stream for error. For example, if you type the below command,

# ls /usr/ /user
ls: cannot access /user: No such file or directory
/usr/:
bin games java lib local share tmp
etc include kerberos libexec sbin src

The blue line is the stderr and the green lines are the stdout. By default, all output will be directed to the screen, but you can redirect both stdout and stderr to a file.
To redirect stdout to a file named /tmp/stdout and display the stderr to the screen:

# ls /usr /user > /tmp/stdout
ls: cannot access /user: No such file or directory

To redirect stderr to a file named /tmp/stderr and display stdout to the screen:

# ls /usr /user 2> /tmp/stderr
/usr:
bin games java lib local share tmp
etc include kerberos libexec sbin src

To redirect both to the file named /tmp/all:

# ls /usr /user > /tmp/all 2>&1

or

# ls /usr /user &> /tmp/all

To append the stderr and stdout to a file, simply:

# ls /usr /user >> /tmp/all 2>&1

To both redirect and display the stderr and stdout, use tee:

# ls /usr /user 2>&1 | tee /tmp/all
ls: cannot access /user: No such file or directory
/usr:
bin
etc
games
include
java
kerberos
lib
libexec
local
sbin
share
src
tmp

Instead of redirecting to a file or display, you can also redirect them to other command. For example, if you want to email your stderr and stdout to root user of the local machine(Make sure your mta service is on, if not the message will not be delivered):

# ls /usr /user 2>&1 | mail root@localhost

If you do not want to see any error display, just redirect the stderr to /dev/null

# ls /user 2> /dev/null

Tuesday, March 4, 2008

Getting help in linux

To remember every commands in linux is a very tough job. Even experienced user cannot boast that they have remembered every single command. To get help about commands in linux, a few commands can be used.

  1. # man command
    • format and display manual pages for commands
  2. # info command
    • information document about commands
  3. # command --help
    • help on commands
  4. # whatis command
    • search the whatis database for complete words
  5. # apropos string
    • search the whatis database for strings
  6. # man -k string
    • search manual pages for strings. similar to apropos
You have to change 'strings' to your desired strings and 'command' to your desired command

Thursday, February 28, 2008

Extracting tar into chosen directory

Extracting tar is a common thing to do and everybody familiar with unix and linux knows how to do it. Usually the files will be extracted to the current directory. There is although one small trick to do to send the files to the desired location. Use the below command:

# tar -xvzf filename.tar.gz -C /desired/path

This command will extract(-x) verbosely(-v) tar gz(-z) file(-f) to the desired location. Hope this will help. Cheers

Resizing qemu image

Qemu uses its own type of file which is qcow that stands for Qemu Copy On Write. Sometimes after you have setup the image, you suddenly find that you need more space. Below are the proven steps on how you can add more space to your image.

  1. Convert qemu image to raw
    • # qemu-img convert -f qcow imagename -O raw imagename.raw
  2. Extend the image size using dd (In this example, to the size of 10G)
    • # dd bs=1 if=/dev/zero of=/path/to/imagename.raw seek=10G count=1 conv=notrunc
  3. Convert back to qemu image
    • # qemu-img convert -f raw imagename.raw -O qcow imagename
  4. Partition the extra disk space you have created using parted
That's all folk

Wednesday, February 13, 2008

Installing subversion with apache on centos

A step by step guide on installing subversion with http support on centos and redhat machine

  1. First of all, install apache/httpd
    • yum install httpd
  2. Make sure you apache is running. You can also type 'http://localhost' at your browser and apache test page should appear if your apache is running
    • /etc/init.d/httpd status
  3. Make it start by default on startup
    • chkconfig httpd on
  4. Edit the apache configuration to suit your need. If not sure, leave the default setting as it is
    • vi /etc/httpd/conf/httpd.conf
  5. Install subversion and mod_dav_svn for apache
    • yum install mod_dav_svn subversion
  6. Go to subversion.conf in /etc/httpd/conf.d/. Edit as below
    • cd /etc/httpd/conf.d/
    • vi subversion.conf
      1. This is the most basic configuration where anyone will have unrestricted access to the repos. Location is the name that will be used in the browser address bar. In this example it will be 'http://localhost/svn/repos' (Click for larger image)
      2. This is a configuration with username and password for the client (Click for larger image)
    • htpasswd -cm /etc/svn-auth-conf --- This command is not needed for the first configuration. To create the first user with password
    • htpasswd -m /etc/svn-auth-conf --- use this command to add another user
  7. Configure your repository
    • mkdir /var/www/svn --- create folder svn
    • cd /var/www/svn --- change diectory to the newly created svn directory
    • svnadmin create repos --- create svn repository named repos
    • chown apache.apache -R repos --- change ownership of 'repos' to apache
    • /etc/init.d/httpd restart --- restart apache
  8. Open you browser and type 'http://localhost/svn/repos'. You can see that a page with 'Revision 0:/' will appear. Congratulation, you just completed the setup for svn server with http.

Tuesday, February 12, 2008

Fedora 8 additional repository

If you are using fedora linux, here are a few additional repositories that you might want to install in addition to the official repository and the installation instruction.

  1. Livna rpm
    • Download the installer from here
    • Install the repository
      • rpm -Uvh livna-release-8.rpm
  2. freshrpms
    • Download the installer from here
    • Install the repository
      • rpm -Uvh freshrpms-release-1.1-1.fc.noarch.rpm
  3. atrpms
    • Import the atrpms signing key
      • rpm --import http://ATrpms.net/RPM-GPG-KEY.atrpms
    • Create atrpms.conf in /etc/yum.repos.d/
      • touch /etc/yum.repos.d/atrpms.conf
    • Add the below lines to the atrpms.conf file
      • [atrpms]
        name=Fedora Core $releasever - $basearch - ATrpms
        baseurl=http://dl.atrpms.net/f$releasever-$basearch/atrpms/stable
        gpgkey=http://ATrpms.net/RPM-GPG-KEY.atrpms
        gpgcheck=1
You can start using yum to install any software and yum will pick source from these new repositories straight away.

Monday, February 11, 2008

Accessing data on xen lvm guest image

Accessing xen guest image is very easy if the image is not lvm partitioned. But the main problem arise when the image is of lvm format and normal mount command cannot be used. Here I will show both the way. The first is when ext filesystem is used, and the second is when lvm is used.

To mount xen guest image (without lvm)

1. check the partition on the image
# fdisk -lu
The result will be something like this:



2. Mount using offset option
# mount -o loop,offset=106929152 /path/to/image /mnt
where 106929152=208846*512, 208846 is the start of the partition. Using this way, you only mount the second partition and not the whole image

3. You can now access your image at /mnt

To mount xen guest image (with lvm)

1. Check the partition on the image
# fdisk -lu /path/to/image

2. You have to install kpartx to handle lvm partiton
# yum install kpartx

3. Run kpartx on the image
# kpartx -av /path/to/image

4. Run vgscan to scan volume group available
# vgscan

5. Run vgchange to activate the volume group in the image
# vgchange -ay VolGroup00

6. Use lvs to see what is the name of your low volume group
# lvs

7. Mount the low volume group
# mount /dev/VolGroup00/LogVol01 /mnt

8. You can access your lvm image at the mounted directory which is /mnt

9. To unmount it, a few commands have to be executed (umount for unmounting, vgchange -an to deactivate volume group, kpartx -d to delete device map and losetup -d to delete loop device used)
# umount /mnt/
# vgchange -an VolGroup00
# kpartx -d /path/to/image
# losetup -d /dev/loop0

Hope this will be useful

Friday, February 1, 2008

Text viewer

To view text in linux, there are several tools that one can use. The purpose of the text viewer is to view the text file and not editing it. Some of it also equipped with search function to ease reading.
If you are using GUI, you can use gedit, which is available if you are using gnome desktop environment. Gedit works as both editor and viewer. If you are using KDE desktop, you can use KEdit.

In the absence of GUI, a few tools can be used, such as less and more. These 2 tools is quite similar with only the difference in name.
To use less:
# less filename

and similarly to use more:
# more filename

Another tool that can be used is cat. Cat's purpose is to concatenate 2 file into one, but if used to a single file, cat will display the file to standard output. To use cat:
# cat filename

Thursday, January 17, 2008

Installing multimedia codecs in linux

Codecs, is an important element when we are talking about playing and watching multimedia files. To install codecs on linux box, it is an easy task. Just follow the below instructions :-) :

  1. Download the codecs source from here. Thank you to the mplayer team for this magnificent codecs source.
  2. Login as superuser
    • # su -
  3. Extract the downloaded archive
    • # tar -xvjf all-20061022.tar.bz2
  4. Make 2 directories if they are not already exist.
    • # mkdir /usr/local/lib/codecs
    • # mkdir /usr/lib/win32
  5. Copy the content of the extracted directory to the newly created directory
    • # cp all-20061022/* /usr/local/lib/codecs
    • # cp all-20061022/* /usr/lib/win32
  6. Change the permission of the directories to 755
    • # chmod 755 /usr/local/lib/codecs
    • # chmod 755 /usr/lib/win32
  7. Finish. Congratulations, you have managed to install multimedia codecs into your linux box. You can test it by playing any video with your favorite media player.

Monday, January 14, 2008

Backup using tar and ssh

Doing backup is important. You do not want to store your backup at the same machine where the data is stored. It is to avoid data loss when the machine broke down. To do this job, you can use tar paired with ssh to archive your important data and transfer it through network to another machine. Below are the steps:

1. Make sure the backup machine is installed with ssh server and the service is running
# yum install openssh-server
# service sshd start

2. Go to the folder where you want to backup. Use tar to archive and send it though network to backup machine
# tar -cvjf - /path/to/backup | ssh user@backupmachine "cat > /home/backup.tar.bz2"

3. Finish. Congatulations, your backup file is now safely kept in the backup machine