Archive for July, 2009


The Commands

Here are a few example of all the ways we can shutdown and reboot on a nix machine.

Power off the Machine Now

sudo halt

Shutdown 10 Minutes From Now

sudo shutdown -h +10 ‘Shutting down in 5 minutes!’

Reboot at 11pm

sudo shutdown -r 23:00

Cancel That

If you decide you don’t need to shutdown after all you can issue a cancel command like this:

sudo shutdown -c

Give a Shutdown Warning Without Actually Shutting Down

sudo shutdown -k now ‘Hey I am going to the server room and this computer might get unplugged. Please save your work.’

Waking your Computer up

David, from crowdway blog, posted a command that tells your computer to wake up 5 minutes from now. To try enter the command and shut your computer down.

echo “+00-00-00 00:05:00″ > /proc/acpi/alarm

Reboot Daily with Cron

Open up cron as root with, ‘sudo crontab -e‘ and add the following line to reboot each night at 11:55pm.

55 23 * * * reboot

Wouldn’t it be nice to be able to have the same setup on all of your nix boxes and be able to make a change to the config files on all the machines at the same time? Well, with Dropbox we can set this up easily.

Dropbox is a free online storage service that comes with a syncing app that works with Windows, Mac and of course Linux. Although Dropbox is typically used to sync files that reside in the /home/Dropbox directory we are going to make use of symbolic links to link files outside this directory in an attempt to sync config files across multiple Linux machines.

Benefits

Here are some of the benefits you will get when syncing your config files with Dropbox

  • Applications behave the same on all systems
  • One change to the config file affects all computers
  • Setting up a new system is as easy as installing Dropbox and making some links
  • Config files are backed up

Get dropbox

Install Dropbox with your favorite package manager, Debian/Ubuntu

You can run Dropbox in Gnome by going: Applications > Internet > Dropbox

When ran for the first time you will be access to create an account or login. Just follow the simple steps to setup your Dropbox.

Setup Dropbox on Linux

When finished you should have a folder in your home directory called, ‘Dropbox‘.

Setting it up

Make a folder in Dropbox named ConfigFiles and move your original config files here. This will store them on Dropbox’s servers for all of your computers to see.

For example:

mv .bashrc Dropbox/ConfigFiles

Then make a symbolic link that points to the original file in the Dropbox sync folder:

ln -s Dropbox/ConfigFiles/.bashrc .bashrc

Files you might want to sync:

  • .bashrc – Bash
  • .bash_profile – Bash Profile
  • .vimrc – Vim configuration
  • .icons – for a consistent icon set

Other Neat Dropbox Uses

Things you can do:

You might prefer to have a clean system on reinstall but sometimes it is nice to reinstall applications from a previous machine/setup. Keeping a backup list of packages will make this a snap. Just give your package manager a list of all the packages you want it to install and let it rip.

Here are the backup and restore methods for each of the major distros/package managers.

Debian / Ubuntu

Backup

dpkg –get-selections > installed-software.log

Restore

dpkg –set-selections < installed-software.log
apt-get dselect-upgrade

Arch Linux

Backup

pacman -Qqe | grep -v “$(pacman -Qmq)” > pkglist

Restore

pacman -S $(cat pkglist)

Fedora

Backup

rpm -qa > installed-software.bak

Restore

yum -y install $(cat installed-software.bak)

Gentoo

Backup

cp /var/lib/portage/world installed-software.bak

Restore

cat installed-software.bak | xargs -n1 emerge -uv

OpenSuse

Backup

rpm -qa –queryformat ‘%{NAME} ‘ > installed-software.bak

Restore

sudo zypper install $(cat installed-software.bak)

sSmtp is an extremely simple, resource conserving, SMTP server that will allow your desktop or server to send email. In this article we are going to use sSMTP to send email through Gmail.

Sometimes we want to enable our servers/desktops to be able to send email without setting up a full featured mail server or configuring postfix to route through Gmail.

