Sunday, November 25, 2012

Linux / Unix ssh-keygen: Create A Host Key File

How do I create a host key file to use with my applications as I can not use system defined /etc/ssh/ssh_host_rsa_key for non-root account under Linux / Unix / Apple OS X / *BSD operating systems?








You need to use a command called ssh-keygen. This command generates, manages and converts authentication keys for ssh. It can create RSA keys for use by SSH protocol version 1 and RSA or DSA keys for use by SSH protocol version 2. he type of key to be generated is specified with the -t option. If invoked without any arguments, ssh-keygen will generate an RSA key for use in SSH protocol 2 connections. The -f option specifies the filename of the key file.

Why create a new host key files?

You may need a new key file:
  1. Your system is compromised.
  2. Your keys are stolen.
  3. You forgotten the passphrase.
  4. Your application need a new host key.
  5. You can not read the default system key files stored in /etc/ssh/ directory but your non-root application needs key.
  6. You got an error message which read as "Could not load host key: /etc/ssh/ssh_host_key*".

ssh-keygen Syntax

The syntax is:
 
ssh-keygen -t 'rsa|dsa|rsa1'  -f /path/to/file
 

Example

Create a host key file in your $HOME/.ssh/myapp as follows. First, create a directory to store your host key file, enter:
$ mkdir -p $HOME/.ssh/myapp
To create a host RSAv2 key file, run:
$ ssh-keygen -t rsa -f $HOME/.ssh/myapp/rsa_key_file
Sample outputs:
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/vivek/.ssh/myapp/rsa_key_file.
Your public key has been saved in /home/vivek/.ssh/myapp/rsa_key_file.pub.
The key fingerprint is:
73:d0:e9:0a:5d:a3:3f:78:33:5d:0d:fe:e4:f4:25:39 vivek@wks01
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|         . .     |
|        . =   .  |
|       . = . . + |
|      . S o   E =|
|       . * . . Bo|
|        o * .   +|
|         . +     |
|                 |
+-----------------+
Type the following commands to verify the keys:
$ ls -l $HOME/.ssh/myapp/
Sample outputs:
total 8
-rw------- 1 vivek vivek 1675 Oct 29 23:12 rsa_key_file
-rw-r--r-- 1 vivek vivek  393 Oct 29 23:12 rsa_key_file.pub
You can now use keys with your app:
$ mycool-app -key $HOME/.ssh/myapp/rsa_key_file -d

HowTo: Nginx Webserver Send Charset utf-8 Under Unix

I was told that to improve resource download parallelization in IE8 I need to send the character set to the HTTP Content-Type response header. How do I set charset utf-8 under nginx web server running on Unix like operating systems?







specified charset to the "Content-Type" response header field. You can view such header using nothting but standard wget or curl command:
curl -I http://example.com

OR

wget --server-response -O /dev/null http://example.com

How do I enable charset HTTP-header in Nginx?

Open your nginx.conf, enter:
# vi nginx.conf
Append/modify the following directive in http, server, or location:
 
charset UTF-8;
 
Save and close the file. Reload the nginx web server, enter:
# /usr/local/nginx/sbin/nginx -s reload

How do I test charset HTTP-header?

Type the following command:
$ wget --server-response -O /dev/null http://www.cyberciti.biz
OR
$ curl -I http://www.cyberciti.biz
Sample outputs:
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 05 Nov 2012 16:42:51 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Whom: l2-com-cyber
Vary: Cookie
Last-Modified: Mon, 05 Nov 2012 16:38:02 GMT
Cache-Control: max-age=311, must-revalidate
X-Galaxy: Andromeda-1
X-Origin-Type: DynamicViaDAL

Linux Shell script to add a user with a password to the system

Our regular reader Imtiaz asks:
How do I add a user with password? I’d like to take input such as username, password from keyboard and add to the system under Linux.
A. You can easily write a shell script that reads username, password from keyboard and add to /etc/passwd and /etc/shadow file using useradd command (create a new user command).
General syntax is as follows:
useradd -m -p encryptedPassword username
Where,
  • -m : The user’s home directory will be created if it does not exist.
  • useradd -p encryptedPassword : The encrypted password, as returned by crypt().
  • username : Add this user to system

Task: Create an encrypted password

You need to create encrypted password using perl crypt():
$ perl -e 'print crypt("password", "salt"),"\n"'
Output:
sa3tHJ3/KuYvI 
Above will display the crypted password (sa3tHJ3/KuYvI) on screen. The Perl crypt() function is a one way encryption method meaning, once a password has been encrypted, it cannot be decrypted. The password string is taken from the user and encrypted with the salt and displayed back on screen.
You can store an encrypted password using following syntax:
$ password="1YelloDog@"
$ pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
$ echo $pass

Output
paU5t8Al/qf6M

Sample shell script to add a user

