検索

カレンダー

2011年8月
« 7月   9月 »
1234567
891011121314
15161718192021
22232425262728
293031  

広告

ブログランキング・にほんブログ村へ
にほんブログ村

東電電力供給情報

カテゴリー

最近のコメント

リンク

Scientific Linux 6で自宅サーバー構築 その18 sshで鍵認証を導入

投稿者:yasu 更新日時:2011年8月25日 0時37分34秒
カテゴリLinux, OpenSSH, Scientific Linux, 自宅サーバー

鍵認証方式のSSHを設定します。

rootにスイッチ可能なユーザをwheelグループに登録

以前CentOSの構築の際にも実施したrootユーザにスイッチできるユーザをwheelグループで制御します。

# PAMの設定を変更してwheelグループのみでrootユーザにスイッチできるようにします
[root@sl6 ~]# vi /etc/pam.d/su
#%PAM-1.0
auth sufficient pam_rootok.so
# Uncomment the following line to implicitly trust users in the "wheel" group.
#auth sufficient pam_wheel.so trust use_uid
# Uncomment the following line to require a user to be in the "wheel" group.
auth required pam_wheel.so use_uid ← コメント解除
auth include system-auth
account sufficient pam_succeed_if.so uid = 0 use_uid quiet
account include system-auth
password include system-auth
session include system-auth
session optional pam_xauth.so

# 保存してviエディタを終了します

# 管理用ユーザ(sl6)をwheelグループに追加します
[root@sl6 ~]# usermod -G wheel sl6

鍵ファイル作成

秘密鍵・公開鍵を作成して鍵認証方式のログインができるようにします。
鍵の作成には一般ユーザで実施します。

# ログインユーザの確認をします
[sl6@sl6 ~]$ whoami
sl6

# もしrootと表示された場合一般ユーザではないので一般ユーザでログインしなおすかsuコマンドで一般ユーザにスイッチしてください

# 鍵を作成します
[sl6@sl6 ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/sl6/.ssh/id_rsa):
Created directory '/home/sl6/.ssh'.
Enter passphrase (empty for no passphrase): 鍵認証用パスフレーズを入力
Enter same passphrase again: 鍵認証用パスフレーズを再度入力
Your identification has been saved in /home/sl6/.ssh/id_rsa.
Your public key has been saved in /home/sl6/.ssh/id_rsa.pub.
The key fingerprint is:
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx sl6@sl6.sa-sa-ki.jp
The key's randomart image is:
+--[ RSA 2048]----+
| . ? . o |
| d o . +p+|
| . - + |
| .% . .|
| S.+ . .|
| a o E= |
| d * % . |
| + |
| t |
+-----------------+

# 鍵ファイルを確認します
[sl6@sl6 ~]$ ls -la .ssh
合計 16
drwx------. 2 sl6 sl6 4096 6月 26 15:06 2011 .
drwx------. 4 sl6 sl6 4096 6月 26 15:06 2011 ..
-rw-------. 1 sl6 sl6 1743 6月 26 15:06 2011 id_rsa    # 秘密鍵(クライアント)
-rw-r--r--. 1 sl6 sl6 404 6月 26 15:06 2011 id_rsa.pub # 公開鍵(サーバー)

# 公開鍵(サーバー)ファイルの名前を変更します
[sl6@sl6 ~]$ mv .ssh/id_rsa.pub ssh.authorized_keys

# 公開鍵(サーバー)ファイルのアクセス権を読み取り専用にします
[sl6@sl6 ~]$ chmod 400 .ssh/authorized_keys
[sl6@sl6 ~]$ ls -la .ssh
合計 16
drwx------. 2 sl6 sl6 4096 6月 26 15:08 2011 .
drwx------. 4 sl6 sl6 4096 6月 26 15:08 2011 ..
-r--------. 1 sl6 sl6 404 6月 26 15:06 2011 authorized_keys ← 読み取り専用になっていることを確認
-rw-------. 1 sl6 sl6 1743 6月 26 15:06 2011 id_rsa

# 秘密鍵(クライアント)は本サーバに接続したいクライアント端末にコピーしてください
# クライアントパソコンにコピーしたらサーバー上からは削除しておきます
[sl6@sl6 ~]$ rm .ssh/id_rsa