sSmtp is an extremely simple, resource conserving, SMTP server that will allow your desktop or server to send email. In this article we are going to use sSMTP to send outgoing email through Gmail.
Install sSMTP

Debian/Ubuntu users can Install with this command or click here to open up apt:
sudo apt-get install ssmtp

We need to then need to edit, ‘/etc/ssmtp/ssmtp.conf’:

root=username@gmail.com
mailhub=smtp.gmail.com:587
rewriteDomain=
hostname=username@gmail.com
UseSTARTTLS=YES
AuthUser=username
AuthPass=password
FromLineOverride=YES

Then add each account that you want to be able to send mail from by editing, ‘/etc/ssmtp/revaliases‘:

root:username@gmail.com:smtp.gmail.com:587
localusername:username@gmail.com:smtp.gmail.com:587
Now try sending an email

You can send an email through your favorite email client, like ‘mutt‘, or type:
sudo ssmtp someemail@email.com

You will then type your message, hit enter and ‘ctrl+d‘

Now that you have a simple outgoing email server setup, you can do all sorts of neat things:

* Configure cron jobs to send log reports to your email address
* Alert you of all kinds of system changes
* Send email alerts when your computer reaches a certain temperature
* Send email through PHP, Python, Ruby, and Perl

or if you want to get just the amount of ram you can do:
cat /proc/meminfo | head -n 1

Another fun thing to do with ram is actually open it up and take a peek. This next command will show you all the string (plain text) values in ram.
sudo dd if=/dev/mem | cat | strings
Getting cpu info

Sometimes in troubleshooting we want to know what processor we are dealing with along with how much cpu is currently being used by our OS and programs. We can do this with these two commands.
cat /proc/cpuinfo
top

Linux top command
Check the temperature of your CPU

Keeping a computer within a safe temperature is the key to maintaining a stable system.
cat /proc/acpi/thermal_zone/THRM/temperature

Check Linux Temperature
cat linux temperature
List PCI and USB devices

To list all the PCI devices in your system issues the following command:
lspci

For USB use:
lsusb
Check out how much hard drive space is left
df -h

Linux show hard drive space
See what hard drives are currently detected

It is often times helpful to know what hard drives are connected to a system and what name was given them in the Linux directory. This info allows us to mount the hard drive and manipulate it.
sudo fdisk -l

fdisk -l
Installed Programs
Packages

Ever want to find all the packages that are installed on your system? You can find all the packages and also find out why they are on your system. You can even determine what packages depend on them if any.

Find all installed packages
dpkg –get-selections | less

Find out why a packages is installed and what depends on it
aptitude why packagename

Find out where the package stores all of its files
dpkg -L packagename
Kill a process
ps -A | grep ProgramName
kill 7207

Linux kill firefox
Miscellaneous
Go to a terminal
Ctrl + Alt + f3
return with, Ctrl + Alt + f7
Show all network connections

There are many great network scanners and assessment tools available for Linux but netstat is a very easy to use often a first step in troubleshooting network issues. We will leave the rest of the network tools for a later article as there is so much to cover.
netstat
List all files that are currently open on the system

This command will allow you to see all the files that are currently open on your system. Limiting the directory or coupling this command with grep is often useful for finding files that are still open restricting the ability to unmount a device. Lsof will also ouput the process id or PID. You can then kill the process using the kill command above.
lsof
Keep an eye on something for awhile

The watch command will repeat a command at a set interval (default 2 seconds) and output the response. This is useful for watching directories that change, watching hard drives fill up when a lot of data is being transfered, or using it with lsusb to watch for USB devices being plugged in.
watch ls
watch df -h
Find where a binary is stored and its libraries

Often times when running a cron command you want to include the absolute path to the command. Sometimes I run scheduled PHP tasks. This can be acomplished by using the ‘whereis‘ command.
whereis php5
Logs
See if you have kernel boot issues
dmesg | less

