2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

vmに入れたUbuntuとsshできるようになるまで

Last updated at Posted at 2021-06-30

はじめに

Docker全盛期ではあるものの、依然としてWindowsやMacで開発していると、UbuntuのvmをVirtualBoxやHyper-Vで立てることがあると思う。

幸いにしてVSCodeはssh接続さえできていればVSCodeからもssh先にアクセスができる。

・・・はい嘘ですね、Windowsがホストの時は
Windows10でvscodeからSSHできるようになるまで
をどうぞ

それはさておき、sshできるようにしていく

前提環境

Host

以下のいずれかで、別のところへのssh自体はすでにできる状態であること

  • mac
  • Windows + msys2

つまり、ssh-keygen, ssh, rsyncくらいは使えることを前提にしている。

Guest

  • Ubuntu 18.04

別にほかのバージョンでも手順は変わらないと思うが念のため。

手順

パスワード認証でVMに接続できるようになる

まずVMを起動し、ホスト名を調べる。私の場合はyumetodo-virtual-machine2だった。

$ hostnamectl
   Static hostname: yumetodo-virtual-machine2
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 0aa80159bab84389b19bb219d8a8dcb7
           Boot ID: 8541e864263841eb98684e5059df77f8
    Virtualization: microsoft
  Operating System: Ubuntu 18.04.5 LTS
            Kernel: Linux 5.4.0-77-generic
      Architecture: x86-64

この時大文字を含んでいるとsshするときに面倒くさいのでhostnamectl set-hostname <new name>して変更しておく。

次にecho $USERでユーザー名を調べる。私の場合はyumetodoだった。

次にssh serverをセットアップする。

$ sudo apt update
$ sudo apt install openssh-server

うまくいけば次のようにactive (running)となるはずだ

$ sudo systemctl start ssh
● ssh.service - OpenBSD Secure Shell server
   Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2021-06-30 09:36:18 JST; 11h ago
 Main PID: 933 (sshd)
    Tasks: 1 (limit: 4662)
   CGroup: /system.slice/ssh.service
           └─933 /usr/sbin/sshd -D

(以下略)

ホスト側で端末を立ちあげる。

ssh <ubuntuのユーザー名>@<ubuntuのホスト名>

ログイン出来たら成功だ。

公開鍵認証でVMに接続できるようになる

いくらLANとはいえ安全とは限らない。またVMのUbuntuのユーザーにパスワードを設定していると思うが、これを毎回入れるのも面倒だ。公開鍵認証しよう。

まず公開鍵をホスト側で生成する。最近の流行はed25519らしいのでそれにする。

$ ssh-keygen -t ed25519
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/yumetodo/.ssh/id_ed25519):

Enter file in which to save the keyで名前を付けられる。自分は/home/yumetodo/.ssh/ubuntu_vm_ed25519とした。

注意点

  • 鍵のパスワードはいらない: ローカルでしか使わんのでパスワードはとくに必要ない
  • VMの上でssh-keygenしない: VMの上は真の乱数を得るための乱数源が不足するためかならず実機で生成する
  • 鍵を使いまわさない: だめ、絶対。またこの意志を表明するためにsshの鍵の名前は何に使うものかわかりやすいものにする。

そうしたら生成した鍵のうち、公開鍵をVMに転送する

$ rsync -au ~/.ssh/ubuntu_vm_ed25519.pub yumetodo@yumetodo-virtual-machine2:~

VM側での作業に移る。パスワード認証でsshしてもいいし、VMのGUI上で端末を動かしてもいい。

$ cd ~
$ mkdir -p .ssh
$ cat ubuntu_vm_ed25519.pub >> .ssh/authorized_keys
$ sudo chmod 700 .ssh
$ sudo chmod 600 .ssh/authorized_keys

ホスト側に戻って、公開鍵を使うように設定する。~/.ssh/configを開いて以下を書き込む。

~/.ssh/config
Host yumetodo-virtual-machine2
    IdentityFile ~/.ssh/ubuntu_vm_ed25519
    User yumetodo

忘れずに権限を書き換える。

Windowsでは
Windows10でvscodeからSSHできるようになるまで - Qiita#~/.ssh/configの権限設定
の手順を行う。

macでは

$ cd ~
$ sudo chmod 700 .ssh
$ sudo chmod 600 .ssh/config

とする。

ここまでくれば公開鍵を使ってsshできるはずだ。

$ ssh yumetodo-virtual-machine2
Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 5.4.0-77-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

0 updates can be applied immediately.

New release '20.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Your Hardware Enablement Stack (HWE) is supported until April 2023.
Last login: Wed Jun 30 20:34:59 2021 from fe80::551c:b446:494a:4e7a%eth0
yumetodo@yumetodo-virtual-machine2 ~ $ 

VMのパスワード認証を無効化する

LAN内からの攻撃を避けるために、パスワード認証を無効化する。portまでは変えなくていいだろう。

VM側でsudo nano /etc/ssh/sshd_configなどとしてPasswordAuthenticationnoにする

/etc/ssh/sshd_config
-#PasswordAuthentication yes
+PasswordAuthentication no

あとはsshdを再起動する

$ sudo systemctl restart ssh
2
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?