Based upon above discussion here is a sample shell script (Download link):
#!/bin/bash
# Script to add a user to Linux system
if [ $(id -u) -eq 0 ]; then
 read -p "Enter username : " username
 read -s -p "Enter password : " password
 egrep "^$username" /etc/passwd >/dev/null
 if [ $? -eq 0 ]; then
  echo "$username exists!"
  exit 1
 else
  pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
  useradd -m -p $pass $username
  [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
 fi
else
 echo "Only root may add a user to the system"
 exit 2
fi
Close and save the script:
$ ./adduser.sh
Only root may add a user to the system
Run as root:
# ./adduser
Output:
Enter username : roja
Enter password : HIDDEN
User has been added to system!
Now user roja can login with a password called HIDDEN.

Bash script to create MySQL database and user

#!/bin/bash
 
EXPECTED_ARGS=3
E_BADARGS=65
MYSQL=`which mysql`
 
Q1="CREATE DATABASE IF NOT EXISTS $1;"
Q2="GRANT ALL ON *.* TO '$2'@'localhost' IDENTIFIED BY '$3';"
Q3="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}${Q3}"
 
if [ $# -ne $EXPECTED_ARGS ]
then
  echo "Usage: $0 dbname dbuser dbpass"
  exit $E_BADARGS
fi
 
$MYSQL -uroot -p -e "$SQL"
 
./createdb testdb testuser secretpass 

Perl create mysql database, user, password

my $host = "localhost";
my $port = "3306";
my $user = "root";
my $pass = "111";
my $db_name = "test";
my $db_user = "test";
my $db_pass = "test";

my $dsn = "dbi:mysql::$host:$port";
my $dbh = DBI->connect($dsn, $user, $pass) or die "Unable to connect: $DBI::errstr\n";

$dbh->do("CREATE DATABASE $db_name");
$dbh->do("CREATE USER $db_user\@$host");
$dbh->do("GRANT ALL ON $db_name.* TO $db_user\@$host IDENTIFIED BY '$db_pass'");

$dbh->disconnect();

Installing VirtualBox On Ubuntu

This tutorial shows how you can install InnoTek's VirtualBox on a Ubuntu desktop. With VirtualBox you can create and run guest operating systems ("virtual machines") such as Linux and Windows under a host operating system. There are two ways of installing VirtualBox: from precompiled binaries that are available for some distributions and come under the PUEL license, and from the sources that are released under the GPL. This article will show both ways.
Currently VirtualBox supports only 32bit host and guest operating systems.
This document comes without warranty of any kind! I want to say that this is not the only way of setting up such a system. There are many ways of achieving this goal but this is the way I take. I do not issue any guarantee that this will work for you!

1 Installing VirtualBox From Precompiled Binaries

The VirtualBox binaries can be downloaded from here: http://www.virtualbox.org/wiki/Downloads. If the PUEL license is ok for you and there's a package for your distribution, you can install that package. For example, there is a VirtualBox .deb package for Ubuntu 6.10 (Edgy Eft), so if you use Ubuntu 6.10, you can use that package. I've also tested this package successfully on Ubuntu 7.04 (Feisty Fawn), so it seems you can use that package there, too.
To install the VirtualBox .deb package, please open a terminal window (Applications > Accessories > Terminal) and become root:
sudo su
Then install some prerequisites for VirtualBox:
apt-get install bcc iasl xsltproc xalan libxalan110-dev uuid-dev zlib1g-dev libidl-dev libsdl1.2-dev libxcursor-dev libqt3-headers libqt3-mt-dev libasound2-dev libstdc++5 linux-headers-`uname -r` build-essential
Then go to the VirtualBox download page and pick the right .deb package for your Ubuntu version and download it to your system:
cd /tmp
wget http://www.virtualbox.org/download/1.3.8/VirtualBox_1.3.8_Ubuntu_edgy_i386.deb
After the download has finished, you can install VirtualBox like this:
dpkg -i VirtualBox_1.3.8_Ubuntu_edgy_i386.deb
You might get asked the following questions:
Do you agree with the PUEL license terms? <-- Yes
Should the vboxdrv kernel module be compiled now? <-- Yes
That's it already. You can now find VirtualBox under Applications > System Tools:


2 Installing VirtualBox From The Sources

If InnoTek's PUEL license doesn't work for you and you prefer the GPL and/or there's no .deb package for your Ubuntu version, you can compile VirtualBox from the sources. The sources are released under the GPL.
To install VirtualBox from the sources, please open a terminal window (Applications > Accessories > Terminal) and become root:
sudo su
Then install some prerequisites for VirtualBox:
apt-get install bcc iasl xsltproc xalan libxalan110-dev uuid-dev zlib1g-dev libidl-dev libsdl1.2-dev libxcursor-dev libqt3-headers libqt3-mt-dev libasound2-dev libstdc++5 linux-headers-`uname -r` build-essential
We can download the latest VirtualBox sources from InnoTek's SVN repository; to do so, we must install subversion first:
apt-get install subversion
Next we download the VirtualBox sources to the /usr/src/virtualbox directory:

Saturday, November 24, 2012

How to install Java 7 on Ubuntu 12.04 LTS

Oracle JDK is no longer included by default in Ubuntu’s repositories due to licensing. OpenJDK is default now but many apps still don’t play nice with it. This is why I installed Oracle JDK.
I’ll walk you through the process of installing Oracle JDK 7 on Ubuntu 12.04 LTS Precise Pangolin, the easy way. For this we will use the “install oracle-java7-installer” package from “WEBUPD8″.
The “install oracle-java7-installer” package from “WEBUPD8″ will download the official binaries from Oracle and install the JAVA 7 JDK, JRE and browser plugins on your machine.

1. Add the “WEBUPD8″ PPA.
danesh@python:~$sudo add-apt-repository ppa:webupd8team/java 
 
2. Update your repositories.
danesh@python:~$sudo apt-get update 
 
3. Install JAVA 7 JDK.
danesh@python:~$sudo apt-get install oracle-java7-installer
 
To uninstall,
sudo apt-get remove oracle-java7-installer