LoginSignup
5
9

More than 3 years have passed since last update.

さくらVPS 初期設定

Last updated at Posted at 2020-05-04

はじめに

VPSを使ってAPIサーバを構築しようと思い、さくらVPS を使ってみました。
かなり特殊なことをしようとしていたので、残念ながらVPS上では実現ができませんでしたが・・・
普段は触らないサーバーサイドをせっかく色々触ってみたので、備忘録として残します。

VPSとは

VPSとはVirtual Private Serverの略です。要するにプライベートな仮想サーバです。(まんま)
クラウドを使って何かサービスを作ろうと思ったときに、「レンタルサーバ」という選択肢もあると思いますが、VPSはレンタルサーバとは大きく異なります。
レンタルサーバとVPSで一番の違いはVPSでは root権限を与えられている ということです。
root権限を持つことによるメリット・デメリットは以下です。

メリット
  • OSを自由に選択できる
  • 好きな言語の実行環境が構築できる
  • サーバーのリソースを自分で自由に割り振れる
デメリット
  • サーバの構築作業を自分でしなければならない
  • 保守・メンテナンスを自分でしなければならない

VPSで一番うれしいことは好きな言語の実行環境が用意できることではないでしょうか。最近ではnode.jsなどをサーバーサイドで動かすことも多いかと思いますが、レンタルサーバでは使える言語が限定されてしまいます。
ただ、 大いなる力には大いなる責任が伴う とでも言うのでしょうか。VPSを使う場合、サーバーの構築からメンテまですべて自分でしなければいけません。サーバーがダウンしたら自分で復旧するようにしなければなりませんし、なんの対策もしていなければあっという間に攻撃されてしまうでしょう。

さくらVPS無料体験

さくらVPSでは2週間の無料体験期間がありました。(2020/05/04時点)契約時に支払い方法など追加しますが、期間終了までに申し込みのキャンセルをすれば大丈夫です。
なので、今回自分の用途では使えなかったので申し込みキャンセルをしました。VPSがどんなものなのか知りたいみたいな方は試してみるのもいいかもしれません。

キャンセル方法は以下です。
https://help.sakura.ad.jp/hc/ja/articles/206205281
ただ、体験期限が切れそうになるとしっかりメールで教えてくれます。親切。

CentOS編

今回インストールしたのはCentOS7となります。ちなみにmac環境にて作業しました。(Linux環境であればそこまで内容は変わらないかと)

rootでログイン

rootのアカウントとパスワードは申込完了後メールで届きます。

$ ssh root@xxx.xx.xx.xxx
root@xxx.xx.xx.xxx's password: 

SAKURA Internet [Virtual Private Server SERVICE]

[root@xxx-xxx-xxxxx ~]#

作業用一般ユーザの作成

初期状態ではログイン可能なユーザは「root」のみです。root はサーバ上で全ての操作が可能な特権ユーザです。そのため、インターネット上の SSH サーバに対する不正アクセスや攻撃対象として日々狙われています。攻撃を受ける危険性を減らすには、root でのログインを禁止し、一般ユーザのみログインを許可する設定を行います。

[root@xxx-xxx-xxxxx ~]# useradd gdate
[root@xxx-xxx-xxxxx ~]# passwd gdate
ユーザー gdate のパスワードを変更。
新しいパスワード:
新しいパスワードを再入力してください:
passwd: すべての認証トークンが正しく更新できました。

wheel グループに対する sudo 設定の有効化

# usermod -G wheel gdate
// 一旦ログアウトし、再びログインする
$ sudo id
[sudo] gdate のパスワード:
uid=0(root) gid=0(root) groups=0(root)

公開鍵認証を使ったログイン設定

// 鍵ペア作成
$ ssh-keygen -t rsa -f id_rsa_sakura
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in id_rsa_sakura.
Your public key has been saved in id_rsa_sakura.pub.

// 公開鍵をさくらサーバに転送
 $ scp id_rsa_sakura.pub gdate@xxx.xx.xx.xxx:~/
gdate@xxx.xx.xx.xxx's password: 
id_rsa_sakura.pub 

// サーバにログイン
$ cd                                    # 確実にホームディレクトリに移動
$ mkdir .ssh                            # ディレクトリ作成
$ chmod 700 .ssh/                       # 自分以外のアクセスを禁止
$ mv id_rsa.pub .ssh/authorized_keys    # 公開鍵リスト作成
$ chmod 600 .ssh/authorized_keys        # 自分以外の読み書きを禁止

より安全な SSH 接続のための設定

rootでのログインの禁止とパスワードでのログインを禁止します。

// サーバにログイン後
$ sudo cp /etc/ssh/ssh_config /etc/ssh/ssh_config.bak # 念の為設定ファイルをバックアップ
$ sudo vim /etc/ssh/sshd_config
PermitRootLogin no           # yes を「no」(許可しない)に
PasswordAuthentication no    # yes を「no」に
// ファイルを保存後、sshd を再起動して設定を有効化します
$ sudo systemctl restart sshd
// 正常に稼働しているかどうかは、次のようにコマンドを実行し、sshd が「Active: active (running)」なのを確認します
$ sudo systemctl status sshd
// Active: active (running) が出てればOK