For more logs just cd into the /var/log directory and start using, cat, less, tail, grep, find or any other tool to view and search.

If you are using a Linux system and want to schedule a task to run in the future you will probably need to know cron. Cron is much like Window’s Scheduled Tasks. The only difference is that cron is conifgured by a simple text file. Although, that text file to the untrained looks very complicated. Many people rely cron task generators to do the work but I hope that after this guide you will be able to make your own tasks without the use of a generator.

Now obvisouly cron is very dependent and sensitive to the time. If you want accurate results from cron you are going to want to setup your computer to sync its clock via NTP. For now if you don’t have that configured you can use this command to get up to date temporarily:

As root:

ntpdate pool.ntp.org
Editing Cron

There are more than one way to edit the cron config files; however many of them require you to restart the service. We don’t want to do that so here is a method to add a task to cron without having to restart the deamon. You will need to login to the user you wish to execute the command and type:

crontab -e

Here is the basic structure for cron.

m h dom mon dow command

m – Minutes
h – Hours (24 time)
dom – Day of the Month
mon – Month
dow – Day of the week
command – The command you want to run. This can contain spaces or point to a bash script.
Examples

Since many of you like to learn by example, I am going to give you pretty much every example I can think of. This should help you get going with cron.

*’s represent wildcards or any.
Under dow 0 and 7 are both Sunday.

10 * * * * echo “This command is run at 10 min past every hour”
22 7 * * * echo “This command is run daily at 7:22 am”
22 20 * * * echo “This command is run daily at 8:22 pm”
00 4 * * 0 echo “This command is run at 4 am every Sunday”
* 4 * * Sun echo “This is the same as the command above”
42 4 1 * * echo “This command is run 4:42 am every 1st of the month”
01 * 19 07 * echo “This command is run hourly on the 19th of July”

Using the ‘-’ allows us to specify ranges of days. I actually used the following entry recently to run a script at the end of the working day.
Execute at 5pm only on weekdays:
* 17 * * 1-5 /path/to/your/code

Using the ‘,’ allows us to specify intervals wihout having to have multiple entries in cron.
This would execute the ask on the 1st, the 10th, the 20th and on the 30th of each month, at 17:59PM.
59 17 1,10,20,30 * * /home/username/backupsite

Using the ‘/’ allows us to divide the day into chunks.
Here, the tasks is executed every 4 hours (24/6 =4).
59 */6 * * * /home/username/backupsite

Here is another example of using chunks of time and a range.
Every 20 mins between the hours or 9am and 5pm
*/20 9-17 * * * /path/to/your/code
Dealing with the Output

Sometimes when we execute a command with cron we want to see the results of our command. Other times we could care less. By default the output from cron gets mailed to the owner of the process, or the person specified in the MAILTO variable. Here are some ways we can change the handling of it.

Structure:

cmd | argument

Example of an email output. This will email the results of my command every 4 hours to user, ‘mark’.
59 */6 * * * /home/username/backupsite | mail -s “Subject of Mail” mark

If we don’t care about the output we can send it to nowhere (literally). For example purposes I will just display the cmd and not the entire cron entry.
cmd | /dev/null

Output to a text file and append it if it exists already.
cmd >> log.file

More information can be found in the man page of cron. For more information just type:

man cron

