LoginSignup
5
8

More than 5 years have passed since last update.

【入門編】VirtualBox でホストOS(mac)から NAT 設定しているゲストOS(主にCentOS)へ SSH 接続する方法

Last updated at Posted at 2016-12-26

VirtualBox にインストールした CentOS に、デフォルトのネットワーク設定の NAT のまま接続するのに少し苦労したので、書いときます。
特に CentOS7 インストール直後で eth0 のIP自動取得が無効になっていたので、それで繋がらなかった。

バックグランドで動いているデーモン(ファイアウォール)を止める

CentOS7 の場合

$ systemctl stop firewalld
$ systemctl disable firewalld

CentOS6 の場合

chkconfig iptables off
chkconfig ip6tables off
service iptables stop
service ip6tables stop

SELinux を無効にする

SELINUX=disabled

SELINUX を disabled に書き換えて、ゲストOSを再起動する

ipv6 を無効にする

CentOS7 の場合

以下のファイルを作成し、以下のように記述

/etc/sysctl.d/disable_ipv6.conf
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
$ sysctl -p /etc/sysctl.d/disable_ipv6.conf

CentOS6 の場合

NETWORKING_IPV6=no

以下のファイルを作成し、以下のように記述

/etc/modprobe.d/disable_ipv6.conf
options ipv6 disable=1

ip6tables は、最初で無効にしているので、 reboot する

eth0 に IP アドレスを自動で割り当てられるようにする

CentOS7 の場合

nmcli device status で eth0 のデバイス名を確認しておく

DEVICE  TYPE      STATE      CONNECTION
enp0s3  ethernet  disconnected  --

DEVICE の 項目の enp0s3 は、各自の環境で異なる可能性があるので、

nmcli con mod enp0s3 connection.autoconnect "yes"
systemctl restart NetworkManager
systemctl restart network

CentOS6 の場合

TODO

ホストOS側でポートフォワーディングの設定をする

ゲストOSが起動していたら shutdown -h now する
VirtualBox のゲストOSの設定 > ネットワーク > 高度 > ポートフォワーディングから、"+"のアイコンをクリックして、
下記画面のように
ポートフォワーディングの設定

名前に ssh(適当で良い)
ホストポート 10022
ゲストポート 22

上記を設定したらゲストOSを起動する

ローカルIP に SSH 接続する

ssh -p 10022 root@127.0.0.1 で ssh 接続できる
rootの箇所は、適宜作成したユーザを指定しても構わない
useradd で作成したユーザは、 passwd ユーザ名 でログインパスワードを設定しておく

5
8
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
8