How to find files within daterange in Ubuntu

Recommended Ubuntu book

I got a virus that hit all php files on one of my servers and had to find all the files that got affected withing the date of the attack. There are a few methods that you can use:

1) To find the files modified within the last day, type:

find /directory_path -mtime -1 -print

where -1 is 24 hours, -2 – 48, and so on.

2) To find the files within modified date range is trickier. You have to create 2 files with start and end dates like listed below, just replace 20111110 and 20111120 with needed dates.

touch -t 201111101059 /tmp/file1
touch -t 201111201200 /tmp/file2
find . -newer /tmp/file1 -a ! -newer /tmp/file2

That’s it.

How to install gitweb in Ubuntu

Recommended Ubuntu book

After being told that I live in the stone age for using svn, I decided to try out git. Finally I got some time to continue learning git and setting the new environment. As a step one, we learned how to install git on Ubuntu. Then, I decided that I wanted to host my own repositories and we learned how to set up gitosis on Ubuntu. Now, after updating to Ubuntu 11.10 – Oneiric Ocelot, I am ready to continue setting up everything I need to start conveniently using git. So, in this post, I will be setting up the gitweb and documenting the process.

Gitweb is a convenient GUI that allows us to quickly see the project and diffs right in the browser. Before we move on to installing git web, make sure you have installed apache. Now, lets move on to installing gitweb:

sudo apt-get install gitweb

Now lets look in our /var/www folder:

cd /var/www

ls

I didn’t have gitweb folder here, so I created it:

sudo mkdir /var/www/gitweb

gitweb files should be in /usr/share/gitweb and we need them in our /var/www/gitweb, so lets create a symbolic link that will point to the files:

cd /var/www/gitweb