上記設定後、念の為サーバに接続した状態で別のタブで接続を試みます

 $ ssh sakura
Enter passphrase for key '/Users/hoge/.ssh/id_rsa_sakura':
// 初回のみパスフレーズを求められます
// ログインできればOK 

さくらVPS側のSSH設定

自分のプロジェクトのgitなどにsshするために設定します。

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/gdate/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/gdate/.ssh/id_rsa.
Your public key has been saved in /home/gdate/.ssh/id_rsa.pub.

// できたパブリック鍵をgithubに登録

$ ssh -T git@github.com
The authenticity of host 'github.com (52.69.186.44)' can't be established.
RSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
RSA key fingerprint is MD5:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no)? yes 
Warning: Permanently added 'github.com,xx.xx.xxx.xx' (RSA) to the list of known hosts.
Enter passphrase for key '/home/gdate/.ssh/id_rsa': 
Hi gdate! You've successfully authenticated, but GitHub does not provide shell access.

Ubuntu編

今回インストールしたのはUbuntu18.04となります。

rootが使えない

Ubuntu のデフォルト設定では root ユーザーはパスワードが設定されていないため利用不可となっています。 基本は管理者として設定したユーザーで sudo コマンドを利用して root 特権を行使するべき

vimがない

viは使い慣れないのでvimを使いたい

$ sudo apt-get install vim
Reading package lists... Done
Building dependency tree       
Reading state information... Done

...

Get:1 http://jp.archive.ubuntu.com/ubuntu bionic/main amd64 libgpm2 amd64 1.20.7-5 [15.1 kB]
Err:2 http://jp.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpython3.6 amd64 3.6.8-1~18.04.1
  404  Not Found

404が返ってきたので、aptをアップデート

$ sudo apt-get update
Hit:1 http://jp.archive.ubuntu.com/ubuntu bionic InRelease
Get:2 http://jp.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]

...

Fetched 7,609 kB in 3s (2,941 kB/s)                           
Reading package lists... Done

$ sudo apt-get install vim
Reading package lists... Done
Building dependency tree       
Reading state information... Done

...

Processing triggers for libc-bin (2.27-3ubuntu1) ...

wheelグループがない

/etc/pam.d/su にwheel ユーザーについて追記

$ sudo vim /etc/pam.d/su
# Uncomment this if you want wheel members to be able to
# su without a password.
# auth       sufficient pam_wheel.so trust
auth sufficient pam_wheel.so trust group=wheel

visudo 編集

$ sudo visudo
// 末尾に以下を追加
# Same thing without a password
%wheel ALL=(ALL) NOPASSWD:ALL

^xで終了しようとすると保存するか聞かれるのでY。さらにFile Name to Write: /etc/sudoers.tmpとか出てくるが、Enterで保存される。

wheel グループ作成とユーザー追加

$ sudo addgroup wheel
Adding group `wheel' (GID 1002) ...
Done.
$ sudo adduser gdate
Adding user `gdate' ...
Adding new group `gdate' (1001) ...
Adding new user `gdate' (1001) with group `gdate' ...
Creating home directory `/home/gdate' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for gdate
Enter the new value, or press ENTER for the default
    Full Name []: 
    Room Number []: 
    Work Phone []: 
    Home Phone []: 
    Other []: 
Is the information correct? [Y/n] y

$ sudo usermod -aG wheel gdate
$ sudo id
uid=0(root) gid=0(root) groups=0(root)

(補足)useraddではホームディレクトリが追加されない

CentOSと同じようにuseraddをするとホームディレクトリが作られない

$ useradd gdate
$ sudo addgroup wheel
$ sudo usermod -aG wheel gdate
// この後ログインし直そうとするとホームディレクトリがないと言われログインできず焦る
// この場合rootでログインし、ユーザー削除後adduserコマンドで作り直す
$ userdel gdate

ssh設定

ubuntuはデフォルトでrootにパスワード設定がされていないため、以下だけ変更

$ sudo vim /etc/ssh/sshd_config
PasswordAuthentication no
// ファイルを保存後、sshd を再起動して設定を有効化します
$ sudo systemctl restart sshd
// 正常に稼働しているかどうかは、次のようにコマンドを実行し、sshd が「Active: active (running)」なのを確認します
$ sudo systemctl status sshd
// Active: active (running) が出てればOK

(補足)OS再インストール直後にやること

サーバ構築中にやっぱOSを変えたいなど思った際に、OSの再インストールをした直後にやること。

サーバの情報が違うためsshしようとすると警告される

$ ssh root@xxx.xx.xx.xxx
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:x.
Please contact your system administrator.
Add correct host key in /Users/xxx/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /Users/xxx/.ssh/known_hosts:4
ECDSA host key for xxx.xx.xx.xxx has changed and you have requested strict checking.
Host key verification failed.

ホストの情報を削除

$ ssh-keygen -R ホスト名

参考

https://vps-news.sakura.ad.jp/tutorials/centos7-initial-settings/
https://qiita.com/white_aspara25/items/c1b9d02310b4731bfbaa

5
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
9