LoginSignup
7
1

More than 3 years have passed since last update.

GCPのVMインスタンスにrootでログインする方法

Last updated at Posted at 2020-10-02

はじめに

GCP上のGCEでVMインスタンスを作成した場合、デフォルトではGCPアカウント名のユーザーでSSHログインすることになる。
このユーザーではパッケージインストールなどの度に sudo コマンドが必要となり面倒なので、本記事ではrootユーザーでログインする方法をメモとして残しておく。

実行環境

【GCP VM環境】
  ・Ubuntu20.04 LTS
  ※前提として、VMインスタンスは作成済みとする。

手順

1.rootユーザーでのログインを許可する
2.Cloud Console からrootユーザーでログイン

1.rootユーザーでのログインを許可する

以下のコマンドよりconfigファイルを編集して、PermitRootLogin yes を追記する

$ sudo nano /etc/ssh/sshd_config
/etc/ssh/sshd_config
~~~省略~~~~~~~~~~~~~
#LoginGraceTime 2m
PermitRootLogin yes
#PermitRootLogin prohibit-password
~~~省略~~~~~~~~~~~~~

設定を反映させるために、サービスの再起動

$ sudo systemctl restart sshd

2.Cloud Console からrootユーザーでログイン

[Cloud Console]を立ち上げる。
Qiita-no011_img01.jpg
以下のコマンドを実行。

$ gcloud compute ssh --project [project_id] --zone [zone] root@[VMインスタンス名]

以上でログイン完了!

■補足

作業が終わったら忘れずに PermitRootLogin no に変更しておく。

$ sudo nano /etc/ssh/sshd_config
/etc/ssh/sshd_config
~~~省略~~~~~~~~~~~~~
#LoginGraceTime 2m
PermitRootLogin no
#PermitRootLogin prohibit-password
~~~省略~~~~~~~~~~~~~
$ sudo systemctl restart sshd
7
1
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
7
1