さくらのVPSプラン乗り換え計画(1)
タイトルの通り、プランの乗り換えにチャレンジ。 VPS の OS は標準の CentOS じゃなくて、使い慣れた Ubuntu にしたかったので、OS の再インストールをした。ところが、用意されている「標準OS」の Ubuntu 16.04 をインストールしてみたけど、ログインができない。パスワードを間違えたのかと思って何度かやり直し(インストール自体のやり直しも含めて)やってみたけどダメ。CentOS6 をインストールすると何故か大丈夫。なんでだ? 仕方がないので「カスタムOS」の Ubuntu 16.04 をインストールしたら(インストールに時間はかかったけど)無事、ログインに成功した。ユーザー名は takatoh。 VPS のページから、コンソールを選んでサーバに接続する。 まずは ssh の設定。/etc/ssh/sshd_config ファイルを編集する。
$ sudo vi /etc/ssh/sshd_config
ポートを変更。
Port 60000
root でのログインを禁止。
PermitRootLogin no
設定を反映するために sshd を再起動。
$ sudo systemctl restart sshd
次にファイアウォールの設定。
$ sudo ufw enable
$ sudo ufw default DENY
$ sudo ufw allow 60000
これで、ローカルの PC から ssh でアクセスできるようになったはず。試してみよう。
takatoh@envelopes $ ssh -p 60000 takatoh@160.16.224.68
takatoh@160.16.224.68's password:
Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-77-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
Last login: Thu May 4 10:48:04 2017
takatoh@tk2-254-36564:~$
OK。 それじゃこんどはパスワード認証をやめて、公開鍵での認証に切り替える。
takatoh@tk2-254-36564:~$ pwd
/home/takatoh
takatoh@tk2-254-36564:~$ mkdir .ssh
takatoh@tk2-254-36564:~$ chmod 700 .ssh
takatoh@tk2-254-36564:~$ cd .ssh
takatoh@tk2-254-36564:~/.ssh$ wget -O authorized_keys https://github.com/takatoh.keys
--2017-05-04 11:08:43-- https://github.com/takatoh.keys
Resolving github.com (github.com)... 192.30.255.113, 192.30.255.112
Connecting to github.com (github.com)|192.30.255.113|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/plain]
Saving to: 'authorized_keys'
authorized_keys [ <=> ] 381 --.-KB/s in 0.001s
2017-05-04 11:08:43 (309 KB/s) - 'authorized_keys' saved [381]
takatoh@tk2-254-36564:~/.ssh$ ls -al
total 12
drwx------ 2 takatoh takatoh 4096 May 4 11:08 .
drwxr-xr-x 4 takatoh takatoh 4096 May 4 11:08 ..
-rw-rw-r-- 1 takatoh takatoh 381 May 4 11:08 authorized_keys
takatoh@tk2-254-36564:~/.ssh$ chmod 600 authorized_keys
takatoh@tk2-254-36564:~/.ssh$ ls -al
total 12
drwx------ 2 takatoh takatoh 4096 May 4 11:08 .
drwxr-xr-x 4 takatoh takatoh 4096 May 4 11:08 ..
-rw------- 1 takatoh takatoh 381 May 4 11:08 authorized_keys
ここでいったんログアウトして、鍵認証でログインできるか試してみる。
takatoh@envelopes $ ssh -p 60000 takatoh@160.16.224.68
Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-77-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
Last login: Thu May 4 11:05:11 2017 from 58.1.225.90
OK。それじゃもう一度 /etc/ssh/sshd_config を編集して、パスワード認証をしないことにする。
PasswordAuthentication no
sshd の再起動。
takatoh@tk2-254-36564:~$ sudo systemctl restart sshd
とりあえずここまで。
