Thursday, June 28, 2012

CentOS / Redhat Apache mod_ssl Configuration


The mod_ssl module provides strong cryptography for the Apache Web server via the Secure Sockets Layer (SSL) and Transport Layer Security (TLS) protocols. How do I install and configure mod_ssl under CentOS / Fedora / Redhat Enterprise Linux?
mod_ssl is the SSL/TLS module for the Apache HTTP server. You can use self signed certificate or 3rd party SSL certificate. This module provides SSL v2/v3 and TLS v1 support for the Apache HTTP Server. It was contributed by Ralf S. Engeschall based on his mod_ssl project and originally derived from work by Ben Laurie. This module relies on OpenSSL to provide the cryptography engine.

Step #1: Install mod_ssl

Type the following command as the root user to install mod_ssl, enter:
# yum install mod ssl

Step #2: Create an SSL Certificate

Type the following commands:
# cd /etc/pki/tls/certs
# openssl genrsa -des3 -out apachekey.pem 2048

Sample outputs:
Generating RSA private key, 2048 bit long modulus
..................+++
...................................+++
e is 65537 (0x10001)
Enter pass phrase for apachekey.pem:
Verifying - Enter pass phrase for apachekey.pem:
Note enter a strong, passphrase to protect the Apache web server key pair.

Generate a Certificate Signing Request (CSR)

