LoginSignup
7
16

More than 5 years have passed since last update.

ConoHaでVPS借りたので初期設定メモ

Last updated at Posted at 2017-04-16

前提

VPSのスペック

  • CentOS 7.3(64bit)
  • Memory : 512MB
  • CPU : 1 core
  • Disk : SSD 20GB

クライアント側の環境

  • macOS Sierra (ver. 10.12.4)

参考にしたページ

ConohaにVPSを設置して、SSHログイン、ポート番号変更、rootログイン禁止までを30分で!
CentOS7のfirewalldでsshのポート番号を変更する方法

サーバ追加前にやること

鍵の作成

クライアント側でターミナルを開いて、ssh-keygenを実行。
パスフレーズは空欄でもいいし、秘密鍵がうっかり流出することに備えて入力してもOK。

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/<ユーザ名>/.ssh/id_rsa):<好きなファイル名>
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
--omit--

ConoHaでサーバ追加

基本的にGUIに沿って進めていけばOK。
注意点としては、

  • 接続許可ポートは「すべて許可」にチェックを入れる
  • 公開鍵をGUIから登録する
    1. SSH Key
    2. 新しいキーを登録を選択
    3. インポートを選択
    4. パブリックキーの欄に先ほど作成した公開鍵(Defultだと/Users/<ユーザ名>/.ssh/id_rsa.pub)をコピペ

クライアント側(Mac)への設定追加

~/.ssh/config
#Conoha
Host conoha
  HostName aaa.bbb.ccc.ddd #サーバに割り当てられたIPv4アドレス(GUIから確認)
  User root
  Port 22
  IdentityFile ~/.ssh/id_rsa #作成した秘密鍵

これでssh conohaでログインできるはず

VPS側の設定

パッと思いつく設定内容といえば……
1. 作業用ユーザ作成
2. rootでのssh禁止
3. ssh パスワードログイン禁止
4. ssh ポート変更

今回は、個人用途なので1と2はスキップ。

sshのパスワードログイン禁止

実は、サーバ作成のときにログイン禁止の設定になってました。
念のため、/etc/ssh/sshd_configを確認して設定が入っていることを確認しておきましょう。

/etc/ssh/sshd_config
--omit--
# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no
PermitEmptyPasswords no #ここはコメントアウトを外しておく
--omit--

設定を変更した場合はリスタートをお忘れなく

# systemctl restart sshd.service

SSHのポート番号を変更する

firewalldでポートを開ける

まずは事前の状態確認から。

# firewall-cmd --get-active-zones 
public
  interfaces: eth0
# firewall-cmd --info-zone public
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources: 
  services: dhcpv6-client ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 
# firewall-cmd --info-service ssh
ssh
  ports: 22/tcp
  protocols: 
  source-ports: 
  modules: 
  destination: 

activeになっているzoneがpublicで、
publicではdhcpv6-clientとsshがserviceとして設定されていて、
sshでは22/tcpが許可されているということですね。

ということで、sshにポートを追加。
下の例では60022にしてますが、49152-65535から好きな数字を選びましょう。

# cp -p /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/ssh.xml
# vi /etc/firewalld/services/ssh.xml
/etc/firewalld/services/ssh.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>SSH</short>
  <description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
  <port protocol="tcp" port="22"/>
  <port protocol="tcp" port="60022"/>
</service>

設定変更できたら、firewalldをリスタート

# firewall-cmd --reload
success
# firewall-cmd --info-service ssh
ssh
  ports: 22/tcp 60022/tcp
  protocols: 
  source-ports: 
  modules: 
  destination: 

sshdの設定変更

sshのポート設定を先ほど開けたポートに変更しましょう。

/etc/ssh/sshd_config
# Port 22
Port 60022

設定変更が終わったらsshdのリスタート。

# systemctl restart sshd.service 

事後確認、22/tcpの閉塞

クライアント側から先ほど設定したポート番号でsshを試行。

$ ssh -p 60022 conoha

余談ですが、当初ここでsshがタイムアウトになってしまい原因を確認したところConoHaのGUIで接続許可ポートがSSH(22)を設定していたせいでした……

上手くいったらクライアント側設定ファイルも書き換えておきましょう。

~/.ssh/config
#Conoha
Host conoha
  HostName aaa.bbb.ccc.ddd #サーバに割り当てられたIPv4アドレス(GUIから確認)
  User root
#  Port 22
  Port 60022
  IdentityFile ~/.ssh/id_rsa #作成した秘密鍵

22/tcpを開けっ放しにしていては変更した意味がないので、VPSにログインして閉じます。

/etc/firewalld/services/ssh.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>SSH</short>
  <description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
  <port protocol="tcp" port="60022"/>
</service>

もう一つ余談ですが、xmlでは#でコメントアウトできないです。
# <port protocol="tcp" port="22"/>と書いて、22番で繋がるじゃねーかと言っていたのは私です。スパッと消してしまいましょう。

17/04/19 追記
XMLのコメントアウトは"<!--"と"-->"の間に挟むんでしたね。すっかり忘れていました。
コメントありがとうございます!
http://gogodiet.net/z/xml/3_3.htm

設定変更後は忘れずリスタート。

# firewall-cmd --reload
success
# firewall-cmd --info-service ssh
ssh
  ports: 60022/tcp
  protocols: 
  source-ports: 
  modules: 
  destination: 

以上です。

7
16
2

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
7
16