前のページ « | 1 | » 次のページ

SSHサーバ(OpenSSH)

2009, 02, 12

SSHは、データの暗号化や公開鍵を利用した認証技術により安全なリモート管理やファイル転送を可能にするツール。パスワードやデータの漏洩といった危険性が高いTelenetやFTPに代わって、リモート管理に良く使われるようになった。接続方式にはSSH1とSSH2がありSSH2の方が、SSH1より強固な暗号化になっているので、ここではSSH2を使用し鍵方式によるログインの設定を行う。

■ OpenSSHの設定
OpenSSH設定ファイルを編集
[root@linux ~]# vi /etc/ssh/sshd_config

スーパーユーザでのログインを禁止
#PermitRootLogin yes
        ↓
PermitRootLogin no  ← #を削除して no に変更

通常のパスワードではなく鍵方式に変更
#PasswordAuthentication yes
        ↓
PasswordAuthentication no  ← #を削除して no に変更

空パスワードの禁止
#PermitEmptyPasswords no
        ↓
PermitEmptyPasswords no  ← #を削除


■ SSHアクセス制限
サーバー自身、内部ネットワーク、特定の外部IP(xxx.xxx.xxx.xxx)からのアクセスを許可
[root@linux ~]# echo "sshd : 127.0.0.1 192.168.1. xxx.xxx.xxx.xxx" >> /etc/hosts.allow

上記以外の全てのアクセスを禁止
[root@linux ~]# echo "sshd : ALL" >> /etc/hosts.deny

会社等からアクセスする場合、自分の会社の接続環境(IPアドレス)を調べ、/etc/hosts.allowへ登録する必要がある。そんな時は診断くんで確認できる。

■ OpenSSHの再起動
[root@linux ~]# /etc/rc.d/init.d/sshd restart
sshdを停止中:                                             [  OK  ]
sshdを起動中:                                             [  OK  ]


■ SSH2による鍵の作成
鍵を作成するユーザーで作業を進める。
[root@linux ~]# su - higo