Type the following command:
# openssl req -new -key apachekey.pem -out apachekey.csr
Sample outputs:
Enter pass phrase for apachekey.pem:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [GB]:IN
State or Province Name (full name) [Berkshire]:MH
Locality Name (eg, city) [Newbury]:Poona
Organization Name (eg, company) [My Company Ltd]:nixCraft LTD
Organizational Unit Name (eg, section) []:IT
Common Name (eg, your name or your server's hostname) []:www.nixcraft.com
Email Address []:vivek@nixcraft.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
You need to provide the information fill or hit [Enter] key to accept defaults, but the Common Name field is very important. You must match the fullyqualified domain name of your server exactly (e.g. www.nixcraft.com) or the certificate will not work. No need to enter the challenge password.

Create the Web Server Certificate

You must signed the CSR to create the web server certificate, enter (you can send it to your CA to sign the same). To sign httpserver.csr using your CA:
# openssl ca -in apachekey.csr -out apachecert.pem

Install SSL Certificate

Copy server key and certificates files /etc/pki/tls/http/, enter:
# cp apachecert.pem /etc/pki/tls/http/
# cp apachekey.pem /etc/pki/tls/http/

Edit /etc/httpd/conf.d/ssl.conf, enter:
# vi /etc/httpd/conf.d/ssl.conf
Listen to the the HTTPS port, enter:
Listen 10.10.29.68:443
Update it as follows to seed appropriately, enteR:
SSLRandomSeed startup file:/dev/urandom 1024
SSLRandomSeed connect file:/dev/urandom 1024
Update VirtualHost as follows:
 
<VirtualHost www.nixcraft.com:443>
    SSLEngine On
    SSLCertificateFile /etc/pki/tls/http/apachecert.pem
    SSLCertificateKeyFile /etc/pki/tls/http/apachekey.pem
    SSLProtocol All -SSLv2
    SSLCipherSuite HIGH:MEDIUM:!aNULL:+MD5
    DocumentRoot "/var/www/html/ssl"
    ServerName www.nixcraft.com:443
</VirtualHost>
 
Save and close the file. Make sure /var/www/html/ssl exits, enter:
# mkdir -p /var/www/html/ssl
Edit /etc/httpd/conf/httpd.conf, enter:
# vi /etc/httpd/conf/httpd.conf
Make sure SSL is used for /var/www/html/ssl and set other options for the same, enter:
 
<Directory /var/www/html/ssl>
         SSLRequireSSL
         SSLOptions +StrictRequire
         SSLRequire %{HTTP_HOST} eq "www.nixcraft.com"
         ErrorDocument 403 https://www.nixcraft.com/sslerror.html
</Directory>
 
Now, you can upload ssl specific php or html pages in /var/www/html/ssl directory and can access them by visiting https://www.nixcraft.com/ url. Do not forgot to restart Apache:
# service httpd restart

Firewall Configuration

Edit /etc/sysconfig/iptables. Add the following lines, ensuring that they appear before the final DROP lines:
-A RH-Firewall-1-INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT
Save and close the file. Restart the firewall:
# service iptables restart

Configuring Tomcat To Use SSL


Setting up SSL for Tomcat can be pided into two main tasks: creating a functional keystore, andconfiguring the Tomcat connectors and applications.  Let's tackle them one at a time.

PART I - The Keystore

Step 1 - Creating the Keystore

The keys Tomcat will use for SSL transactions are stored in a password-protected file called, creatively, the "keystore."  The first step to enabling SSL on your server is to create and edit this file.  You can create this file in one of two ways - by importing an existing key into the keystore, or by creating an entirely new key.  
In the interest of simplicity, this guide will only cover the latter (but you can find instructions for importing keys on Apache's Tomcat Documentation site).
A program called keytool, which is included with your JDK, will do the actual work of creating your new keystore.  To create a new keystore using this program, enter the following command at the command-line, substituting syntax appropriate for your OS:
$JAVA_HOME/bin/keytool -genkey -alias [youralias] -keyalg RSA -keystore [/preferred/keystore/path]
Use an [alias] and [path] of your choice.
Next, keytool will ask you to enter the password you want to use for the keystore.  Again, choose whatever you like (but don't forget it).
After you choose the keystore password, you will enter the information required for the Certificate, such as your company and your name.  Make sure this information is accurate, as you will have to submit this file to the Certificate Authority of your choice to obtain a certificate.
The last thing keytool will ask you to specify is the key password, which is the password specific to this specific certificates.  Rather than enter anything at this prompt, just press ENTER.  
This will cause keytool to set the key password to a value equivalent to the keystore password.  Matching passwords are REQUIRED for Tomcat to access the certificate.  If you choose two different passwords, any attempts to access the keystore will result in a crash (so don't do it).  
Congratulations - if you followed the directions correctly, you should now have a usable keystore file named [youralias], located in the directory you chose.

Step 2 - Creating the Certificate Signing Request

Now that you've created your keystore, it's time to create a file called the Certificate Signing Request, or CSR, which will be used by the Certificate Authority of your choice to generate the Certificate SSL will present to other parties during the handshake.
You can use the keytool to create this file, as well.  To do so, enter the following at the command line:
$JAVA_HOME/bin/keytool -certreq -keyalg RSA -alias [youralias] -file [yourcertificatname].csr -keystore [path/to/your/keystore]
Substitute the values you chose earlier for the [placeholders].  
If you follow the instructions correctly, keytool will create a file called yourcertificatename.csr, which you can submit to the CA you've chosen via the process they provide on their website.  Using this file, they will generate a custom certificate for your server, which you can download according to the instructions they provide on their website.

Step 3 - Installing Your New Certificate

Getting tired yet?  Don't worry - there's light at the end of the tunnel.  This is the last thing you'll need to do to create a keystore for Tomcat to use.  Well, the last two things.  Hang in there!
SSL verifies the authenticity of a site's certificate by using something called a "chain of trust," which basically means that during the handshake, SSL initiates an additional handshake with the Certificate Authority specified in your site's certificate, to verify that you haven't simply made up your own CA.  
In order to "anchor" your certificate's chain of trust, you have to download an additional certificate, called a "Root Certificate," from your CA, and then import both this certificate and your site's new certificate into your keystore.  Your CA should provide information about obtaining a Root Certificate on their website.   
Once you've downloaded both your own Certificate and the Root certificate provided by your CA, import them into your keystore with the following commands, replacing the [placeholders]:
To import the Root Certificate -
keytool -import -alias root -keystore [path/to/your/keystore] -trustcacerts -file [path/to/the/root_certificate]
To import your new Certificate - 
keytool -import -alias [youralias] -keystore [path/to/your/keystore] -file [path/to/your_keystore]
Do everything right?  Then pat yourself on the back - you are now the proud owner of a functional, certified keystore.

PART II - Configuring Tomcat to use SSL

Now that we have a functional keystore populated with valid certificates, it's time to configure Tomcat to use SSL.  First, we'll configure Tomcat's SSL connectors, and then we'll specify which webapps we want to use SSL by default. 

Step 1 - Configuring Tomcat's SSL Connectors

Tomcat's global Connector options are configured in Tomcat's main configuration file, "$CATALINA_BASE/conf/server.xml", so you should open this file now.  The Connectors we are looking for connect on port 8443 by default, so search for this port, until you come across an entry that looks like this:
<!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the JSSE configuration, when using APR, the 
         connector should be using the OpenSSL style configuration
         described in the APR documentation -->
    <!--
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->
You'll notice that the comment enclosing this connector talks about a choice between APR and JSSE configurations.  This refers to the implementation of SSL you are intending to use.  JSSE, which is Tomcat's default configuration, is supported by default, and included in all JDKs after version 1.4.  So if you don't even know what APR is, you only need to uncomment this entry, and add some additional information to allow Tomcat to find your keystore:
<Connector port="8443" maxThreads="150" scheme="https" secure="true" SSLEnabled="true" keystoreFile="path/to/your/keystore" keystorePass="YourKeystorePassword" clientAuth="false" keyAlias="yourAlias" sslProtocol="TLS"/>
If, on the other hand, you know that using the Apache Portable Runtime (APR), also known as Tomcat's "native library," is by far the best practice to follow, especially when using Tomcat as a standalone web server (which you probably are), and have already installed it on your server, then you'll need to alter this entry as follows to allow Tomcat to use APR's OpenSSL implementation in place of JSSE, or trying to use SSL will generate an error:
<Connector port="8443" scheme="https" secure="true" SSLEnabled="true" SSLCertificateFile="/path/to/your/certificate.crt" SSLCertificateKeyFile="/path/to/your/keyfile" SSLPassword="YourKeystorePassword" SSLCertificateChainFile="path/to/your/root/certificate" keyAlias="yourAlias" SSLProtocol="TLSv1"/>
Notice that if you are using APR, the "SSLCertificateFile" and "SSLCertificateKey"-type attributes are used in place of the keystoreFile attribute.  For more information on the differences between using APR in place of JSSE, consult Apache's Tomcat APR Documentation
Restart Tomcat.  Once you're up and running again, test your configuration by connecting to a secure page, using a URL such as https://[yourhost]:8443.  If you followed the directions correctly, you should be able to view the page over a secure HTTPS connection!

Step 2 - Limiting SSL Usage

Enabling SSL in Tomcat's server.xml file causes all files to be run both as secure and insecure pages, which can cause unnecessary server load.  You can choose which applications offer SSL connections on a per-application basis by adding the following <security-constraint> element to the application's WEB-INF/web.xml file:
<security-constraint>
<web-resource-collection>
<web-resource-name>YourAppsName</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
This configuration allows you to set SSL options for all an application's pages in one place.  For example, to disable SSL for all your application's pages, change "CONFIDENTIAL" to "NONE".

Additional Considerations

In the interest of simplicity, this guide does not cover all of the elements of SSL configuration (although they are covered extensively in Apache's Tomcat SSL Documentation).  We will, however, provide you with a short list of other options and important areas of note to consider as you tweak your SSL configuration.

Specifying Implementation

If you have configured connectors for both APR and JSSE, Tomcat will use APR by default if you have installed the native libraries.  You can force it to use JSSE by modifying a Connector's "protocol" attribute as follows:
<Connector protocol="org.apache.coyote.http11.HTTP11NioProtocol">
If you want to force APR, you can do so with a similar edit:
<Connector protocol="org.apache.coyote.http11.Http11AprProtocol">

Common Errors Caused By Aliases and Passwords

If you encounter any errors with your SSL configuration, make sure that you have correctly entered settings such as keystore passwords and aliases.  These values are case sensitive for some of the supported keystore formats.

Unix Sed Tutorial: Append, Insert, Replace, and Count File Lines


Source: http://www.thegeekstuff.com/
This article is part of the on going Unix sed command tutorial series. In our previous articles we learned sed with single commands — printingdeletionsubstituteand file write.
Sed provides lot of commands to perform number of operations with the lines in a file.
In this article let us review how to append, insert, replace a line in a file and how to get line numbers of a file.
Let us first create thegeekstuff.txt file that will be used in all the examples mentioned below.
$cat thegeekstuff.txt
Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.

Append Lines Using Sed Command

Sed provides the command “a” which appends a line after every line with the address or pattern.
Syntax:

#sed 'ADDRESS a\
 Line which you want to append' filename

#sed '/PATTERN/ a\
 Line which you want to append' filename

Sed Append Example 1. Add a line after the 3rd line of the file.

Add the line “Cool gadgets and websites” after the 3rd line. sed “a” command inserts the line after match.
$ sed '3 a\
> Cool gadgets and websites' thegeekstuff.txt

Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Cool gadgets and websites
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.

Sed Append Example 2. Append a line after every line matching the pattern

The below sed command will add the line “Linux Scripting” after every line that matches the pattern “Sysadmin”.
$ sed '/Sysadmin/a \
> Linux Scripting' thegeekstuff.txt

Linux Sysadmin
Linux Scripting
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.
Linux Scripting

Sed Append Example 3. Append a line at the end of the file

The following example, appends the line “Website Design” at the end of the file.
$ sed '$ a\
> Website Design' thegeekstuff.txt

Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.
Website Design

Insert Lines Using Sed Command

Sed command “i” is used to insert a line before every line with the range or pattern.
Syntax:

#sed 'ADDRESS i\
 Line which you want to insert' filename

#sed '/PATTERN/ i\
 Line which you want to insert' filename

Sed Insert Example 1. Add a line before the 4th line of the line.

Add a line “Cool gadgets and websites” before 4th line. “a” command inserts the line after match whereas “i” inserts before match.
$ sed '4 i\
> Cool gadgets and websites' thegeekstuff.txt

Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Cool gadgets and websites
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.

Sed Insert Example 2. Insert a line before every line with the pattern

The below sed command will add a line “Linux Scripting” before every line that matches with the pattern called ‘Sysadmin”.
$ sed '/Sysadmin/i \
> Linux Scripting' thegeekstuff.txt

Linux Scripting
Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Linux Scripting
Windows- Sysadmin, reboot etc.

Sed Insert Example 3. Insert a line before the last line of the file.

Append a line “Website Design” before the last line of the file.
$ sed '$ i\
> Website Design' thegeekstuff.txt

Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Website Design
Windows- Sysadmin, reboot etc.

Replace Lines Using Sed Command

“c” command in sed used to replace every line matches with the pattern or ranges with the new given line.
Syntax:

#sed 'ADDRESS c\
 new line' filename

#sed '/PATTERN/ c\
 new line' filename

Sed Replace Example 1. Replace a first line of the file

The below command replaces the first line of the file with the “The Geek Stuff”.
$ sed '1 c\
> The Geek Stuff' thegeekstuff.txt

The Geek Stuff
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.

Sed Replace Example 2. Replace a line which matches the pattern

Replace everyline which has a pattern “Linux Sysadmin” to “Linux Sysadmin – Scripting”.
$ sed '/Linux Sysadmin/c \
> Linux Sysadmin - Scripting' thegeekstuff.txt

Linux Sysadmin - Scripting
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.

Sed Replace Example 3. Replace the last line of the file

Sed command given below replaces the last line of the file with “Last Line of the file”.
$ sed '$ c\
> Last line of the file' thegeekstuff.txt

Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Last line of the file

Print Line Numbers Using Sed Command

“=” is a command in sed to print the current line number to the standard output.
Syntax:

#sed '=' filename
The above send command syntax prints line number in the first line and the original line from the file in the next line .
sed ‘=’ command accepts only one address, so if you want to print line number for a range of lines, you must use the curly braces.
Syntax:

# sed -n '/PATTERN/,/PATTERN/ {
=
p
}' filename

Sed Line Number Example 1. Find the line number which contains the pattern

The below sed command prints the line number for which matches with the pattern “Databases”
$ sed -n '/Databases/=' thegeekstuff.txt

2

Sed Line Number Example 2. Printing Range of line numbers

Print the line numbers for the lines matches from the pattern “Oracle” to “Productivity”.
$ sed -n '/Oracle/,/Productivity/{
> =
> p
> }' thegeekstuff.txt

2
Databases - Oracle, mySQL etc.
3
Security (Firewall, Network, Online Security etc)
4
Storage in Linux
5
Productivity (Too many technologies to explore, not much time available)

Sed Line Number Example 3. Print the total number of lines in a file

Line number of the last line of the file will be the total lines in a file. Pattern $ specifies the last line of the file.
$ sed -n '$=' thegeekstuff.txt

6

Unix Sed Tutorial: Printing File Lines using Address and Patterns

Source: http://www.thegeekstuff.com/


Let us review how to print file lines using address and patterns in this first part of sed tutorial.
We’ll be posting several awesome sed tutorials with examples in the upcoming weeks.

Unix Sed Introduction

  • sed is a “non-interactive” stream-oriented editor. Since its an “non-interactive” it can be used to automate editing if desired.
  • The name sed is an abbreviation for stream editor, and the utility derives many of its commands from the ed line-editor (ed was the first UNIX text editor).
  • This allows you to edit multiple files, or to perform common editing operations without ever having to open vi or emacs.
  • sed reads from a file or from its standard input, and outputs to its standard output.
  • sed has two buffers which are called pattern buffer and hold buffer. Both are initially empty.

Unix Sed Working methodology

This is called as one execution cycle. Cycle continues till end of file/input is reached.
  1. Read a entire line from stdin/file.
  2. Removes any trailing newline.
  3. Places the line, in its pattern buffer.
  4. Modify the pattern buffer according to the supplied commands.
  5. Print the pattern buffer to stdout.

Printing Operation in Sed

Linux Sed command allows you to print only specific lines based on the line number or pattern matches. “p” is a command for printing the data from the pattern buffer.
To suppress automatic printing of pattern space use -n command with sed. sed -n option will not print anything, unless an explicit request to print is found.
Syntax:
# sed -n 'ADDRESS'p filename

# sed -n '/PATTERN/p' filename
Let us first create thegeekstuff.txt file that will be used in all the examples mentioned below.


# cat thegeekstuff.txt
1. Linux - Sysadmin, Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Hardware
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.

5 Sed ADDRESS Format Examples

Sed Address Format 1: NUMBER

This will match only Nth line in the input.

# sed -n ‘N’p filename
For example, 3p prints third line of input file thegeekstuff.txt as shown below.
# sed -n '3'p thegeekstuff.txt
3. Hardware

Sed Address Format 2: NUMBER1~NUMBER2

M~N with “p” command prints every Nth line starting from line M.

# sed -n ‘M~N’p filename
For example, 3~2p prints every 2nd line starting from 3rd line as shown below.
# sed -n '3~2'p thegeekstuff.txt
3. Hardware
5. Storage
7. Productivity (Too many technologies to explore, not much time available)
9. Software Development

Sed Address Format 3: START,END

M,N with “p” command prints Mth line to Nth line.

# sed -n ‘M,N’p filename
For example, 4,8p prints from 4th line to 8th line from input file thegeekstuff.txt
# sed -n '4,8'p thegeekstuff.txt
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design

Sed Address Format 4: ‘$’ Last Line

$ with “p” command matches only the last line from the input.

# sed -n ‘$’p filename
For example, $p prints only the last line as shown below.
# sed -n '$'p thegeekstuff.txt
10.Windows- Sysadmin, reboot etc.

Sed Address Format 5: NUMBER,$

N,$ with “p” command prints from Nth line to end of file.

# sed -n ‘N,$p’ filename
For example 4,$p prints from 4th line to end of file.
# sed -n '4,$p' thegeekstuff.txt
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.

6 Sed PATTERN Format Examples

Sed Pattern Format 1: PATTERN

PATTERN could be unix regular expression. The below command prints only the line which matches the given pattern.

# sed -n /PATTERN/p filename
For example, following prints the line only which matches the pattern “Sysadmin”.
# sed -n /Sysadmin/p thegeekstuff.txt
1. Linux - Sysadmin, Scripting etc.
10.Windows- Sysadmin, reboot etc.

Sed Pattern Format 2: /PATTERN/,ADDRESS


# sed -n ‘/PATTERN/,Np’ filename
For example, following prints lines which matches the pattern to Nth line, from input. 3rd line matches the pattern “Hardware”, so it prints from 3rd line to 6th line.
# sed -n '/Hardware/,6p' thegeekstuff.txt
3. Hardware
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites

Sed Pattern Format 3: ADDRESS,/PATTERN/

It prints from the Nth line of the input, to the line which matches the pattern. If the pattern doesnt match, it prints upto end of the input.

# sed -n ‘N,/PATTERN/p’ filename
For example, 4th line matches the pattern “Security”, so it prints from 3rd line to 4th line.
# sed -n '3,/Security/p' thegeekstuff.txt
3. Hardware
4. Security (Firewall, Network, Online Security etc)

Sed Pattern Format 4: /PATTERN/,$

It prints from the line matches the given pattern to end of file.

# sed -n ‘/PATTERN/,$p’ filename
# sed -n '/Website/,$p' thegeekstuff.txt
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.

Sed Pattern Format 5: /PATTERN/,+N

It prints the lines which matches the pattern and next N lines following the matched line.

# sed -n ‘/PATTERN/,+Np’ filename
For example, following prints the 5th line which matches the pattern /Storage/ and next two lines following /Storage/.
# sed -n '/Storage/,+2p' thegeekstuff.txt
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)

Sed Pattern Format 6: /PATTERN/,/PATTERN/

Prints the section of file between two regular expression (including the matched line ).

# sed -n ‘/P1/,/P2/p’ filename
For example, 5th line matches “Storage” and 8th line matches “Design”, so it prints 5th to 8th.
# sed -n '/Storage/,/Design/p' thegeekstuff.txt
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design

Thursday, June 14, 2012

Fixed: Can't test without successful make


Today, when I was installing some missing Perl modules for SpamAssassin on a bare Ubuntu 8.04 LTS Hardy Heron installation I had to use the CPAN shell for a few of those modules. But when I tried to install the first of the Perl modules not available via apt, IP::Country::Fast, I got the following output:
Warning: prerequisite Geography::Countries  not found.
Writing Makefile for IP::Country
    -- NOT OK
Running make test
  Can't test without successful make
Running make install
  make had returned bad status, install seems impossible
At first I thought this had to do with the Perl module Geography::Countries not being installed as well, so I tried to install that one via the CPAN shell. But alas, this also resulted in the "NOT OK" message:
Writing Makefile for Geography::Countries
    -- NOT OK
Running make test
  Can't test without successful make
Running make install
  make had returned bad status, install seems impossible
After some thinking I realised that I was working on a bare Ubuntu installation and make might be missing... And since I had used the default configuration of CPAN instead of manual configuration the first time I started the CPAN shell using perl -MCPAN -e shell I overlooked this. On the other hand the "NOT OK" warning CPAN gives is extremely unclear.
Anyway, I left the CPAN shell and used apt-get install make to install make. Next I used which make to obtain the path to make, since I had to configure this into the CPAN shell.
Back into the CPAN shell I first verified the current setting of the make option using o conf make:
cpan> o conf make
    make   
The above might be read as "the current value of make is make" but it actually shows the name of the option, make, followed by its value, which is empty.
After I set the value of make to the path of make and commited this change, see commands and output below, I was able to install the required Perl modules.
cpan> o conf make /usr/bin/make
    make               /usr/bin/make

cpan> o conf commit
commit: wrote /etc/perl/CPAN/Config.pm
Well... until I had to install a module that required a C++ compiler.