Wednesday, July 15, 2009

Add Linux users with a crypted password

If you routinely setup Linux systems with the same user accounts, you might find it helpful to be able to add the users with a single command, without typing in the password and other info for each user like you have to do with adduser. A single-line, no-questions-ask command also lends itself well to batch scripting.

To do this you'll need to know the username, the system user id and group id (UID and GUID) and plain text password for the user you're about to add.

Start by making the crypted password hash. You can make the password hash with the following command (this is tested on Debian Linux):

    mkpasswd -H md5

mkpasswd will ask you for the password:

    Password:

Type in your password, hit enter and mkpasswd will show you the hash:

    $1$uv.y5wtb$remRyh2SeDD9mgZ81aYuB1

Here is the full command. Put the hash mkpasswd printed in single quotes at the end of the line like this:

    useradd -g 1003 -m -u 1003 -s /bin/bash johndoe -p '$1$uv.y5wtb$remByh2ShDD9mgZ81aYuB1'

The shell parameter "-s /bin/bash" is not strictly necessary but I've found it best to include it to avoid potential accidents.

Note that when using useradd like this, the groups must be preexisting. If the group doesn't already exist, add it with:

    groupadd groupname

It's a good idea to clear your shell history after adding all your users this way. An easy way to clear the history is by running the following commands, one at a time:

    HISTFILESIZE=1
    ls
    ^d

(^d means Ctrl-d: hold down the control key, tap the d key, then let go of the controll key)

Sunday, July 12, 2009

Installing PINE on Debian Etch 4.0 from source or binary

The Pine email client is not included in the Debian Linux "main " repository because of licensing issues.
It is included in non-free. If you don't already have SSL installed, or, if you have the old version installed, you can just add non-free to your /etc/apt/sources.list, so that it looks (somewhat) like this:

deb http://ftp.us.debian.org/debian/ etch main contrib non-free
deb-src http://ftp.us.debian.org/debian/ etch main contrib non-free
deb http://security.debian.org/ etch/updates main contrib non-free
deb-src http://security.debian.org/ etch/updates main contrib non-free

and then run     apt-get update ; apt-get install pine.

However, I already had a newer version of libssl installed, and pine demanded the old SSL libary as a dependency before it would install. I can't downgrade on this system, so I decided to install pine from source. I tried the source from pine's home page, and it wasn't compiling, so I got the Debian pine package source with apt and told it to compile it. It worked pretty well. The command to use is:

    apt-get --compile source pine

That left pine_4.64-3_i386.deb in my working directory, along with a bunch of other files. You might want to build in a temporary directory.

After the build I installed pine with:

    dpkg -i pine*i386.deb

You can probably use a similar set of steps on Debian Lenny 5.0, but I haven't tried it.

You can also try the newer "alpine" package.

Thursday, July 9, 2009

Emacs Example Tutorial: replace words text with a regexp

In Emacs' normal find-and-replace mode you can't replace special characters like "beginning of line" or "end of line" with anything because it treats the special characters representing end of line (^ and $, respectively) as regular characters - a literal ^ and a literal $.

If you want to replace the beginning of line or end of line with another string (and this can come in extremely handy some times) you have to use a different kind of replace: RegExp replace (regexp stands for the term regular expression, which is a phrase used to describe computer pattern matching code or directions. Think "filters"). To get Emacs into this mode type:

M-x replace-regexp

Remember, M is the meta key, which is usually equal to holding down the alt key or tapping on the escape key. So you can think of this as:

ALT x

Emacs will prompt you that it is waiting for more instructions by displaying "M-x". Then you type in:

replace-regexp

and hit enter. Emacs will now ask you "Replace regexp: " - it is waiting to know the regular expression that matches the characters you want to replace.

So let's say you have a file of information - a list of names of employees, for example. This hypothetical list contains nothing but their names. Let's say you want to add a note at the end of the line that reads "Note: give this employee a new mouse pad:."

While emacs is sitting there asking you "Replace regexp: " type in just the dollar sign and then hit enter: $

Now Emacs will ask you "Replace regexp $ with: "

You can now type in any text you want, and then hit enter - Emacs will append that text to the end of the line. Unlike the regular find-and-replace, replace-regexp does not ask you for confirmation one line at a time, it will simply apply this "replacement" to every line of the file at the end-of-line mark.

As a very cool side benefit, you can combine this with block editing mode to constrain the replacement to a fixed block of lines. For more on block editing mode and how to define a block, see this article on emacs block editing.