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.

Thursday, June 11, 2009

Emacs Example Tutorial: replace words in a file

This example shows how to search-and-replace words in a file.

First we assume you are used to searching for strings or characters in a word, if you are not, please see this article on how to use emacs to find words and letters in a file.

You can replace strings or characters (words, letters) in a file in a manner very similar to find. You type this command (remember, M is the meta key, which is usually equal to holding down the alt key or tapping on the escape key):


    M %

So you can also think of this as:

    Alt Shift 5

After you type that key command sequence, Emacs will ask you:

    Query replace:

Then you type in the characters that you are looking for that you want to replace and hit enter. Then Emacs will ask you:

    Query replace [search-string-here] with:

Now you type in the characters you want to insert in place of the old word and again hit enter. Emacs will find the first instance of the "search" string (the word you want to replace), it will highlight that occurrence and then ask you:

    Query replacing [search-string-here] with [replacement-string-here]: (? for help)

If you want that occurrence to be replaced, you just tap the y key for "yes". Emacs will automatically move on to the next occurrence of the string you searched for and ask again:

    Query replacing [search-string-here] with [replacement-string-here]: (? for help)

If you want to skip any particular occurrence, just tap the n key for "no". Emacs will continue find and replace until you reach the bottom of the file.

Friday, April 3, 2009

Emacs Example Tutorial: find words and letters in a file

When using Emacs probably my #1 most used and favorite feature is to "find" or search for a string - strings are groups of characters that may or may not be part of a word, a whole word, or a sentence (to be exact there is no maximum length that I know of).

Once emacs is open just hit Ctrl-s (Hold down the Control Key and while holding it, tap the s key once, then let go of Ctrl). Many people will recognize this as the typical "save" command many graphical application use to save a file. Don't worry, emacs has a different save command [link]. After you have hit Ctrl s, emacs will wait for you to start typing the characters (letters) you are looking for. It sill search "as you go", meaning that if you type "f" it will jump right to the first "f", then if you type "i" it will move to the first occurrence of "fi", then if you continue typing "nd" it will move to the first occurrence of the word "find". If it does not find the character sequence you've typed in, it will tell you so.

After it finds the first occurrence of your string of characters, you can hit "Ctrl s" again to find the next occurrence of that same string in your document. You can continue hitting "Ctrl s" until you get to the end of your file, at which point Emacs will tell you "Failing I-search: [search-string-here]" which means it has hit the end of the file. If you continue taping "Ctrl s" even after that, Emacs will wrap your search back around to the start of the file and continue searching from the top again. You will know it has gone back to the top of the file when you see Emacs display "Overwrapped I-search: [search-string-here]" at the bottom of the window.

Sunday, March 8, 2009

Emacs Example Tutorial: exit emacs and save a file or discard changes

For those who are just starting to learn emacs, the key command sequence to save a file is:

Ctrl x s

That means hold down the Control key, tap the x key once, and the s key once, let go of Control key. If you're used to using "Cntl+s" in a graphical progam to save file, this key binding might seem a little awkward at first but you'll get used to it quickly.

You can also choose whether or not to save by simply exiting emacs and telling it "y" for "yes" or "n" for "no" when it asks you if you want to save your changes (also known as "Exit saving changes") by typing the "quit" key command sequence:

Ctrl x c

Emacs will then prompt you "Save file /path/to/filename? (y, n, !, ., q, C-r or C-h)" which means you must answer with one of the options listed inthe parenthesis. To save just tap the "y" key and you're done.

If you decide you don't want to save your changes, tap "n" for "no". Emacs will then double-check your decision by asking you:

Modified buffers exist; exit anyway? (yes or no)

Type "yes", which in this context means "yes I really do want to exit and discard my changes"
If you type "no" emacs will send you back to editing mode with Emacs still open and your unsaved changes still visible and accessable in the window.

Tuesday, March 3, 2009

Installing Mac OS X software updates from the commandline

I don't know why I didn't know this existed earlier - it's the most helpful computer administration command I've found in a year.

Apple's Software updates can be downloaded and installed on Mac OS X with the command "softwareupdate". This makes Updating your OS X remotely via ssh a snap compared to using ARD or VNC.

The command:

softwareupdate -l

will scan the apple update site for pending updates, and display them complete with a commandline-friendly label (name).

You can then install the updates you want with

softwareupdate -i [update_label]

For example:

$ softwareupdate -l
Software Update Tool
Copyright 2002-2005 Apple

Software Update found the following new or updated software:

   * JavaForMacOSX104Release7-1.0
      Java for Mac OS X 10.4, Release 7 (1.0), 82580 [recommended]

$ softwareupdate -i JavaForMacOSX104Release7-1.0

The command output shows which upates are recommended and which will requite a system restart.

The install command prints out a handy character based progress meter while it's downloading and installing:

Downloading Java for Mac OS X 10.4, Release 7
 Downloading Java for Mac OS X 10.4, Release 7
 0..20..40..60..80..100

 Expanding Java for Mac OS X 10.4, Release 7
 Installing Java for Mac OS X 10.4, Release 7
 0..20..40..60..80..100

 Done.

This is one terrific feature!