Ubuntu: Two factor authentication for SSH

Secure Shell (SSH) is a cryptographic network protocol for secure data communication, remote shell services or command execution between two network connected computers. Usually the network is insecure over which the computers are connected.

Install the two factor authentication

Open a terminal session (Ctrl+Alt+T)

wget https://google-authenticator.googlecode.com/files/libpam-google-authenticator-1.0-source.tar.bz2
tar -xvf libpam-google-authenticator-1.0-source.tar.bz2

Google authenticator is fetched to the pwd.
To install authenticator:

sudo apt-get install libpam0g-dev
cd libpam-google-authenticator-1.0
make
sudo make install

Now run:

google-authenticator

This will now ask you to configure your autheticator by asking (Y/N) questions.

Configure SSH to use the Google Authenticator
Open the pam.d/sshd file:

sudo vim /etc/pam.d/sshd

Add this line to the top of the file:

auth       required     pam_google_authenticator.so

Save file and exit (Esc + :wq)
Now open, sshd_config file

sudo vim /etc/ssh/sshd_config

Scroll down the list till you find:

ChallengeResponseAuthentication no

Change it to “yes”

ChallengeResponseAuthentication yes

Save file and exit(Esc + :wq)
Restart the ssh server:

sudo service ssh restart

Setting up new account in your Google Authenticator app

1. Open the Google Authenticator app in your smartphone. Press Menu and select “setup an account”

google-authenticator-setup-account

2. Press “Enter key provided”.

google-authenticator-enter-key

3. Give your account a name and enter the secret key generated earlier.

Now when you connect via SSH to your remote computer, you will see the request for the verification key.

Note: The two-factor authentication only works for password-based login. If you are already using a public/private key for your SSH session, it will bypass the two-factor authentication and log you in directly.

.Net implementation: binary serialization format used by Google

Protobuf-net is the .net implementation of protocol buffers. Protocol buffers is the the binary serialization format used by Google for data communications. The advantages of using protocol buffer include:

  • small in size – efficient data storage (far smaller than xml)
  • cheap to process – both at the client and server
  • platform independent – portable between different programming architectures
  • extensible – to add new data to old messages

Protobuf-net can serialize the .net objects to their binary streams and deserialize the binary streams to .net objects with efficient compression.

Protobuf-net can be used with your existing project by including its .dll. The package can be downloaded from . Follow the steps below to use it with existing project in Monodevelop.

  1. Create a C# console project.
  2. In the References, click on .Net Assembly and add the protobuf-net.dll.
  3. Now you can use the Protobuf library in your project.
  4. Use Serializer class to serialize and deserialize the data.

Sample code

using System;
using System.IO;
using ProtoBuf;

namespace protobufnet
{
	[ProtoContract]
	public class Person
	{
	    [ProtoMember(1)]
	    public string Name { get; set; }
	    [ProtoMember(2)]
	    public int Age { get; set; }
	    [ProtoMember(3)]
	    public DateTime DateOfBirth { get; set; }        
	    [ProtoMember(4)]        
	    public Address Address { get; set; }
	}
	
	[ProtoContract]
	public class Address
	{
	    [ProtoMember(1)]
	    public string Number { get; set; }
	    [ProtoMember(2)]
	    public string StreetName { get; set; }
	}
	
	class MainClass
	{		
		public void serializeDeserializeData()
		{
			var person = new Person {
		        Name = "Fred",
		        Address = new Address {
		            Number = "Flat 1",
		            StreetName = "The Meadows"
		        }
		    };
		    using (var file = File.Create("person.bin")) {
		        Serializer.Serialize(file, person);
		    }	
			
			Person newPerson;
            using (FileStream file = File.OpenRead("person.bin"))
            {
                newPerson = Serializer.Deserialize<Person>(file);
            }

            Console.WriteLine("expected Name {0}", person.Name);
            Console.WriteLine("actual Name {0}", newPerson.Name);
		}
		
		public static void Main (string[] args)
		{			
			MainClass mainclass = new MainClass();
			mainclass.serializeDeserializeData();
			Console.WriteLine ("Serialization and Deserialization completed!");
		}
	}
}