sudo ln -s /usr/share/gitweb/* .

To check if these are symbolic links, you can type:

ls -l

it should show what these files are linked to.

Next, locate the path to your git repositories. As per our install, we have it in /home/git/repositories. So, lets edit gitweb config to point to the right directory:

sudo gedit /etc/gitweb.conf

and replace:

$projectroot = "/var/cache/git";

to our path to git repositories, /home/git/repositories in my case to:

$projectroot = "/home/git/repositories";

Save the file and lets move on to next step – editing apache configuration. Open apache config for gitweb:

sudo gedit /etc/apache2/conf.d/gitweb

And edit it as follows (replace the paths if yours are different):

    RewriteEngine on
    RewriteRule ^/gitweb/([a-zA-Z0-9_\-]+\.git)/?(\?.*)?$ /cgi-bin/gitweb.cgi/$1 [L,PT]

    Alias /gitweb /home/git/repositories

    Options Indexes FollowSymlinks ExecCGI
    DirectoryIndex /cgi-bin/gitweb.cgi
    AllowOverride None

and save. Now its time to restart apache to apply the changes:

sudo /etc/init.d/apache2 restart

I got an error at first saying:

Syntax error on line 1 of /etc/apache2/conf.d/gitweb:
Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration
Action 'configtest' failed.
The Apache error log may have more information.
...fail!

Which was because I needed to add rewrite package to apache:

sudo a2enmod rewrite

This time apache started with no errors.

Now you should be able to go to http://localhost/gitweb and see the gitweb page. If you have no public repositories yet, you may see “404 – No projects found” notice. To test it out, I went to my repo folder:

cd /home/git/repositories/

and changed the permissions on my gitosis-admin.git:

sudo chmod -R 755 gitosis-admin.git

Refresh the localhost/gitweb and ta-da! It shows a repository where I could click around and check things out. Since I didn’t want this repo to show publicly, I had to change the permissions back to:

sudo chmod -R 750 gitosis-admin.git

This is it for now. In the next git series posts I will try to go over creating new repositories, adding users, general git usage, installing redmine for project management and anything else I may find useful.

How to restore Unity when it disappears in Ubuntu 11.10

Recommended Ubuntu book

Well, after playing around the compizConfig Setting Manager I somehow screwed up the whole system. Unity along with any kind of menu was gone. Terminal Windows did not come up when pressing ALT+F# and even ctrl+alt+delete were not working. The only way terminal was able to come up was by pressing Ctrl+Alt+T . I did

unity --reset

but it did not work. What worked for me was typing:

ccsm

and enabling it again, disabling everything else that was conflicting. Hope this helps in case you are looking for the solution. 🙂

How to configure Unity in Ubuntu 11.10

Recommended Ubuntu book

I have been poking around the new Unity settings in Oneiric Ocelot.

First, check if you have compizconfig settings manager. If not, install by typing the following command in the terminal window:


sudo apt-get install compizconfig-settings-manager

Then go to unity dash and start typing compiz – it should show you the settings manager. Click it and go to “Desktop” options. I guess unity is a part of compiz now and this is where you can find “Ubuntu Unity Plugin” settings to edit the appearance.

I am kind of upset to see that there is no option to move the dock around. 🙁

Ubuntu Oneiric Ocelot – 11.10 review

Recommended Ubuntu book

Ububtu Oneiric Ocelot - 11.10

Ububtu Oneiric Ocelot - 11.10

As you all may know, the new version of Ubuntu was released on October 13, 2011, which is just over a week ago as of the time of writing this post. I wanted to upgrade right away, but decided to wait a bit for the user response as upgrades don’t always go smooth. Well, this one worked like a charm for me.

It took a little over an hour to perform the full upgrade. I made a backup just in case, which I also recommend doing. So far just a few programs need to be reinstalled. A ton of packages got deleted, but it’s mostly because they have a newer and improved versions.

As for the changes, the first thing I noticed was the desktop. It does look a lot better due to improved unity shell. I remember how much I hated it with the last upgrade when it just got added to Ubuntu and I am still not a big fan of it. It is, however, a lot more convenient and clean. I went from “definitely removing” to “may be I’ll poke around and keep it for a while”. I also don’t like the dock sitting on the right side, but I still have to look if I can relocate and change the size of it. The dash interface became a lot more usable too.

There are a few other small, but noticeable improvements, such as a new gnome login screen, login manager now uses LightDM.  Overall performance got slightly improved. Default mail client is now mozilla Thunderbird. Ubuntu now has a better support of 32 bit apps on 64 bit systems. It has a new backup client, which I still have to look into as it looks like they give you 5gb of online storage for free. Improved software center looks a lot better. These are just a few changes that I was pleased with.

There are other changes, but I think these are the biggest ones, at least for me. I like where it is going and will be waiting patiently for the next release. I want to see more improvements to unity as it looks like they put more focus on it now and it is here to stick.

How to install gitosis on Ubuntu

Recommended Ubuntu book

In the previous post we went over installing git on Ubuntu, we are now installing a package called gitosis so we can host git repositories.
First, make sure you have python-setuptools package installed since gitosis needs python. Open the terminal window and type:

sudo apt-get install python-setuptools

Now we need to get gitosis, type these commands one after another to create src folder in your user directory, clone the source and install it:


cd ~/
sudo mkdir src
cd src/
sudo git clone git://eagain.net/gitosis.git
cd gitosis/
sudo python setup.py install

Next, lets create a user that will own the repositories.


sudo adduser \
--system \
--shell /bin/sh \
--gecos 'git version control' \
--group \
--disabled-password \
--home /home/git \
git

Now we need a public ssh key, we’ll create a local one, it will ask you for a passphrase, enter the one you like:


ssh-keygen -t rsa

Now we need to let the git user use this key:


cp ~/.ssh/id_rsa.pub /tmp/
sudo -H -u git gitosis-init < /tmp/id_rsa.pub

Now lets change the permission on on post-update hook:


sudo chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update

Now lets clone gitosis-admin repository (you can get the hostname by typing hostname command in the terminal), navigate to the folder you want the repository to reside in and type:


sudo git clone git@YOUR_SERVER_NAME:gitosis-admin.git

That’s it, you got a local gitosis-admin repo with a config file and a keydir directory.

I will post how to use git and will try to install gitweb (convenient web interface) next.

Edit: I wrote another post on how to install gitweb in Ubuntu if you wish to move on to the next step. 🙂

How to install git on Ubuntu

Recommended Ubuntu book

As I finished installing svn server, I was given and advice to use git instead. So I started to look into it and decided to give it a try. This post is a start of a process.

You need git first. So, lets start installing it. Open the terminal and type:


sudo apt-get install git-core

Thats’s it. Short and up to the point. You can now check the version to make sure it is installed:


git --version

At the time of writing I have 1.7.1.4 🙂

In case you need a server, move on to the next point – installing gitosis on Ubuntu.

svn post-commit hook in Ubuntu

Recommended Ubuntu book

So, I’ve been messing around the scripts in Ubuntu. It’s fun… 2 am here and I finally got the post-commit hook to work the way I wanted it to. Had to learn quite a bit about bash scripts and php calling the bash scripts. 🙂

As I started in previous post – adding svn hooks in Ubuntu, this is a part 2 of that post. I am using SVNManager, and loading these hooks in the same /repo-templates folder I created in previous post.

To start, remove previous post-commit file from the folder if you created one as I made a new one. Then, lets create the files and make them executable:


sudo touch post-commit
sudo touch post-commit-email
sudo touch post-commit-send
sudo chmod +x post-commit post-commit-email post-commit-send

Lets start editing the post-commit file first:


sudo gedit post-commit

Here is what I got:

#!/bin/sh
 
REPOS="$1"
REV="$2"

$REPOS/hooks/post-commit-email $REPOS $REV

Next, I created post-commit-email file in php, change the path to php if needed (to get the path, type ‘which php’ in terminal). Also, make sure you get the right database connection and password:

sudo gedit post-commit-email

#!/usr/bin/php
 $val) {
  if($flag) {
	$emails .= ',';
  }
  $emails .= $val;
  $flag = 1;
}

mysql_close($con);

exec($repos.'/hooks/post-commit-send '.$repos.' '.$rev.' '.$repos_name.' '.$emails.'');

?>

At last, post-commit-send file:


sudo gedit post-commit-send

#!/bin/sh

REPOS="$1"
REV="$2"
REPOSNAME="$3"
SENDTO="$4"
SENDFROM=root@localhost

LIMITDIFF=200
CHANGELOG=`svnlook log -r $REV $REPOS`
AUTHOR=`svnlook author -r $REV $REPOS`
CHANGED=`svnlook changed -r $REV $REPOS`
DIFF=`svnlook diff -r $REV $REPOS | head --lines=$LIMITDIFF`
DATE=`date`
TMPFILE=/tmp/svn$REV-$RANDOM.message
	 
SUBJECT="New SVN Commit By $AUTHOR On $REPOSNAME, Revision #$REV"
echo "-------------------- SVN Commit Notification --------------------
Repository: $REPOSNAME
Revision:   $REV
Author:     $AUTHOR
Date:       $DATE
	 
-----------------------------------------------------------------
Log Message:
-----------------------------------------------------------------
$CHANGELOG
	 
-----------------------------------------------------------------
Changes:
-----------------------------------------------------------------
$CHANGED
 
-----------------------------------------------------------------
Diff: (only first $LIMITDIFF lines shown)
-----------------------------------------------------------------
$DIFF
" > $TMPFILE

#echo $SENDTO >> $REPOS/hooks/error_log 
	 
# Send email
cat $TMPFILE | mail -r "$SENDFROM" -s "$SUBJECT" "$SENDTO"
 
# Cleanup
rm $TMPFILE

Change the email if needed and make sure that the backtick character is correct as it may change when you copy from this page. I got the last file from this post and modified it a bit.

Let me know if this worked for you. 🙂

How to add svn hooks with SVNManager in Ubuntu

Recommended Ubuntu book

So, once we set up our svn and SVNManager in Ubuntu, it would probably be nice to get emails every time a user in the repository makes a commit.
Svnmanager supports the post create script, which will be fired every time a new repository gets created. Lets start:

sudo gedit /var/www/svnmanager/config.php

at the end of the config, uncomment and edit the line to:

$post_create_script = "/bin/bash /var/www/newrepo-script";

Now lets create the newrepo-script:


$post_create_script = "/bin/bash /var/www/newrepo-script";

And add these lines:

#!/bin/bash
 
cp -R /var/www/repo-templates/* $1/hooks/

which pretty much just tells to copy the content of the templates folder to the newly created repository’s /hooks/ directory. /hooks directory will execute the hooks that we place there on each event that we create. We will add post-commit hook for this example.


cd /var/www
sudo mkdir repo-templates

now the hook


sudo gedit post-commit

Add these lines and save:

#!/bin/sh
 
REPOS="$1"
REV="$2"

/usr/bin/svnnotify                    \
    --repos-path    "$REPOS"                \
    --revision      "$REV"                  \
    --subject-cx                            \
    --with-diff                             \
    --handler       HTML::ColorDiff         \
    --to            to_email@email.com   \
    --from          root@localhost

Feel free to edit the email addresses. Now we need to make the script executable:


sudo chmod +x post-commit

Make sure that you have svnnotify package. Type:

which svnnotify

and if it shows the path, make sure it is the same as we made in post-commit file, if not, edit post-commit file. If path doesn’t show, chances are you don’t have the package installed. Install it by:


sudo apt-get install libsvn-notify-perl

This should do it. If you want to send the emails to every user in the repository, you may have to edit the hook. I found another good tutorial about it here.

Update: I created the hook the way I wanted it, for complete code visit this page – svn post-commit hook in Ubuntu.

How to install postfix to relay mail through gmail in Ubuntu

Recommended Ubuntu book

Once I got my svnmanager working on Ubuntu, I still had problems sending mail. I pointed the domain mx record to the right direction, but for some reason my port 25 was refusing connections. So I decided to relay the mail through my gmail account.

First, install postfix if you haven’t already:


sudo apt-get install postfix

Now, to set up postfix, make sure that you have these files:

/etc/postfix/main.cf

/etc/postfix/generic

/etc/postfix/generic.db

/etc/postfix/sasl/passwd

/etc/postfix/sasl/passwd.db

which you can create via touch command like:

 

cd /etc/postfix
sudo touch generic

Next, edit /etc/postfix/main.cf (via command: sudo gedit /etc/postfix/main.cf), here is mine

# See /usr/share/postfix/main.cf.dist for a commented, more complete version


# Debian specific:  Specifying a file name will cause the first
# line of that file to be used as the name.  The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h

#readme_directory = no

# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.

myhostname = localhost
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
#myorigin = /etc/mailname
mydestination = my-ubuntu, localhost.localdomain, localhost
relayhost = [smtp.gmail.com]:587
mynetworks = 127.0.0.0/8
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = loopback-only
inet_protocols = all

##########################################
##### non debconf entries start here #####

##### client TLS parameters #####
smtp_tls_loglevel=1
smtp_tls_security_level=encrypt
smtp_sasl_auth_enable=yes
smtp_sasl_password_maps=hash:/etc/postfix/sasl/passwd
smtp_sasl_security_options = noanonymous

##### map username@localhost to username@gmail.com #####
smtp_generic_maps=hash:/etc/postfix/generic

Next, edit /etc/postfix/generic


sudo gedit /etc/postfix/generic

and have replace user_name and email_address with your credentials, add all users that may send email through gmail:


user_name@localhost email_address@gmail.com
root@localhost email_address@gmail.com
svnmanager@192.168.1.5 email_address@gmail.com

Now lets generate generic.db from this file:


cd /etc/postfix
sudo postmap generic

Next, lets add your gmail credentials to password file:


cd /etc/postfix/sasl
sudo gedit passwd

Copy this, replace email_address with your gmail email username and gmail_password with your gmail password:


[smtp.gmail.com]:587 email_address@gmail.com:gmail_password

Save the file, lets generate passwd.db now:


cd /etc/postfix/sasl
sudo postmap passwd
chown root.root passwd passwd.db
chmod 600 passwd passwd.db

Restast postfix:


/etc/init.d/postfix restart

Test (replace user_name with your user name):


echo 'test email' | mail -s 'just a test' email_address@gmail.com
sudo sendmail -bv user_name
sudo sendmail -bv email_address@gmail.com

You should receive the email now, sent from your localhost.

If there are any problems, check you /var/log/mail.log