There are many music players that offer the functionality to wakeup to a song or playlist– both on Linux or Windows. But with each option I’ve tried, I’ve never been really happy with the results. For such a simple task, it always seemed overly-complicated. Also, the main downside I found in using a media player plugin, is that you’ll need to have the player running for it to actually work in the morning. Below I describe how to create your own music alarm clock, using only command-line utilities found on most Linux distributions. It uses quite a few different tools, and the tutorial will hit on quite a few different concepts. So, without further adieu…

  1. The first thing you will need is to create a playlist. I used Rhythmbox, since that’s where I store all of my music anyway. Create a playlist with songs you’d like to wake up to. When you’ve got enough, save it in .m3u format, somewhere where you’ll find it later. I put mine in my home directory.
  2. Next, we’ll need to make sure we have all the tools for the job. We’ll be using cron to schedule our tasks, amixer to set our volume, and mplayer to finally play our music. To make sure you have each of these installed, issue the following command:

    sudo aptitude install cron alsa-utils mplayer

  3. Next, we need to actually add the scheduled task. First I’m simply going to give you the commands, and I’ll explain what’s going on afterwards. In a terminal, enter the command:

    crontab -e

    Note: this will open your default text editor, which if you haven’t set it, will probably default to vim.

  4. Go to the end of the page by pressing Shift+G. Then start a new line pressing “o”. Once you’re there, type in or paste the following line:

    30 7 * * 1-5 /usr/bin/amixer set PCM 35\% && /usr/bin/X11/xterm -display :0 -bg black -fg white -e /usr/bin/mplayer -shuffle -playlist ~/.alarm-playlist

  5. Press “ESC” to stop typing. Then enter the command “:wq” (no quotes) to save and quit. If everything went well, you should see the line:

    crontab: installing new crontab

Cool, you’re done! Now, let me explain what all that was, so you can go back and customize it on your own. Remember, any time you want to learn more about a command, you can use the “man” command. For example,

man crontab

to learn about crontab.

So first of all, we used “aptitude” to install a few packages from the Ubuntu repositories. This is probably familiar to you, or you may be using “apt-get”. They are basically the same, but “aptitude” has a few advantages– you should switch to using it if you haven’t already.

Then, we used “crontab -e”. Cron is the name of the task scheduler in Linux, and this command opens up our own personal “scheduled task list”. You can always use “crontab -e” to edit your tasks, or “crontab -l” just to view them.

Now, on to that crazy line I had you type in:

30 7 * * 1-5 /usr/bin/amixer set PCM 35\% && /usr/bin/X11/xterm -display :0 -bg black -fg white -e /usr/bin/mplayer -shuffle -playlist ~/.alarm-playlist

Each entry in your personal crontab has the following format:

minute hour day-of-month month day-of-week command

So, in our case, our “minute” is 30, “hour” is 7, day-of-month is * (any), “month” is * (any), “day-of-week” is 1-5, and “command” is… the rest of that. This basically means that we’ve scheduled our command to execute at 7:30 am on Monday through Friday. Changing these options should be self explanatory. Now, let’s pick apart our “command” one part at a time.

/usr/bin/amixer set PCM 35\%

First thing to note, is that it’s a good idea to use full paths for any command you execute from cron. To find out the full path to a command, use

which {command}

In this case, we’re using amixer, which is a utility for changing the volume on your computer. I set mine to 35% to wake up to, but you can use anything. Also note here that we can’t use simply “35%”, because cron uses ‘%’ as a special character. Therefore, we preceed it with ‘\’.

Next thing to notice is “&&”. This essentially strings two commands together– it won’t start the next command until our first one has finished. So, onto our next command:

/usr/bin/X11/xterm -display :0 -bg black -fg white -e …

This is actually another compound command. xterm is another terminal that we are going to launch our music alarm in, so we can easily shut if off in the morning. We set all sorts of parameters to make the terminal look nice, but the important one is following the “-e”: that’s the command we will run in the new terminal:

/usr/bin/mplayer -shuffle -playlist ~/.alarm-playlist

Ahhh, finally, this is where we finally play our music. mplayer is a command-line music player with a very basic interface, and easy controls. You can run this line in a normal terminal now to make sure it works. We use the parameters “shuffle” to randomize our playlist, and then “-playlist …” to tell it what to play. Make sure you change “~/.alarm-playlist” to your own location.

And we’re done! At this point you should have a fully-functioning music alarm clock. Now go back and tweak it out with preferences that work for you.

Bonus: Move the alarm clock command to a shell script, and keep increasing your volume every minute or so.

