Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

 

Sử dụng công cụ Putty, SecureCRT, đăng nhập SSH Server từ


xa
1.    Chuẩn bị
Mở trình duyệt, gõ ftp://192.168.0.10/ToolPUTTY/
Vào ToolPUTTY để tải file putty x64.zip về và giải nén.
    Dùng Putty/Secure CRT tạo 02 khóa (pub key, pri key),
    NSD tự tạo ra mật khẩu riêng (passprhase)
    Upload  tập tin khóa pub lên Server 192.168.0.10 theo tài khoản NSD.
    Dùng putty để đăng nhập từ xa vào Server 192.168.0.10 với khóa pri
key và passphrase vừa tạo
    Sau khi dang nhập, các giao dịch  giữa hai bên sẽ được mã hóa bằng cặp
khóa công khai và bí mật
  

2.    Tạo cặp khóa công khai và bí mật


Chạy chương trình PUTTYGEN.EXE để tạo cặp khóa (XYZpub, XYZpri)
CHÚ Ý:     Di chuyển chuột trên cửa sổ để sinh khóa.
 
 
Hình 6.1 PuTTYGen - Giao diện tạo khóa
 
 Hình 6.1 PuTTYGen - Giao diện tạo và lưu khóa
Chọn lần lượt các nút:
         Save public key: Lưu khóa công khai với tên file: XYZpub                    XYZ là tên
NSD
         Save private key: Lưu khóa bí mật với tên file: XYZpri.ppk
 
2.1          Tải khóa public lên hệ thống để xác thực khi đăng nhập bằng ssh

2.1.1        Cách 1: Sử dụng chương trình PSCP và PLINK để sao chép và thiết lập khóa
 
Trên Windows, mở cửa sổ lệnh, chuyển vào thư mục hiện hành:
cd  C:\putty x64

 Thực hiện các lệnh sau. CHÚ Ý: Thay XYZ bằng tài khoản NSD, password: 123456
C:\putty x64>dir
 Volume in drive C has no label.
 Volume Serial Number is 664D-51A0
 
 Directory of C:\ putty x64
 
10/23/2018  02:33 PM    <DIR>          .
10/23/2018  02:33 PM    <DIR>          ..
07/04/2017  08:34 PM           313.912 PAGEANT.EXE
07/04/2017  08:34 PM           617.048 PLINK.EXE
10/23/2018  02:33 PM             1.482 pri.ppk
07/04/2017  08:34 PM           626.744 PSCP.EXE
07/04/2017  08:34 PM           643.144 PSFTP.EXE
10/23/2018  02:32 PM               477 XYZpub
07/04/2017  08:36 PM           283.045 PUTTY.CHM
07/04/2017  08:36 PM            33.608 PUTTY.CNT
07/04/2017  08:34 PM           854.072 PUTTY.EXE
07/04/2017  08:36 PM           699.745 PUTTY.HLP
07/04/2017  08:35 PM           407.104 PUTTYGEN.EXE
              11 File(s)      4.480.381 bytes
               2 Dir(s)   6.013.984.768 bytes free

// Tải khóa public lên Server bằng cách sử dụng chương trình PSCP.EXE
C:\putty x64> PSCP.EXE -pw 123456 XYZpub userID@192.168.0.10:XYZpub

pub                       | 0 kB |   0.5 kB/s | ETA: 00:00:00 | 100%

C:\putty x64> plink -ssh userID@192.168.0.10 -pw 123456 “ssh-keygen -if


XYZpub > $HOME/pubopenssh”
C:\putty x64> plink -ssh userID@192.168.0.10 -pw 123456 mkdir $HOME/.ssh
C:\putty x64> plink -ssh userID@192.168.0.10 -pw 123456 “cat
$HOME/pubopenssh > $HOME/.ssh/authorized_keys”
C:\putty x64> plink -ssh userID@192.168.0.10 -pw 123456 “chmod 600
$HOME/.ssh/authorized_keys”
C:\putty x64> plink -ssh userID@192.168.0.10 -pw 123456 ls -la $HOME/.ssh/
C:\putty x32>plink -ssh user01@10.10.43.12 -pw 123456 ls -la $HOME/.ssh/
total 12
drwx------  2 user01 root 4096 Nov  5  2018 .
drwx------ 30 user01 root 4096 Oct 11 10:11 ..
-rw-------  1 user01 root  381 Oct 11 10:11 authorized_keys

 
C:\putty x64> plink -ssh userID @192.168.0.10 -pw 123456  cat
$HOME/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEApHFSmJuUYsuY0cNGMikwNNsw0Q50MT0RNKMtLk+/
RTCDCWWSfG8du37Id+trFNOJexvs4CK7AQOsEJW2vjoy5RuqALztQLPpmZZ7OB6Whb63OToU7EA+XMME0Ud
cd9ifCnHpyj7/l+x6dboDoniiY+4TZRwUiXMOwuN9xurXEcFMZzBjYQderFkdwakpz2t2qBiG4lIchztt/
JygzhT08K7QF6bp7yPHiXi9OpBRqQpmkiQ7gZugbAaz3cKqCUexph/cL+2CsL/
zN3WRJeDfLYJEbo5e0iCsxQ3g48C2vGzxOKSKNM1oWXqpb9XpDH/iAXM2C2k4qdeApYuJE9zFKQ==
 
