Thursday, July 8, 2010

Removing packages from Debian/Ubuntu

Just a quick note so I don't waste time revisiting this topic again.

A coworker of mine forced a package install without repositories so all the dependencies obviously failed. Trying to remove it, I did a

dpkg --remove

But running an apt-get said it was still installed. Running

dpkg --list

Gave me a status of "rc" - which left a lot of files laying around. (Configs, etc.)

Finally, I just did a

dpkg --purge

And it cleared out all the junk.

From there, I was able to do a normal apt-get against the repositories.

Wednesday, June 30, 2010

Using Exim as a Mail Relay

One service that always seems required by any platform I've ever worked on is an outbound mail transfer agent. Essentially, everybody wants to send their customers notifications and needs a clearinghouse to pass it through.

This is all well and good, but there are a few inevitable problems one is likely to run into when putting one up.

By far, I've had the most success with Exim running on Debian since both are very lightweight, incredibly reliable and closely intertwined. (Exim is Debian's default MTA.)

Step 1 is to provide the basic setup. Log in, su to root and run the following:

dpkg-reconfigure exim4-config

You'll walk through some basic setup info. Essentially, you want to specify the host as an internet mail server and provide the ip addresses of your internal hosts are valid hosts to relay for.

When that's complete, you need to cd into /etc/exim4 and modify the exim4.conf.template file. Find the rewrite section by looking for

begin rewrite

Then add your rewrite rule under it. This will make it apply to all messages that pass thorugh Exim.

The generic rule I usually use looks like this:

*@* $1@mycompanysdomain Ffrs

What it essentially says is "match everthing (*@*) and replace what's in front of the @ with what was to the left of the original and everything after the @ with with explicit domain name - also, apply it to the following types of addresses: F - envelope From field, f - From header, r - Reply-To header, s - Sender header"

With that rule in place, restart Exim:

invoke-rc.d exim4 restart

And the config will be written out to /var/lib/exim4/configuration.autogenerated.

The best way to test is to use the Exim command line to see what rewrites will get applied:

# exim -brw root@localhost
sender: root@
mycompanysdomain
from: root@mycompanysdomain
to: root@localhost
cc: root@localhost
bcc: root@localhost
reply-to: root@
mycompanysdomain
env-from: root@mycompanysdomain
env-to: root@localhost

With that done, you need to take care of the blacklists. First, add an A record to your domain's authoritative DNS WITH A PTR. The reverse lookup is used as a method to ensure the mail server is a legitimate member of your domain. NOTE: A lot of people think MX records are required but that's incorrect. MX records are for inbound mail only.

Now fire off some messages to big mail destinations like gmail and yahoo. Tail the /var/log/exim4/mainlog to see what results you get. Yahoo especially is good about telling you where your IP is blacklisted for some reason. If it is, you'll get a lot of dropped messages till you contact the Blacklister and petition to have it removed.

On that same note, be careful about sending out a bunch of unsoliticed test messages. I've seen hosts get black-listed for that reason alone and it's a pain to get delisted.

Friday, May 21, 2010

Checking for Root

Good programmers know to check for assumptions their code is making before executing it. For an operator, assuming that you've su'd to root before running an installer you've written is a bad idea.

The easiest way to resolve that is to simply use the EUID bash variable to check the effective user-id you're executing under and then exit if it's not zero.

I cribbed the following

if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi

From this excellent article:

http://www.cyberciti.biz/tips/shell-root-user-check-script.html

Thursday, May 20, 2010

Awk Alternative Delimiters

As it's always best to automate, it's often useful to fill the values of shell variables with the returned values of system commands.

For instance, if you wanted the ip of eth0, you could run

/sbin/ifconfig eth0

And you'd get back something like

[nsmc@nsmc-dt automation]$ /sbin/ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:26:F2:AC:C2:FE
inet addr:10.1.1.99 Bcast:10.1.3.255 Mask:255.255.252.0
inet6 addr: fe80::226:f2ff:feac:c2fe/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1142141 errors:0 dropped:0 overruns:0 frame:0
TX packets:223927 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:212209959 (202.3 MiB) TX bytes:205106885 (195.6 MiB)
Interrupt:18 Base address:0x2000

Then you could vi into your config script and export the value to a var manually.

-OR-

You could set the variable with the returned value of a system command after clearing away some noise.

