KOYAMA Yoshiaki のブログ

プログラミングについての試行錯誤をつらつら書き溜めていきます。

'さくらインターネットのレンタルサーバー スタンダード' と ssh

以前、漫画家のすがやみつるさんが

プログラミングができなくても作れるTwitter botの作り方
http://pha22.net/twitterbot/

を利用して Twitter bot (EasyBotter) を作成したと発言されていました。

COOL ONLINE
http://www.cool.ne.jp/
PHP version 4.3.2

私は COOL ONLINE 正規会員登録しています。COOL ONLINE の PHP は version 4.3.2 のようです。Twitter bot (EasyBotter) では PHP 5 以上が必要です。



低額の料金で VPS が利用できる 'さくらの VPS' に興味を持っていたので

さくらインターネット|価値あるサービスと満足を提供するデータセンター
http://www.sakura.ad.jp/

で資料を読んでいました。 たまたま 'さくらの VPS' ではなく 'さくらのレンタルサーバー' も試しに調べてみると ssh、cron が使え、PHP 5.2.14 と記載されていました。アクセス解析も付いています。

プログラミングができなくても作れるTwitter botの作り方
http://pha22.net/twitterbot/

Twitter bot (EasyBotter) を試すために 'さくらインターネットレンタルサーバー スタンダード' に移転することにしました。 ssh を利用してみたかったことも動機の一つです。

Yoshiaki's HomePage
http://koyama-.cool.ne.jp/

上記ページを下記ページに移転します。上の COOL ONLINE ホームページは、来年の 2月頃に終了予定です。もう少し早まるかもしれません。

Yoshiaki's HomePage
http://kyoshiaki.sakura.ne.jp/osx/index.html


Twitter bot (EasyBotter) は何の問題なくインストールすることができました。新規に Twitter bot (EasyBotter) 用の Twitter アカウント KYoshiakiBot を作成し、

Create cool applications! | dev.twitter.com
http://dev.twitter.com/

アプリケーション名を YKEasyBotter で登録しました。上部の Your apps をクリックして

Twitter Applications | dev.twitter.com
http://dev.twitter.com/apps

現在登録されているアプリケーション一覧を表示し、YKEasyBotter をクリックすると Consumer key、Consumer secret を確認できます。Access Token を見つけるのに少し苦労しました。結局、Consumer key などが表示されているページの右側にある My Access Token をクリックすることで Access Token (oauth_token)、Access Token Secret (oauth_token_secret) を見つけることができました。


残念ながら、私が確認したかった OAuth 認証の仕組みではありませんでした。Twitterbot (EasyBotter) 自体は何の問題もなく動作しました。'さくらインターネットレンタルサーバー スタンダード'で は cron も使うことができます。


'さくらインターネットレンタルサーバー スタンダード' には Webalizer によるアクセス解析が付いてきます。しかし、COOL ONLINE で

COOL ONLINE
http://www.cool.ne.jp/

Yoshiaki's HomePage
http://koyama-.cool.ne.jp/

高機能アクセス解析CGI Professional版 - futomi's CGI Cafe
http://www.futomi.com/library/accp.html

利用していたシェアウェアの '高機能アクセス解析CGI Professional版 - futomi's CGI Cafe' と比べると非力です。しかし、'さくらインターネットレンタルサーバー スタンダード' では、一日ごとのアクセスをログファイルに作成してくれます。そのログファイルをコマンドを実行すると自動的にダウンロードし、解析して表示する Python スクリプトとシェルスクリプトを作成しました。いずれ公開したいと思います。




さくらインターネットレンタルサーバー スタンダード' では ssh も利用できます。



'さくらの VPS' のために Kindle 版の Pro Linux System Administration 本を購入したのですが、ssh の使用方法も解説されていました。良書です。まだ、半分ほどしか読んでいませんが!


Mac OS X Snow Leopard (バージョン 10.6.5 ビルド 10H574) 上の 'ターミナル' と Ubuntu 10.10 'Terminal' 上での簡単な ssh の利用方法解説したいと思います。間違いもあるかもしれません。


SSH の仕組みを利用するコマンドは

  • ssh username@remotehost
  • scp foo.txt username@remotehost:/home/username/www/
  • sftp username@remotehost

などがあります。

ただし、アカウント: username、初期ドメイン: remotehost は

会員メニュートップ
https://secure.sakura.ad.jp/menu/top/

の '契約情報'/'契約サービスの確認'/'サーバ設定' をクリックして 'さくらインターネットサーバコントロールパネル' 上部の 'お客様情報' で確認してください。

さくらインターネットサーバコントロールパネル
https://secure.sakura.ad.jp/rscontrol/rs/

以下の説明で、アカウント: username、初期ドメイン: remotehost を自分の環境に合わせて変更してください。また、ssh で使用するパスワードは、さくらのウェブ上の会員 ID でログインするパスワードではないので注意してください。

  • ssh username@remotehost
  • scp foo.txt username@remotehost:/home/username/www/
  • sftp username@remotehost

