Archive for the ‘Linux’ category

Install & use mysql++ on linux(debian squeeze, ubuntu)

July 28th, 2011

The key to use mysql++ on debian/ubuntu system is to find where the mysql++ .h head files and the .so binary files.

Firrst install mysqlclient and mysql++ on debian squeeze:

root# apt-get install libmysqlclient-dev libmysqld-dev libmysql++-dev libmysql++-doc libmysql++3

This will install the mysql head files in /usr/include/mysql, mysql++ head files in /usr/include/mysql, and libmysqlpp.so and libmysqlclient.so in /usr/lib. These are all the files we need.

Now let us use the mysql++ library.

Save the simple code below as test.cpp

#include <iostream>
#include <mysql++.h>

int main() {

mysqlpp::String s(“hello, world”);

std::cout << s << std::endl;

return 0;

}

And save the makefile as Makefile:

CXX := g++
CXXFLAGS := -I/usr/include/mysql -I/usr/include/mysql++

LDFLAGS := -L/usr/lib -lmysqlpp -lmysqlclient -lnsl -lz -lm
EXECUTABLE := main

all: test
clean:        rm -f $(EXECUTABLE) *.o

Finally, excute

make all

The compile result would be test.

move Mysql’s datadir on ubuntu

March 8th, 2011

