1. 認証鍵(ファイルキー)の作成
SSHバージョンと暗号化アルゴリズムごとの公開鍵+秘密鍵ペアの作成方法。
> ssh-keygen -t rsa1 -C "SSH1 RSA key"
> ssh-keygen -t rsa -C "SSH2 RSA key"
> ssh-keygen -t dsa -C "SSH2 DSA key"
パスワード不要の鍵を作成する場合は、以下のようにする。
> ssh-keygen -t rsa -N "" -C "SSH2 RSA key"
公開鍵をsshd_configで指定されたファイルにまとめておく。
cat identity.pub id_dsa.pub id_rsa.pub > authorized_keys
ちなみに、authorized_keysをauthrized_keyとミスタイプしてエラーを出す愚者は、僕だけでいい・・・。
なお、WinSCP3ではPuTTY形式のキーのみの対応ってことで、この方法で生成したPrivateKeyを使いたい場合は、PuTTY Key Generatorでimportして.ppkな形式に変換する。
2. 特定のコマンドのみを実行可能な認証鍵の作成
認証鍵の作成の手順で作成された公開鍵に、command=""という記述を追加する。
以下はauthorized_keysの最初のほうの例。(コマンドをls -aのみに制限した場合)
command="ls -a" ssh-rsa AAA(以下略...)
以上の設定でログインすると、自動的にls -aが実行され、コネクションが閉じられる。
3. SSH Portforwardingの使い方
SSHによってリモートポートをローカルポートへ転送する手順。
オプションは、ssh [-L [bind_address:]port:host:hostport] hostnameとなる。
bind_address(省略可)は、割り当てたいローカルのIPアドレス、portはそのポート(他で使用してないこと)。
hostは、リモートのホスト名になる。これは接続先SSHサーバからみた接続先なので、接続先SSHサーバ自体ならlocalhostになる。(たぶん)
hostnameは接続先SSHサバになる。
- リモートのHTTPポートへローカルのポート8080でアクセスする例
-
> ssh -L 8080:localhost:80 example.com
ログインが成功すると、ローカルのマシンではポート8080がLISTENになっているはずなので、そこにHTTPで接続を試みる。
> lynx http://localhost:8080/
リモートのWebサーバに接続できれば成功。
- リモートのSambaにアクセスする例
-
ssh -L 192.168.0.1:139:localhost:139 exsample.com
ローカルのマシンがWindowsの場合は、初期状態でWindows共有に使われているので注意が必要。
192.168.0.1のIPを持つネットワーク接続の「Microsoft ネットワーク用ファイルとプリンタ共有」と「NetBIOS over TCP/IP」をオフにすることでローカルのポートが利用可能になる。
4. SCPの使い方
SCPコマンドでカレントディレクトリにファイルをダウンロードする例:
> scp hoge@example.com:~/download_file .
SCPコマンドでファイルをアップロードする例:
> scp ./upload_file hoge@example.com:~/
のように書く。
ディレクトリを転送したい場合は、-rオプションをつける。
5. sshdの設定
以下、sshd_config の設定例。SSH2, RSA鍵認証, ユーザ hoge限定 の場合の設定になっている。
# ポート番号とListenするアドレスの設定
Port 22
Protocol 2
#ListenAddress 0.0.0.0
#ListenAddress ::
# ホストキーの設定(使わない)
# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
#HostKey /etc/ssh/ssh_host_dsa_key
# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 3600
#ServerKeyBits 768
# ログの設定
#obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
#LogLevel INFO
# Authentication:
#LoginGraceTime 120
# Rootのログイン禁止
PermitRootLogin no
# 特定ユーザ(hoge,foo)のログインのみ許可
AllowUsers hoge foo
# 特定グループ(workgroup)のユーザのログインのみ許可
# AllowGroups workgroup
StrictModes yes
# RSA キー の設定 (これを使用する)
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
# Rhostsの設定(使わない)
RhostsAuthentication no
# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no
# PAM認証(使わない)
PasswordAuthentication no
PermitEmptyPasswords no
# ChallengeResponse認証(使わない)
ChallengeResponseAuthentication no
# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#AFSTokenPassing no
# Kerberos TGT Passing only works with the AFS kaserver
#KerberosTgtPassing no
#X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PrintMotd yes
#PrintLastLog yes
#KeepAlive yes
#UseLogin no
# 特権分離の設定(使う)
UsePrivilegeSeparation yes
#PermitUserEnvironment no
#Compression yes
#MaxStartups 10
# no default banner path
#Banner /some/path
#VerifyReverseMapping no
# override default of no subsystems
Subsystem sftp /usr/libexec/sftp-server