LoginSignup
13
7

More than 5 years have passed since last update.

Google Compute Engine(GCP)のポート(sshd)設定でハマった

Last updated at Posted at 2018-08-16

夏休みということで、久しぶりにプログラミングに励んでみようと思ってGCPを触ってみたら、いきなりハマったのでメモ。

目的はGoolge Cloud Platform(GCP)におけるGoogle Compute Engine(GCE)の無料枠であるmicroインスタンスを構築してみること。

様々なところで、無料枠のGCE構築は手順はあるので、それに沿って実施したところ、sshdの設定で躓いた。
今、思うと初歩的なところなのだが、意外とハマりそうな気がした。

躓いたところは、Port設定の部分。
ひとまずVMインスタンスを作成した後、セキュリティ対応として、基本的に構築するCentOSのPort設定(デフォルトで22)を変更するというもの。それに合わせてCentOSで標準に備わっているSELinux と iptablesを無効化していく。

SELinux と iptablesの無効化

ここでは、sshd対応(独自Port設定)のために、既存のSELinuxとiptablesを無効化していく。
※別途、コンソールよりファイアウォールのルールを追加する必要あり。

sudo systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

続いて、 /etc/selinux/config を編集

$ sudo vi /etc/selinux/config

下記のように変更。

SELINUX=disabled

続いて、Port設定を変更していくのだが、ここで問題が起こる。

Port設定の変更

$ sudo vi /etc/ssh/sshd_config

sshdの設定を以下のように編集

#Port 22
Port XXXXXX # 好きな番号(制限はある)

リスタートで設定を変更する。

$ sudo systemctl restart sshd

を実施したところ、以下のようなエラーで進めないことになる。

Job for sshd.service failed because the control process exited with error code. See "systemctl status sshd.service" and "journalctl -xe" for details.

結論を言うと、SELinuxの設定を変更をまず適応する必要があった。
そのため、GCPのコンソールよりインスタンスの再起動を実施。

SELinuxが無効になっていることを確認。

$ sudo systemctl is-enabled firewalld
disabled
$ getenforce
Disabled

ともに disabled になっていること。

sshdのステータスも確認してみると、、、

$ sudo systemctl status sshd.service
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2018-XX-XX XX:XX:XX UTC; 42s ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 764 (sshd)
   CGroup: /system.slice/sshd.service
           └─764 /usr/sbin/sshd -D

$ sudo systemctl restart sshd

問題のない様子なので、sshdをリロードし設定を改めて適応。

結果、自分で設定したPortで接続が可能となる。

$ gcloud compute --project "プロジェクト名" ssh --zone "us-east1-b" "XXXXX-instance" --ssh-flag="-p XXXXX"

Portを指定した接続は上記のように、 --ssh-flag="-p XXXXX"
を付け加えるだけ。

基本的なところだが、何となくVMインスタンスを構築しようとするとハマってしまった。

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