C:\putty x32>

Chạy chương trình putty.exe để đăng nhập từ xa đến máy 192.168.0.10 sử dụng cặp
khóa pub/pri.ppk vừa tạo:
 

C:\putty x64> putty.exe


   
Kết quả login sử dụng khóa passphrase của NSD như sau:
 
 

Hình 1. Đăng nhập từ xa bằng cặp khóa pub/pri.ppk

2.1.2        Cách 2. Đăng nhập vào hệ thống để tạo khóa


 
// Tải khóa public lên Server bằng cách sử dụng chương trình PSCP.EXE
C:\putty x64> PSCP.EXE -pw 123456 XYZpub userID@192.168.0.10:XYZpub
pub                       | 0 kB |   0.5 kB/s | ETA: 00:00:00 | 100%

 
Chạy chương trình putty.exe để đăng nhập từ xa vào máy 192.168.0.10 với tài khoản NSD.
Sau khi đăng nhập vào Linux, thực hiện các lệnh sau:
 
USERID@VMKali:~$ ls -lF
total 48
-rw-r--r-- 1 XXX cntt  477 Oct 23 15:06 XYZpub
 

USERID@VMKali:~$ cat XYZpub


---- BEGIN SSH2 PUBLIC KEY ----
Comment: "rsa-key-20181023"
AAAAB3NzaC1yc2EAAAABJQAAAQEAioabMsJklJDlH9L0sohRfHhko478SsWXNH2/
GZL8nce66V5ru/hxZuywC6XVSeCEgQegy/AkYOpvrg8lBBmzhhQYdQZQWDB9Dfh1
loLdIxx3Wb5lQ642HTwzvY91tJRb7ea6hYrHhg6//evLueQFlw==
---- END SSH2 PUBLIC KEY ----

USERID@VMKali:~$ ssh-keygen -if XYZpub > pubopenssh


USERID@VMKali:~$ mkdir $HOME/.ssh
USERID@VMKali:~$ cat $HOME/pubopenssh > $HOME/.ssh/authorized_keys
USERID@VMKali:~$ chmod 600 $HOME/.ssh/authorized_keys
USERID@VMKali:~$ ls -la $HOME/.ssh/
total 12
drwxr-xr-x  2 XXX cntt 4096 Oct 23 14:42 .
drwx------ 25 XXX cntt 4096 Oct 23 14:41 ..
-rw-r--r--  1 XXX cntt  381 Oct 23 14:42 authorized_keys

USERID@VMKali:~$ cat $HOME/.ssh/authorized_keys


ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEAioabMsJklJDlH9L0sohRfHhko478SsWXNH2/r+b//
9UeIWr16bYz61Z0hQK/
4RFFIcuOZHq8rmRmFTXJ76wEGN7fNYNbwIG2fcZyU7E0z7fYBNUCr6Ig2wKYReaQiX/6/
xahAKV1I2O5YnVMhMUZ4xvO21TbW4kBV6ozyEiE0qoB3thr0xyWsJ9Ey7GUIQp4bJwLkXcP853cgyNsMVV3
9YjIABmkO6BV8xxAGZL8nce66V5ru/hxZuywC6XVSeCEgQegy/
AkYOpvrg8lBBmzhhQYdQZQWDB9Dfh1loLdIxx3Wb5lQ642HTwzvY91tJRb7ea6hYrHhg6//evLueQFlw==
USERID@VMKali:~$

Chạy chương trình putty.exe để đăng nhập từ xa đến máy 192.168.0.10 sử dụng cặp
khóa công khai/khóa bí mật vừa tạo:
C:\putty x64> putty.exe

3.    Public-Key Authentication in SecureCRT


Public-key authentication is a proven, it’s better then allowing password authentication.
As long as you keep your private key file safe, you’re less likely to encounter a break-in
of your Unix/Linux servers. In this tutorial, I’ll try to show you the steps that you need to
create public/private key using SecureCRT and use it with Ubuntu Server.
 
To create a public/private key pair in SecureCRT, open up SecureCRT, click on “ Quick
Connect“, from there click on the “Properties“:
 
Note: Uncheck all other options
Click “Create Identity File…” and the key generation wizard starts:

Click “Next” to get started:


