Tuesday, May 25, 2010

Disable Sudo password prompts on Ubuntu

A simple trick to disable the sudo password prompts on Ubuntu (I have tested this on Ubuntu 9.10 Karmic Koala). A word of caution, be aware that by doing this you are sacrificing the security that sudo provides.

You basically need to edit the /etc/sudoers file. This file cannot be changed by any user. You need to sign in as root and then use the visudo command to start editing the file.

user@machine:~$ sudo su
[sudo] password for user:
root@machine:/home/user# visudo

In this file, right at the bottom you will find a line:
%admin ALL=(ALL) ALL

Replace it with:

This will disable the requirement to enter the password every time you run a sudo command. Save and close (ESC, :wq, ENTER)

So you can now run a command like sudo reboot without having to enter the password:

user@machine:~$ sudo reboot

Broadcast message from user@machine
(/dev/pts/0) at 11:17 ...

The system is going down for reboot NOW!

Again, be careful and know what you are doing. Also, it might be a good idea to make a copy of your /etc/sudoers file before your make any changes to it.

One way to use this trick could be in a scenario where you want to run a sudo command through a script and do not want to use the -S option of sudo (-S option allows you to provide the password from the script itself; this can be dangerous if someone gets access to your script)

Thursday, May 20, 2010

Bash chit-sheet

