Setting up a passwordless login over SSH

Here’s how you can set up a public key to allow you to login to a remote server over SSH without a password. (I draw mainly on the instructions here.)

First, open a terminal on your machine. We’ll call it mybox. We’re going to create a public key.

[me@mybox ~]$ ssh-keygen -t rsa

This will ask you first where you want to save the public key you’re creating. (The default location ~/.ssh/id_rsa is fine.) It will then ask you for a passphrase. You can just press enter twice at those prompts to skip the passphrase (I always have).

Now make sure there is a .ssh directory in the home folder on the server where you want to log in (we’ll call it myserver):

[me@mybox ~]$ ssh me@myserver mkdir -p .ssh

Next we’ll append the public key from mybox to the authorized_keys list on myserver:

[me@mybox ~]$ cat .ssh/id_rsa.pub | ssh me@myserver ‘cat >> .ssh/authorized_keys’

That’s it. You can test it with any command that uses an ssh tunnel (ssh or even svn or git, for example). Also note that you can use this same public key to set up passwordless login on any machine you like.

Now another handy thing is adding preconfigured setups to your ssh config file. It’s located in ~/.ssh/config. Here’s an example setup:

host server
    Hostname myserver
    User me

(See this reference for explanation on all the possible parameters you can use.)

Now you can type ssh server on the command line and get logged right in–no need to specify your username or enter a password.

Now mind you, this could potentially be a dangerous setup. Only use this on a well-secured computer where you are the only person with access to your terminal. Any hacker that gets in to your machine can now log in as you to myserver without having to know your username or password. Convenience comes at a price.

Sending email through Gmail using PHP

Sending email is often a necessary feature in web applications. Here’s how you can send email from PHP by using SMTP through Gmail.

First, you’re going to need two PEAR packages: Mail and Net_SMTP. Make sure the directory where these files are found is listed in the include_path directive in your php.ini file.

This is based largely on a code snippet from email.about.com. Modify according to your needs. Entries in bold are ones you will definitely need to change.

<?php

require_once(‘Mail.php’);

$from = ‘Sender <sender@gmail.com>‘;
$to = ‘Receiver <receiver@something.com>‘;
$subject = ‘Sent from PHP on my machine‘;
$body = ‘This is a message I sent from PHP using the ‘
. ‘PEAR Mail package and SMTP through Gmail. Enjoy!
‘;

$host = ’smtp.gmail.com’;
$port = 587; //According to Google you need to use 465 or 587
$username = ‘sender‘;
$password = ‘your_password‘;

$headers = array(‘From’ => $from,
‘To’ => $to,
‘Subject’ => $subject);
$smtp = Mail::factory(’smtp’,
array(
‘host’ => $host,
‘port’ => $port,
‘auth’ => true,
‘username’ => $username,
‘password’ => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
echo $mail->getMessage();
} else {
echo “Message sent successfully!”;
}
echo “\n”;

?>

Installing Ubuntu in a dual-boot configuration

Ubuntu Circle of FriendsI’ve installed Ubuntu several times on various machines in a dual-boot format, but I’ve never blogged about it. In all cases, it’s been with Windows on the first partition (mostly with XP, but also once with Vista on a new machine), so I’ll use that as my example here.

First things first. Get Windows ready. This usually includes defragmenting to get all your files at the beginning of the partition. That’s pretty much it.

Next, get the install CD (from Ubuntu or your favorite distribution).

Boot into the installation CD. Ubuntu comes with a partition editor (GParted) on the CD, which you’ll need soon.

Resize your Windows partition in order to leave enough space for (a) the linux system itself, (b) a swap partition if your RAM isn’t plentiful, and (c) any other shared partitions you may want to make:

  • (a) In the case of Ubuntu, the minimum hard drive space required is 4 GB, although in my experience at least 10-15 GB is better. This partition will probably be of type ext4 (at least, it is in the latest version of Ubuntu)
  • (b) Depending on how much RAM you have and how much hard disk space is available, you may want to add a swap partition. This is a sort of paging file used to swap out segments of the RAM that aren’t used very often, freeing up memory for more active applications. On my old machine, which has 512 MB RAM, I added a 1 GB swap partition. On my current machine with 4 GB RAM, I don’t have any swap space at all and everything runs fine. It’s up to you.
  • (c) If you want to be able to share files between Windows and linux (and haven’t yet converted to a cooler solution like Dropbox), it might be wise to make a FAT32 partition that can be read by both operating systems. Make this one however large you need.

Be sure to tell the partition editor that you’re going to want to put the root file system (/) on your ext4 partition, and ensure that your swap partition (if you made one) is correctly identified as such.

If you’ve made it this far, you’re almost done. Now run the installer and let it work its magic.

Ubuntu’s always installed GRUB without a hitch. But if you do run into trouble, this documentation from Ubuntu is a good place to look.

Congratulations, and welcome to linux!

Oracle 10g ORA_ROWSCN Pseudocolumn

Oracle 10g maintains a pseudocolumn the tracks the revision number of records. It’s called ORA_ROWSCN. (SCN stands for System Change Number.) There’s also a handy function, SCN_TO_TIMESTAMP, which will convert that revision number to a human-readable date and time.

But there are a few caveats:

  • The revision numbers are tracked on a block level, meaning that even if you modify only one row, surrounding rows may also receive the new version number. You can turn on row-level tracking when you create the table, but there’s no way to do so after the fact.
  • SCN_TO_TIMESTAMP will only be able to convert the SCN to a timestamp if the revision occurred within the last five days. Otherwise you get the lovely error: “ORA-08181: specified number is not a valid system change number”.

This article has a good description and two enlightening examples for those interested.

Alarm clock design

The last few days I’ve found myself waking up later than I plan, despite all my good intentions. And despite setting my alarm.

Aside from the fact that I really just ought to drag myself out of bed when I know I want to get up, I’m beginning to think my alarm clock (which is actually my cell phone) isn’t really fulfilling its purpose. We’ll put the blame on that device for the sake of argument.

What is an alarm clock for, anyway? It’s supposed to wake me up at some predetermined time (presumably a time I have selected). But in reality, all my alarm does at the moment is just beep at me. I invariably am too tired to get up when it goes off the first time, so I hit snooze, the button directly under the screen on the left. My phone faithfully plays it’s annoying little melody exactly five minutes later. And usually I hit snooze again.

In theory, I’ll eventually get tired of hitting snooze and just get out of bed. But I also don’t want to annoy my roommate with the incessant repetition of that lovely little tune. So I hit the “stop” button, which is directly under the right side of the screen. And I go back to sleep.

Somehow I seem to unconsciously know exactly when I will have to get up in order to have just enough time to shower, eat breakfast, and run to class. So a large percentage of the time I really do wake up “on time.” But the fact remains that the alarm clock is failing in its purpose.

First design flaw: The button to turn off the alarm is rather close to the button that just snoozes it. More than once I have accidentally hit the “stop” button, fully expecting the arousing tune to play again in five minutes. It doesn’t, and I disgruntedly scramble out of bed half an hour later and glare at the contraption. The point: Even if I’m sleepy, I shouldn’t be in danger of shutting down the alarm clock when all I really want to do is have it wait five minutes.

Second design flaw: Now this is the one that I don’t know how to fix. The purpose of the alarm clock is to get me out of bed when I had planned. All my phone does is beep at me and presume that I’ll take care of the rest. I have seen a few creative ideas (an R2D2 noisemaker, a helicopter, and an air-compressing bed-shaking wonder), but I don’t know that those are what I’m looking for.

I’ll keep thinking….

Next Page »