Wednesday, February 7, 2007

SSH login without password

Loging into a remote machine using SSH protocol to administer it is a daily activity for most of the system administrators. SSH is considered as one of the highly secure remote login methods. Usually we have to supply a username and password pair to login to the remote machine. But some times it becomes necessary to login to the remote machine without entering a password. From the security point of view this practice is discouraged but if you have to do scripting which involves remote access you don't have another choice.

To achieve ssh login without password, we have to use authentication based on public keys.

Scenario:

You have a local machine "LinuxBox1" in which you have logged in as "safeer". You want to login to "LinuxBox2" as "nebu" without password. Remember that you should know the password of Nebu on LinuxBox2 at least until the set up is complete. This is not a way to impersonate as other users, you have to have privilege to use the account on the remote machine.

Now generate the public/private key pair.

[safeer@linuxbox1 ~]$ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/safeer/.ssh/id_dsa):
Created directory '/home/safeer/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/safeer/.ssh/id_dsa.
Your public key has been saved in /home/safeer/.ssh/id_dsa.pub.
The key fingerprint is:
fd:e3:b0:a5:35:09:6c:c4:14:25:ae:3d:85:dd:82:5d safeer@linuxbox1

This will create a directory .ssh in the home directory (if not already exist) and the permission will be 0700.
/home/safeer/.ssh/id_dsa is the private key for this user
/home/safeer/.ssh/id_dsa.pub is the public key
Permissions for files will be
-rw------- id_dsa
-rw-r--r-- id_dsa.pub
Leave your passphrase as blank(press return key).
Here I have used the DSA encryption algorithm to create the private/public key pair. Alternatively, you can use RSA algorithm (which actually is the default) for which the command will be "ssh-keygen -t rsa"

To login to Linuxbox2 as Nebu, append the public key /home/safeer/.ssh/id_dsa.pub to /home/nebu/.ssh/authorized_keys
(If this file is not already there create it with permission 600)

[safeer@linuxbox1 ~]$scp .ssh/id_dsa.pub nebu@linuxbox2:/home/nebu
nebu@linuxbpx2's password:
[safeer@linuxbox1 ~]$ssh nebu@linuxbox2
nebu@linuxbox2's password:
Last login: Wed Feb 7 09:25:01 2007 from linuxbox5
[nebu@linuxbox2 ~]$mkdir .ssh
[nebu@linuxbox2 ~]$chmod 0700 .ssh
[nebu@linuxbox2 ~]$cat id_dsa.pub >> .ssh/authorized_keys
[nebu@linuxbox2 ~]$chmod 0600 .ssh/authorized_keys
[nebu@linuxbox2 ~]$rm -f id_dsa.pub

Or you can use this single command:

[safeer@linuxbox1 ~]$cat .ssh/id_dsa.pub |ssh nebu@linuxbox2 "mkdir .ssh;chmod 0700 .ssh;cat >> .ssh/authorized_keys;chmod 0600 .ssh/authorized_keys"
nebu@linuxbpx2's password:
[safeer@linuxbox1 ~]$
Everything is setup now. Just try

[safeer@linuxbox1 ~]$ssh nebu@linuxbox2
Last login: Wed Feb 7 09:35:08 2007 from linuxbox1
[nebu@linuxbox2 ~]$

Now you can use ssh,scp and sftp without password.
Please note that to work this public key authentication should be enabled on the server by editing the file "/etc/ssh/sshd_config"

and add/edit the entry

# Allow Identity Auth for SSH2?
PubkeyAuthentication yes

1 comment:



  1. Sau đó đấu giá hội tiếp tục đấu giá, ngoại trừ đan dược, còn có Ma pháp quyển trục cấp tám sơ kỳ, ngoài ra còn có một số linh dược, nhưng Nhạc Thành cũng không cảm thấy hứng thú.

    Sau đó, Tôn Thi Thi cũng nhìn trúng một thanh trường kiếm, đây là Tôn Thi Thi mua cho U Cơ với giá khởi điểm mười vạn kim tệ, kết quả Tôn Thi Thi vừa gọi giá mười một vạn kim tệ, đột nhiên toàn trường tạo thành một mảnh tức cười, cuối cùng không người nào dám cùng Tôn Thi Thi đấu giá, thanh trường kiếm này vốn bình thường giá tiền một trăm vạn, đây là do bát phẩm Luyện Khí sư luyện chế ra, mười một vạn kim tệ được Tôn Thi Thi mua được, cô gái xinh đẹp cũng chỉ có thể đành tuyên bố.

    Lúc này Nhạc Thành cũng chỉ im lặng, không nghĩ tới Tôn Thi Thi dùng mười một vạn là mua được một thanh tdongtam
    mu moi ra hom nay
    tim phong tro
    http://nhatroso.com/
    nhạc sàn
    tổng đài tư vấn luật
    văn phòng luật hà nội
    tổng đài tư vấn luật
    thành lập công ty trọn gói
    http://we-cooking.com/
    chém gió
    trung tâm tiếng anhrường kiếm, dựa theo Nhạc Thành dự đoán, đại tiểu thư ít nhất cũng phải mất hai trăm vạn kim tệ.

    Đấu giá hội cuối cùng kết thúc, nhóm người Nhạc Thành đi xuống, lúc này người của thương hội cầm lấy vật phẩm mà Tôn Thi Thi đã mua mang đến, thương hội có ba người là ngũ tinh Đấu tôn và một lục tinh đấu tôn hộ tống ba kiện vật phẩm đấu giá.

    Nhạc Thành lấy ra một tấm tinh tạp giao cho Khiếu Thiên Hổ, Khiếu Thiên Hổ đưa cho lục tinh

    ReplyDelete