Select RSA as the Key Type and click “Next“:
Next, you are prompted to enter a passphrase twice (I leave it blank, but you should use
something pretty long phrase, that you’re going to remember):
Next, it will ask you for your desired key length in bits. Take the default “ 1024” and
click “Next“:
Once it has been generated, click “Next” then you will need to save the public key and
private key to your computer. Save it in a safe place!

   
Click “Finish” When asked whether or not you want to upload the following key, say
NO!
Login to Linux Server (192.168.0.10) as fllow:
// scp file.txt remote_username@10.10.0.2:/remote/directory
Login to Linux server (IP addr: 192.168.0.10) with your username and password and cat
the content of the public key:
user01@VMKali:~$ ls -lF
total 56
-rw-r--r-- 1 user01 root  477 Nov  5  2018 102140098
drwxr-xr-x 2 user01 root 4096 Nov  3  2018 Desktop/
drwxr-xr-x 2 user01 root 4096 Nov  3  2018 Documents/
drwxr-xr-x 2 user01 root 4096 Nov  3  2018 Downloads/
-rw-r--r-- 1 user01 root  328 Oct 11 15:08 IdentityXXX.pub
drwxr-xr-x 2 user01 root 4096 Nov  3  2018 Music/
drwxr-xr-x 2 user01 root 4096 Nov  3  2018 Pictures/
drwxr-xr-x 2 user01 root 4096 Nov  3  2018 Public/
drwxr-xr-x 2 user01 root 4096 Nov  3  2018 Templates/
drwxr-xr-x 2 user01 root 4096 Nov  3  2018 Videos/
drwxr-xr-x 2 user01 root 4096 Nov  3  2018 document/
drwxr-xr-x 2 user01 root 4096 Mar  6  2019 log/
drwxr-xr-x 3 user01 root 4096 Aug  6 09:06 snort/
drwxr-xr-t 2 user01 root 4096 Nov  3  2018 thinclient_drives/
user01@VMKali:~$

user01@VMKali:~$ cat IdentityXXX.pub


---- BEGIN SSH2 PUBLIC KEY ----
Subject: ntkhoi
Comment: "ntkhoi@TKHOI"
ModBitSize: 1024
AAAAB3NzaC1yc2EAAAADAQABAAAAgQDOek0Ad+5xJnFPXOZyLWL/0BI04QFAVHd1
F+T56y+QLLp9PLyp46MgnyVGDoP/UBrLEOZzAhE+qxL1I9Hy1JxA9gbov7qT3OCQ
07io7Jqt7VDMLrZd/P3kAGw8UK3WZEso0bWMlZ00ZZDoYkwIfn2YHb5GfTjwIIja
uTNohkcIUQ==
---- END SSH2 PUBLIC KEY ----
First you need to format the public key properly, for this you need to delete the first four
lines as well as delete the last line and get JUST the key on a single line with “ssh-rsa” in
front.
Note: Open the file with “nano IdentityXXX.pub” and you will be able to edit it properly.
user01@VMKali:~$ nano IdentityXXX.pub

user01@VMKali:~$ cat IdentityXXX.pub


ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDOek0Ad+5xJnFPXOZyLWL/0BI04QFAVHd1
F+T56y+QLLp9PLyp46MgnyVGDoP/UBrLEOZzAhE+qxL1I9Hy1JxA9gbov7qT3OCQ
07io7Jqt7VDMLrZd/P3kAGw8UK3WZEso0bWMlZ00ZZDoYkwIfn2YHb5GfTjwIIja
uTNohkcIUQ==
user01@VMKali:~$

If .ssh folder doesn’t exist then you can use these command to create it!
user01@VMKali:~$ mkdir ~/.ssh
user01@VMKali:~$ chmod 700 ~/.ssh

Create the “authorized_keys” file inside .ssh folder and set the permission on it:
user01@VMKali:~$ touch .ssh/authorized_keys
user01@VMKali:~$ chmod 750 .ssh/authorized_keys
Now that the RSA key is properly formatted, you can add it to the “ authorized_keys”
file.
user01@VMKali:~$ cat IdentityXXX.pub >> .ssh/authorized_keys
user01@VMKali:~$ cat .ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQCbyMxD97njbRS+bhRCKhjNGi72fKifWrpW
5TwCY/U8fyQPLzt1v70S2NUPwXKmFTkGNNt5ldgnbQodYUDzCYkN2TOsapRTsQ/4
bgcJz35wGAJGqs4/3XWx3DAGXpi9l0Ys5ch5kTNF9fNK3lb522jxtX9nI7gWywRH
wf/wWHfGBw==
user01@VMKali:~$

Remove the public key (In this case, it is Identity.pub):


user01@VMKali:~$ rm IdentityXXX.pub

Connecting via SecureCRT using your key pair:


If this is the same session you were already working with above, your identity file should
already be filled in…if not, just click the browse button “…” and find your private key.
Click “OK“. (You can also refer this link for more help):

  3.    Bài tập
1)     Sử dụng putty hoặc lệnh ssh truy xuất từ xa đến một Server trên Internet sử dụng
khóa công khai và khóa riêng.
 
 
-----------------------------------------------------------------

You might also like