Monday, March 28, 2016

How to Enable Search History Feature with Bash

Assume your ~/.bash_history file has the following content:
gcc test.c
gcc -g test.c
cat test.c
vim test.c
ssh root@localhost
ssh root@192.168.1.1
g++ test.cpp
g++ test.cxx

Now, say you type
$ gcc[up arrow]

Normally, the [up arrow] key will simply give you the last command, that is
$ g++ test.cxx

However, this is quite annoying. When you type gcc first and pressing the [up arrow] should intuitively tell bash to search for all the previous commands starting with gcc. What you probably expected was
$ gcc -g test.c
or
$ gcc test.c

Well, there is a way to do this. Edit your ~/.bashrc file and add the following lines:
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'

NOTE: if you are on Mac OS X, append the above two lines to ~/.bash_profile instead.

Save the file and exit bash. Now when you restart bash, you should see search history feature enabled. That is, when you type
$ gcc[up arrow]
will show you
$ gcc -g test.c

Another [up arrow] will show you
$ gcc test.c

Similarly, typing
$ ssh[up arrow]
will show
$ ssh root@192.168.1.1

One more [up arrow] will yield
$ ssh root@localhost

Sunday, March 27, 2016

How to Build and Add Custom C Library

For those who are familiar with Matlab, you may know the functions tic and toc. These two functions are used to print out elapsed time in between tic and toc. Let us implement this in C and create a library, so that we can use these functions easily later.

First, create tictoc.c file with the following:
#include <tictoc.h>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>

/* indicates whether tic has been called */
static int tic_flag = 0;

/* tic timeval */
static struct timeval start;

void tic() {
    tic_flag = 1;
    if (gettimeofday(&start, NULL)) {
        fprintf(stderr, "libtictoc: error from gettimeofday()\n");
    }
}

void toc() {
    if (tic_flag == 0) {
        fprintf(stderr, "libtictoc: tic() has not been called\n");
        return;
    }
    struct timeval end;
    if (gettimeofday(&end, NULL)) {
        fprintf(stderr, "libtictoc: error from gettimeofday()\n");
        return;
    }
    printf("Time elapsed: %lfs\n", 
            (double) (end.tv_sec-start.tv_sec)
            + (double)(end.tv_usec-start.tv_usec)/1000000.0);
}


Next, create tictoc.h file with the following:
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

#ifndef _TICTOC_H_
#define _TICTOC_H_

extern void tic();
extern void toc();

#endif // _TICTOC_H_

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus

Note that the first three lines and the last three lines in the header enable C++ compatibility.

Now, let's see if we can compile this
$ gcc -c -Wall  -I . tictoc.c
You should see it compile successfully, and tictoc.o file has been created.

Make sure you have binutils installed on the system. For Ubuntu/Debian, simply run
$ sudo apt-get install -y binutils

For Mac OS X, run
$ brew install binutils

To create a library file, we run
$ ar -cvq libtictoc.a tictoc.o

This command will use tictoc.o file to create libtictoc.a library file. To examine the newly created file, run
$ ar -t libtictoc.a
You should see tictoc.o file listed.

We are now ready to copy the library file and the header file into the default user library and include folders.
$ sudo cp libtictoc.a /usr/local/lib
$ sudo cp tictoc.h /usr/local/include

Finally, we test whether the library works well. Create a test.c file with the following:
#include <tictoc.h>
#include <unistd.h>
int main() {
    tic();
    sleep(1);
    toc();
    sleep(2);
    toc();
    return 0;
}


Let's try to compile it
$ gcc -Wall test.c
You should see some error saying undefined reference to tic and toc. This is because we have not told gcc to look for libtictoc library. 

Well, let's do it.
$ gcc -Wall -l tictoc test.c
The -l option tells gcc to search for library, and the proceeding string (tictoc) preceded by lib will be the name of the library. That means, -l tictoc is asking gcc to look for a library file named libtictoc within the default library folders. Since we have copied over libtictoc.a file into /usr/local/lib, which is one of the default library search folders, gcc will be able to locate it. To find out the default load library path is, look into /etc/ld.so.conf file.

Note that .a extension is used for static library whereas .so extension is used for dynamically linked library.

It should compile now. Let's run it and see if it actually works.
$ ./a.out
You wills see something like time elapsed 1s and 3s, just as you expected!

Sunday, March 20, 2016

How to Read CPU Temperature in Ubuntu 14.04 LTS