SSHサーバー設定変更

SSHサーバーの設定を変更して鍵認証ができるようにします。
他にもrootユーザでのログインやパスワード無しユーザのログインを禁止します。
※rootユーザで作業します

[root@sl6 ~]# vi /etc/ssh/sshd_config
# Authentication:

#LoginGraceTime 2m
PermitRootLogin no # コメント解除してyesからnoに変更するとrootでログインできなくなります
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
PermitEmptyPasswords no # コメントを解除するとパスワードなしのログインを禁止します
PasswordAuthentication no # yesからnoに変更すると公開鍵暗号認証に変更します

X11Forwarding no # コメントを解除します
#X11Forwarding yes # コメント化します

# 保存してviエディタを終了します
# SSHサーバー(サービス)を再起動して設定を反映します
[root@sl6 ~]# /etc/rc.d/init.d/sshd restart
sshd を停止中: [ OK ]
sshd を起動中: [ OK ]

これで鍵認証方式でログインができるようになりました。

鍵認証方式とはいえアクセス制御をしておいた方がよりセキュアになるので下記設定を追加します。
※SSH接続可能なクライアントをローカルホストと自宅ネットワーク内のパソコンからに制限する場合

# 全ての端末からのSSH接続を禁止します
[root@sl6 ~]# echo "sshd: ALL" >> /etc/hosts.deny

# ローカルホスト(サーバー自身)のSSH接続を許可します
[root@sl6 ~]# echo "sshd: 127.0.0.1" >> /etc/hosts.allow

# 自宅ネットワーク内のパソコンからのSSH接続を許可します
※ yasuの自宅ネットワークは192.168.で始まるIPアドレスで構成しているので各自設定する場合は読み替えてください。
[root@sl6 ~]# echo "sshd: 192.168." >> /etc/hosts.allow

■関連記事
Scientific Linux 6で自宅サーバー構築 その1 サーバー機Express 5800 GT110bのオンボードRAIDでRAID1設定
Scientific Linux 6で自宅サーバー構築 その2 Scientific Linux 6をExpress5800/GT110bにインストール
Scientific Linux 6で自宅サーバー構築 その3 Scientific Linux 6インストール後の初期設定
Scientific Linux 6で自宅サーバー構築 番外編その1 Scientific Linux 6のcronについて
Scientific Linux 6で自宅サーバー構築 その4 Scientific Linux 6 にClamAVをインストールしてウィルス対策
Scientific Linux 6で自宅サーバー構築 その5 Scientific Linux 6 にTripwireをインストールしてファイル改ざんを検知する
Scientific Linux 6で自宅サーバー構築 その6 Scientific Linux 6 にchkrootkitをインストールしてrootkit感染を検知する
Scientific Linux 6で自宅サーバー構築 その7 NTPサーバーで自動時刻合わせ
Scientific Linux 6で自宅サーバー構築 その8 バッファオーバーフロー攻撃対策を行う
Scientific Linux 6で自宅サーバー構築 その9 logwatchでサーバー監視
Scientific Linux 6で自宅サーバー構築 その10 DNSサーバーを構築
Scientific Linux 6で自宅サーバー構築 番外編その2 Scientific Linuxを6.0から6.1にアップグレードする
Scientific Linux 6で自宅サーバー構築 その11 PostfixとDovecotとSMTP-AUTHでメールサーバーを構築する
Scientific Linux 6で自宅サーバー構築 その12 メールサーバーにSSLを導入する
Scientific Linux 6で自宅サーバー構築 その13 Apache PHP MySQLをインストール
Scientific Linux 6で自宅サーバー構築 その14 WebサーバーにSSLを導入する
Scientific Linux 6で自宅サーバー構築 その15 WordPressを導入する
Scientific Linux 6で自宅サーバー構築 その16 Piwikでアクセスログ集計
Scientific Linux 6で自宅サーバー構築 その17 Squidをインストール
Scientific Linux 6で自宅サーバー構築 その18 sshで鍵認証を導入
Scientific Linux 6で自宅サーバー構築 その19 WebDavの設定
Scientific Linux 6で自宅サーバー構築 その20 lm_sensorをインストール

“Scientific Linux 6で自宅サーバー構築 その18 sshで鍵認証を導入”にコメントはありません

コメントする