上のコマンドを何も設定せずに実行するとパスワードを尋ねられますが、RSA 公開鍵暗号による認証を利用するとパスワードやパスフレーズ (passphrase) : (秘密鍵用のパスワード) を入力せずにログインすることができます。


sshRSA 公開鍵暗号による認証を利用するには

1. コマンド ssh-keygen を使って公開鍵、秘密鍵を作成する。パスフレーズ (passphrase) : (秘密鍵用のパスワード) は、ssh-agent やキーチェーンで登録できるので安全のためにできるだけ入力してください。

2. コマンド ssh-copy-id を使って ~./ssh フォルダのファイル authorized_keys に公開鍵を追加。Mac OS X 上では、コマンド ssh-copy-id がないので手動でコピーする必要があります。

3. コマンド ssh-agent、 eval `ssh-agent`、 ssh-add を使って パスフレーズ (passphrase) : (秘密鍵用のパスワード) を登録。Mac OS X の場合、キーチェーンが パスフレーズ (passphrase)  : (秘密鍵用のパスワード) を管理してくれます。

おおまかに上の3つの手順で設定します。


1. Mac OS X Snow Leopard (バージョン 10.6.5 ビルド 10H574) 上の 'ターミナル' 上での ssh 設定


1-1. Mac OS X 上の 'ターミナル' で、コマンド ssh-keygen -t rsa -b 2048 で公開鍵、秘密鍵を作成。パスフレーズ (passphrase) : (秘密鍵用のパスワード) も安全のためできるだけ登録してください。

~ $ ssh-keygen -t rsa -b 2048
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/xxxx/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/xxxx/.ssh/id_rsa.
Your public key has been saved in /Users/xxxx/.ssh/id_rsa.pub.
The key fingerprint is:
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx xxxx@xxxxx-xxxxx-xx-xxxx-x.local
The key's randomart image is:
+--[ RSA 2048]----+
|         .F++oo..|
|          xxx+ . |
|           =+.. x|
|       . . .+. xx|
|        G .  .xx |
|              ...|
|               ..|
|               ..|
|              .. |
+-----------------+
~ $ 


1-2. ~/.ssh フォルダにファイル id_rsa (公開鍵) 、d_rsa.pub (秘密鍵) が作成されているのを確認。

~ $ cd .ssh
~/.ssh $ ls
id_rsa		id_rsa.pub	known_hosts


1-3. リモートホスト (さくら側) に ファイル id_rsa.pub をファイル authorized_keys としてコピー。パスワードを尋ねられるので入力してください。

~/ $ cd .ssh
~/.ssh $ scp id_rsa.pub username@remotehost:/home/username/.ssh/authorized_keys
username@remotehost's password: 
id_rsa.pub                                    100%  422     0.4KB/s   00:00    


1-4 最初にコマンド ssh を実行するとパスフレーズを尋ねられます。パスフレーズ (passphrase) : (秘密鍵用のパスワード) を入力して 'パスワードをキーチェーンに保存' にチェックを入れてください。これで ssh、scp、sftp を実行しても パスフレーズ (passphrase) : (秘密鍵用のパスワード) を尋ねられることはありません。

~/.ssh $ ssh usernamei@useraname.sakura.ne.jp
Identity added: /Users/yoshiaki/.ssh/id_rsa (/Users/xxxx/.ssh/id_rsa)
Last login: Sun xxx  5 xx:43:14 2010 from xx.xx.xx.xx
Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994
	The Regents of the University of California.  All rights reserved.

FreeBSD 7.1-RELEASE-p15 (SAKURA11S) #10: Tue Nov 16 09:48:35 JST 2010

Welcome to FreeBSD!

%exit
logout
Connection to remotehost closed.


上図の赤枠部分より、秘密鍵のパスフレーズがアプリケーション 'キーチェーンアクセス' で 保存されているのが確認できます。


1-5 リモートホスト (さくら側) のファイル authorized_keys のパーミッションは 600 にしてください。

~ $ ssh username@remotehost
Last login: xxx xxx 00 xx:32:00 2010 from xx.xx.xx.xx
Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994
	The Regents of the University of California.  All rights reserved.

FreeBSD 7.1-RELEASE-p15 (SAKURA11S) #10: Tue xxx 16 09:48:35 JST 2010

Welcome to FreeBSD!

%cd .ssh
%ls
authorized_keys
%chmod 600 authorized_keys 
%ls -alF
total 10
drwx------   x xxxx  xxxx  512 xxx 0 16:54 ./
drwx---r-x   xx xxxx  xxxx  512 xxx 00 19:53 ../
-rw-------   xx xxxx xxxx  819 xxx  0 19:50 authorized_keys
%exit
logout
Connection to remotehost closed.
~ $ 


2. Ubuntu 10.10 'Terminal' 上での簡単な ssh の設定方法