Some simple tricks that can make bash scripting a breeze:
  1. To truncate a float to int:

  2. To split a string (of 2 words) into its components:

  3. To do simple floating point arithmetic, use bc:
    AVERAGE=$(echo "scale=2; ($FLOAT1 + $FLOAT2 ) / 2" | bc )

  4. Conditional if statement:
    if [[ $INT1 -gt $INT2 ]]; then echo greater; fi

  5. To check the number of arguments for a bash script and to issue an error message if the number of arguments if insufficient, use the following code snippet at the top of the script:
    if [ $# -ne $EXPECTED_ARGS ]; then
    echo "Usage: ./script_name arg1 arg2"
    echo " Example: ./sum 1 2"
    exit $E_BADARGS

  6. To find the process ID of a running process, use pgrep:
    pgrep -fl PROCESS_NAME

  7. To kill a process using the process name instead of the process ID, use:
    kill $(ps -ef |grep PROCESS_NAME | grep -v grep | awk '{print $2}')

  8. Find files that contain a text string:
    grep -lir "text to find" *
    The -l switch outputs only the names of files in which the text occurs (instead of each line containing the text), the -i switch ignores the case, and the -r descends into subdirectories.

Wednesday, May 19, 2010

Clock source and NTP control

To view the available clocksource options for your machine, look into the /sys/devices/system/clocksource/clocksource0 directory. This directory also shows the current clock source that your machine is using. You will need sudo privileges to access this information:

user@machine:/sys/devices/system/clocksource/clocksource0$ sudo more available_clocksource
tsc hpet acpi_pm pit jiffies
user@machine:/sys/devices/system/clocksource/clocksource0$ sudo more current_clocksource

Usually, your machine will pick the "best" clock source from the available options at start up, but if you want to force the selection of a particular clock source, you can do this by including the desired option in the configuration file that the system uses at the time of start-up, namely /boot/grub/menu.lst

user@machine:/boot/grub$ sudo vim menu.lst

Add the clock source option at the end of the line declaring the kernel path, as shown below:

title Ubuntu 8.04.3 LTS, kernel 2.6.24-26-generic
root (hd0,0)
kernel /boot/vmlinuz-2.6.24-26-generic root=UUID=cea24a64-f038-469c-b716-226ab0da2f93 ro quiet splash clocksource=tsc
initrd /boot/initrd.img-2.6.24-26-generic

Save and restart your machine. The desired clock source should show up in the /sys/devices/system/clocksource/clocksource0/current_clocksource file.

To start/stop or restart the ntp demon on your machine from the command line, do:
user@machine$ sudo /etc/init.d/ntp stop
[sudo] password for user:
* Stopping NTP server ntpd [ OK ]

Thursday, May 6, 2010

Add commands to bash

This post is about adding user defined commands to bash so that you can run applications only using the command rather than specifying the whole path of the application executable.

The code provided here has been tested on Ubuntu 9.10 (Karmic Koala).

There are two ways to accomplish the mission, the first is to put your application is the default path that bash already looks into while trying to execute your command, and the second is to add another location as a path that bash must check.

For the first option, run $PATH in the terminal. Your terminal output would look something like

bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

If you put your scripts/executables in any of the above locations, bash will find it and execute it without you having to specify the whole path.

For the second option, you need to edit the ~/.bashrc file and add the location of the executable to the PATH locations. Suppose this location is /home/pbhat/bin/. Concatenate the following line to the end of ~/.bashrc file.

export PATH

Save and close the file and now run the following command in the terminal:
source ~.bashrc

Unless you run this command the updated path will not show up.

Inflate multiple files with one command

If you have many multiple archives which have been compressed using different encryptions (tar, tar.gz or zip) in one directory, you can use the script provided in this post to inflate them into the directories with the same name as the original archive, that is:

file1.zip will get inflated to file1/
file2.tar will get inflated to file2/
file3.tar.gz will get inflated to file3/

So here is the script:


cd $1

for i in $(ls)
dir_name=$(echo $i | sed "s/\([^.]*\)[.].*/\1/")

if [[ "$i" =~ "zip" ]]; then
mkdir -p $dir_name
cd $dir_name;
unzip $i
cd ../
elif [[ "$i" =~ "tar.gz" ]]; then
mkdir -p $dir_name
cd $dir_name
tar -xzvf ../$i
cd ../
elif [[ "$i" =~ "tar" ]]; then
mkdir -p $dir_name
cd $dir_name
tar -xvf ../$i
cd ../

The argument to this code is the full path of the directory that contains the archived files. Notice that the script searches for the strings, "tar", "zip" and "tar.gz" in the filenames. If you have other files in the directory which contain these strings in their names, they can get overwritten!

You can save the script using a name of your choice (I have named it open.sh, as indicated in the terminal output shown below)

terminal$ ls directory_path/
file1.tar file2.zip file3.tar.gz
terminal$ ./open.sh directory_path/
terminal$ ls directory_path/
file1 file1.tar file2 file2.zip file3 file3.tar.gz

I would like to thank Girish Venkatasubramanian for advice on this script.

Setting up automatic back-ups

To set up automatic back-ups from your machine to a remoteserver use rsync and cron/anacron commands that now come pre-installed on your Linux machine.

Here is the reference from the Linux gazette that I used to set up my backups. Since my machine is not necessarily turned on 24 hours a day, I used anacron in stead of cron to schedule my backups.

To set-up a new job for anacron, edit the /etc/anacrontab file
1 20 backup rsync -r -e ssh --delete /home/username/thesis username@remoteserver:backups/thesis/thesis/

For the above command to run you should have the public/private key for password-less login to the remoteserver set-up before hand.

On the remoteserver, you must set up cron-jobs to store timely snapshots of the backups. This would help if your current versions are corrupted and you would like to roll back to a previous version.

Here is the code that must be added to your cron file (edit it using crontab -e ) to do that (root access not required):

# Back up mail files with snapshots of 6, 4, 3, 2, 1 months and 3, 2, 1 weeks
# Order 4m->6m, 3m->4m, 2m->3m, 1m->2m, 3w->1m, 2w->3w, 1w->2w, mirror->1w

# At 3am on the 1st of Jan,Mar,May,Jul,Sep,Nov copy the 4m to the 6m
00 03 1 1,3,5,7,9,11 * cp -f /backups/thesis/backup/4month.tar.gz /backups/thesis/backup/6month.tar.gz

# At 3.02am on the 1st of every month move the 3m to the 4m (and continue for other months)
02 03 1 * * cp -f /backups/thesis/backup/3month.tar.gz /backups/thesis/backup/4month.tar.gz
04 03 1 * * cp -f /backups/thesis/backup/2month.tar.gz /backups/thesis/backup/3month.tar.gz
06 03 1 * * cp -f /backups/thesis/backup/1month.tar.gz /backups/thesis/backup/2month.tar.gz
08 03 1 * * cp -f /backups/thesis/backup/3week.tar.gz /backups/thesis/backup/1month.tar.gz

# And then every Sunday take care of the weekly snapshots and the archiving of the mirror
10 03 * * 0 cp -f /backups/thesis/backup/2week.tar.gz /backups/thesis/backup/3week.tar.gz
12 03 * * 0 cp -f /backups/thesis/backup/1week.tar.gz /backups/thesis/backup/2week.tar.gz
14 03 * * 0 rm -f /backups/thesis/backup/1week.tar.gz
16 03 * * 0 tar zcf /backups/thesis/backup/1week.tar.gz /backups/thesis/thesis/*