I’m an avid Linux user. What to most Windows users is their Desktop, is my home directory to me. When ever there is a download to save, a file to create or an archive to unpack – it will be done in my home directory.

This quickly turned into a mess in the past. My first attempt to solve the problem was to use a subdirectory called temp. Of course this didn’t solve anything, it just moved the problem to a different directory.

The system that finally worked for me is using daily temp directories. To make this easy to manage I use a simple command defined in my ~/.bashrc. Let’s have a look at the code first:

export TD="$HOME/temp/`date +'%Y-%m-%d'`"
td(){
    td=$TD
    if [ ! -z "$1" ]; then
        td="$HOME/temp/`date -d "$1 days" +'%Y-%m-%d'`";
    fi
    mkdir -p $td; cd $td
    unset td
}

The command is called td. When called it checks if today’s temp dir already exists or creates it if necessary. It then changes into the directory.

The command also accepts an argument to access previous temp directories. Just add a minus and the number of days you want to go back.

Quick example? It works like this:

~$ date
Wed Feb 27 20:24:37 CET 2008
~$ td
~/temp/2008-02-27$ td -2
~/temp/2008-02-25$

Additionally to the td command a variable called $TD is defined. It just points to today’s directory. This is quite handy if you need to copy something from or to your temp directory.

My new system helps me to have a clean workspace for trying stuff everyday. All I have to do is to delete old temp dirs from time to time when disk space becomes scarce.

What’s your way to keep your $HOME clean and your mind sane?

Installing and configuring an FTP server on Ubuntu 9.04 Jaunty Jackalope is pretty straightforward. By default Ubuntu uses a package called vsftpd as an FTP server, which is currently the most popular FTP package for Linux systems. To install the vsftpd package, use the following command at the Terminal:

sudo apt-get install vsftpd

Follow the default prompts, and the vsftpd server will be installed on your computer. Once the installation is finished, you’ll want to configure it for greater security. The configuration data for vsftpd is located in the /etc/vsftpd.conf file. First, you’ll want to make a backup copy of the vsftpd.conf file:

sudo cp /etc/vsftpd.conf ~

This will make a backup copy of the original vsftpd.conf file in your home directory.

Next, open up a text editor to make changes to the vsftpd.conf file:

sudo gedit /etc/vsftpd.conf

(New users will probably find gedit the easiest to use due the graphical interface, but more experienced users can opt for vi or emacs instead.)

Once you are editing the file, you’ll want to make a few changes. Change this:

anonymous_enable=YES

To this:

anonymous_enable=NO

This will disallow anonymous access to your FTP server. Unless you have a really, really, really good reason for permitting anonymous access, and you know what you’re doing in terms of network security, I’d recommend leaving the anonymous access off. Especially if your Ubuntu FTP server is sitting on the Internet; having an anonymous FTP server on the public Internet is asking for all kinds of trouble.

Of course, with anonymous access off, you’ll need to permit local users to log in. Do this by changing this directive:

#local_enable=YES

To this:

local_enable=YES

(Putting a # in front of a line disables it; this is called “commenting it out”.)

Once you’ve made these changes, you’ll need to restart the vsftpd server so it reads its new directives:

sudo /etc/init.d/vsftpd restart

You can then test your Ubuntu machine’s FTP service from the local command line:

ftp 127.0.0.1

PrefixSuffix is a GUI application that renames batches of files by changing the beginning or end of their names.

Install prefixsuffix in Ubuntu

sudo aptitude install prefixsuffix

This will complete the installation

Using prefixsuffix

If you want to open prefixsuffix goto Applications—>Accessories—>PrefixSuffix

Once it opens you should see similar to the following screen


In the above screen select your prefix or suffix and folder location click on rename files

Useful Tip

create a new bash-script with the following command line in your Nautilus scripts home (~/.gnome2/nautilus-scripts/)

#!/bin/sh
prefixsuffix $@

then save it to Rename.sh and chmod it to 700.

Now you can to batch rename from Nautilus at current prompt position.