VPCのセキュリティグループ

– セキュリティグループは、インスタンスの仮想ファイアウォールとして機能し、インバウンドトラフィックとアウトバウンドトラフィックをコントロール
– VPC 内でインスタンスを起動した場合、そのインスタンスには最大 5 つのセキュリティグループを割り当てることができる
– 起動時に特定のグループを指定しないと、インスタンスは VPC のデフォルトのセキュリティグループに自動的に割り当てられる

AWS「ネットワーク&セキュリティ」の「セキュリティグループ」を見ます。

セキュリティグループとは
インバウンド
送信元 プロトコル ポート範囲 コメント

アウトバウンド
送信先 プロトコル ポート範囲 コメント

SSHをAnywhere(0.0.0.0/0)に対して開放し、万一キーペアが流出してしまった場合、クラッキングを受けた場合、SSHに脆弱性が発見された場合など、
様々なリスクがある。

なるほど、セキュリティグループ(VPC)でインバウンド、アウトバウンドを制御しているのね。

vagrant boxの確認

作成したboxをvagrantに登録する

vagrant box add --name centOS7 centOS7.box

あれ、なんかうまくいかねーと思ったら、.boxの拡張子は不要のようです。
こちらでやり直します。

vagrant box add --name centOS7 centOS7

>vagrant box add –name centos7 centOS7
==> box: Box file was not detected as metadata. Adding it directly…
==> box: Adding box ‘centos7’ (v0) for provider:
box: Unpacking necessary files from: file://C:/Users/hoge/MyVagrant/centos7/centOS7
box: Progress: 100% (Rate: 9.9M/s, Estimated time remaining: –:–:–)
==> box: Successfully added box ‘centos7’ (v0) for ‘virtualbox’!

ほおおおおおおおおおお

>vagrant box list
bento/centos-6.7 (virtualbox, 2.2.7)
bento/centos-6.8 (virtualbox, 2.3.0)
centos7 (virtualbox, 0)
centos72 (virtualbox, 0)
trusty64 (virtualbox, 0)
ubuntu/trusty64 (virtualbox, 20161207.0.0)

>vagrant init centos7
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
ぎゃあああああああああああああああああ

> vagrant up

おお、なるほど、virtual boxで一から作成することもできるし、

udevルールの削除

udev ルールとは?
>OSがRed Hat Enterprise Linux 6の場合,サーバ対応の環境設定のscsi_deviceオペランドまたはdmmp_deviceオペランドに指定した共有ディスクのデバイスファイルのオーナー,グループ,およびパーミッションを設定するときは,udevルールファイルで設定

共有ディスクのパーミッションか。

ln -fs /dev/null /etc/udev/rules.d/70-persistent-net.rules

ネットワーク個別設定の削除
[root@localhost git-2.19.1]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
ifcfg-enp0s3 ifdown-ippp ifdown-routes ifup ifup-ipv6 ifup-ppp ifup-tunnel
ifcfg-lo ifdown-ipv6 ifdown-sit ifup-aliases ifup-isdn ifup-routes ifup-wireless
ifdown ifdown-isdn ifdown-Team ifup-bnep ifup-plip ifup-sit init.ipv6-global
ifdown-bnep ifdown-post ifdown-TeamPort ifup-eth ifup-plusb ifup-Team network-functions
ifdown-eth ifdown-ppp ifdown-tunnel ifup-ippp ifup-post ifup-TeamPort network-functions-ipv6

でたーーーーーーーーー ネットワーク。DMMで完膚なきまでやられたからなー

ifcfg-enp0s3をみると、UUIDはあるが、HWADDRはないぞ。

HWADDR=****
UUID=****

box作成の最適化

yum clean all

次にフラグメーションを解消する

dd if=/dev/zero of=/EMPTY bs=1M

[root@localhost network-scripts]# dd if=/dev/zero of=/EMPTY bs=1M
dd: error writing ‘/EMPTY’: No space left on device
4442+0 records in
4441+0 records out
4657184768 bytes (4.7 GB) copied, 15.4136 s, 302 MB/s

いよいよ
boxの作成
boxを作成する。コマンドは以下の通り。これで本当に出来るんか?半信半疑

vagrant package --base CentOS7 --output centOS7

ん、なんかできてるっぽい?
>vagrant package –base CentOS7 –output centOS7
==> CentOS7: Clearing any previously set forwarded ports…
==> CentOS7: Exporting VM…
==> CentOS7: Compressing package to: C:/Users/narishige/MyVagrant/centos7/centOS7

