よくわからないエンジニア

よく分からないエンジニア(無音鈴鹿)の日々の記録

よくわからないエンジニア

AWS Ubuntuの初期設定(16.04)

唐突ですが、Ubuntuを使ってみたくなったので、初期設定の備忘録。
使用したのはAWSのUbuntu Server 16.04 LTS (HVM), SSD Volume Typeを利用してます。

目次

[:contents] 尚、AWS自体の設定は今のところ書いてません。インターフェースとかガンガン変わるだろうし、備忘録にもならなそう。
公式ドキュメントや、Black Beltのスライドを見た方が学習効率良さそうです。

初期設定・確認

SSH接続

AWSの場合、VPC周りを設定して上げれば、構築完了した時点でパブリックIPとSSHキーが手元にあると思うので、まずは「Ubuntu」ユーザーでログインします。
基本sudo操作ですが、初期設定は面倒くさいので、sudo su -でrootになっちゃいました。
まずはsshのポート番号変えます。VPC側でアクセス元制限しているのでそこまでしなくても良いかと思いますが念の為。

# vim /etc/ssh/sshd_config
Port 54322

適当なポートに変更しました。一緒にPermitRootLoginをnoにしようとしたのですが…

# grep PermitRootLogin /etc/ssh/sshd_config
PermitRootLogin prohibit-password

なんか見知らぬprohibit-passwordという設定が入っています。
調べてみると、"すべてのインタラクティブな認証法を禁止する"という事。sshのバージョンによってこのパラメーターが利用出来るようです。

# ssh -V
OpenSSH_7.2p2 Ubuntu-4ubuntu2.2, OpenSSL 1.0.2g  1 Mar 2016

基本、sshでのrootログインはなんだろうが拒否したいので、noに変更します。

# vim /etc/ssh/sshd_config
PermitRootLogin no

ここまできたらsshdをリスタートします。

# service sshd restart

Ubuntuバージョン確認

とりあえずバージョンなりも確認。

# cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.3 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.3 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.3 LTS
Release:        16.04
Codename:       xenial

ホスト名確認/変更

ホスト名がIPになっているので変更します。

# hostnamectl set-hostname test-ubuntu01
# hostnamectl
   Static hostname: test-ubuntu01
         Icon name: computer-vm
           Chassis: vm
        Machine ID: *
           Boot ID: *
    Virtualization: xen
  Operating System: Ubuntu 16.04.3 LTS
            Kernel: Linux 4.4.0-1047-aws
      Architecture: x86-64

後、sudoを使用すると「sudo: unable to resolve host test-ubuntu01」と出続けるので、これも出ないようにhostsを編集します。

# vim /etc/hosts
# echo 127.0.0.1 $(hostname) >> /etc/hosts

ユーザー追加/sudo権限付与

作業用ユーザーを作ります。いつものようにengineerで。

# adduser engineer
adduser: Only root may add a user or group to the system.
ubuntu@test-ubuntu01:~$ sudo su -
root@test-ubuntu01:~# adduser engineer
Adding user `engineer' ...
Adding new group `engineer' (1002) ...
Adding new user `engineer' (1002) with group `engineer' ...
Creating home directory `/home/engineer' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for engineer
Enter the new value, or press ENTER for the default
        Full Name []:
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n] y
# gpasswd -a engineer sudo
Adding user engineer to group sudo
# grep sudo /etc/group
sudo:x:27:ubuntu,engineer

作成したユーザーがsudo 使えればオッケーです。

ネットワーク設定

現状のIPデフォルトGW,DNSの確認。

# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 0e:7b:a3:97:52:84 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.70/24 brd 192.168.0.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::c7b:a3ff:fe97:5284/64 scope link
       valid_lft forever preferred_lft forever
# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 eth0
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
# 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.0.2
search ap-northeast-1.compute.internal

今回は特に固定IPにする必要も無いので、このままにしておきます。

パッケージの更新

Ubuntuはapt使います。とりあえずパッケージリストを更新します。

# apt-get update
Hit:1 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu xenial InRelease
Get:2 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
Get:3 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
Get:4 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Fetched 306 kB in 1s (217 kB/s)
Reading package lists... Done

今入っているパッケージだけを最新にしたければ「apt-get upgrade」を、新規のパッケージインストール/不要なパッケージ削除/ディストリビューションの更新も実施したければ「apt-get dist-upgrade」を実行します。今回は、「apt-get dist-upgrade」を実行しました。

# apt-get dist-upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following NEW packages will be installed:
  linux-aws-headers-4.4.0-1050 linux-headers-4.4.0-1050-aws linux-image-4.4.0-1050-aws
The following packages will be upgraded:
  bind9-host cloud-initramfs-copymods cloud-initramfs-dyn-netconf curl distro-info-data dnsutils gcc-5-base grub-common grub-pc grub-pc-bin grub2-common iproute2 libbind9-140 libc-bin libc6 libcurl3-gnutls
  libdns-export162 libdns162 libisc-export160 libisc160 libisccc140 libisccfg140 liblwres141 libpam-systemd libstdc++6 libsystemd0 libtasn1-6 libudev1 linux-aws linux-headers-aws linux-image-aws locales multiarch-support
  openssh-client openssh-server openssh-sftp-server overlayroot rsync systemd systemd-sysv udev update-notifier-common
42 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 33.7 MB/49.2 MB of archives.
After this operation, 133 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
中略

途中でgrubの設定ファイルについて聞かれました。
f:id:unknownengineer:20180207135245j:plain
置き換えません。デフォルトの2を選択してEnter押しました。

localeの設定

初期状態だと、LANGで指定出来る日本語がありません。

# locale -a
C
C.UTF-8
en_US.utf8
POSIX

日本語パックインストールします。

# apt-get install language-pack-ja
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  language-pack-ja-base
The following NEW packages will be installed:
  language-pack-ja language-pack-ja-base
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 3,163 kB of archives.
After this operation, 14.8 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu xenial-updates/main amd64 language-pack-ja-base all 1:16.04+20160627 [2,016 kB]
Get:2 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu xenial-updates/main amd64 language-pack-ja all 1:16.04+20171210 [1,147 kB]
Fetched 3,163 kB in 0s (10.5 MB/s)
Selecting previously unselected package language-pack-ja-base.
(Reading database ... 76215 files and directories currently installed.)
Preparing to unpack .../language-pack-ja-base_1%3a16.04+20160627_all.deb ...
Unpacking language-pack-ja-base (1:16.04+20160627) ...
Selecting previously unselected package language-pack-ja.
Preparing to unpack .../language-pack-ja_1%3a16.04+20171210_all.deb ...
Unpacking language-pack-ja (1:16.04+20171210) ...
Replacing files in old package language-pack-ja-base (1:16.04+20160627) ...
Setting up language-pack-ja (1:16.04+20171210) ...
Setting up language-pack-ja-base (1:16.04+20160627) ...
Generating locales (this might take a while)...
  ja_JP.UTF-8... done
Generation complete.
# locale -a
C
C.UTF-8
en_US.utf8
ja_JP.utf8
POSIX

インストールしたらlocaleを変更します。

# update-locale LANG=ja_JP.utf8

timezoneの変更

最後にtimezoneを変更します。

# date
Wed Feb  7 04:59:37 UTC 2018
# cp /etc/localtime /etc/localtime.org
# ln -sf  /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
# date
Wed Feb  7 14:01:02 JST 2018

再起動

再起動して設定を確認します。

#shutdown -r now

とりあえずこんな感じで初期設定終了とします。