*NIX Tricks

[linux] How to install a package from the source code

Posted in linux, osx by kousik on October 31, 2009

Installing a package (AKA software) from source code is often considered to be the secret cult of the worshipers of the Geek God. But, you know, it’s not that difficult. Let’s see how we can do it.

First, some preliminaries. Why should I build it form the source code? Well, source codes are more or less platform independent. If you have a suitable compiler and necessary libraries, you may install it on any platform of your choice. Source code files are available  in ASCII plain text  format (not binaries) — so you can study it and understand what it does and whether/ how it may affect your system. Reading  the description of a software distributed as a binary does not even come close to that — you really don’t know what that piece of  software does until you actually run it. Needless to add, you may include your changes to the open-source code to suit your needs as well in accordance with the relevant license.

Secondly, where can I find the source code? The source code for a open-source package is often distributed in compressed format (i.e., with an extension “tar.gz”, “tgz” or “tar.bz2” — that’s why it’s also called tarball). You may download the tarball from a reliable source, e.g. homepage of the package developer, Free Software Directory, Open Source Software directory, linux.org, Ubuntu archive, Darwin ports, Fink and Open Source Apple website. Last three are specifically intended for OSX running Darwin. Wikipedia also has a nice list of Free and Open Source softwares.

Now, let’s do the installation step-by step. Create a directory (say, ~/packages) where you want to save the packages, navigate to that directory, save the tarball (say, newpack.tar.gz) there and uncompress it:

$ tar zxvf newpack.tar.gz

Replace “zxvf” by “jxvf” if it has a “tar.bz2” extension instead of “tar.gz” or “tgz”.

Then navigate to the uncompressed directory (usually newpack, but be sure of  that first using the ls command) using

$ cd newpack

and read the instruction files (e.g., INSTRUCTIONS, INSTALL, README), if any, for special instructions using your favorite text editor. Otherwise, the normal procedure for installation is the following series of commands: (wait for each one to finish before you issue the next command!)

$ ./configure
$ make
$ sudo make install

You need to enter the superuser password in the last step as it copies the executable in the system’s executable directory (e.g., /bin, /sbin/). That’s it!

However, you may want to clean up the mess (especially the object files) created by the installation process using:

$ make clean

In a spree of cleaning up, however, don’t delete the file named Makefile: you need this if you want to uninstall the package (the above command won’t delete it).

In order to uninstall the package, navigate to the same directory (~/packages/newpack/), and issue the following command:

$ sudo make uninstall

You need to enter the superuser password for this.

Reference: here.

[bash] SSH to remote servers accessible only through a central server

Posted in bash, ssh by kousik on October 9, 2009

We have several Red hat servers node001 through node065 which are only accessible from outside via a central server, central.com. I can think of three different solutions to connect to a node, say node010, via ssh:
1. First SSH to central.com then to node010
2. Forward port 22 of node010 to local machine’s port 2200 via central.com
$ ssh kousik@central.com -L 2200:node010:22
Then ssh to localhost’s port 2200
$ ssh localhost -p 2200
3. $ ssh -YC -t kousik@central.com ssh -YC kousik@node$num

It’s the last one that caught my attention recently — so much so that I put a whole function in the ~/.bash_aliases on my local machine:

#
# A function to ssh to a node: put this in the ~/.bash_aliases file
# The input is the node number
# Example: to ssh to node010 type go2 followed by either 10, 0010 or 0000010 .....
# ..... which is `arithmetically' equal to 10.
#
go2() { num="$*";
  echo "you entered '"$num"' ";
  num="${num#[Nn]ode}"; # Ignore "node", if entered, and consider the rest 
  if [ "$num" -ge 1 2>& /dev/null ] && [ "$num" -le 65 2>&/dev/null ]; then
     num=`echo $num|bc` # for correct arithmetic conversion to a decimal integer
     if [ $num -lt 10 ]; then
        num="00$num";
     else
        num="0$num";
     fi;
     echo "Logging on to  node$num in 1s";
     sleep 1;
     ssh -YC -t kousik@central.com ssh -YC kousik@node$num;
  else
     echo "Please enter a valid integer (1--65)"
  fi;
}