これマジ?
2018/10/27 15:04 882,256,211 centOS7

gitのインストール

[root@localhost ~]# git –version
git version 1.8.3.1

gitを入れます。

yum remove git

# gitに必要なライブラリ
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-ExtUtils-MakeMaker

gitの最新版は?
https://mirrors.edge.kernel.org/pub/software/scm/git/
2018/10/5 2.19.1 これか!!
git-2.19.1.tar.gz 05-Oct-2018 16:56 7M

ミラーサイトを参考にwgetしてみる。 おお、俺、だんだん高度なことやってる? (やってる気分)

wget https://www.kernel.org/pub/software/scm/git/git-2.19.1.tar.gz 
ls

tar zxvf git-2.19.1.tar.gz
cd git-2.19.1

#ここからインストール
./configure --prefix=/usr/local
echo $?
make
echo $?
make install
git --version
git version 2.16.5

あれ、2.19入れたと思ったけど、2.16.5だ。まあいいか。

VirtualBox_Guest_Additionsのインストール

Guest Additions CD イメージの挿入は設定済み
デバイスから操作すると、

[root@localhost ~]# mkdir /media/cdrom
[root@localhost ~]# mount -r /dev/cdrom /media/cdrom
[root@localhost ~]# ls-l /media/cdrom
-bash: ls-l: command not found
[root@localhost ~]# ls -l /media/cdrom
total 51462
dr-xr-xr-x 2 root root     2048 Jul 27  2017 32Bit
dr-xr-xr-x 2 root root     2048 Jul 27  2017 64Bit
-r-xr-xr-x 1 root root      647 Aug 17  2016 AUTORUN.INF
-r-xr-xr-x 1 root root     6381 Jul 27  2017 autorun.sh
dr-xr-xr-x 2 root root     2048 Jul 27  2017 cert
dr-xr-xr-x 2 root root     4096 Jul 27  2017 OS2
-r-xr-xr-x 1 root root     4824 Jul 27  2017 runasroot.sh
-r-xr-xr-x 1 root root  8160718 Jul 27  2017 VBoxLinuxAdditions.run
-r-xr-xr-x 1 root root 17789440 Jul 27  2017 VBoxSolarisAdditions.pkg
-r-xr-xr-x 1 root root 16407888 Jul 27  2017 VBoxWindowsAdditions-amd64.exe
-r-xr-xr-x 1 root root   268496 Jul 27  2017 VBoxWindowsAdditions.exe
-r-xr-xr-x 1 root root 10046608 Jul 27  2017 VBoxWindowsAdditions-x86.exe

あれ、なんだこれ。
lsコマンドの-lはリストを縦に表示

/media/は
>一時的なマウント用のディレクトリ。マウントとは接続された装置をOSに認識させて使うといった意味になります。このディレクトリは一時的なマウント用なので、例えば「USBメモリを接続した」とかした場合にここにファイルのような扱いでそのデバイスが置かれます。/mediaとの違いは「一時的な」という部分になりますね。

/dev
>主に記憶デバイスとかを入れておくディレクトリ。UNIXでは「デバイスをファイルのような扱いにして操作する」って設計になっているのでデバイスを追加とかするとここにファイルのような扱いでデバイスが置かれます。

mountコマンド
「mount」は、HDDやUSBメモリ、DVD-ROMなどのフォーマット済みの領域(ファイルシステム)を指定したディレクトリ(マウントポイント)と一時的に結び付けてアクセスできるようにするコマンド

うお、マニアックになってきた。
mount -r は読み取り専用

shコマンド シェルの起動

[root@localhost ~]# sh /media/cdrom/VBoxLinuxAdditions.run
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.1.26 Guest Additions for Linux...........
VirtualBox Guest Additions installer
Copying additional installer modules ...
Installing additional modules ...
vboxadd.sh: Starting the VirtualBox Guest Additions.
Failed to set up service vboxadd, please check the log file
/var/log/VBoxGuestAdditions.log for details.

[root@localhost ~]# cat /var/log/VBoxGuestAdditions.log

vboxadd.sh: failed: Look at /var/log/vboxadd-install.log to find out what went wrong.
chcon: can’t apply partial context to unlabeled file ‘/usr/lib64/VBoxGuestAdditions/mount.vboxsf’
vboxadd.sh: failed: Look at /var/log/vboxadd-install.log to find out what went wrong.
vboxadd.sh: failed: modprobe vboxguest failed.