Using an iterative approach, we could peel back each layer till we get the piece we want:

/sbin/ifconfig eth0 | grep "inet "

That gives us the "inet" line and not the "inet6" line.

[nsmc@nsmc-dt automation]$ /sbin/ifconfig eth0 | grep "inet "
inet addr:10.1.1.99 Bcast:10.1.3.255 Mask:255.255.252.0

But we only want the piece behind the first colon. Here we need to use a little awk trickery. awk uses spaces for it's default field delimiters. Let's change that to a colon (:) and see what we get.

[nsmc@nsmc-dt automation]$ /sbin/ifconfig eth0 \
| grep "inet " | awk -F \: '{print $2}'




10.1.1.99 Bcast

Close. Now we can pass it through a standard awk filter and just get the piece we want:

[nsmc@nsmc-dt automation]$ /sbin/ifconfig eth0 | grep "inet " \
| awk -F \: '{print $2}' | awk '{print $1}'




10.1.1.99

Now we just need to assign to our variable at runtime using the backtic's:

export PRIVATE_HOST_IP=`/sbin/ifconfig eth0 | grep "inet " \
| awk -F \: '{print $2}' | awk '{print $1}'`


Tuesday, April 27, 2010

Escaping Single-Quotes in sed

This is a just a re-post of this excellent article by Stuart Coleville:


Using single quotes in BASH ensures that the shell doesn’t expand the contents of the quoted string and this is useful most of the time. However if you want to use single quotes within a single quoted string things don’t work out as you might expect.

If I want to use sed to match some text with a single quotes in it, I will run into trouble if I run:

sed 's/user \= 'root/user \= 'moi/g'

Alternatively if I run (on the same snippet):

sed 's/user \= \'root/user \= \'moi/g'

I will get:

/bin/bash: -c: line 1: unexpected EOF while looking for matching `''
/bin/bash: -c: line 2: syntax error: unexpected end of file

This doesn’t work because the escaped single-quotes (\') are not expanded and are therefore treated literally.

To single quotes work you need to break out of the single quoted string then escape your single quote. Like so:

sed 's/user \= '\''root/user \= '\''moi/g'

Because \' is not inside of single quotes the single-quote is properly escaped and the output is as we’d expect:

user = 'root' ->  user = 'moi'

In conclusion, the title of this post is a bit of a misnomer. You actually can’t put single quotes inside of a single-quoted string. However breaking out allows us to get to where we want to be.

Sunday, April 18, 2010

Installing Amazon EC2 tools to Debian Lenny

I was reading through Bill Childers excellent article in Linux Journal on running Ubuntu under EC2 and I thought I add my notes on how to make the client tools run on Debian.

First, install the Sun JDK (see my previous post on how to do that).

With that done, download the tools from this page:

AWS Developer Community

Or simply by using this link:

ec2-api-tools.zip

Next, unzip them.

Then create a configuration file to export the proper environment variables when your shell fires up:

$ vi ~/.ec2.conf
Add the lines:

export EC2_CERT=whereveryourx509lives/cert-whatever-x509.pem
export EC2_PRIVATE_KEY=whereveryourx509pklives/pk-whatever-x509.pem

export JAVA_HOME=/usr/lib/jvm/java-6-sun/jre

export EC2_HOME=whereveryourec2toolswererunzipped

Save it.

Now add this to the bottom of your .bashrc:

. ~/.ec2.conf
Start a new shell and test for the var's using:

$ export | grep EC2
if they're there, you're all set.

Now test by cd'ing into your ec2 tools bin folder and running:

ec2-describe-images -o self -o amazon
If you get a huge java stack trace, changes are your jdk isn't installed properly.

Otherwise, you should get a very long list of all the amazon AMI's available to run from.

Installing the JDK to Debian Lenny

The first thing to keep in mind is that Debian tries with all its might to remain free and open. In that regard, it resists including proprietary packages - even popular ones - in its main repositories.

So to give debian access to everything, you need to edit your

/etc/apt/source.list

use the following command in vi to include the contrib and non-free packages in your configured repositories:

:%s/main/main contrib non-free/g

:wq

For the sake of being thorough, run

# apt-get update

then

# apt-get install sun-java6-jdk

Answer in the affirmative to the various prompts.

When you're complete, adjust your alternatives like so:

#update-java-alternatives -s java-6-sun

et voila