2-1. Ubuntu 上の 'Terminal' で、コマンド ssh-keygen -t rsa -b 2048 で公開鍵、秘密鍵を作成。パスフレーズ (passphrase) : (秘密鍵用のパスワード) も安全のため登録しておく。

ubuntu:~$ ssh-keygen -t rsa -b 2048
Generating public/private rsa key pair.
Enter file in which to save the key (/home/xxxx/.ssh/id_rsa): 
Created directory '/home/xxxx/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/xxxx/.ssh/id_rsa.
Your public key has been saved in /home/xxxx/.ssh/id_rsa.pub.
The key fingerprint is:
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx xxxxi@ubuntu
The key's randomart image is:
+--[ RSA 2048]----+
|        ...      |
|       . x       |
|      . . .      |
|       x .       |
|      x x x      |
|     x x x .     |
|    x.x . .      |
|     --x .       |
|    .x--.        |
+-----------------+


2-2. ~/.ssh フォルダにファイル id_rsa (公開鍵) 、d_rsa.pub (秘密鍵) が作成されているのを確認。

ubuntu:~$ cd .ssh
ubuntu:~/.ssh$ ls
id_rsa  id_rsa.pub  known_hosts
ubuntu:~/.ssh$ 


2-3. コマンド ssh-copy-id を使って、リモートホスト (さくら側) に ファイル id_rsa.pub をファイル authorized_keys に追加。パスワードを尋ねられるので入力してください。

ubuntu:~$ ssh-copy-id username@remotehost
The authenticity of host 'remotehost (xx.xx.xx.xx)' can't be established.
DSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'remotehost,xx.xx.xx.xx' (DSA) to the list of known hosts.
username@remotehost's password: 
Now try logging into the machine, with "ssh 'username@remotehost'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

ubuntu:~$ 


2-4. 試しに ssh で、リモートホスト (さくら側) にログインしてみると、パスフレーズ (passphrase) : (秘密鍵用のパスワード) を尋ねてきます。

ubuntu:~$ ssh username@remotehost
Enter passphrase for key '/home/xxxx/.ssh/id_rsa': 
Last login: xxx xxx  5 xx:33:26 2010 from xx.xx.xx.xx
Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994
	The Regents of the University of California.  All rights reserved.

FreeBSD 7.1-RELEASE-p15 (SAKURA11S) #10: Tue Nov 16 09:48:35 JST 2010

Welcome to FreeBSD!

%exit
logout
Connection to remotehost closed.
ubuntu:~$ 


2-5 コマンド ssh-agent で環境変数を確認。

ubuntu:~$ ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-TlhOE20539/agent.20539; export SSH_AUTH_SOCK;
SSH_AGENT_PID=20540; export SSH_AGENT_PID;
echo Agent pid 20540;


2-6 ssh-agent -k で停止。

ubuntu:~$ ssh-agent -k
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
echo Agent pid 1465 killed;


2-7 コマンド eval `ssh-agent` を実行して、環境変数を設定。文字 ` と ' の違いに注意してください。

ubuntu:~$ eval `ssh-agent`
Agent pid 20550
ubuntu:~$  


2-8 ssh-add を使ってパスフレーズ (passphrase) : (秘密鍵用のパスワード) を追加。

ubuntu:~$ ssh-add
Enter passphrase for /home/xxxx/.ssh/id_rsa: 
Identity added: /home/xxxx/.ssh/id_rsa (/home/yoshiaki/.ssh/id_rsa)

ubuntu:~$


2.9 これで ssh、scp、sftp でログインしても パスフレーズ (passphrase) : (秘密鍵用のパスワード) を尋ねられません。

ubuntu:~$ ssh username@remotehost
Last login: Mon xxx  x xx:53:59 2010 from xx.xx.xx.xx
Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994
	The Regents of the University of California.  All rights reserved.

FreeBSD 7.1-RELEASE-p15 (SAKURA11S) #10: Tue Nov 16 09:48:35 JST 2010

Welcome to FreeBSD!

%


2-10 リモートホスト (さくら側) のファイル authorized_keys のパーミッションは 600 にしてください。

~ $ ssh username@remotehost
Last login: xxx xxx 00 xx:32:00 2010 from xx.xx.xx.xx
Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994
	The Regents of the University of California.  All rights reserved.

FreeBSD 7.1-RELEASE-p15 (SAKURA11S) #10: Tue Nov 16 09:48:35 JST 2010

Welcome to FreeBSD!

%cd
%cd .ssh
%ls
authorized_keys
%chmod 600 authorized_keys 
%ls -alF
total 10
drwx------   x xxxx  xxxx  512 xxx 0 16:54 ./
drwx---r-x   xx xxxx  xxxx  512 xxx 00 19:53 ../
-rw-------   xx xxxx xxxx  819 xxx  0 19:50 authorized_keys
%exit
logout
Connection to remotehost closed.
~ $