Install the necessary package
$ sudo apt-get install lm-sensors 

Start the sensors
$ sudo sensors-detect
$ sudo service kmod start

Print out the measurement
$ sensors

That's it!

How to Setup Personal Cloud Server on Ubuntu 14.04 LTS using OwnCloud

This post is based on https://youtu.be/aotBluDGAAE.

First, download the latest build of owncloud, which is 9.0.0 at the time of writing this post.
$ wget https://download.owncloud.org/community/owncloud-9.0.0.tar.bz2

Extract the file using tar
$ tar fxj owncloud-9.0.0.tar.bz2

Install necessary packages (^ key is not a mistake below)
$ sudo apt-get install lamp-server^ php5-gd php5-curl php5-intl php5-mcrypt php5-imagick -y

During the installation of SQL server, it will ask for the root password. Make a note of this password, as it will be used in the setup later on.

Upload the directory to the server
$ sudo cp -rfp owncloud /var/www/html/

Change the permission of the server
$ sudo chown -R www-data:www-data /var/www/html/owncloud

A security measure
$ sudo a2enmod rewrite

Modify the apache configuration file
$ sudo vim /etc/apache2/apache2.conf

Change AllowOverride option to All under <Directory /var/www/>

Restart the server.
$ sudo service apache2 restart

Open up a web browser, and go to the address: http://server_ip/owncloud
where server_ip is the IP address of the owncloud server you just setup

Setup the administrator. In the first box, choose owncloud admin username and password. In the second box, enter root as the username and SQL password from the previous step.

That's it! When you install the desktop or mobile client, make sure to enter the server address as http://server_ip/owncloud.



NOTE: if you have firewall setup, you may need to open up the TCP port 80.

Thursday, March 3, 2016

How to Boot into Console Mode in Ubuntu

If you want to change the default boot option of Ubuntu Desktop into a console mode, here is how:

1. Backup the grub file
$ sudo cp -n /etc/default/grub /etc/default/grub.bk

2. Open up the grub file
$ sudo vim /etc/default/grub

3. Modify the menu entries
a) Comment out GRUB_CMD_LINE_LINUX_DEFAULT="quiet splash"
b) Change GRUB_CMD_LINE_LINUX="" to GRUB_CMD_LINE_LINUX="text"
c) Uncomment GRUB_TERMINAL=console

When complete, the file should look something like below:
...
GRUB_DEFAULT=0
GRUB_HIDDEN_TIMEOUT=0
GRUB_HIDDEN_TIMEOUT_QUIET=true
GRUB_TIMEOUT=10
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
#GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
GRUB_CMDLINE_LINUX="text"

# Uncomment to enable BadRAM filtering, modify to suit your needs
# This works with Linux (no patch required) and with any kernel that obtains
# the memory map information from GRUB (GNU Mach, kernel of FreeBSD ...)
#GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"

# Uncomment to disable graphical terminal (grub-pc only)
GRUB_TERMINAL=console
...

4. Update grub
$ sudo update-grub

Only for Ubuntu 15.10 or up that uses systemd:
$ sudo systemctl enable multi-user.target --force
$ sudo systemctl set-default multi-user.target

5. Reboot and see if it works
$ sudo reboot

By the way, to start GUI, simply run the following command after logging into the console
$ sudo service lightdm start

Tuesday, March 1, 2016

How to Debug Remote using gdbserver

In this post, I will go through the method of debugging remotely using gdbserver. This is useful when you want to debug a program that constantly requires user's input. For example, consider the simple program test.c below:

#include <stdio.h>
int main (int argc, char** argv) {
    printf("input a positive number: ");
    int i;
    scanf ("%d", &i);
    for (; i>0; i--)
       printf("%d\n", i); 
return 0;
}

This program will require a user input and behave accordingly. Let's start debugging this remotely.

First, we need to compile it of course,
$ gcc -g test.c

Next, we need to install gdbserver
$ sudo apt-get install gdbserver -y

Let's run gdbserver and open up connection
$ gdbserver :1234 a.out 10
It should be waiting for connection to be made on port 1234. Don't forget the argument to the program, which is 10 in this case.

Now, we will open up another terminal and start gdb of a.out binary we just compiled
$ gdb -q

In the gdb prompt, run
(gdb) target remote :1234
(gdb) b test.c:7
(gdb) c

Now, you will see interactive gdb running on one of the terminal windows and the program running on the other!

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.