The first conditional statement checks to see if the input is really an integer and is within the correct range: 0-65. The redirections of STDERR to /dev/null are there to suppress appearance of error messages due to non-integer inputs. The reason for using bc for correct arithmetic expansion may be clear from my comment here.

Tagged with: , , , ,

[Network] Use VNC to connect to a remote Linux machine

Posted in network by kousik on September 26, 2009

Let’s assume that you are on computer A and you wan to connect to a remote linux machine B using VNC. You need to follow these steps in order to achieve just that:
1. Install x11vnc and openssh-server on B. The corresponding daemons should start automatically, but if not, then
$ /sbin/service sshd start
$ vncserver :0 -localhost
.

2. Make sure that on B the firewall (if installed and active) allows connection to port 22 from anywhere (for ssh connection) and to 5900 from localhost (i.e., 127.0.0.1). These are the commands (in case of ufw as the firewall) to allow traffic to the specific ports.
$ sudo ufw allow 22
$sudo ufw allow from 127.0.0.1 to any port 5900

3. If B is behind a router firewall, you need to forward port 22 to the router. (You should do this in the router configuration page).
4. On A, create a newfile, vnc2b.sh and enter the following


#!/bin/sh
ssh -f -L 5900:localhost:5900 username@remote_hostname \
x11vnc -safer -localhost -nopw -once -display :0 \
&& sleep 5 \
&& vncviewer localhost:0

and make the file executable:
$ chmode u+x vnc2b.sh.

Next time when you want to vnc to B, just type:
$ ./vnc2b.sh

Reference: here.

Tagged with: , , , ,

[network] Port forwarding via SSH

Posted in network, ssh by kousik on September 14, 2009

Let’s say you have physical access to your home computer h.com and your work computer w.com and besides that you work on a remote server r.com. Let’s also assume that the usernames are huser, wuser and ruser, respectively.

There may be various accessibility scenarios, but let’s just assume that you can access the remote servers from both home and work computers, but you cannot access home computer from work directly and vice versa. A possible solution to have all the computers accessible is forward a port on the remote machine to the local port 22 (we are interested in ssh connection only, at least for the time being!).

Forwarding remote port:
So at home before you leave for work, forward port 2222 of r.com to port 22 of local machine h.com
$ ssh ruser@r.com -R 22:localhost:2222
Type in localhost as it is! It creates a secure socket from remote machine’s port 2222 to local machine’s port 22, meaning the ssh traffic coming to port 2222 of r.com will be forwarded to port 22 of h.com.
When you get to work, first ssh to the remote machine from your work computer:
$ ssh ruser@r.com.
You can then access the files at your home while you are still on h.com by simply doing
$ ssh huser@r.com -p 2222 (you’ll be asked for the password for your home computer). The flag -p stands for port.

Now, before you leave your work, forward port 22 of w.com to another port (say, 2223) of r.com using the above method. At home, you first ssh to r.com; and the using
$ ssh wuser@r.com -p 2223 (you’ll be asked for the password for the work computer),
you may access files on your work computer.

Forwarding local port:
I don’t know how I can best use it yet, but anyway, here is how to do it along with one “possible” use of it. Let’s say from your home computer you cannot ssh to r2.com, another remote server, but you can from r.com. In order to connect to r2.com form home, the most obvious way is to connect to r.com from h.com first, and then to r2.com. You have to do this for every new ssh connection r2.com from h.com. But if you forward an available local port (say, 2224) to port 22 of r2.com via r.com, it'll be a lot easier. Issue the following command while you are on h.com:
$ ssh ruser@r.com -L 2224:r2.com:22.
This will enable you to connect to r2.com from h.com by using
$ ssh r2user@localhost -p 2224
in another terminal on the home computer (r2user is the username for r2.com).

File copying between those computers also becomes a breeze:
$ scp -P 2224 file_to_be_copied r2user@localhost:/desired_dir/copied_file
(notice the capital P).

N.B. (1) In order to check if a port (say, 2225) is available on the locahost, try
$ nc localhost 2225,
(2) Needless to say, you must have sshd running on the machines that you want to connect to via ssh.

Follow

Get every new post delivered to your Inbox.