鍵の作成を行う
[higo@linux ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.

何も入力しないでエンターキー押す
Enter file in which to save the key (/home/higo/.ssh/id_rsa): 
Created directory '/home/higo/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/higo/.ssh/id_rsa.
Your public key has been saved in /home/higo/.ssh/id_rsa.pub.
The key fingerprint is:
7d:cb:ab:06:c6:bf:52:79:60:57:11:ae:f8:f3:b3:d0 higo@linux.yokensaka.com

鍵の作成先を表示する
[higo@linux ~]$ ls -la /home/higo/.ssh/
合計 20
drwx------  2 higo higo 4096 10月 28 02:31 .
drwx------ 21 higo higo 4096 10月 28 02:31 ..
-rw-------  1 higo higo 1743 10月 28 02:31 id_rsa      ← 秘密鍵
-rw-r--r--  1 higo higo  406 10月 28 02:31 id_rsa.pub  ← 公開鍵

公開鍵をauthorized_keysに追加
[higo@linux ~]$ cat /home/higo/.ssh/id_rsa.pub >> /home/higo/.ssh/authorized_keys

公開鍵を自分のみアクセスできるように変更
[higo@linux ~]$ chmod 600 /home/higo/.ssh/authorized_keys

公開鍵を削除
[higo@linux ~]$ rm -f /home/higo/.ssh/id_rsa.pub

鍵の作成先を表示
[higo@linux ~]$ ls -la /home/higo/.ssh/
合計 20
drwx------  2 higo higo 4096 10月 28 02:32 .
drwx------ 21 higo higo 4096 10月 28 02:31 ..
-rw-------  1 higo higo  406 10月 28 02:32 authorized_keys  ← 公開鍵
-rw-------  1 higo higo 1743 10月 28 02:31 id_rsa           ← 秘密鍵


■ 秘密鍵の抜き取り
1.USBメモリーにコピーする方法
su - コマンドでスーパーユーザ(root)になる、ハイフン(-)を付けないと、
変更後のユーザー用環境が設定されない為、思い通りの操作が出来なくなる。
[higo@linux ~]$ su -

rootのパスワードを入力。
Password:
[root@linux ~]#        ← スーパーユーザー root になった

USBメモリーをマウントして秘密鍵をUSBメモリーに移動
USBメモリーをマウントするマウントポイントを作成。
[root@linux ~]# mkdir /mnt/usbfm

コマンド入力時にオプション設定を付けずにマウントできるように、/etc/fstab に設定を登録。
[root@linux ~]# vi /etc/fstab

/dev/VolGroup00/LogVol00 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/VolGroup00/LogVol01 swap                    swap    defaults        0 0
/dev/sda1        /mnt/usbfm       auto    noauto,user     0 0  ← 1行追加

USBメモリーを挿してからUSBメモリーをマウント
[root@linux ~]# mount /mnt/usbfm

秘密鍵をUSBメモリーに移動
[root@linux ~]# mv /home/higo/.ssh/id_rsa /mnt/usbfm

USBメモリーのマウントを解除
[root@linux ~]# umount /mnt/usbfm

2.FTPでコピーする方法
スーパーユーザ(root)になる。
[higo@fadora ~]$ su -

rootのパスワードを入力。
Password:

FTPサーバをインストール
[root@linux ~]# yum install vsftpd
Loading "installonlyn" plugin
Setting up Install Process
Setting up repositories
core                      100% |=========================| 1.1 kB    00:00     
extras                    100% |=========================| 1.1 kB    00:00     
updates                   100% |=========================| 1.2 kB    00:00     
Reading repository metadata in from local files
Parsing package install arguments
Resolving Dependencies
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for vsftpd to pack into transaction set.
vsftpd-2.0.5-8.i386.rpm   100% |=========================|  16 kB    00:00     
---> Package vsftpd.i386 0:2.0.5-8 set to be updated
--> Running transaction check

Dependencies Resolved

============================================================
 Package                 Arch       Version          Repository        Size 
============================================================
Installing:
 vsftpd                  i386       2.0.5-8          core              137 k

Transaction Summary
============================================================
Install      1 Package(s)         
Update       0 Package(s)         
Remove       0 Package(s)         

Total download size: 137 k
Is this ok [y/N]: y
Downloading Packages:
(1/1): vsftpd-2.0.5-8.i38 100% |=========================| 137 kB    00:00     
warning: rpmts_HdrFromFdno: Header V3 DSA signature: NOKEY, key ID 4f2a6fd2
Importing GPG key 0x4F2A6FD2 "Fedora Project "
Is this ok [y/N]: y
Importing GPG key 0xDB42A60E "Red Hat, Inc "
Is this ok [y/N]: y
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing: vsftpd                       ######################### [1/1] 
Installed: vsftpd.i386 0:2.0.5-8
Complete!

FTPサーバを起動させる
[root@linux ~]# /etc/rc.d/init.d/vsftpd start
vsftpd 用の vsftpd を起動中:                               [  OK  ]


ここからは、クライアント(IP:192.168.1.3)のコマンドプロンプトで作業する。
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

ローカルのカレントをC:\にしておく
C:\Documents and Settings\client>cd \
C:\>
FTPでサーバに接続
C:\>ftp 192.168.1.10
Connected to 192.168.1.10.
220 (vsFTPd 2.0.1)
ユーザ名を入力
User (192.168.1.10:(none)): higo
331 Please specify the password.
パスワードを入力
Password:
230 Login successful.
転送モードをバイナリモードにする
ftp> bin
200 Switching to Binary mode.
秘密鍵を取得
ftp> get /home/higo/.ssh/id_rsa
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for /home/higo/.ssh/id_rsa (951 bytes).
226 File send OK.
ftp: 1,675 bytes received in 0.00Seconds 1675000.00Kbytes/sec.
サーバの秘密鍵を削除
ftp> delete /home/higo/.ssh/id_rsa
250 Delete operation successful.
FTP接続を終了
ftp> bye
221 Goodbye.
C:\>
ローカルの確認
C:\>dir
ドライブ C のボリューム ラベルがありません。
ボリューム シリアル番号は 8433-4372 です

C:\ のディレクトリ

2004/05/05  10:48     25 .DAT
2003/11/08  23:47 <DIR>  Apache Group
・
・
・
2006/10/27  18:20   1,675 id_rsa ← コピーされている
・
・
・
2006/09/18  05:52 <DIR>  Program Files
・
・
・
              13 個のファイル        850,256 バイト
              19 個のディレクトリ 9,977,475,072 バイトの空き領域

コマンドプロンプトを終了
C:\>exit


■ 外部からの接続
外部からの接続には、ルーターの設定が必要。
ルータの設定で22番のポートを開ける。

■ ポートチェック【ポート開放確認】
「管理しているサーバーが外部から接続アクセスできるか?」「ポートは開放されているか?」「portは閉じているか?」「ルータのポートは開放されているか」等の
ポートチェック・ポートの疎通確認テストはこちらで

■ 秘密鍵の抜き取りが出来たら、次はリモート接続(PuTTY)の設定

前のページ « | 1 | » 次のページ