Linux Tips and Tricks

Debootstrap and CHROOT

In Debian-based distributions (eg ubuntu) you can easily create a fully-featured chroot environment for running stuff, including services. There are some good sides to that. For example, if you are messing about with repositories and dependencies, you can do that in a chrooted environment without destroying your host system. Also, you can run services in your chroot environment (and install them there). It's almost like virtualisation (but without virtualizing ;)). I like to use chroot on debootstrap for testing and to chroot a few services like apache. If someone hacks into your system using eg a php bug, they won't be able to break your host system, as they can't break out of the chroot jail ;). Well, it's quite simple, although, you need to tweak a few stuff to run services there. Well, here goes:

aptitude install debootstrap
mkdir /mnt/sys01
debootstrap --arch i386 jaunty /mnt/sys01 http://archive.ubuntu.com/ubuntu
cp /etc/hosts /mnt/sys01/etc
cp /etc/hostname /mnt/sys01/etc
cd /mnt
mount -t proc proc /mnt/sys01/proc/
mount -t sysfs sysfs /mnt/sys01/sys/
chroot sys01
locale-gen en_GB.UTF-8
aptitude update && aptitude upgrade
exit
From now on, every time you type in chroot /mnt/sys01 voila! You are in the chroot environment. It's a full thing, so aptitude has it's own repo configured, it has it's own installed packages, etc... Like a new system :)

Ubuntu VMWare installation

It is actually quite tricky to install vmware on ubuntu. The main problems I found with it are: a lot of dependencies are needed to be able to verify the serial, you need to install a patch for vmware to work on ubuntu, couldn't get vmware1.0.9 running on ubuntu. I am going to walk you through the installation of VMWare on the Ubuntu Server edition 9.0.4 now.
This will install the vmware update patch for kernel 2.6.27 and 2.6.28. If you have older kernel versions, you will need to find a different patch that korrespons to it, or downgrade to an odler vmware version (eg 1.0.7). to find vmware updates, look on google for 'vmware update' or 'vmware any any update' (both without the quotes)

 # go to http://register.vmware.com/content/registration.html and get your free serial number first
 # go to root, I hate typing sudo with every command
sudo su -
 # install needed dependencies
apt-get install linux-headers-`uname -r` build-essential xinetd psmisc libxtst6 libxtst-dev psmisc libxt6 libxt-dev libice6 libice-dev libsm6 libsm-dev libxrender1 libxrender-dev g++-4.1 wget
 # download latest vmware (1.0.9), patch and unpack
mkdir /root/src
cd /root/src
wget http://download3.vmware.com/software/vmserver/VMware-server-1.0.9-156507.tar.gz
wget http://www.insecure.ws/warehouse/vmware-update-2.6.27-5.5.7-2.tar.gz
tar -zxvf VMware-server-1.0.9-156507.tar.gz
tar -zxvf vmware-update-2.6.27-5.5.7-2.tar.gz
cd vmware-server-distrib
./vmware-install.pl
 # follow all instructions until you get to the question if you want to run vmware-config.pl. Say NO
cd ../vmware-update*/
./runme.pl
 # the script will patch the configuration and ask if you want to run vmware-config.pl. Say YES this time
 # now follow instructions to finish installation

Simple Ubuntu Web Repository

First gather all the packages that you want to put in your repository. I normally install a fresh Ubuntu on vmware to start with. Then I update the installation and install the packages I need. Once done, I take all required packages from apt cache and use those (so that I have full upgrade path with required dependencies). For the purposes of this tutorial, let's pretend we want to mirros apache2 with all dependencies and an upgrade of Ubuntu install (a snapshot you could say).
First install Ubuntu on vmware, then start it, login and perform the following

 # go to root
sudo su -
 # update list and upgrade the installation
aptitude update && aptitude upgrade
aptitude install dpkg-dev
 # install apache2
aptitude install apache2
 # create a tree and move the packages
mkdir -p /tmp/repo/dists/jaunty/main/binary-i386
mv /var/cache/apt/archives/*.deb /tmp/repo/dists/jaunty/main/binary-i386
 # and now a nasty hack to fix the fact that apt cache renamed all colons (:) to %3a string :/
cd /tmp/repo/dists/jaunty/main/binary-i386
ls *.deb |grep '%3a' |while read FN; do NEWFN=`echo $FN |sed 's/%3a/:/g'`; echo $NEWFN; mv $FN $NEWFN; done
ls *.deb |grep '%3A' |while read FN; do NEWFN=`echo $FN |sed 's/%3A/:/g'`; echo $NEWFN; mv $FN $NEWFN; done
 # run dpkg to create the package list (Packages.gz)
cd /tmp/repo
dpkg-scanpackages -m dists/jaunty/main/binary-i386 |gzip -c > dists/jaunty/main/binary-i386/Packages.gz
 # ok, now copy of the repo directory to your webserver
 # if on the same server
cd /tmp
mv repo /var/www
 # if on another server - replace USERRNAME and IP with actual ssh username and IP
scp -r /tmp/repo USERNAME@IP:/var/www
 # if you get problems with scp copy permissions
scp -r /tmp/repo USERNAME@IP:/tmp
#login to the IP server and perform this on it
mv /tmp/repo /var/www
That's done :) You have a nice basic working repository. Just note that if dependencies don't resolve, you will have to navigate to the directory where your repositories are on the webserver and perform the nasty %3a hack fix from above there as well. Now, on the machine where you want to use the repository you would do this:
sudo su -
vi /etc/apt/sources.list
 # comment out all the entries here that are not commented out
 # I normally comment out using 3 ### so that I know which lines were the ones I made changes to
 # Now just above the 1st entry you commented out (starting with deb) insert this line:
deb http://IP_OF_REPO_SERVER/repo/ jaunty main

 # now save the file and perform the below to update the packages list
aptitude update
 # and that's done. Now if you do any of the below, aptitude will use your repository
 # 'fraid as this is a basic one, it will have problems with trusting your packages and ask for extra confirmation before install
 # but that shouldn't be a problem ;)
aptitude upgrade
aptitude install apache2

How to Reset forgotten MySQL root password

Ok, now I've forgotten MySQL root password a few times and even worse off, after installation I sometimes couldn't login with the password I set. So, to reset the password, stop mysql, create a text file with the following:

/etc/init.d/mysql stop
vi /tmp/mysql_reset.sql

UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
FLUSH PRIVILEGES;
Now run the below in order to reset the password
mysqld --init-file=/tmp/mysql_reset.sql
 # now give it time (up to 5 minutes), sometimes it may take that long I found. Not sure why, but it sometimes does
 # by the way, I been told the below works better, but never tested it, it should do better
mysqld_safe --init-file=/tmp/mysql_reset.sql &
 # make sure mysql is dead now
ps -A |grep mysql |awk '{print $1}' |while read FN; do kill $FN; sleep 3; kill -9 $FN; done
 # now start mysql service
/etc/init.d/mysql start