Mysql’s datadir (on ubuntu) by default is /var/lib/mysql. Sometimes, if your DB grows bigger and bigger, and finally exceeds the limit of /var partition, you may think of moving it to some other place. Here are the steps.

  1. stop the server
    sudo service mysql stop

    or

    sudo /etc/init.d/mysql stop
  2. copy data to the new folder
    For example, you want to set the new folder in /home2/: 

    sudo cp /var/lib/mysql /home2/

    Now /home2/mysql is the new datadir. Of course, you can change it to whatever name you want.

    Here I deleted the log file ib_logfile* by:

    sudo rm /home2/mysql/ib_logfile0

    Maybe you also want to change the original datadir to some other name to make sure it won’t be in effect any more.

    sudo mv /var/lib/mysql /var/lib/mysql.bak
  3. change the permission of the new directory
    Make sure the owner of /home2/mysql and all its subdirectories is mysql:mysql. When necessary, use chown to change the permission:sudo chown -R mysql:mysql /home2/mysql 

    Also make sure the permissions of the new datadir and its subdiretories are the same as those under /var/lib/mysql. In my experiment, the permission of table files in each database was changed to 400 when copying, so I had to use chmod to correct them:

    sudo chown -R me:me /home2/mysql;
    sudo chmod 660 /home2/mysql/*/*;
    sudo chown -R mysql:mysql /home2/mysql

    The reason why I changed the owner to me (my username on my computer) at the beginning and change it back to mysql at last is so that I can use the wildcast * to change all files’ permission at once.

  4. modify my.cnf and point datadir to the new directory
  5. sudo vi /etc/mysql/my.cnf

    Find datadir and change it to:

    datadir=/home2/mysql

    It seems in Mysql 4 you also need to change the basedir and socket accordingly, but I’m not sure.

  6. add the new diretory into the AppArmor configure file
  7. This part is special on some Linux distributions like Ubuntu >= 7.04. AppArmor is a Linux Security Module implementation of name-based access controls. It confines individual programs to a set of listed files and posix 1003.1e draft capabilities.

    You can use

    sudo aa-status

    to check whether mysql is confined by AppArmor. If so, use

    sudo vi /etc/apparmor.d/usr.sbin.mysqld

    to add the new datadir at the end of the file.

  8. restart apparmor service
    sudo /etc/init.d/apparmor reload
  9. start mysql server
    sudo service mysql start

    or

    sudo /etc/init.d/mysql start

    Refer to /var/log/mysql/error.log to see if there are any errors.

  10. do some tests.
    List all the database, select some records from a table, create a new table and see whether it’s in the new datadir, insert some records, delete them, and whatever.
    WARNING: you may need to run Mysql with the new datadir for a few days before deleting the old datadir.

export mysql data to a custom directory on ubuntu linux

March 8th, 2011

On ubuntu,  mysql is confined with AppArmor to certian directories. Thus when you try to export data into a directory other than /tmp, you’ll get a Errcode 13. So if you really want to export db data inoto a custom dictory, you need to add it to the AppMrmor configure file.

First, check whether mysql is restricted to certain dirs:

sudo aa-status

If you see mysqld listed, then add the custom dirs at the end of the configure file:

sudo vi /etc/apparmor.d/usr.sbin.mysqld

Finally, reload the service:

sudo /etc/init.d/apparmor reload

compress & decompress files under Linux

February 24th, 2011

.zip

compress:

zip -r xxx.zip dir

decompress:

unzip xxx.zip

.rar

compress:

rar a xxx.rar dir

decompress:

unrar e xxx.rar

.bz2

compress:

bzip2 file

decompress:

bunzip2 xxx.bz2 (NOTE: use the switch -k to keep the .bz2 file)

.gz

compress:

gzip file (NOTE: -r, --recursive   operate recursively on directories)

decompress:

gunzip xxx.gz

tar.bz2

compress:

tar -cjf xxx.tar.bz2 dir

decompress:

tar -jxvf xxx.tar.bz2

tar.gz

compress:

tar -czf xxx.tar.gz dir

decompress:

tar -zxvf xxx.tar.gz

Enable USB support in Virtualbox

January 14th, 2011

OK, I only tried a Windows XP client on the Ubuntu 10.10 host. here I’m not sure it will work on other hosts or clients. :-)

I was using Virtualbox OSE which I installed through Ubuntu repository. However, only Virtualbox PUEL supports USB.

So this is what I did:

First, uninstall virtualbox-ose:
sudo apt-get remove virtualbox-ose
. also run
sudo apt-get autoremove
to remove unnecessary dependencies.

Now install virtualbox PUEL version. There are 2 ways:

  • Download .deb package and install it directly. Here is the download page: http://www.virtualbox.org/wiki/Linux_Downloads
  • Add http://download.virtualbox.org/ to your repository source list, and install it via
    sudo apt-get install
    . The source url for ubuntu 10.10 isdeb http://download.virtualbox.org/virtualbox/debian maverick contrib, add the public key via
    wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add -
    , and install it by
    sudo apt-get update;
    sudo apt-get install virtualbox-4.0

    . The lastest info can be found here: http://www.virtualbox.org/wiki/Linux_Downloads

NOTE: uninstalling virtualbox-ose and reinstalling virtualbox PUEL won’t affect the installed guest machines.

Install VirtualBox Extension Pack to suport USB 2.0. The lastest version is still listed in the download page: http://www.virtualbox.org/wiki/Downloads. Here is the directly link for VirtualBox 4.0.0: http://download.virtualbox.org/virtualbox/4.0.0/Oracle_VM_VirtualBox_Extension_Pack-4.0.0-69151.vbox-extpack

Update guest additions. You can download the guest additions ISO file here: http://download.virtualbox.org/virtualbox/. Remember to choose the right version, here I chose 4.0.0.

Now, plugin your usb device, and add a usb filter in your guest machine’s settings. Remember, you have to all these before you turn on your guest machine machine. Maybe you think now everything is done, you boot your guest machine and it will tell you a new device is found. No way!

Refer to http://ubuntuforums.org/showthread.php?p=2202027#18 for the coming steps.

Create a group “usbfs”
sudo addgroup usbfs
, and add yourself to this group:
sudo usermod -aG usbfs <your accout>

Add this line
none /proc/bus/usb usbfs devgid=1001,devmode=664 0 0
(remember to change the “1001″ to your own group id for “usbfs”!) to the end of the file “/etc/fstab”.

Reboot the host machine (here is ubuntu).

In the reference, it says: *USB DEVICES HAVE TO BE UNMOUNTED BEFORE VIRTUAL MACHINE CAN RECOGNIZE THEM*. However, I didn’t do this and it also worked. So I think with version 4.0.0, it’s not necessary.

Now start the guest machine (here is Winsows XP), it should report new USB devices found.

kill a process by name on Linux

November 23rd, 2010

Way 1: find the pid of the process and then kill it by pid.

Step 1: How to find pid:
$> ps aux | grep amsn
output:

weiping 7208 0.4 2.2 260684 86808 ? Sl 09:22 0:15 wish8.5 /usr/bin/amsn weiping 7804 0.0 0.0 8952 876 pts/10 S+ 10:15 0:00 grep amsn

or
$> pidof wish8.5
output:

7208

Step 2: Kill the process
$> kill 7208

Way 2: use killall

killall -9 wish8.5

Way 3: use pkill

pkill wish8.5

Way 4: use ps + kill

export sspid=`ps aux | grep wish8.5| awk 'NR==1{print $2}' | cut -d' ' -f1`;kill $sspid

bash commands

November 12th, 2010

1. for loop

for (( EXP1; EXP2; EXP3 ))
do
	command1
	command2
	command3
done

eg:

#!/bin/bash
for (( i=0; i<5; i++ ))
do
	echo "Hello $i..."
done

2. if / else / fi statements

basic grammar (note the spaces between the [ ]):

if [ EXP ]; then
	statements...
else
	statements 2...
fi

eg:

step=5
alpha=$(echo "scale=1; $step/10" | bc)
if [ $(echo "$alpha<1" | bc) -eq 1 ]; then
	alpha=0$alpha
fi

2.1 Arithmetic Comparisons

-lt <
-gt >
-le <=
-ge >=
-eq ==
-ne !=

2.2 String Comparisons

= equal
!= not equal
< less then
> greater then
-n s1 string s1 is not empty
-z s1 string s1 is empty

3. real number calculation

(echo "scale=2; EXP" | bc)

where scale=2 sets the number of the digits after decimal point.
eg1:

a=1;b=2;
c=$(echo "scale=2; $a/$b" | bc)
echo $c

output:

.50

eg2:

exp="1223/234";
d=$(echo "scale=2; ${exp}" | bc)
echo $d

output:

5.22

reference: http://linuxconfig.org/Bash_scripting_Tutorial

4. functions

basic grammar:

function div() {
    STATEMENTS
    return XXX
}>
NOTE: A bash function can only return a number. There is no way to return directly a string. So we need to use $() to capture the result outputted in the function body.
eg:
# ----------------------------------------------
# return $1/$2 with 1 digit after decimal point.
# ----------------------------------------------
function div() {
   res=$(echo "scale=1; $1/$2" | bc)
   if [ $(echo "$res<1" | bc) -eq 1 ]; then
      res=0$res
   fi
   echo $res
}
# use the function
step=5
alpha=$(div $step 10)

use “checkInstall” instead of “make install”

July 22nd, 2010

as said:

CheckInstall keeps track of all files installed by a “make install” or equivalent, creates a Slackware, RPM, or Debian package with those files, and adds it to the installed packages database, allowing for easy package removal or distribution.

detail: https://help.ubuntu.com/community/CheckInstall

set up postfix on debian

July 13th, 2010

Suppose you have 2 sites: aaa.com & bbb.com. And you would like to set aaa.com as a local domain, and bbb.com as a virtual domain. That means emails of user@aaa.com will go to the system user’s personal mail dir while user@bbb.com has no actual corresponding system user. Details about local domain & virtual domain, please see paragraph “Virtual domains versus local domains” at http://workaround.org/ispmail/etch.

part 1: configure local domain aaa.com

  • install postfix
    apt-get install postfix
  • config postfix
    add the domain to config files using psotconf:
    postconf -e "myorigin = aaa.com"

    add hostname:
    postconf -e "myhostname=aaa.com"
  • Reload Postfix Server:
    postfix reload

Now you can try to send a email via telnet.
telnet localhost 25
You should see:

Trying 127.0.0.1…
Connected to localhost.
Escape character is ‘^]’.
220 aaa.com ESMTP Postfix (Debian/GNU)

Now send a test mail to yourself:

mail from:<test@testsite.com>
rcpt to: <user@aaa.com>
data
To: user@aaa.com
From: test@testsite.com
Subject: First test mail to yourself
Damn it! It worked! Hahahahaha…
Bye…

End the input with an enter, then a dot “.”, and an enter again.
<enter>
.
<enter>

And then quit:
quit

Now use mail to check your email.

part 2: configure virtual domain bbb.com

  • create a user and take his home dir as the virtual server’s base dir.
    #> groupadd -g 5000 vmail
    #> useradd -g vmail -u 5000 vmail -d /home/vmail -m
  • set the virtual_uid_maps and virtual_gid_maps to these IDs:
    #> postconf -e virtual_uid_maps=static:5000
    #> postconf -e virtual_gid_maps=static:5000
  • add bbb.com as a virtual domain into virtual_mailbox_domains
    #> postconf -e virtual_mailbox_domains=bbb.com
  • set the base dir
    #> postconf -e virtual_mailbox_base=/var/spool/vmail
  • reload postfix
    #> postfix reload;
  • add virtual email addresses
    • tell which file the email addresses are stored in
      #> postconf -e virtual_mailbox_maps=hash:/etc/postfix/vmailhost
    • create the file and add some addresses in there.
      #> vi /etc/postfix/vmailhost
      with content like this:
      service@bbb.com bbb.com/service/
      webmaster@bbb.com bbb.com/webmaster/

      So, the mail files for service will actually be stored in /home/vmail/bbb.com/service, and mail files for webmaster will be in /home/vmail/bbb.com/webmaster
    • Now, map the file into DB
      #> postmap /etc/postfix/vmailhost
  • Probably you also need to create the dirs for these 2 addresses
    mkdir /home/vmail/bbb.com/service/bbc.com
    for service@bbb.com
    #> mkdir /home/vmail/bbb.com/service/bbc.com/service
    #> cd /home/vmail/bbb.com/service/bbc.com/service
    #> mkdir new curr tmp
    And do the same for webmaster@bbb.com
  • change the owner and permissions if you created this folders manually and with ROOT privilege:
    #> chown -R vmail:vmail /home/vmail/bbb.com/
    #> chmod -R 700 /home/vmail/bbb.com/

OK, now you can send mail via telnet to have a test.

part 3: configure alias for service@bbb.com

If you want to forward received mails of service@bbb.com, you can use a alias to forward to your another email address, let’s say service.bbb@gmail.com.

  • tell where is the alias configuration file
    #> postconf -e "alias_maps = hash:/etc/postfix/aliases"
    postfix reload;
  • add forward rules in the file:
    #> vi /etc/postfix/aliases
    with the conetent:
    service@bbb.com service.bbb@gmail.com
    and map it:
    #> postmap /etc/postfix/aliases

Note

  • Every configuration done by postconf, you can actually do it by editing /etc/postfix/main.cf directly.
  • Every time you change mail.cf with command postmap, do not forget to reload postfix with: #> postfix reload.
  • Every time you change a db file, do not forget to re-postmap it with: #> postmap /etc/postfix/xxxxx.
  • If you want to set both sites into virtual server, only set localhost as your destination and both sites into virtual domain.

Important debug method

First, all check the log which should be /var/log/mail.log, no matter you want to find the cause of the failure or to check that your email is sent/received successfully.

Second, use mutt to try to send/receive emails

Important references

domain

———————– v1.1: 27/08/2010 ————————

How to add SASL auth

atually a complete instruction for install postfix on debian
official postfix manual

———————– v1.2: 27/08/2010 ————————

How to set up SPF

http://www.howtoforge.com/postfix_spf

———————– v1.3: 03/09/2010 ————————

How to set up domain keys & dkim

Setting up domain keys with dkfilter:

Note: as the dkfilter author has said, dkfilter is deprecated now and DKIMproxy is recommended.

Follow this link : http://bliki.rimuhosting.com/space/knowledgebase/linux/mail/dkfilter+setup except the settings in file /etc/postfix/master.cf should be:

pickup    fifo  n       -       n       60      1       pickup
    -o content_filter=dksign:[127.0.0.1]:10027

And, you may also need to add these as said in http://jason.long.name/dkfilter/:

#
# modify the default submission service to specify a content filter
# and restrict it to local clients and SASL authenticated clients only
#
submission  inet  n     -       n       -       -       smtpd
    -o smtpd_etrn_restrictions=reject
    -o smtpd_sasl_auth_enable=yes
    -o content_filter=dksign:[127.0.0.1]:10027
    -o receive_override_options=no_address_mappings
    -o smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject

#
# specify the location of the DomainKeys signing filter
#
dksign    unix  -       -       n       -       10      smtp
    -o smtp_send_xforward_command=yes
    -o smtp_discard_ehlo_keywords=8bitmime

#
# service for accepting messages FROM the DomainKeys signing filter
#
127.0.0.1:10028 inet  n  -      n       -       10      smtpd
    -o content_filter=
    -o receive_override_options=no_unknown_recipient_checks,no_header_body_checks
    -o smtpd_helo_restrictions=
    -o smtpd_client_restrictions=
    -o smtpd_sender_restrictions=
    -o smtpd_recipient_restrictions=permit_mynetworks,reject
    -o mynetworks=127.0.0.0/8
    -o smtpd_authorized_xforward_hosts=127.0.0.0/8

How to generate key:

perl -MMIME::Base64 -e 'print encode_base64("cihangcihangpassword");'

How to test:

telnet localhost 25
ehlo localhost
auth plain Y2loYW5nAGNpaGFuZwB4TDM4MjVNdjZ1

Setting up Domain Keys and/or DKIM with DKIMproxy:

didn’t try. see here: Mail-DKIM and DKIMproxy.

install sopcast player in Ubuntu 10.04(Lucid Lynx) AMD64

June 12th, 2010

If you are installing sopcast player on a X86 machine, you can read this article:

http://www.ubuntugeek.com/how-to-install-sopcast-player-in-ubuntu-9-10karmic.html.

But if you want to install sopcast on a AMD64 machine, you need to manually install sp-auth as said in here:

http://tinformix.blogspot.com/2009/05/how-to-sopcast-on-gentoo-amd64.html.

In summary (for AMD64):

  • add the sopcast player PPA:
    sudo add-apt-repository ppa:jason-scheunemann/ppa
  • update the source list:
    sudo apt-get update
  • install sp-auth:
    wget http://download.easetuner.com/download/sp-auth.tgz
    tar xzf sp-auth.tgz
    sudo mv sp-auth /usr/local/share
    sudo ln -s /usr/local/share/sp-auth/sp-sc-auth /usr/local/bin/sp-sc
  • install spcast-player:
    sudo apt-get install sopcast-player