[root@localhost ~]# umount /media/cdrom
[root@localhost ~]# ls -l /media/cdrom
total 0

yumリポジトリの登録

yum install epel-release
curl -s https://setup.ius.io/ | sh
rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi
yum install http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

yum update

開発環境用パッケージインストール

yum groupinstall "Development tools"
yum install man man-pages man-pages-overrides man-pages-ja

centos7のiptables無効化(firewalld)

[root@localhost ~]# service iptables stop
Redirecting to /bin/systemctl stop iptables.service
Failed to stop iptables.service: Unit iptables.service not loaded.
[root@localhost ~]# service iptables status
Redirecting to /bin/systemctl status iptables.service
Unit iptables.service could not be found.

なに?
あ、centos7では「iptables」から「firewalld」に変更されている。

[root@localhost ~]# systemctl is-enabled firewalld
enabled

enabledになってます。
firewalldを無効化にします。

systemctl stop firewalld

[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Sat 2018-10-27 12:18:33 JST; 9s ago
     Docs: man:firewalld(1)
  Process: 623 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 623 (code=exited, status=0/SUCCESS)

Oct 27 12:01:06 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
Oct 27 12:01:09 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
Oct 27 12:18:33 localhost.localdomain systemd[1]: Stopping firewalld - dynamic firewall daemon...
Oct 27 12:18:33 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon.

activeがinactive(dead)で無効化されているのでOK
続いて自動起動

[root@localhost ~]# systemctl is-enabled firewalld
enabled

firewalldの自動起動を無効化

[root@localhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# systemctl is-enabled firewalld
disabled

しゃああああああああああああああああああ

iptablesの無効化

iptablesとは一般的なLinuxに搭載されているパケットフィルタ
パケットとは、ネットワーク上を流れるデータの事。パケットフィルタとは、このパケットを選別して、通すものと通さないものを分けるということを意味
iptablesは商用製品に匹敵する、とても高機能かつ高性能なパケットフィルタ
サーバ自身を守ることはもちろん、パケット転送などにも対応しているため、ネットワークアダプタを二つ搭載したマシンの上で動作させパケット転送制御を行うことで、ネットワーク型ファイアウォールとしても使用可能

現在設定されているポリシー
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all — anywhere anywhere ctstate RELATED,EST ABLISHED
ACCEPT all — anywhere anywhere
INPUT_direct all — anywhere anywhere
INPUT_ZONES_SOURCE all — anywhere anywhere
INPUT_ZONES all — anywhere anywhere
DROP all — anywhere anywhere ctstate INVALID
REJECT all — anywhere anywhere reject-with icmp-ho st-prohibited

Chain FORWARD (policy ACCEPT)
target prot opt source destination
ACCEPT all — anywhere anywhere ctstate RELATED,EST ABLISHED
ACCEPT all — anywhere anywhere
FORWARD_direct all — anywhere anywhere
FORWARD_IN_ZONES_SOURCE all — anywhere anywhere
FORWARD_IN_ZONES all — anywhere anywhere
FORWARD_OUT_ZONES_SOURCE all — anywhere anywhere
FORWARD_OUT_ZONES all — anywhere anywhere
DROP all — anywhere anywhere ctstate INVALID
REJECT all — anywhere anywhere reject-with icmp-ho st-prohibited

Chain OUTPUT (policy ACCEPT)
target prot opt source destination
OUTPUT_direct all — anywhere anywhere

Chain FORWARD_IN_ZONES (1 references)
target prot opt source destination
FWDI_public all — anywhere anywhere [goto]
FWDI_public all — anywhere anywhere [goto]

Chain FORWARD_IN_ZONES_SOURCE (1 references)
target prot opt source destination

Chain FORWARD_OUT_ZONES (1 references)
target prot opt source destination
FWDO_public all — anywhere anywhere [goto]
FWDO_public all — anywhere anywhere [goto]

Chain FORWARD_OUT_ZONES_SOURCE (1 references)
target prot opt source destination

Chain FORWARD_direct (1 references)
target prot opt source destination

Chain FWDI_public (2 references)
target prot opt source destination
FWDI_public_log all — anywhere anywhere
FWDI_public_deny all — anywhere anywhere
FWDI_public_allow all — anywhere anywhere
ACCEPT icmp — anywhere anywhere

Chain FWDI_public_allow (1 references)
target prot opt source destination

Chain FWDI_public_deny (1 references)
target prot opt source destination

Chain FWDI_public_log (1 references)
target prot opt source destination

Chain FWDO_public (2 references)
target prot opt source destination
FWDO_public_log all — anywhere anywhere
FWDO_public_deny all — anywhere anywhere
FWDO_public_allow all — anywhere anywhere

Chain FWDO_public_allow (1 references)
target prot opt source destination

Chain FWDO_public_deny (1 references)
target prot opt source destination

Chain FWDO_public_log (1 references)
target prot opt source destination

Chain INPUT_ZONES (1 references)
target prot opt source destination
IN_public all — anywhere anywhere [goto]
IN_public all — anywhere anywhere [goto]

Chain INPUT_ZONES_SOURCE (1 references)
target prot opt source destination

Chain INPUT_direct (1 references)
target prot opt source destination

Chain IN_public (2 references)
target prot opt source destination
IN_public_log all — anywhere anywhere
IN_public_deny all — anywhere anywhere
IN_public_allow all — anywhere anywhere
ACCEPT icmp — anywhere anywhere

Chain IN_public_allow (1 references)
target prot opt source destination
ACCEPT tcp — anywhere anywhere tcp dpt:ssh ctstate NEW

Chain IN_public_deny (1 references)
target prot opt source destination

Chain IN_public_log (1 references)
target prot opt source destination

Chain OUTPUT_direct (1 references)
target prot opt source destination

chain forward, chain inputなどがある

Chain INPUT (policy DROP)
Chain FORWARD (policy DROP)
Chain OUTPUT (policy ACCEPT)

ルーティングして、input, forward, outputしてアプリケーションに接続している。
あああああああああ、これやべーやつだ。

selinuxの無効化

selinuxとは
セキュリティ関連のLinuxカーネル制御機能。Linuxディストリビューションではなく、強制アクセス制御機能を加えるモジュール。
Security-Enhanced-Linuxの略称
SELinuxを利用すると細かいアクセス制御が可能になる。例えば、サービス毎に最小限の権限を与え管理することなどが可能になる
root権限ですら制限をかけることも可能

なるほど、awsのAMIみたいなもんか。

[vagrant@localhost ~]$ getenforce
Enforcing

詳細を見てみる
[root@localhost ~]# sestatus
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: enforcing
Policy MLS status: enabled
Policy deny_unknown status: allowed
Max kernel policy version: 31

statusがenabledになってますね。これを変更していきます。

vi /etc/sysconfig/selinux

SELINUX=disabled

[root@localhost ~]# getenforce
Disabled

centos7.5で所々設定をしていきましょ~

vagrantユーザー設定

[root@localhost ~]# useradd -g wheel vagrant
[root@localhost ~]# id vagrant
uid=1000(vagrant) gid=10(wheel) groups=10(wheel)
[root@localhost ~]# passwd vagrant
Changing password for user vagrant.

ssh設定

[root@localhost vagrant]# cd /.ssh
[root@localhost .ssh]# curl -k -L -o authorized_keys 'https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
100   409  100   409    0     0    229      0  0:00:01  0:00:01 --:--:--   229
[root@localhost .ssh]# chmod 600 authorized_keys
[root@localhost .ssh]# chown -R vagrant:wheel /home/vagrant/.ssh
chown: cannot access ‘/home/vagrant/.ssh’: No such file or directory

うんうん!
続いてsshd設定:UseDNS
これがyesになっていると、サーバがリモートホストのIPアドレスを名前解決しにかかってしまうようで、名前解決の方法がない場合はタイムアウトするまで時間遅延が発生しちゃう

sshd
あれ、useDNSの項目ないけど。。

あ、下の方にコメントアウトでありました♪

vi /etc/ssh/sshd_config

UseDNS no

service sshd restart
Redirecting to /bin/systemctl restart sshd.service

sudoの設定

visudo

# コメントアウト
#defaults !visiblepw

## Allows people in group wheel to run all commands
%wheel  ALL=(ALL)       NOPASSWD: ALL

vagrantユーザになってsudo実行
[root@localhost .ssh]# su – vagrant
[vagrant@localhost ~]$ sudo ls /root
anaconda-ks.cfg
[vagrant@localhost ~]$

OK
長くなったので、一旦休憩