LoginSignup
2
1

More than 5 years have passed since last update.

lxcコンテナでLinuC202の学習環境を整える(随時更新中)

Last updated at Posted at 2018-11-15

本記事の目的

本記事では、lxcコンテナを使って LinuC202 ver4.5 / LPIC202 ver4.5 の試験勉強をするための環境を整えることを目的としています。
LinuC202の受験対策のための学習ネタは含まれておりません。

想定している環境

  • (ホストマシン)Windows10
  • (仮想化環境)OracleVirtualBox 5.2.22
  • (仮想マシン)Ubuntu16.04
  • (コンテナ環境)LXD3.0.1 / LXC3.0.1
  • (コンテナ)CentOS6.10

本記事で想定している環境については、以前書いた Oracle VM VirtualBox で Linux演習環境のイメージを作成する を参考にしています。

共通する環境設定

コンテナ作成

ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lxc launch images:centos/6 centos6-linuc202-***
centos6-linuc202-*** を作成中
centos6-linuc202-*** を起動中
ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lxc exec centos6-linuc202-*** /bin/bash
[root@centos6-linuc202-*** ~]#

モジュールディレクトリ追加

ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lxc config device add centos6-linuc202-*** modules disk source=/lib/modules/ path=/lib/modules
デバイス modules が centos6-linuc202-*** に追加されました
ubuntu@ubuntu1604:~$

パッケージインストール

ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lxc exec centos6-linuc202-*** /bin/bash
[root@centos6-linuc202-*** ~]#
[root@centos6-linuc202-*** ~]# yum install -y tree lsof sysstat tcpdump man man-pages-ja
[root@centos6-linuc202-*** ~]#

タイムゾーン修正

ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lxc exec centos6-linuc202-*** /bin/bash
[root@centos6-linuc202-*** ~]#
[root@centos6-linuc202-*** ~]# date
Tue Jan 22 22:51:55 EST 2019
[root@centos6-linuc202-*** ~]#
[root@centos6-linuc202-*** ~]# rm -f /etc/localtime && ln -s /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
[root@centos6-linuc202-*** ~]#
[root@centos6-linuc202-*** ~]# date
Wed Jan 23 12:52:29 JST 2019
[root@centos6-linuc202-*** ~]#

主題ごとの環境設定

主題207 ドメインネームサーバー

ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lxc exec centos6-linuc202-207 /bin/bash
[root@centos6-linuc202-207 ~]#
[root@centos6-linuc202-207 ~]# yum install -y bind bind-chroot bind-utils
[root@centos6-linuc202-207 ~]#
[root@centos6-linuc202-207 ~]# rpm -qa | grep bind
bind-libs-9.8.2-0.68.rc1.el6_10.1.x86_64
bind-9.8.2-0.68.rc1.el6_10.1.x86_64
bind-utils-9.8.2-0.68.rc1.el6_10.1.x86_64
bind-chroot-9.8.2-0.68.rc1.el6_10.1.x86_64
[root@centos6-linuc202-207 ~]#
[root@centos6-linuc202-207 ~]# named -v
BIND 9.8.2rc1-RedHat-9.8.2-0.68.rc1.el6_10.1
[root@centos6-linuc202-207 ~]#

主題208 HTTPサービス

208.1 Apacheの基本設定(重要度4)

Samba4
ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lxc exec centos6-linuc202-208 /bin/bash
[root@centos6-linuc202-208 ~]#
[root@centos6-linuc202-208 ~]# yum install -y httpd mod_ssl
[root@centos6-linuc202-208 ~]#
[root@centos6-linuc202-208 ~]# rpm -qa | grep httpd
httpd-tools-2.2.15-69.el6.centos.x86_64
httpd-2.2.15-69.el6.centos.x86_64
[root@centos6-linuc202-208 ~]#
[root@centos6-linuc202-208 ~]# httpd -v
Server version: Apache/2.2.15 (Unix)
Server built:   Jun 19 2018 15:45:13
[root@centos6-linuc202-208 ~]#
[root@centos6-linuc202-208 ~]#
[root@centos6-linuc202-208 ~]# tree -L 2 /etc/httpd/
/etc/httpd/
|-- conf
|   |-- httpd.conf
|   `-- magic
|-- conf.d
|   |-- README
|   `-- welcome.conf
|-- logs -> ../../var/log/httpd
|-- modules -> ../../usr/lib64/httpd/modules
`-- run -> ../../var/run/httpd

5 directories, 4 files
[root@centos6-linuc202-208 ~]#

208.2 HTTPS向けのApacheの設定(重要度3)

ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lxc exec centos6-linuc202-208 /bin/bash
[root@centos6-linuc202-208 ~]#
[root@centos6-linuc202-208 ~]# yum install -y httpd mod_ssl
[root@centos6-linuc202-208 ~]#
[root@centos6-linuc202-208 ~]# rpm -qa | grep mod_ssl
mod_ssl-2.2.15-69.el6.centos.x86_64
[root@centos6-linuc202-208 ~]#
[root@centos6-linuc202-208 ~]#
[root@centos6-linuc202-208 ~]# tree -L 2 /etc/httpd/
/etc/httpd/
|-- conf
|   |-- httpd.conf
|   `-- magic
|-- conf.d
|   |-- README
|   |-- ssl.conf
|   `-- welcome.conf
|-- logs -> ../../var/log/httpd
|-- modules -> ../../usr/lib64/httpd/modules
`-- run -> ../../var/run/httpd

5 directories, 5 files
[root@centos6-linuc202-208 ~]#

208.3 キャッシュプロキシとしてのSquidの設定(重要度2)

ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lxc exec centos6-linuc202-208 /bin/bash
[root@centos6-linuc202-208 ~]#
[root@centos6-linuc202-208 ~]# yum install -y squid
[root@centos6-linuc202-208 ~]#
[root@centos6-linuc202-208 ~]# rpm -qa | grep squid
squid-3.1.23-24.el6.x86_64
[root@centos6-linuc202-208 ~]#
[root@centos6-linuc202-208 ~]# squid -v
Squid Cache: Version 3.1.23
 :
[root@centos6-linuc202-208 ~]#
[root@centos6-linuc202-208 ~]#
[root@centos6-linuc202-208 ~]# tree -L 2 /etc/squid/
/etc/squid/
|-- cachemgr.conf
|-- cachemgr.conf.default
|-- errorpage.css
|-- errorpage.css.default
|-- mime.conf
|-- mime.conf.default
|-- msntauth.conf
|-- msntauth.conf.default
|-- squid.conf
`-- squid.conf.default

0 directories, 10 files
[root@centos6-linuc202-208 ~]#

208.4 WebサーバーおよびリバースプロキシとしてのNginxの実装(重要度2)

ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lxc exec centos6-linuc202-208 /bin/bash
[root@centos6-linuc202-208 ~]#
[root@centos6-linuc202-208 ~]# yum install -y epel-release
[root@centos6-linuc202-208 ~]#
[root@centos6-linuc202-208 ~]# yum install -y nginx
[root@centos6-linuc202-208 ~]#
[root@centos6-linuc202-208 ~]# rpm -qa | grep nginx
nginx-filesystem-1.10.2-1.el6.noarch
nginx-mod-mail-1.10.2-1.el6.x86_64
nginx-mod-stream-1.10.2-1.el6.x86_64
nginx-mod-http-perl-1.10.2-1.el6.x86_64
nginx-mod-http-geoip-1.10.2-1.el6.x86_64
nginx-mod-http-image-filter-1.10.2-1.el6.x86_64
nginx-mod-http-xslt-filter-1.10.2-1.el6.x86_64
nginx-1.10.2-1.el6.x86_64
nginx-all-modules-1.10.2-1.el6.noarch
[root@centos6-linuc202-208 ~]#
[root@centos6-linuc202-208 ~]# nginx -v
nginx version: nginx/1.10.2
[root@centos6-linuc202-208 ~]#
[root@centos6-linuc202-208 ~]# tree -L 2 /etc/nginx/
/etc/nginx/
|-- conf.d
|   |-- default.conf
|   |-- ssl.conf
|   `-- virtual.conf
|-- default.d
|-- fastcgi.conf
|-- fastcgi.conf.default
|-- fastcgi_params
|-- fastcgi_params.default
|-- koi-utf
|-- koi-win
|-- mime.types
|-- mime.types.default
|-- nginx.conf
|-- nginx.conf.default
|-- scgi_params
|-- scgi_params.default
|-- uwsgi_params
|-- uwsgi_params.default
`-- win-utf

2 directories, 18 files
[root@centos6-linuc202-208 ~]#

主題209 ファイル共有

209.1 Sambaサーバーの設定(重要度5)

Samba4
ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lxc exec centos6-linuc202-209 /bin/bash
[root@centos6-linuc202-209 ~]#
[root@centos6-linuc202-209 ~]# yum install -y samba4-* 
[root@centos6-linuc202-209 ~]#
[root@centos6-linuc202-209 ~]# rpm -qa | grep samba4
samba4-common-4.2.10-15.el6.x86_64
samba4-winbind-4.2.10-15.el6.x86_64
samba4-python-4.2.10-15.el6.x86_64
samba4-test-4.2.10-15.el6.x86_64
samba4-dc-libs-4.2.10-15.el6.x86_64
samba4-winbind-clients-4.2.10-15.el6.x86_64
samba4-libs-4.2.10-15.el6.x86_64
samba4-4.2.10-15.el6.x86_64
samba4-devel-4.2.10-15.el6.x86_64
samba4-pidl-4.2.10-15.el6.x86_64
samba4-winbind-krb5-locator-4.2.10-15.el6.x86_64
samba4-dc-4.2.10-15.el6.x86_64
samba4-client-4.2.10-15.el6.x86_64
[root@centos6-linuc202-209 ~]#
[root@centos6-linuc202-209 ~]# ls -l /etc/samba/
total 16
-rw-r--r-- 1 root root    20 Jun 19  2018 lmhosts
-rw-r--r-- 1 root root 11464 Jun 19  2018 smb.conf
[root@centos6-linuc202-209 ~]#

209.2 NFSサーバーの設定(重要度3)

NFSv3
ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lxc exec centos6-linuc202-209 /bin/bash
[root@centos6-linuc202-209 ~]#
[root@centos6-linuc202-209 ~]# yum install -y nfs-utils rpcbind
[root@centos6-linuc202-209 ~]#
NFSv4
ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lxc exec centos6-linuc202-209 /bin/bash
[root@centos6-linuc202-209 ~]#
[root@centos6-linuc202-209 ~]# yum install -y nfs-utils rpcbind nfs4-acl-tools
[root@centos6-linuc202-209 ~]#

/proc/fs/nfsd がないというエラーが出る

[root@centos6-linuc202-209 ~]#
[root@centos6-linuc202-209 ~]# /etc/init.d/rpcbind start
Starting rpcbind:                                          [  OK  ]
[root@centos6-linuc202-209 ~]#
[root@centos6-linuc202-209 ~]# /etc/init.d/nfs start
Starting NFS services:                                     [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Starting NFS daemon: rpc.nfsd: Unable to access /proc/fs/nfsd errno 2 (No such file or directory).
Please try, as root, 'mount -t nfsd nfsd /proc/fs/nfsd' and then restart rpc.nfsd to correct the problem
                                                           [FAILED]
[root@centos6-linuc202-209 ~]#

仮想マシン(ubuntu1604)側でコンテナ設定を追加し、コンテナを再起動する

ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lxc config set centos6-linuc202-209 raw.lxc "lxc.apparmor.profile=unconfined"
ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lxc config set centos6-linuc202-209 security.privileged "true"
ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lxc config show centos6-linuc202-209
architecture: i686
config:
 :
  raw.lxc: lxc.apparmor.profile=unconfined
  security.privileged: "true"
 :
ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lxc restart centos6-linuc202-209
ubuntu@ubuntu1604:~$
[root@centos6-linuc202-209 ~]#
[root@centos6-linuc202-209 ~]# /etc/init.d/rpcbind restart
Stopping rpcbind:                                          [FAILED]
Starting rpcbind:                                          [  OK  ]
[root@centos6-linuc202-209 ~]# /etc/init.d/nfs restart
Shutting down NFS daemon:                                  [FAILED]
Shutting down NFS mountd:                                  [  OK  ]
Shutting down NFS services:                                [  OK  ]
Shutting down RPC idmapd:                                  [  OK  ]
Starting NFS services:                                     [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
Starting RPC idmapd:                                       [  OK  ]
[root@centos6-linuc202-209 ~]#
[root@centos6-linuc202-209 ~]# /etc/init.d/rpcbind status
rpcbind (pid  321) is running...
[root@centos6-linuc202-209 ~]#
[root@centos6-linuc202-209 ~]# /etc/init.d/nfs status
rpc.svcgssd is stopped
rpc.mountd (pid 666) is running...
nfsd dead but subsys locked
[root@centos6-linuc202-209 ~]#
[root@centos6-linuc202-209 ~]# ss -antl | grep 2049
LISTEN     0      64                       :::2049                    :::*
LISTEN     0      64                        *:2049                     *:*
[root@centos6-linuc202-209 ~]#
[root@centos6-linuc202-209 ~]# nfsstat
Error: No Client Stats (/proc/net/rpc/nfs: No such file or directory).
[root@centos6-linuc202-209 ~]#

これでNFSサーバーを立てることができました。
/etc/exports と /etc/sysconfig/nfs を編集してネットワーク越しに mount できるようにします。

[root@centos6-linuc202-209 ~]#
[root@centos6-linuc202-209 ~]# mkdir -p /exports/data
[root@centos6-linuc202-209 ~]#
[root@centos6-linuc202-209 ~]# vi /etc/exports
 :
/exports/data *(rw,no_root_squash)
 :
[root@centos6-linuc202-209 ~]#
[root@centos6-linuc202-209 ~]# exportfs -r
[root@centos6-linuc202-209 ~]#
[root@centos6-linuc202-209 ~]# exportfs -v
/exports/data   <world>(rw,wdelay,no_root_squash,no_subtree_check,sec=sys,rw,no_root_squash,no_all_squash)
[root@centos6-linuc202-209 ~]#

マウントポイントを作成してマウントしてみる

[root@centos6-linuc202-209 ~]#
[root@centos6-linuc202-209 ~]# mkdir -p /mnt/nfs
[root@centos6-linuc202-209 ~]#
[root@centos6-linuc202-209 ~]# mount -t nfs localhost:/exports/data /mnt/nfs
[root@centos6-linuc202-209 ~]#
[root@centos6-linuc202-209 ~]# df -h /mnt/nfs
Filesystem            Size  Used Avail Use% Mounted on
localhost:/exports/data
                       18G  5.2G   12G  31% /mnt/nfs
[root@centos6-linuc202-209 ~]#

nfsstatコマンドが使えないのは引き続き調査します・・

主題210 ネットワーククライアントの管理

210.4 OpenLDAPサーバーの設定

ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lxc exec centos6-linuc202-210 /bin/bash
[root@centos6-linuc202-210 ~]#
[root@centos6-linuc202-210 ~]# yum install -y openldap openldap-servers openldap-clients
[root@centos6-linuc202-210 ~]#

LDAP Admin のようなLDAPクライアントツールがあると動作確認が簡単です。

主題211 電子メールサービス

主題212 システムのセキュリティ

212.1 ルータを構成する(重要度3)

lxcコンテナで iptables コマンドを実行するとエラーが出て実行できない場合がある。

ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lxc exec centos6-linuc202-212 /bin/bash
[root@centos6-linuc202-212 ~]#
[root@centos6-linuc202-212 ~]# iptables -L
iptables v1.4.7: can't initialize iptables table `filter': iptables who? (do you need to insmod?)
Perhaps iptables or your kernel needs to be upgraded.
[root@centos6-linuc202-212 ~]#

これは、仮想マシン(ubuntu1604)側でiptablesを使うために必要なモジュール(ip_tables, iptable_filter, iptable_nat)がロードされていないため。

[root@centos6-linuc202-212 ~]#
[root@centos6-linuc202-212 ~]#
[root@centos6-linuc202-212 ~]#lsmod | grep ip
multipath              16384  0 
[root@centos6-linuc202-212 ~]#
[root@centos6-linuc202-212 ~]# exit
ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lsmod | grep ip
multipath              16384  0
ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$

そこで、起動時に必要なモジュール(ip_tables, iptable_filter, iptable_nat)が読み込まれるようにするために /etc/modules-load.d/modules.conf を設定する。

ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ sudo su - 
[sudo] ubuntu のパスワード: *******
root@ubuntu1604:~#
root@ubuntu1604:~# vi /etc/modules-load.d/modules.conf
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.

ip_tables ← ★追加
iptable_filter ← ★追加
iptable_nat ← ★追加

root@ubuntu1604:~#
root@ubuntu1604:~# reboot
root@ubuntu1604:~#

再起動が完了したら、iptables関連モジュールがロードされていることを確認

ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lsmod | grep ip
iptable_nat            16384  0
nf_conntrack_ipv4      20480  1
nf_defrag_ipv4         16384  1 nf_conntrack_ipv4
nf_nat_ipv4            16384  1 iptable_nat
nf_nat                 28672  1 nf_nat_ipv4
nf_conntrack          106496  3 nf_nat,nf_nat_ipv4,nf_conntrack_ipv4
iptable_filter         16384  0
ip_tables              24576  2 iptable_filter,iptable_nat
x_tables               36864  3 ip_tables,iptable_filter,ebtables
multipath              16384  0
ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lxc exec centos6-linuc202-212 /bin/bash
[root@centos6-linuc202-212 ~]#
[root@centos6-linuc202-212 ~]# lsmod | grep ip
iptable_nat            16384  0
nf_conntrack_ipv4      20480  1
nf_defrag_ipv4         16384  1 nf_conntrack_ipv4
nf_nat_ipv4            16384  1 iptable_nat
nf_nat                 28672  1 nf_nat_ipv4
nf_conntrack          106496  3 nf_conntrack_ipv4,nf_nat_ipv4,nf_nat
iptable_filter         16384  0
ip_tables              24576  2 iptable_nat,iptable_filter
x_tables               36864  3 ebtables,iptable_filter,ip_tables
multipath              16384  0
[root@centos6-linuc202-212 ~]#

モジュールがロードされていればlxcコンテナでも iptables コマンドが使えるようになる

ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lxc exec centos6-linuc202-212 /bin/bash
[root@centos6-linuc202-212 ~]#
[root@centos6-linuc202-212 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
[root@centos6-linuc202-212 ~]#
[root@centos6-linuc202-212 ~]#

212.2 FTPサーバの保護(重要度2)

CentOS6の標準リポジトリには vsftpd が含まれている。
ProFTPD や Pure-FTPD は epelリポジトリに含まれている。

ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ lxc exec centos6-linuc202-212 /bin/bash
[root@centos6-linuc202-212 ~]#
[root@centos6-linuc202-212 ~]# yum install epel-release
[root@centos6-linuc202-212 ~]#
[root@centos6-linuc202-212 ~]# yum install pure-ftpd proftpd vsftpd
[root@centos6-linuc202-212 ~]#
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