Sunday, February 28, 2016

How to Compile the Latest Development Version of GNU Package - Findutils

In this post, I will discuss how to download and compile the latest development version of findutils GNU package. Findutils package consists of find, locate, updatedb, and xargs. More info and stable releases of this package can be found at http://www.gnu.org/software/findutils/. This post intends to help people contribute to free and open source packages and programs, as GNU/Linux and GPL have changed the world significantly, and we can be all part of the movement. Although I will only discuss the Findutils package here, most of other GNU packages can be obtained in the similar manner.

The instruction here will be based on Ubuntu 14.04 LTS 64bit version.
We need to install necessary packages first as usual:
$ sudo apt-get install -y autoconf automake git gettext m4 dejagnu autopoint bison texinfo

If you are trying to build the package on Mac OS X, you will need to install development tools first.
$ sudo xcode-select --install

Then you would want to install homebrew.
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then, you will need to install the necessary packages with
$ brew install autoconf automake git gettext dejagnu bison texinfo

Finally, you will need to create a symlink of autopoint
$ sudo ln -s /usr/local/Cellar/gettext/0.19.7/bin/autopoint /usr/local/bin/autopoint

Now, you are ready to follow along for both Linux and Mac OS X.

We will download the latest development version using git
$ git clone git://git.sv.gnu.org/findutils

When complete, go to the findutils directory and run bootstrap
$ cd findutils && ./bootstrap

It will probably complain about gettext version.
autopoint: *** The AM_GNU_GETTEXT_VERSION declaration in your configure.ac
               file requires the infrastructure from gettext-0.19.3 but this version
               is older. Please upgrade to gettext-0.19.3 or newer.
autopoint: *** Stop.

To see what version of gettext is currently installed, we can run
$ gettext --version
I am getting 0.18.3 as of now. This is the latest version available from the Ubuntu repository. We can manually install the newer version by building the executable from the source.

Let's then download and build the latest gettext!
$ cd .. && wget https://ftp.gnu.org/gnu/gettext/gettext-latest.tar.gz

Unarchive the source files
$ tar xfz gettext-latest.tar.gz

Let's configure it!
$ mkdir gettext-0.19.7/build && cd gettext-0.19.7/build
$ ../configure

When complete, we can compile it
$ make -j 2
where the number 2 represents the desired # of cores to use when compiling.

By the way, if you intend to debug the source code, you may want to define a macro DEBUG
$ make -j 2 CFLAGS="-D DEBUG -g"
which sets the option -D DEBUG and -g to the compiler.

After some time, it should build successfully. We are now ready to install the latest get text on the system.
$ sudo make install

Finally, we should now have the latest version of gettext
$ gettext --version
which returns 0.19.7 as expected.

OK, let's go back to findutils and continue our process of bootstrapping
$ cd ../../findutils/ && ./bootstrap

The last step is to simply configure and compile!
$ mkdir build && cd build
$ ./configure
$ make -j 2

When complete, we can execute the new program and verify if it works
$ find/find --version
$ xargs/xargs --version
$ locate/locate --version

They all should say version 4.7.0-git as of today. The latest stable release findutils version is 4.6.0 as can be seen from http://ftp.gnu.org/pub/gnu/findutils/.

You could install it on the system if you want, but I would not recommend so, as it is not a stable release version yet.


Thursday, February 18, 2016

Setup SSH Login Email Alert on Ubuntu 14.04 or Debian

Anytime one enables ssh server, the machine will be at great risk from random attempts all around the world. It may be wise to setup an alert mail when someone logs into the machine remotely. In this post, we will look into how to do so.

I will make use of mail.mailutils program to send emails out here, but there are many other alternative mail out packages.
$ sudo apt-get install -y mailutils postfix
When prompted with postfix configuration, just choose the default setting, which is Internet Site.

Next, create a bash script file that will be executed when someone logs in remotely. In this example, I will place it in the /etc/ssh directory.
$ sudo vim /etc/ssh/ssh_alert.sh

Add the following content to the file
#!/bin/bash
# replace with sender's email address
sender="sender_address@some_mail.com"
# replace with recipient's email address
recipient="recipient_address@some_mail.com"
time=$(date)
if [ "$PAM_TYPE" != "close_session" ]; then
# replace with host name
host="ubuntu-server"
subject="SSH Login: $PAM_USER from $PAM_RHOST on $host at $time"

message="SSH login $PAM_USER from $PAM_RHOST at $time on $host"
echo "$message" | mail.mailutils -r "$sender" -s "$subject" "$recipient"
fi

Next, enable execute
$ sudo chmod u+x /etc/ssh/ssh_alert.sh

Now, we need to make sure that the script actually works. To do so, simply run it
$ sudo /etc/ssh/ssh_alert.sh

If you see a message saying that the email was sent successfully, then you can set it up so that a ssh remote login will execute the script and allow the remote login only if the email was successfully sent. To see whether the execution returns successful, type in
$ echo $?
The output of 0 means successful.

When the mail out is successful, open up /etc/pam.d/sshd
$ sudo vim /etc/pam.d/sshd

Add the following line to /etc/pam/sshd
session required pam_exec.so seteuid /etc/ssh/ssh_alert.sh

That's it. You should now receive the email when someone logs into the server through ssh.

Mount NFS Directory from Ubuntu and Mac OS X Clients

Here, I assume that you have set the NFS server following the previous post. In this post, I will show how the client machine can be configured to connect to the NFS directory.

Make sure that the client's IP address is specified in the server's /etc/export file. In the previous post, this should be 192.168.0.101. Also, I am going to assume that the NFS server IP address is 111.111.111.111.

Instructions for Ubuntu Client
First, install necessary package
$ sudo apt-get install -y nfs-common

Create a mount point
$ sudo mkdir -p /mnt/nfs

Mount the NFS directory
$ sudo mount 111.111.111.111:/var/nfs /mnt/nfs

That's it. To unmount, simply type in
$ sudo umount /mnt/nfs


Instructions for Mac OS X Client
First, create a mount point
$ sudo mkdir -p /private/nfs

Mount the NFS directory
$ sudo mount -o resvport,rw -t nfs 111.111.111.111:/var/nfs /private/nfs

That's it. To unmount, simply type in
$ sudo umount /private/nfs


How to Setup Network File System (NFS) on Ubuntu 14.04

In this tutorial, I will cover how to setup Ubuntu 14.04 (either desktop or server edition) as a NFS server.

First, install nfs-kernel-server
$ sudo apt-get install -y nfs-kernel-server

Next, create a directory which will be used as a network file system. In this example, I will use /var/nfs folder
$ sudo mkdir /var/nfs

Change the owner of the directory
$ sudo chown nobody:nogroup /var/nfs

Change the permission of the directory if you want to allow the clients to have access to read/write
$ sudo chomod 777 /var/nfs

Modify the export config file to let it know which directory will be used as NFS
$ sudo vim /etc/exports

Add the following line to the /etc/exports file
/var/nfs 192.168.0.101(rw,sync,no_subtree_check)

Here, I am assuming that this directory will only be accessed by 192.168.0.101. You may want to change this address if you want. Also, make sure to set your DHCP to reserve this IP address to the desired client.

Create NFS table that holds exports
$ sudo exportfs -a

Finally, simply start up the server
$ sudo service nfs-kernel-server start

That's it. In the next post, I will discuss how to connect to NFS directory from the client side.

By the way, you may want to note the server's IP address
$ ifconfig

This IP address will be needed for a client to connect to, which will be discussed din the next post.