Sunday, November 30, 2014

Passwordless SSH Login - Linux

Passwordless SSH login
Hate typing your SSH password every time you need to login remotely to your machine? Set up public key authentication, and you can securely login without ever having to type a password.
For this guide, let's say I have two machines, a laptop and a desktop. I move around a lot with my laptop (SSH client), and frequently need to access files or run commands on my desktop (SSH server) but hate typing the password to connect each time.
Ensure proper configuration
Assumption: OpenSSH is used to login remotely using a password. If not, usually all you have to do is install openssh-server on the server and openssh-client on the client. You might also have to open up TCP port 22 on the server if that's not done for you.
Pretty much all modern distros (certainly all the popular ones) will have pubkey authentication enabled in the SSH configuration. On the server, it's enabled as long as you do NOT see this line in /etc/ssh/sshd_config:
On the client, pubkey authentication is enabled as long as that line appears in neither /etc/ssh/ssh_config nor ~/.ssh/config.

Generating a public/private key pair
To begin setting up public key authentication, you'll need to first generate public and private keys on the client. Generating a key pair is simple. Just run ssh-keygen and push enter a lot to accept the defaults:

[vikas@client ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/vikas/.ssh/id_rsa):
Created directory '/home/vikas/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/vikas/.ssh/id_rsa.
Your public key has been saved in /home/vikas/.ssh/id_rsa.pub.
The key fingerprint is:
2b:7d:96:a2:c7:d9:67:6f:9e:d7:0d:f2:07:5a:77:18
Now, in ~/.ssh/, you'll have two files. id_rsa is the private key, which you must keep secret. id_rsa.pub is the public key, which we're going to copy to the server.

Putting the key on the server
Now we need to copy the public key to the server and add it to the list of authorized keys. First, scp the key to the server:

[vikas@client ~]$ scp ~/.ssh/id_rsa.pub vikas@serverIP
vikas@serverIP  password:
id_rsa.pub                                    100%  393     0.4KB/s   00:00
Next, log on to the server and append the public key to the server's authorized_keys file:

[vikas@client ~]$ ssh vikas@serverIP
vikas@serverIP password:
Last login: Sun Nov  30 16:04:12 2014 from client.vikas
[vikas@serverIP ~]$ cat id_rsa.pub >> ~/.ssh/authorized_keys
[vikas@serverIP ~]$ rm id_rsa.pub
Done!

Now if you try to make an SSH connection, the server will have the client's public key and the client will have its private key, and magic will happen. You should not be prompted for a password any more:

[vikas@client ~]$ ssh vikas@serverIP
Last login: Sun Nov  30 16:08:55 2014 from client.vikas
[vikas@serverIP ~]$
ssh, scp, sftp, and any GUI tools built on top of these (like KDE's fish KIOslave) will no longer prompt for a password.

One caveat

I want to point out one more thing. The most common cause for SSH public key authentication mysteriously not working is incorrect file permissions on the ~/.ssh/ files. As a general rule, your file permissions (on both machines) should be at least as restrictive as:

~/.ssh/                      rwx------ (700)
~/.ssh/authorized_keys       rw-r--r-- (644)
~/.ssh/id_rsa                rw------- (600)
~/.ssh/id_rsa.pub            rw-r--r-- (644)

Wednesday, July 2, 2014

Java Wrapper Class

Java is an object-oriented language and can view everything as an object. A simple file can be treated as an object (with java.io.File), an address of a system can be seen as an object (with java.util.URL), an image can be treated as an object (with java.awt.Image) and a simple data type can be converted into an object (with wrapper classes). Wrapper classes are used to convert any data type into an object.

The primitive data types are not objects; they do not belong to any class; they are defined in the language itself. Sometimes, it is required to convert data types into objects in Java language. For example, upto JDK1.4, the data structures accept only objects to store. A data type is to be converted into an object and then added to a Stack or Vector etc. For this conversion, the designers introduced wrapper classes.

What are Wrapper classes?
As the name says, a wrapper class wraps (encloses) around a data type and gives it an object appearance. Wherever, the data type is required as an object, this object can be used. Wrapper classes include methods to unwrap the object and give back the data type. It can be compared with a chocolate. The manufacturer wraps the chocolate with some foil or paper to prevent from pollution. The user takes the chocolate, removes and throws the wrapper and eats it.
Observe the following conversion.
int k = 100;
Integer it1 = new Integer(k);
The int data type k is converted into an object, it1 using Integer class. The it1 object can be used in Java programming wherever k is required an object.

The following code can be used to unwrap (getting back int from Integer object) the object it1.
int m = it1.intValue();
System.out.println(m*m); // prints 10000
intValue() is a method of Integer class that returns an int data type.

List of Wrapper Class
Primitive data typeWrapper class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean