macbookからSSH接続し作業できるようにします。
環境
タイトルの通りです。
$ /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.3 LTS"
今回の作業
- SSHサーバーをインストール
- IPアドレスを固定化
- 鍵認証の設定
SSHサーバーをインストール
下記順序で実行します。
$ sudo apt update
$ sudo apt-get install openssh-server
$ ssh -V
OpenSSH_7.2p2 Ubuntu-4ubuntu2.2, OpenSSL 1.0.2g 1 Mar 2016
IPアドレスを固定化
まず必要な情報を調べます。IPアドレスはDHCPで振られているIPアドレスに設定します。
※必要な情報=IPアドレス、ネットマスク、ゲートウェイ、DNSネームサーバ
$ ifconfig
enp0s31f6 Link encap:Ethernet HWaddr 60:45:cb:a8:1d:fa
inet addr:192.168.2.210 Bcast:192.168.3.255 Mask:255.255.252.0
inet6 addr: fe80::6245:cbff:fea8:1dfa/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:6128976 errors:0 dropped:5541 overruns:0 frame:0
TX packets:2343747 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:7706871980 (7.7 GB) TX bytes:179475234 (179.4 MB)
Interrupt:16 Memory:f7300000-f7320000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:443 errors:0 dropped:0 overruns:0 frame:0
TX packets:443 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:85833 (85.8 KB) TX bytes:85833 (85.8 KB)
$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 enp0s31f6
192.168.0.0 0.0.0.0 255.255.252.0 U 0 0 0 enp0s31f6
$ cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 192.168.1.1
上記で調べた値をメモります。
項目 | 設定値 |
---|---|
IPアドレス | 192.168.2.210 |
ネットマスク | 255.255.252.0 |
ゲートウェイ | 192.168.1.1 |
DNSネームサーバ | 192.168.1.1 |
設定ファイルを編集します。
$ sudo vim /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto enp0s31f6
# ↓削除
#iface enp0s31f6 inet dhcp
# ↓追加
iface enp0s31f6 inet static
address 192.168.2.219
netmask 255.255.252.0
gateway 192.168.1.1
dns-nameservers 192.168.1.1
再起動して反映します。
$ sudo ip addr flush dev enp0s31f6
$ sudo service networking restart
鍵認証の設定
まずmacbookで認証用キーを作成します。
$ cd ~/.ssh
$ ssh-keygen -f id_rsa_hoge[任意のファイル名でおk]
指定したファイル名で秘密鍵と公開鍵が作成されます。
秘密鍵:id_rsa_hoge
公開鍵:id_rsa_hoge.pub
公開鍵の値をメモります。
$ cat id_rsa_hoge.pub
ssh-rsa AAAAB3NzaC1yc2・・・・・
サーバー(Ubuntu)側の設定をします。
$ useradd hoge
$ cd /home/hoge
$ mkdir .ssh
$ cd .ssh/
$ vim authorized_keys
→公開鍵の値を貼り付け
$ chmod 600 authorized_keys
$ chmod 700 .ssh/
macbookのSSH設定を行います。
Host ubuntu
HostName 192.168.2.210
Port 22
User hoge
IdentityFile ~/.ssh/id_rsa_hoge
以上で設定完了です。最後に繋がるか確認。
$ ssh ubuntu
お疲れ様でした。