Quantcast
Channel: Linux –俺的備忘録 〜なんかいろいろ〜
Viewing all 743 articles
Browse latest View live

Linuxコンソールでソートせずにユニークな値を求める

$
0
0

Linuxコンソール上で、ソートせずにユニークな値を取得したい時がある。
例えば、以下のようなファイルからユニークな値を取得する際、よく使われる方法としてはsort+uniqコマンドの組み合わせなのだが、それだと順番がずれてしまう。
あくまでもその文字列が最初に登場した順番でユニークな値を取得したいとする。

[root@BS-PUB-CENT7-01 ~]# cat /tmp/test.txt
test:0
test:2
test:6
test:9
test:4
test:9
test:9
test:9
test:4
test:0
test:2
test:8
test:0
test:7
test:0
[root@BS-PUB-CENT7-01 ~]# cat /tmp/test.txt | sort | uniq
test:0
test:2
test:4
test:6
test:7
test:8
test:9

そんな時は、以下の方法がある。

 

1.awkで取得する

前にこちらでも触れたことがあるが、awkで以下のようにコマンドを実行することで、同じ文字列が2回目の場合は出力させないようにできる。

コマンド | awk '!a[$1]++'
[root@BS-PUB-CENT7-01 ~]# cat /tmp/test.txt | awk '!a[$1]++'
test:0
test:2
test:6
test:9
test:4
test:8
test:7

2.Perlで取得する

awkと同じように、Perlでも同様の処理が行える。

コマンド | perl -ne 'print unless $seen{$_}++'
[root@BS-PUB-CENT7-01 ~]# cat /tmp/test.txt | perl -ne 'print unless $seen{$_}++'
test:0
test:2
test:6
test:9
test:4
test:8
test:7

 

3.johnパッケージのuniqueコマンドを利用する

変わり種としては、脆弱性スキャンを行うツールであるjohnパッケージ(John the Ripper)の中にあるuniqueコマンドが同じような動作をする(ただし、一度違うファイルに書き出す必要があるようだ)。

コマンド | unique 出力先PATH
blacknon@BS-PUB-UBUNTU-01:~$ cat /tmp/test.txt | unique /tmp/test.txt.uniq
blacknon@BS-PUB-UBUNTU-01:~$ cat /tmp/test.txt.uniq
test:0
test:2
test:6
test:9
test:4
test:8
test:7

 

新しいLinuxの教科書 新しいLinuxの教科書

yumコマンドで覚えておきたい使い方19個

$
0
0

CentOSなど、RHEL系のディストリビューションを使っているなら必ず一度は使ったことがあるであろうyumコマンド。
みんな使うけど、installとかupdateばかりで他のオプションはそんなに使われてない場合が多いような気もするが、使いこなせるとかなり便利な機能がいっぱいある。
…といっても、機能がありすぎるので、役に立ちそうなものだけを抜粋している。

1.パッケージをインストールする

yumを使ったことがあるなら、必ず一度は使っている使い方。
指定したパッケージを、yumリポジトリ(yumでインストールされるrpmパッケージのデータベースやデータそのものを持つ、貯蔵庫のようなもの)からインストールする使い方だ。
使い方も説明不要かと思うが、サブコマンド「install」にインストールするパッケージ名を引数として与えればよいだけだ。

yum install パッケージ名
[root@BS-PUB-CENT7-01 ~]# yum install wget
読み込んだプラグイン:fastestmirror
base                                                                    | 3.6 kB  00:00:00
extras                                                                  | 3.4 kB  00:00:00
updates                                                                 | 3.4 kB  00:00:00
(1/2): extras/7/x86_64/primary_db                                       | 160 kB  00:00:00
(2/2): updates/7/x86_64/primary_db                                      | 7.1 MB  00:00:00
Loading mirror speeds from cached hostfile
 * base: mirror.nus.edu.sg
 * extras: mirror.nus.edu.sg
 * updates: mirror.fairway.ne.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ wget.x86_64 0:1.14-10.el7_0.1 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

===============================================================================================
 Package           アーキテクチャー    バージョン                      リポジトリー       容量
===============================================================================================
インストール中:
 wget              x86_64              1.14-10.el7_0.1                 base              545 k

トランザクションの要約
===============================================================================================
インストール  1 パッケージ

総ダウンロード容量: 545 k
インストール容量: 2.0 M
Is this ok [y/d/N]: y
Downloading packages:
wget-1.14-10.el7_0.1.x86_64.rpm                                         | 545 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  インストール中          : wget-1.14-10.el7_0.1.x86_64                                    1/1
  検証中                  : wget-1.14-10.el7_0.1.x86_64                                    1/1

インストール:
  wget.x86_64 0:1.14-10.el7_0.1

完了しました!

 

インストール時に聞かれる「y/n」については、「-y」オプションを付与することで自動的に処理がされる。
これについては他のサブコマンド処理でも同じだ。

 

2.パッケージをアップデートする

これも一度は使ったことがあるだろう。
すでにインストールされているパッケージのバージョンを上げる処理。引数に何も指定しないとアップデート可能なすべてのパッケージがアップデートされてしまうが、引数に対象のパッケージ名を与えることでそのパッケージのみをアップデートさせることができるようになる。

yum update
yum update パッケージ名 # パッケージを指定する場合

 

特定のバージョンにアップデートさせる場合は、パッケージ名-バージョンで指定してやるといいだろう。
指定できるバージョンについては、こちらにあるように「yum –showduplicates list パッケージ名」で確認してやればよい。

yum update パッケージ名-バージョン
[root@BS-PUB-CENT7-01 ~]# yum install httpd-2.4.6-40.el7.centos
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.nus.edu.sg
 * extras: mirror.nus.edu.sg
 * updates: mirror.fairway.ne.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ httpd.x86_64 0:2.4.6-40.el7.centos を インストール
--> 依存性の処理をしています: httpd-tools = 2.4.6-40.el7.centos のパッケージ: httpd-2.4.6-40.el7.centos.x86_64
--> 依存性の処理をしています: /etc/mime.types のパッケージ: httpd-2.4.6-40.el7.centos.x86_64
--> 依存性の処理をしています: libaprutil-1.so.0()(64bit) のパッケージ: httpd-2.4.6-40.el7.centos.x86_64
--> 依存性の処理をしています: libapr-1.so.0()(64bit) のパッケージ: httpd-2.4.6-40.el7.centos.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ apr.x86_64 0:1.4.8-3.el7 を インストール
---> パッケージ apr-util.x86_64 0:1.5.2-6.el7 を インストール
---> パッケージ httpd-tools.x86_64 0:2.4.6-40.el7.centos を インストール
---> パッケージ mailcap.noarch 0:2.1.41-2.el7 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

===============================================================================================
 Package                アーキテクチャー  バージョン                     リポジトリー     容量
===============================================================================================
インストール中:
 httpd                  x86_64            2.4.6-40.el7.centos            base            2.7 M
依存性関連でのインストールをします:
 apr                    x86_64            1.4.8-3.el7                    base            103 k
 apr-util               x86_64            1.5.2-6.el7                    base             92 k
 httpd-tools            x86_64            2.4.6-40.el7.centos            base             82 k
 mailcap                noarch            2.1.41-2.el7                   base             31 k

トランザクションの要約
===============================================================================================
インストール  1 パッケージ (+4 個の依存関係のパッケージ)

総ダウンロード容量: 3.0 M
インストール容量: 10 M
Is this ok [y/d/N]: y
Downloading packages:
(1/5): apr-1.4.8-3.el7.x86_64.rpm                                       | 103 kB  00:00:00
(2/5): apr-util-1.5.2-6.el7.x86_64.rpm                                  |  92 kB  00:00:00
(3/5): mailcap-2.1.41-2.el7.noarch.rpm                                  |  31 kB  00:00:00
(4/5): httpd-tools-2.4.6-40.el7.centos.x86_64.rpm                       |  82 kB  00:00:00
(5/5): httpd-2.4.6-40.el7.centos.x86_64.rpm                             | 2.7 MB  00:00:00
-----------------------------------------------------------------------------------------------
合計                                                           2.8 MB/s | 3.0 MB  00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  インストール中          : apr-1.4.8-3.el7.x86_64                                         1/5
  インストール中          : apr-util-1.5.2-6.el7.x86_64                                    2/5
  インストール中          : httpd-tools-2.4.6-40.el7.centos.x86_64                         3/5
  インストール中          : mailcap-2.1.41-2.el7.noarch                                    4/5
  インストール中          : httpd-2.4.6-40.el7.centos.x86_64                               5/5
  検証中                  : httpd-2.4.6-40.el7.centos.x86_64                               1/5
  検証中                  : apr-1.4.8-3.el7.x86_64                                         2/5
  検証中                  : mailcap-2.1.41-2.el7.noarch                                    3/5
  検証中                  : httpd-tools-2.4.6-40.el7.centos.x86_64                         4/5
  検証中                  : apr-util-1.5.2-6.el7.x86_64                                    5/5

インストール:
  httpd.x86_64 0:2.4.6-40.el7.centos

依存性関連をインストールしました:
  apr.x86_64 0:1.4.8-3.el7                            apr-util.x86_64 0:1.5.2-6.el7
  httpd-tools.x86_64 0:2.4.6-40.el7.centos            mailcap.noarch 0:2.1.41-2.el7

完了しました!
[root@BS-PUB-CENT7-01 ~]# yum --showduplicates list httpd
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.nus.edu.sg
 * extras: mirror.nus.edu.sg
 * updates: mirror.fairway.ne.jp
インストール済みパッケージ
httpd.x86_64                           2.4.6-40.el7.centos                              @base
利用可能なパッケージ
httpd.x86_64                           2.4.6-40.el7.centos                              base
httpd.x86_64                           2.4.6-40.el7.centos.1                            updates
httpd.x86_64                           2.4.6-40.el7.centos.4                            updates
[root@BS-PUB-WORDPRESS-02 ~]# yum install httpd-2.4.6-40.el7.centos.4
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.nus.edu.sg
 * extras: mirror.nus.edu.sg
 * updates: mirror.fairway.ne.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ httpd.x86_64 0:2.4.6-40.el7.centos を 更新
---> パッケージ httpd.x86_64 0:2.4.6-40.el7.centos.4 を アップデート
--> 依存性の処理をしています: httpd-tools = 2.4.6-40.el7.centos.4 のパッケージ: httpd-2.4.6-40.el7.centos.4.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ httpd-tools.x86_64 0:2.4.6-40.el7.centos を 更新
---> パッケージ httpd-tools.x86_64 0:2.4.6-40.el7.centos.4 を アップデート
--> 依存性解決を終了しました。

依存性を解決しました

===============================================================================================
 Package              アーキテクチャー
                                      バージョン                        リポジトリー      容量
===============================================================================================
更新します:
 httpd                x86_64          2.4.6-40.el7.centos.4             updates          2.7 M
依存性関連での更新をします:
 httpd-tools          x86_64          2.4.6-40.el7.centos.4             updates           83 k

トランザクションの要約
===============================================================================================
更新  1 パッケージ (+1 個の依存関係のパッケージ)

総ダウンロード容量: 2.8 M
Is this ok [y/d/N]: y
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/2): httpd-tools-2.4.6-40.el7.centos.4.x86_64.rpm                     |  83 kB  00:00:00
(2/2): httpd-2.4.6-40.el7.centos.4.x86_64.rpm                           | 2.7 MB  00:00:00
-----------------------------------------------------------------------------------------------
合計                                                           3.4 MB/s | 2.8 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  更新します              : httpd-tools-2.4.6-40.el7.centos.4.x86_64                       1/4
  更新します              : httpd-2.4.6-40.el7.centos.4.x86_64                             2/4
  整理中                  : httpd-2.4.6-40.el7.centos.x86_64                               3/4
  整理中                  : httpd-tools-2.4.6-40.el7.centos.x86_64                         4/4
  検証中                  : httpd-tools-2.4.6-40.el7.centos.4.x86_64                       1/4
  検証中                  : httpd-2.4.6-40.el7.centos.4.x86_64                             2/4
  検証中                  : httpd-2.4.6-40.el7.centos.x86_64                               3/4
  検証中                  : httpd-tools-2.4.6-40.el7.centos.x86_64                         4/4

更新:
  httpd.x86_64 0:2.4.6-40.el7.centos.4

依存性を更新しました:
  httpd-tools.x86_64 0:2.4.6-40.el7.centos.4

完了しました!

2-1.yum upgradeについて

また、似たようなコマンドで「yum upgrade」というコマンドがある。

yum upgrade

 

これは、「yum update –obsoletes」と同じ処理を行っているコマンドだ。
updateの際にすでに旧式となっているパッケージを削除し、適切なパッケージに置き換えてくれる処理として覚えておくといいだろう。

【参考】

 

2-2.アップデート可能なパッケージの一覧を取得する

アップデート可能なパッケージの一覧を取得したいだけなら、以下のコマンドで行える。

yum check-update
[root@BS-PUB-CENT7-01 ~]# yum check-update
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * epel: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp

elasticsearch.noarch                         2.4.0-1                          elasticsearch-2.x
Security: kernel-3.10.0-327.28.3.el7.x86_64 is an installed security update
Security: kernel-3.10.0-327.28.2.el7.x86_64 is the currently running version

 

2-3.指定したパッケージを除外する

特定のパッケージをアップデートから除外する場合は、「–exclude」オプションで対象のパッケージを指定する。

yum update --exclude=パッケージ名
[root@BS-PUB-CENT7-01 test]# yum check-update
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * epel: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp

elasticsearch.noarch                         2.4.0-1                          elasticsearch-2.x
Security: kernel-3.10.0-327.28.3.el7.x86_64 is an installed security update
Security: kernel-3.10.0-327.28.2.el7.x86_64 is the currently running version
[root@BS-PUB-CENT7-01 test]# yum update --exclude=elasticsearch
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * epel: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
No packages marked for update
[root@BS-PUB-CENT7-01 test]# yum update
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * epel: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ elasticsearch.noarch 0:2.3.4-1 を 更新
---> パッケージ elasticsearch.noarch 0:2.4.0-1 を アップデート
--> 依存性解決を終了しました。

依存性を解決しました

===============================================================================================
 Package                 アーキテクチャー バージョン         リポジトリー                 容量
===============================================================================================
更新します:
 elasticsearch           noarch           2.4.0-1            elasticsearch-2.x            26 M

トランザクションの要約
===============================================================================================
更新  1 パッケージ

総ダウンロード容量: 26 M
Is this ok [y/d/N]:

 

2-4.セキュリティ関連のアップデートのみ実行させる

セキュリティ関連のアップデートのみを実行させる場合は、「–security」オプションを付与してアップデートを行わせる。

yum --security update

ただし、これについてはCentOSではデフォルトのリポジトリにはセキュリティアップデートの情報が登録されないため正常に動かない。
CentOSでこの機能を利用するには、こちらにある手順を用いてセキュリティ情報を管理しているリポジトリを登録してやるとよいだろう。

 

3.パッケージをダウングレードする

パッケージを一つ前のバージョンに戻したい場合は、以下のようにコマンドを実行することでダウングレードが行える。

yum downgrade パッケージ名

 

もしくは、バージョンを指定させる場合には以下のようにコマンドを実行するとよいだろう。
指定できるバージョンについては、アップデートと同じように「yum –showduplicates list パッケージ名」で確認してやればよい。

yum downgrade パッケージ名-バージョン
[root@BS-PUB-CENT7-01 ~]# yum --showduplicates list elasticsearch
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * epel: ftp.iij.ad.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
インストール済みパッケージ
elasticsearch.noarch                         2.3.5-1                         @elasticsearch-2.x
利用可能なパッケージ
elasticsearch.noarch                         2.0.0-1                         elasticsearch-2.x
elasticsearch.noarch                         2.0.1-1                         elasticsearch-2.x
elasticsearch.noarch                         2.0.2-1                         elasticsearch-2.x
elasticsearch.noarch                         2.1.0-1                         elasticsearch-2.x
elasticsearch.noarch                         2.1.1-1                         elasticsearch-2.x
elasticsearch.noarch                         2.1.2-1                         elasticsearch-2.x
elasticsearch.noarch                         2.2.0-1                         elasticsearch-2.x
elasticsearch.noarch                         2.2.1-1                         elasticsearch-2.x
elasticsearch.noarch                         2.2.2-1                         elasticsearch-2.x
elasticsearch.noarch                         2.3.0-1                         elasticsearch-2.x
elasticsearch.noarch                         2.3.1-1                         elasticsearch-2.x
elasticsearch.noarch                         2.3.2-1                         elasticsearch-2.x
elasticsearch.noarch                         2.3.3-1                         elasticsearch-2.x
elasticsearch.noarch                         2.3.4-1                         elasticsearch-2.x
elasticsearch.noarch                         2.3.5-1                         elasticsearch-2.x
elasticsearch.noarch                         2.4.0-1                         elasticsearch-2.x
[root@BS-PUB-CENT7-01 ~]# yum downgrade elasticsearch
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * epel: ftp.iij.ad.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ elasticsearch.noarch 0:2.3.4-1 を ダウングレード
---> パッケージ elasticsearch.noarch 0:2.3.5-1 を 削除
--> 依存性解決を終了しました。

依存性を解決しました

===============================================================================================
 Package                 アーキテクチャー バージョン         リポジトリー                 容量
===============================================================================================
ダウングレード中:
 elasticsearch           noarch           2.3.4-1            elasticsearch-2.x            26 M

トランザクションの要約
===============================================================================================
ダウングレード  1 パッケージ

総ダウンロード容量: 26 M
Is this ok [y/d/N]: y
Downloading packages:
elasticsearch-2.3.4.rpm                                                 |  26 MB  00:00:14
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
警告: RPMDB は yum 以外で変更されました。
  インストール中          : elasticsearch-2.3.4-1.noarch                                   1/2
  整理中                  : elasticsearch-2.3.5-1.noarch                                   2/2
  検証中                  : elasticsearch-2.3.4-1.noarch                                   1/2
  検証中                  : elasticsearch-2.3.5-1.noarch                                   2/2

削除しました:
  elasticsearch.noarch 0:2.3.5-1

インストール:
  elasticsearch.noarch 0:2.3.4-1

完了しました!

4.パッケージをアンインストールする

パッケージをアンインストールする場合は、以下のようにサブコマンドとして「erase」を指定してやればよい。

yum erase パッケージ名
[root@BS-PUB-CENT7-01 ~]# yum erase wget
読み込んだプラグイン:fastestmirror
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ wget.x86_64 0:1.14-10.el7_0.1 を 削除
--> 依存性解決を終了しました。

依存性を解決しました

===============================================================================================
 Package           アーキテクチャー    バージョン                     リポジトリー        容量
===============================================================================================
削除中:
 wget              x86_64              1.14-10.el7_0.1                @base              2.0 M

トランザクションの要約
===============================================================================================
削除  1 パッケージ

インストール容量: 2.0 M
上記の処理を行います。よろしいでしょうか? [y/N]y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  削除中                  : wget-1.14-10.el7_0.1.x86_64                                    1/1
  検証中                  : wget-1.14-10.el7_0.1.x86_64                                    1/1

削除しました:
  wget.x86_64 0:1.14-10.el7_0.1

完了しました!

 

5.パッケージのリストを取得する

「yum list」を実行することで、設定されているリポジトリやローカルにインストールされているパッケージのリストを取得することができる。
さらに引数を付け加えることでupdateで指定できるパッケージやインストール済のパッケージのリストを抽出して取得することができる。

yum list
yum list installed # インストール済のパッケージを表示させる
yum list updates # アップデート可能なパッケージを表示させる
yum list available # 利用可能なパッケージを表示させる
yum list extras # 利用できないパッケージを表示させる

 

6.パッケージで利用できる各バージョンのリストを取得する

yumで、パッケージで利用できる各バージョンのリストを取得するには、以下のコマンドを実行すればよい。
現在のバージョン、ダウングレード、アップデートできるバージョンがそれぞれ色分けされて表示される。

yum --showduplicates list パッケージ名

20160909_072639000000

[root@BS-PUB-CENT7-01 ~]# yum --showduplicates list elasticsearch
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * epel: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
インストール済みパッケージ
elasticsearch.noarch                         2.3.4-1                         @elasticsearch-2.x
利用可能なパッケージ
elasticsearch.noarch                         2.0.0-1                         elasticsearch-2.x
elasticsearch.noarch                         2.0.1-1                         elasticsearch-2.x
elasticsearch.noarch                         2.0.2-1                         elasticsearch-2.x
elasticsearch.noarch                         2.1.0-1                         elasticsearch-2.x
elasticsearch.noarch                         2.1.1-1                         elasticsearch-2.x
elasticsearch.noarch                         2.1.2-1                         elasticsearch-2.x
elasticsearch.noarch                         2.2.0-1                         elasticsearch-2.x
elasticsearch.noarch                         2.2.1-1                         elasticsearch-2.x
elasticsearch.noarch                         2.2.2-1                         elasticsearch-2.x
elasticsearch.noarch                         2.3.0-1                         elasticsearch-2.x
elasticsearch.noarch                         2.3.1-1                         elasticsearch-2.x
elasticsearch.noarch                         2.3.2-1                         elasticsearch-2.x
elasticsearch.noarch                         2.3.3-1                         elasticsearch-2.x
elasticsearch.noarch                         2.3.4-1                         elasticsearch-2.x
elasticsearch.noarch                         2.3.5-1                         elasticsearch-2.x
elasticsearch.noarch                         2.4.0-1                         elasticsearch-2.x

 

7.パッケージの情報を取得する

サブコマンドとして「info」を指定することで、パッケージの情報を取得することができる。

yum info # 全パッケージ
yum info パッケージ名

20160909_081356000000

[root@BS-PUB-CENT7-01 ~]# yum info wget
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * epel: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
利用可能なパッケージ
名前                : wget
アーキテクチャー    : x86_64
バージョン          : 1.14
リリース            : 10.el7_0.1
容量                : 545 k
リポジトリー        : base/7/x86_64
要約                : A utility for retrieving files using the HTTP or FTP protocols
URL                 : http://www.gnu.org/software/wget/
ライセンス          : GPLv3+
説明                : GNU Wget is a file retrieval utility which can use either the HTTP or
                    : FTP protocols. Wget features include the ability to work in the
                    : background while you are logged out, recursive retrieval of
                    : directories, file name wildcard matching, remote file timestamp
                    : storage and comparison, use of Rest with FTP servers and Range with
                    : HTTP servers to retrieve files over slow or unstable connections,
                    : support for Proxy servers, and configurability.

 

8.グループインストールをする

yumでは、グループ名を指定することでパッケージをまとめてインストールさせることができる。

yum groups install グループ名

 

指定できるグループ名のリストおよびその内容については、以下のコマンドで確認できる。
なお、yum全体でいえることだが「-v」オプションを付与することでより細かい情報を表示させることもできる。

yum groups list # グループ名一覧
yum groups info グループ名 # 指定したグループの内容
[root@BS-PUB-CENT7-01 ~]# yum groups list
読み込んだプラグイン:fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * epel: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
Available Environment Groups:
   最小限のインストール
   Compute Node
   インフラストラクチャサーバー
   ファイルとプリントサーバー
   MATE デスクトップ
   ベーシック Web サーバー
   仮想化ホスト
   サーバー (GUI 使用)
   GNOME Desktop
   KDE Plasma Workspaces
   開発およびクリエイティブワークステーション
利用可能なグループ
   CIFS ファイルサーバー
   Eclipse
   FCoE ストレージクライアント
   Fedora パッケージャ
   Haskell のサポート
   Milkymist
   MySQL データベースクライアント
   MySQL データベースサーバー
   NFS サーバー
   SNMP のサポート
   TeX のサポート
   TurboGears アプリケーションフレームワーク
   WBEM のサポート
   Xfce
   iSCSI ストレージクライアント
   グラフィカル管理ツール
   コンソールインターネットツール
   サーバープラットフォーム
   サーバー環境の開発環境
   システム管理
   システム管理ツール
   ストレージ可用性ツール
   スマートカードサポート
   セキュリティツール
   デスクトップ
   デスクトップ環境
   デスクトップ環境の開発環境
   ネットワークストレージサーバー
   メッセージングクライアントのサポート
   メッセージングサーバーのサポート
   レガシーな UNIX 互換性
   互換性ライブラリ
   仮想化
   教育用ソフトウェア
   汎用デスクトップ
   科学的サポート
   開発ツール
   電子ラボラトリ
完了
[root@BS-PUB-CENT7-01 ~]# yum groups list -v
プラグイン「fastestmirror」を読み込んでいます
Config time: 0.011
Yum version: 3.4.3
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Setting up Package Sacks
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * epel: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
pkgsack time: 0.033
group time: 0.216
Available Environment Groups:
   最小限のインストール (minimal)
   Compute Node (compute-node-environment)
   インフラストラクチャサーバー (infrastructure-server-environment)
   ファイルとプリントサーバー (file-print-server-environment)
   MATE デスクトップ (mate-desktop-environment)
   ベーシック Web サーバー (web-server-environment)
   仮想化ホスト (virtualization-host-environment)
   サーバー (GUI 使用) (graphical-server-environment)
   GNOME Desktop (gnome-desktop-environment)
   KDE Plasma Workspaces (kde-desktop-environment)
   開発およびクリエイティブワークステーション (developer-workstation-environment)
利用可能なグループ
   CIFS ファイルサーバー (cifs-file-server)
   Eclipse (eclipse)
   FCoE ストレージクライアント (storage-client-fcoe)
   Fedora パッケージャ (fedora-packager)
   Haskell のサポート (haskell)
   Milkymist (milkymist)
   MySQL データベースクライアント (mysql-client)
   MySQL データベースサーバー (mysql)
   NFS サーバー (nfs-file-server)
   SNMP のサポート (system-management-snmp)
   TeX のサポート (tex)
   TurboGears アプリケーションフレームワーク (turbogears)
   WBEM のサポート (system-management-wbem)
   Xfce (xfce-desktop)
   iSCSI ストレージクライアント (storage-client-iscsi)
   グラフィカル管理ツール (graphical-admin-tools)
   コンソールインターネットツール (console-internet)
   サーバープラットフォーム (server-platform)
   サーバー環境の開発環境 (server-platform-devel)
   システム管理 (system-management)
   システム管理ツール (system-admin-tools)
   ストレージ可用性ツール (storage-client-multipath)
   スマートカードサポート (smart-card)
   セキュリティツール (security-tools)
   デスクトップ (basic-desktop)
   デスクトップ環境 (desktop-platform)
   デスクトップ環境の開発環境 (desktop-platform-devel)
   ネットワークストレージサーバー (storage-server)
   メッセージングクライアントのサポート (system-management-messaging-client)
   メッセージングサーバーのサポート (system-management-messaging-server)
   レガシーな UNIX 互換性 (legacy-unix)
   互換性ライブラリ (compat-libraries)
   仮想化 (virtualization)
   教育用ソフトウェア (education)
   汎用デスクトップ (general-desktop)
   科学的サポート (scientific)
   開発ツール (development)
   電子ラボラトリ (electronic-lab)
完了
[root@BS-PUB-CENT7-01 ~]# yum groups info -v security-tools
プラグイン「fastestmirror」を読み込んでいます
Config time: 0.016
Yum version: 3.4.3
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Setting up Package Sacks
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * epel: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
pkgsack time: 0.032
group time: 0.216

グループ: セキュリティツール
 グループ ID: security-tools
rpmdb time: 0.000
 説明: 整合性や信用を検証するセキュリティツール
 標準パッケージ:
   +scap-security-guide-0.1.25-3.el7.centos.0.1.noarch                                base
 オプション パッケージ:
   aide-0.15.1-9.el7.x86_64                                                           base
   hmaccalc-0.9.13-4.el7.x86_64                                                       base
   openscap-1.2.9-5.el7_2.i686                                                        updates
   openscap-1.2.9-5.el7_2.x86_64                                                      updates
   openscap-utils-1.2.9-5.el7_2.x86_64                                                updates
   scap-workbench-1.0.2-2.el7.x86_64                                                  base
   strongimcv-5.2.0-3.el7.i686                                                        base
   strongimcv-5.2.0-3.el7.x86_64                                                      base
   tncfhh-0.8.3-16.el7.i686                                                           base
   tncfhh-0.8.3-16.el7.x86_64                                                         base
   tpm-quote-tools-1.0.2-3.el7.x86_64                                                 base
   tpm-tools-1.3.8-6.el7.i686                                                         base
   tpm-tools-1.3.8-6.el7.x86_64                                                       base
   trousers-0.3.13-1.el7.i686                                                         base
   trousers-0.3.13-1.el7.x86_64                                                       @anaconda

 

また、アンインストールする場合には「groups remove」を、アップデートする場合には「groups update」を用いる。

yum groups remove グループ名 # アンインストール
yum groups update グループ名 # アップデート

 

9.メタデータなどのキャッシュファイルを削除・更新する

メタデータなどのキャッシュファイルが正常な状態になっておらず、yumでの処理が失敗するといったことがあるなら、「yum clean all」を用いる。

yum clean all

 

また、パッケージ情報を保持するメタデータの更新の間隔は、installやupdateの時に設定ファイルにある「metadata_expire」の値(デフォルトだと90分)の間隔で更新される。
もし今すぐにでもパッケージの最新情報を取得したい場合は、以下のコマンドを実行する。

yum makecache

 

 

10.リポジトリの一覧を取得する

使用できるリポジトリの一覧を取得する場合は、以下のコマンドを実行する。

yum repolist
[root@BS-PUB-CENT7-01 ~]# yum repolist
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * epel: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
リポジトリー ID               リポジトリー名                                             状態
base/7/x86_64                 CentOS-7 - Base                                             9,007
elasticsearch-2.x             Elasticsearch repository for 2.x packages                      16
epel/x86_64                   Extra Packages for Enterprise Linux 7 - x86_64             10,540
extras/7/x86_64               CentOS-7 - Extras                                             376
graylog/x86_64                graylog                                                        10
mongodb-org-3.2/7             MongoDB Repository                                             50
security                      CentOS-7 - Security                                             0
updates/7/x86_64              CentOS-7 - Updates                                          2,311
repolist: 22,310
[root@BS-PUB-CENT7-01 ~]# yum repolist -v
プラグイン「fastestmirror」を読み込んでいます
Config time: 0.011
Yum version: 3.4.3
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * epel: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
Setting up Package Sacks
pkgsack time: 0.015
リポジトリー ID            : base/7/x86_64
リポジトリーの名前         : CentOS-7 - Base
リポジトリーのリビジョン   : 1449700451
リポジトリー更新日         : Thu Dec 10 07:35:45 2015
リポジトリー内パッケージ数 : 9,007
リポジトリー容量           : 6.5 G
リポジトリーのミラー       : http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock
リポジトリー基準 URL       : http://ftp.riken.jp/Linux/centos/7.2.1511/os/x86_64/ (9 more)
リポジトリーの期限         : 21,600 秒 (最終: Fri Sep  9 07:46:43 2016)
Repo ファイル名: /etc/yum.repos.d/CentOS-Base.repo

リポジトリー ID            : elasticsearch-2.x
リポジトリーの名前         : Elasticsearch repository for 2.x packages
リポジトリーのリビジョン   : 1472465635
リポジトリー更新日         : Mon Aug 29 19:13:56 2016
リポジトリー内パッケージ数 : 16
リポジトリー容量           : 431 M
リポジトリー基準 URL       : https://packages.elastic.co/elasticsearch/2.x/centos/
リポジトリーの期限         : 21,600 秒 (最終: Fri Sep  9 07:46:45 2016)
Repo ファイル名: /etc/yum.repos.d/elasticsearch.repo

リポジトリー ID            : epel/x86_64
リポジトリーの名前         : Extra Packages for Enterprise Linux 7 - x86_64
リポジトリーのリビジョン   : 1473219190
リポジトリーのタグ         : binary-x86_64
リポジトリー更新日         : Wed Sep  7 15:03:11 2016
リポジトリー内パッケージ数 : 10,540
リポジトリー容量           : 11 G
リポジトリーメタリンク     : https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=x86_64
  更新日                   : Wed Sep  7 15:03:11 2016
リポジトリー基準 URL       : http://ftp.riken.jp/Linux/fedora/epel/7/x86_64/ (15 more)
リポジトリーの期限         : 21,600 秒 (最終: Fri Sep  9 07:46:46 2016)
Repo ファイル名: /etc/yum.repos.d/epel.repo

リポジトリー ID            : extras/7/x86_64
リポジトリーの名前         : CentOS-7 - Extras
リポジトリーのリビジョン   : 1473158555
リポジトリー更新日         : Tue Sep  6 19:43:22 2016
リポジトリー内パッケージ数 : 376
リポジトリー容量           : 1.0 G
リポジトリーのミラー       : http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=extras&infra=stock
リポジトリー基準 URL       : http://ftp.riken.jp/Linux/centos/7.2.1511/extras/x86_64/ (9 more)
リポジトリーの期限         : 21,600 秒 (最終: Fri Sep  9 07:46:46 2016)
Repo ファイル名: /etc/yum.repos.d/CentOS-Base.repo

リポジトリー ID            : graylog/x86_64
リポジトリーの名前         : graylog
リポジトリーのリビジョン   : 1472742118
リポジトリー更新日         : Fri Sep  2 00:01:58 2016
リポジトリー内パッケージ数 : 10
リポジトリー容量           : 723 M
リポジトリー基準 URL       : https://packages.graylog2.org/repo/el/stable/2.0/x86_64/
リポジトリーの期限         : 21,600 秒 (最終: Fri Sep  9 07:46:49 2016)
Repo ファイル名: /etc/yum.repos.d/graylog.repo

リポジトリー ID            : mongodb-org-3.2/7
リポジトリーの名前         : MongoDB Repository
リポジトリーのリビジョン   : 1471372913
リポジトリー更新日         : Wed Aug 17 03:41:56 2016
リポジトリー内パッケージ数 : 50
リポジトリー容量           : 629 M
リポジトリー基準 URL       : https://repo.mongodb.org/yum/redhat/7/mongodb-org/3.2/x86_64/
リポジトリーの期限         : 21,600 秒 (最終: Fri Sep  9 07:46:49 2016)
Repo ファイル名: /etc/yum.repos.d/mongodb-org-3.2.repo

リポジトリー ID            : security
リポジトリーの名前         : CentOS-7 - Security
リポジトリーのリビジョン   : 1472865910
リポジトリー更新日         : Sat Sep  3 10:25:19 2016
リポジトリー内パッケージ数 : 0
リポジトリー容量           : 0
リポジトリー基準 URL       : file:///security/
リポジトリーの期限         : 21,600 秒 (最終: Fri Sep  9 07:46:49 2016)
Repo ファイル名: /etc/yum.repos.d/CentOS-Base.repo

リポジトリー ID            : updates/7/x86_64
リポジトリーの名前         : CentOS-7 - Updates
リポジトリーのリビジョン   : 1473131199
リポジトリー更新日         : Tue Sep  6 12:10:55 2016
リポジトリー内パッケージ数 : 2,311
リポジトリー容量           : 6.4 G
リポジトリーのミラー       : http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=updates&infra=stock
リポジトリー基準 URL       : http://ftp.riken.jp/Linux/centos/7.2.1511/updates/x86_64/ (9 more)
リポジトリーの期限         : 21,600 秒 (最終: Fri Sep  9 07:46:49 2016)
Repo ファイル名: /etc/yum.repos.d/CentOS-Base.repo

repolist: 22,310

 

11.リポジトリの使用有無を指定する

yum実行時にリポジトリを使用するかどうかは、以下のオプションで指定できる。

yum --enablerepo=リポジトリ名 ~ # リポジトリを使用する
yum --disablerepo=リポジトリ名 ~ # リポジトリを使用しない

 

なお、各リポジトリがデフォルトで有効・無効になっているかは、「/etc/yum.repos.d/」配下にある各リポジトリの構成ファイルで設定されている。
リポジトリが有効か無効かを調べるには、上記構成ファイルを見るほかに以下のコマンドで確認する方法もある。

yum repoinfo リポジトリ
[root@BS-PUB-CENT7-01 ~]# yum repoinfo epel
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * epel: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
リポジトリー ID            : epel/x86_64
リポジトリーの名前         : Extra Packages for Enterprise Linux 7 - x86_64
リポジトリーの状態         : 有効
リポジトリーのリビジョン   : 1473219190
リポジトリーのタグ         : binary-x86_64
リポジトリー更新日         : Wed Sep  7 15:03:11 2016
リポジトリー内パッケージ数 : 10,540
リポジトリー容量           : 11 G
リポジトリーメタリンク     : https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=x86_64
  更新日                   : Wed Sep  7 15:03:11 2016
リポジトリー基準 URL       : http://ftp.riken.jp/Linux/fedora/epel/7/x86_64/ (15 more)
リポジトリーの期限         : 21,600 秒 (最終: Fri Sep  9 07:46:46 2016)
Repo ファイル名: /etc/yum.repos.d/epel.repo

repolist: 10,540

 

12.パッケージを検索する

パッケージ名、詳細から検索をする場合は、「yum search」コマンドで検索ができる。

yum search テキスト
[root@BS-PUB-CENT7-01 ~]# yum search bash
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * epel: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
====================================== N/S matched: bash ======================================
bash-argsparse.noarch : An high level argument parsing library for bash
bash-completion.noarch : Programmable completion for Bash
bash-completion-extras.noarch : Additional programmable completions for Bash
bash-doc.x86_64 : Documentation files for bash
bashdb.noarch : BASH debugger, the BASH symbolic debugger
bashmount.noarch : A menu-driven bash script for mounting removable media
libguestfs-bash-completion.noarch : Bash tab-completion scripts for libguestfs tools
pcp-pmda-bash.x86_64 : Performance Co-Pilot (PCP) metrics for the Bash shell
python-django-bash-completion.noarch : bash completion files for Django
bash.x86_64 : The GNU Bourne Again shell
bats.noarch : Bash Automated Testing System
python-argcomplete.noarch : Bash tab completion for argparse
sys_basher.x86_64 : A multithreaded hardware exerciser

  Name and summary matches only, use "search all" for everything.

13.指定したファイル・コマンド名から、どのパッケージが提供しているものか確認する

特定のコマンドやファイルがどのパッケージからインストールされるものなのかを調べる場合は、以下のようにコマンドを実行する。

yum provides コマンド名・ファイルPATH
[root@BS-PUB-CENT7-01 ~]# yum provides nping
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * epel: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
base/7/x86_64/filelists_db                                              | 6.2 MB  00:00:01
elasticsearch-2.x/filelists_db                                          | 8.1 kB  00:00:00
epel/x86_64/filelists_db                                                | 7.2 MB  00:00:23
extras/7/x86_64/filelists_db                                            | 426 kB  00:00:00
graylog/x86_64/filelists_db                                             | 3.5 kB  00:00:00
mongodb-org-3.2/7/filelists_db                                          | 6.2 kB  00:00:00
security/filelists_db                                                   |  586 B  00:00:00
updates/7/x86_64/filelists_db                                           | 4.4 MB  00:00:00
2:nmap-6.40-7.el7.x86_64 : Network exploration tool and security scanner
リポジトリー        : base
一致          :
ファイル名    : /usr/bin/nping


上の例では、nmapパッケージに含まれるnpingコマンドを検索している。
結果、ちゃんとnmapパッケージを提示してくれたことがわかる。

 

14.yumで過去に行った処理を確認する

yumでは、過去に行った処理についてちゃんと記録している。
以下のコマンドで、過去の処理について一覧を表示させることができる。

yum history
[root@BS-PUB-CENT7-01 ~]# yum history
読み込んだプラグイン:fastestmirror
ID     | ログイン ユーザー        | 日時             | 操作           | 変更
-------------------------------------------------------------------------------
    20 | root               | 2016-09-09 08:02 | Erase          |    1
    19 | root               | 2016-09-09 08:02 | Install        |    5
    18 | root               | 2016-09-08 23:51 | Erase          |    1
    17 | root               | 2016-09-08 23:49 | Downgrade      |    1  <
    16 | root               | 2016-09-03 10:26 | I, U           |    4 >
    15 | root               | 2016-09-03 09:43 | Install        |    4
    14 | root               | 2016-09-03 09:38 | Install        |    1
    13 | root               | 2016-09-02 07:47 | Install        |  177
    12 | root               | 2016-09-02 07:45 | Update         |    1  <
    11 | root               | 2016-09-01 21:15 | Install        |    4 >
    10 | root               | 2016-09-01 06:34 | Install        |    1
     9 | root               | 2016-09-01 06:32 | Install        |   33
     8 | root               | 2016-08-30 07:43 | Install        |    1
     7 | root               | 2016-08-30 07:43 | Install        |    1
     6 | root               | 2016-08-30 07:41 | Install        |    1 E<
     5 | root               | 2016-08-30 07:35 | Install        |    5 >
     4 | root               | 2016-08-30 07:22 | Install        |    1 E<
     3 | root               | 2016-08-30 07:08 | Install        |    1 >
     2 | root               | 2016-08-19 08:57 | I, U           |   84
     1 | システム <未設定>        | 2016-01-01 22:59 | Install        |  297
history list

 

とはいえ、これだけではまだ何とも詳細がわからない。
そこで、以下コマンドで対象の履歴番号を指定して実行することで、その詳細を確認する。

yum history info 履歴番号
[root@BS-PUB-CENT7-01 ~]# yum history info 16
読み込んだプラグイン:fastestmirror
トランザクション ID : 16
開始時間            : Sat Sep  3 10:26:43 2016
開始 rpmdb          : 531:1e2be643da9f69ee5ee6a71d1edce293e2a16457
終了時間            :            10:28:56 2016 (133 秒)
終了 rpmdb          : 532:090b2d84288ccfd0620dabff0ff67ea1ae2d9770
ユーザー            : root
終了コード          : 成功
コマンドライン      : --security update
トランザクションの実行:
    インストール rpm-4.11.3-17.el7.x86_64                      @anaconda
    インストール yum-3.4.3-132.el7.centos.0.1.noarch           @anaconda
    インストール yum-plugin-fastestmirror-1.1.31-34.el7.noarch @anaconda
切り替えたパッケージ:
    インストール kernel-3.10.0-327.28.3.el7.x86_64            @updates
    更新   kernel-tools-3.10.0-327.28.2.el7.x86_64      @updates
    更新                3.10.0-327.28.3.el7.x86_64      @updates
    更新   kernel-tools-libs-3.10.0-327.28.2.el7.x86_64 @updates
    更新                     3.10.0-327.28.3.el7.x86_64 @updates
    更新   python-perf-3.10.0-327.28.2.el7.x86_64       @updates
    更新               3.10.0-327.28.3.el7.x86_64       @updates
history info

 

15.yumで過去に行った処理を取り消す

yumで過去に行った処理を取り消すこともできる。
取り消す履歴番号を調べたあとに、以下のコマンドを実行する。

yum history undo 履歴番号
[root@BS-PUB-CENT7-01 ~]# yum history
読み込んだプラグイン:fastestmirror
ID     | ログイン ユーザー        | 日時             | 操作           | 変更
-------------------------------------------------------------------------------
    20 | root               | 2016-09-09 08:02 | Erase          |    1
    19 | root               | 2016-09-09 08:02 | Install        |    5
    18 | root               | 2016-09-08 23:51 | Erase          |    1
    17 | root               | 2016-09-08 23:49 | Downgrade      |    1  <
    16 | root               | 2016-09-03 10:26 | I, U           |    4 >
    15 | root               | 2016-09-03 09:43 | Install        |    4
    14 | root               | 2016-09-03 09:38 | Install        |    1
    13 | root               | 2016-09-02 07:47 | Install        |  177
    12 | root               | 2016-09-02 07:45 | Update         |    1  <
    11 | root               | 2016-09-01 21:15 | Install        |    4 >
    10 | root               | 2016-09-01 06:34 | Install        |    1
     9 | root               | 2016-09-01 06:32 | Install        |   33
     8 | root               | 2016-08-30 07:43 | Install        |    1
     7 | root               | 2016-08-30 07:43 | Install        |    1
     6 | root               | 2016-08-30 07:41 | Install        |    1 E<
     5 | root               | 2016-08-30 07:35 | Install        |    5 >
     4 | root               | 2016-08-30 07:22 | Install        |    1 E<
     3 | root               | 2016-08-30 07:08 | Install        |    1 >
     2 | root               | 2016-08-19 08:57 | I, U           |   84
     1 | システム <未設定>        | 2016-01-01 22:59 | Install        |  297
history list
[root@BS-PUB-CENT7-01 ~]# yum history info 20
読み込んだプラグイン:fastestmirror
トランザクション ID : 20
開始時間            : Fri Sep  9 08:02:42 2016
開始 rpmdb          : 537:d92e64f2f7ef42c9916e57a0498d3ca3855c7d89
終了時間            :            08:02:43 2016 (1 秒)
終了 rpmdb          : 536:964566783155d79ae1fe21b57d5315a296d60eab
ユーザー            : root
終了コード          : 成功
コマンドライン      : groups remove security-tools
トランザクションの実行:
    インストール rpm-4.11.3-17.el7.x86_64                      @anaconda
    インストール yum-3.4.3-132.el7.centos.0.1.noarch           @anaconda
    インストール yum-plugin-fastestmirror-1.1.31-34.el7.noarch @anaconda
切り替えたパッケージ:
    削除 scap-security-guide-0.1.25-3.el7.centos.0.1.noarch @base
history info
[root@BS-PUB-CENT7-01 ~]# yum history undo 20
読み込んだプラグイン:fastestmirror
Undoing transaction 20, from Fri Sep  9 08:02:42 2016
    削除 scap-security-guide-0.1.25-3.el7.centos.0.1.noarch @base
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * epel: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ scap-security-guide.noarch 0:0.1.25-3.el7.centos.0.1 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

===============================================================================================
 Package                     アーキテクチャー
                                            バージョン                      リポジトリー  容量
===============================================================================================
インストール中:
 scap-security-guide         noarch         0.1.25-3.el7.centos.0.1         base         672 k

トランザクションの要約
===============================================================================================
インストール  1 パッケージ

総ダウンロード容量: 672 k
インストール容量: 11 M
Is this ok [y/d/N]: y
Downloading packages:
scap-security-guide-0.1.25-3.el7.centos.0.1.noarch.rpm                  | 672 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  インストール中          : scap-security-guide-0.1.25-3.el7.centos.0.1.noarch             1/1
  検証中                  : scap-security-guide-0.1.25-3.el7.centos.0.1.noarch             1/1

インストール:
  scap-security-guide.noarch 0:0.1.25-3.el7.centos.0.1

完了しました!

 

上の例では、一度行ったアンインストールを取り消した結果、パッケージを再度インストールしている。

 

16.対話的にコマンドを実行する

「yum shell」というコマンドを使うことで、yumに関する処理を対話的に行えるモードに切り替えることができる。

yum shell

20160910_084131000000

 

17.パッケージをインストールせず、rpmファイルだけダウンロードだけする

yumからインストールをするのではなく、ネットワークから隔離された環境で使うためのrpmパッケージがほしい場合は、依存するパッケージを含めてそれらをまとめてダウンロードすることもできる。

yum install --downloadonly パッケージ名
yum install --downloadonly パッケージ名 --downloaddir=PATH

 

ただ、ちょっと問題があってこのコマンドを実行したマシンにすでにインストール済の場合、パッケージのダウンロードができない。
この場合は、「yum-utils」にある「yumdownloader」もしくは「repotrack」コマンドを利用する。「yum-utils」はそのままyumでインストールができる。

yum install yum-utils

 

で、yumdownloader、repotrackがインストールできたら、以下のコマンドでダウンロードする。
特にrepotrackはすごく、インストール済のパッケージでも依存パッケージ(それこそsystemdすら)とってくる。ただ、なぜかvimなどの一部パッケージについてはとってこれなかった。
yumdownloaderはコマンドを実行したOSでインストール済みのパッケージは持ってこれないので、使い分けだろう。

yumdownloader --resolve パッケージ名
repotrack パッケージ名
[root@BS-PUB-CENT7-01 test]# repotrack httpd
Downloading acl-2.2.51-12.el7.x86_64.rpm
Downloading apr-1.4.8-3.el7.x86_64.rpm
Downloading apr-util-1.5.2-6.el7.x86_64.rpm
Downloading audit-libs-2.4.1-5.el7.x86_64.rpm
Downloading audit-libs-2.4.1-5.el7.i686.rpm
Downloading basesystem-10.0-7.el7.centos.noarch.rpm
Downloading bash-4.2.46-20.el7_2.x86_64.rpm
Downloading binutils-2.23.52.0.1-55.el7.x86_64.rpm
Downloading bzip2-libs-1.0.6-13.el7.x86_64.rpm
Downloading bzip2-libs-1.0.6-13.el7.i686.rpm
Downloading ca-certificates-2015.2.6-70.1.el7_2.noarch.rpm
Downloading centos-logos-70.0.6-3.el7.centos.noarch.rpm
Downloading centos-release-7-2.1511.el7.centos.2.10.x86_64.rpm
Downloading chkconfig-1.3.61-5.el7_2.1.x86_64.rpm
Downloading coreutils-8.22-15.el7_2.1.x86_64.rpm
Downloading cpio-2.11-24.el7.x86_64.rpm
Downloading cracklib-2.9.0-11.el7.x86_64.rpm
Downloading cracklib-2.9.0-11.el7.i686.rpm
Downloading cracklib-dicts-2.9.0-11.el7.x86_64.rpm
Downloading cryptsetup-libs-1.6.7-1.el7.x86_64.rpm
Downloading curl-7.29.0-25.el7.centos.x86_64.rpm
Downloading cyrus-sasl-lib-2.1.26-20.el7_2.x86_64.rpm
Downloading cyrus-sasl-lib-2.1.26-20.el7_2.i686.rpm
Downloading dbus-1.6.12-14.el7_2.x86_64.rpm
Downloading dbus-libs-1.6.12-14.el7_2.x86_64.rpm
Downloading device-mapper-1.02.107-5.el7_2.5.x86_64.rpm
Downloading device-mapper-libs-1.02.107-5.el7_2.5.i686.rpm
Downloading device-mapper-libs-1.02.107-5.el7_2.5.x86_64.rpm
Downloading diffutils-3.3-4.el7.i686.rpm
Downloading diffutils-3.3-4.el7.x86_64.rpm
Downloading dracut-033-360.el7_2.1.x86_64.rpm
Downloading elfutils-libelf-0.163-3.el7.x86_64.rpm
Downloading elfutils-libelf-0.163-3.el7.i686.rpm
Downloading elfutils-libs-0.163-3.el7.x86_64.rpm
Downloading elfutils-libs-0.163-3.el7.i686.rpm
Downloading expat-2.1.0-8.el7.x86_64.rpm
Downloading filesystem-3.2-20.el7.x86_64.rpm
Downloading findutils-4.5.11-5.el7.x86_64.rpm
Downloading gawk-4.0.2-4.el7.x86_64.rpm
Downloading glib2-2.42.2-5.el7.i686.rpm
Downloading glib2-2.42.2-5.el7.x86_64.rpm
Downloading glibc-2.17-106.el7_2.8.i686.rpm
Downloading glibc-2.17-106.el7_2.8.x86_64.rpm
Downloading glibc-common-2.17-106.el7_2.8.x86_64.rpm
Downloading gmp-6.0.0-12.el7_1.i686.rpm
Downloading gmp-6.0.0-12.el7_1.x86_64.rpm
Downloading grep-2.20-2.el7.x86_64.rpm
Downloading gzip-1.5-8.el7.x86_64.rpm
Downloading hardlink-1.0-19.el7.x86_64.rpm
Downloading httpd-2.4.6-40.el7.centos.4.x86_64.rpm
Downloading httpd-tools-2.4.6-40.el7.centos.4.x86_64.rpm
Downloading info-5.1-4.el7.x86_64.rpm
Downloading keyutils-libs-1.5.8-3.el7.i686.rpm
Downloading keyutils-libs-1.5.8-3.el7.x86_64.rpm
Downloading kmod-20-5.el7.x86_64.rpm
Downloading kmod-libs-20-5.el7.x86_64.rpm
Downloading kpartx-0.4.9-85.el7_2.5.x86_64.rpm
Downloading krb5-libs-1.13.2-12.el7_2.i686.rpm
Downloading krb5-libs-1.13.2-12.el7_2.x86_64.rpm
Downloading libacl-2.2.51-12.el7.x86_64.rpm
Downloading libacl-2.2.51-12.el7.i686.rpm
Downloading libattr-2.4.46-12.el7.i686.rpm
Downloading libattr-2.4.46-12.el7.x86_64.rpm
Downloading libblkid-2.23.2-26.el7_2.3.i686.rpm
Downloading libblkid-2.23.2-26.el7_2.3.x86_64.rpm
Downloading libcap-2.22-8.el7.x86_64.rpm
Downloading libcap-2.22-8.el7.i686.rpm
Downloading libcap-ng-0.7.5-4.el7.i686.rpm
Downloading libcap-ng-0.7.5-4.el7.x86_64.rpm
Downloading libcom_err-1.42.9-7.el7.x86_64.rpm
Downloading libcom_err-1.42.9-7.el7.i686.rpm
Downloading libcurl-7.29.0-25.el7.centos.i686.rpm
Downloading libcurl-7.29.0-25.el7.centos.x86_64.rpm
Downloading libdb-5.3.21-19.el7.i686.rpm
Downloading libdb-5.3.21-19.el7.x86_64.rpm
Downloading libdb-utils-5.3.21-19.el7.x86_64.rpm
Downloading libffi-3.0.13-16.el7.i686.rpm
Downloading libffi-3.0.13-16.el7.x86_64.rpm
Downloading libgcc-4.8.5-4.el7.x86_64.rpm
Downloading libgcc-4.8.5-4.el7.i686.rpm
Downloading libgcrypt-1.5.3-12.el7_1.1.x86_64.rpm
Downloading libgcrypt-1.5.3-12.el7_1.1.i686.rpm
Downloading libgpg-error-1.12-3.el7.i686.rpm
Downloading libgpg-error-1.12-3.el7.x86_64.rpm
Downloading libidn-1.28-4.el7.i686.rpm
Downloading libidn-1.28-4.el7.x86_64.rpm
Downloading libmount-2.23.2-26.el7_2.3.i686.rpm
Downloading libmount-2.23.2-26.el7_2.3.x86_64.rpm
Downloading libpwquality-1.2.3-4.el7.i686.rpm
Downloading libpwquality-1.2.3-4.el7.x86_64.rpm
Downloading libselinux-2.2.2-6.el7.x86_64.rpm
Downloading libselinux-2.2.2-6.el7.i686.rpm
Downloading libsemanage-2.1.10-18.el7.x86_64.rpm
Downloading libsepol-2.1.9-3.el7.i686.rpm
Downloading libsepol-2.1.9-3.el7.x86_64.rpm
Downloading libssh2-1.4.3-10.el7_2.1.x86_64.rpm
Downloading libssh2-1.4.3-10.el7_2.1.i686.rpm
Downloading libstdc++-4.8.5-4.el7.i686.rpm
Downloading libstdc++-4.8.5-4.el7.x86_64.rpm
Downloading libtasn1-3.8-2.el7.x86_64.rpm
Downloading libuser-0.60-7.el7_1.i686.rpm
Downloading libuser-0.60-7.el7_1.x86_64.rpm
Downloading libutempter-1.1.6-4.el7.x86_64.rpm
Downloading libutempter-1.1.6-4.el7.i686.rpm
Downloading libuuid-2.23.2-26.el7_2.3.x86_64.rpm
Downloading libuuid-2.23.2-26.el7_2.3.i686.rpm
Downloading libverto-0.2.5-4.el7.i686.rpm
Downloading libverto-0.2.5-4.el7.x86_64.rpm
Downloading libxml2-2.9.1-6.el7_2.3.x86_64.rpm
Downloading lua-5.1.4-14.el7.x86_64.rpm
Downloading mailcap-2.1.41-2.el7.noarch.rpm
Downloading ncurses-5.9-13.20130511.el7.x86_64.rpm
Downloading ncurses-base-5.9-13.20130511.el7.noarch.rpm
Downloading ncurses-libs-5.9-13.20130511.el7.x86_64.rpm
Downloading ncurses-libs-5.9-13.20130511.el7.i686.rpm
Downloading nspr-4.11.0-1.el7_2.x86_64.rpm
Downloading nspr-4.11.0-1.el7_2.i686.rpm
Downloading nss-3.21.0-9.el7_2.x86_64.rpm
Downloading nss-3.21.0-9.el7_2.i686.rpm
Downloading nss-softokn-3.16.2.3-14.2.el7_2.x86_64.rpm
Downloading nss-softokn-3.16.2.3-14.2.el7_2.i686.rpm
Downloading nss-softokn-freebl-3.16.2.3-14.2.el7_2.i686.rpm
Downloading nss-softokn-freebl-3.16.2.3-14.2.el7_2.x86_64.rpm
Downloading nss-sysinit-3.21.0-9.el7_2.x86_64.rpm
Downloading nss-tools-3.21.0-9.el7_2.x86_64.rpm
Downloading nss-util-3.21.0-2.2.el7_2.i686.rpm
Downloading nss-util-3.21.0-2.2.el7_2.x86_64.rpm
Downloading openldap-2.4.40-9.el7_2.i686.rpm
Downloading openldap-2.4.40-9.el7_2.x86_64.rpm
Downloading openssl-libs-1.0.1e-51.el7_2.5.x86_64.rpm
Downloading openssl-libs-1.0.1e-51.el7_2.5.i686.rpm
Downloading p11-kit-0.20.7-3.el7.i686.rpm
Downloading p11-kit-0.20.7-3.el7.x86_64.rpm
Downloading p11-kit-trust-0.20.7-3.el7.x86_64.rpm
Downloading pam-1.1.8-12.el7_1.1.x86_64.rpm
Downloading pam-1.1.8-12.el7_1.1.i686.rpm
Downloading pcre-8.32-15.el7_2.1.i686.rpm
Downloading pcre-8.32-15.el7_2.1.x86_64.rpm
Downloading pkgconfig-0.27.1-4.el7.x86_64.rpm
Downloading pkgconfig-0.27.1-4.el7.i686.rpm
Downloading popt-1.13-16.el7.i686.rpm
Downloading popt-1.13-16.el7.x86_64.rpm
Downloading procps-ng-3.3.10-5.el7_2.x86_64.rpm
Downloading procps-ng-3.3.10-5.el7_2.i686.rpm
Downloading qrencode-libs-3.4.1-3.el7.x86_64.rpm
Downloading readline-6.2-9.el7.i686.rpm
Downloading readline-6.2-9.el7.x86_64.rpm
Downloading rpm-4.11.3-17.el7.x86_64.rpm
Downloading rpm-libs-4.11.3-17.el7.x86_64.rpm
Downloading sed-4.2.2-5.el7.x86_64.rpm
Downloading setup-2.8.71-6.el7.noarch.rpm
Downloading shadow-utils-4.1.5.1-18.el7.x86_64.rpm
Downloading shared-mime-info-1.1-9.el7.x86_64.rpm
Downloading sqlite-3.7.17-8.el7.i686.rpm
Downloading sqlite-3.7.17-8.el7.x86_64.rpm
Downloading systemd-219-19.el7_2.12.x86_64.rpm
Downloading systemd-libs-219-19.el7_2.12.x86_64.rpm
Downloading systemd-libs-219-19.el7_2.12.i686.rpm
Downloading tzdata-2016f-1.el7.noarch.rpm
Downloading ustr-1.0.4-16.el7.x86_64.rpm
Downloading util-linux-2.23.2-26.el7_2.3.x86_64.rpm
Downloading util-linux-2.23.2-26.el7_2.3.i686.rpm
Downloading xz-5.1.2-12alpha.el7.x86_64.rpm
Downloading xz-libs-5.1.2-12alpha.el7.x86_64.rpm
Downloading xz-libs-5.1.2-12alpha.el7.i686.rpm
Downloading zlib-1.2.7-15.el7.x86_64.rpm
Downloading zlib-1.2.7-15.el7.i686.rpm

 

18.ローカルにあるrpmファイルからインストール・アップデートを行う

前にこちらにも書いたのだが、ローカルにあるrpmファイルをインストールする際、rpmコマンドからだと依存関係に気を付けて順番通りにインストールするのは難しい。
yumのlocalinstallなら、ローカルのrpmファイルをふつうにインストールすることができる(ただし、すべてのパッケージを引数として指定する必要はある。)。

yum localinstall --nogpgcheck rpmパッケージ1 rpmパッケージ2 ...

 

また、ローカルのrpmファイルからパッケージのアップデートをする場合は、「localupdate」を用いる。

yum localupdate --nogpgcheck rpmパッケージ ...

 

19.パッケージに変更を加える前にファイルシステムのスナップショットを取得する

ファイルシステムがLVMかBtrfsならば、「yum-plugin-fs-snapshot」パッケージをインストールすることで、yumコマンドが何か変更を加える前に自動的にスナップショットを取得させるようにできる。
まず、以下のコマンドで「yum-plugin-fs-snapshot」をインストール。

yum install yum-plugin-fs-snapshot

 

次に、設定ファイルを編集して機能を有効にする。

sed -i '/[lvm]/,$s/^enabled = 0/enabled = 1/g;/#lvcreate_size_args/s/#//g' /etc/yum/pluginconf.d/fs-snapshot.conf

 

あとは、yumで変更処理を行うだけで勝手にスナップショットを取得してくれるようになる。
VGの空き領域がないとエラーになるので、なるべく余裕を持った容量を用意しておこう。

[root@BS-PUB-CENT7-01 test]# yum install httpd
読み込んだプラグイン:fastestmirror, fs-snapshot
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * epel: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ httpd.x86_64 0:2.4.6-40.el7.centos.4 を インストール
--> 依存性の処理をしています: httpd-tools = 2.4.6-40.el7.centos.4 のパッケージ: httpd-2.4.6-40.el7.centos.4.x86_64
--> 依存性の処理をしています: libaprutil-1.so.0()(64bit) のパッケージ: httpd-2.4.6-40.el7.centos.4.x86_64
--> 依存性の処理をしています: libapr-1.so.0()(64bit) のパッケージ: httpd-2.4.6-40.el7.centos.4.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ apr.x86_64 0:1.4.8-3.el7 を インストール
---> パッケージ apr-util.x86_64 0:1.5.2-6.el7 を インストール
---> パッケージ httpd-tools.x86_64 0:2.4.6-40.el7.centos.4 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

===============================================================================================
 Package              アーキテクチャー
                                      バージョン                        リポジトリー      容量
===============================================================================================
インストール中:
 httpd                x86_64          2.4.6-40.el7.centos.4             updates          2.7 M
依存性関連でのインストールをします:
 apr                  x86_64          1.4.8-3.el7                       base             103 k
 apr-util             x86_64          1.5.2-6.el7                       base              92 k
 httpd-tools          x86_64          2.4.6-40.el7.centos.4             updates           83 k

トランザクションの要約
===============================================================================================
インストール  1 パッケージ (+3 個の依存関係のパッケージ)

合計容量: 3.0 M
インストール容量: 9.9 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
fs-snapshot: snapshotting / (/dev/centos/root): root_yum_20160910105209
  インストール中          : apr-1.4.8-3.el7.x86_64                                         1/4
  インストール中          : apr-util-1.5.2-6.el7.x86_64                                    2/4
  インストール中          : httpd-tools-2.4.6-40.el7.centos.4.x86_64                       3/4
  インストール中          : httpd-2.4.6-40.el7.centos.4.x86_64                             4/4
  検証中                  : httpd-tools-2.4.6-40.el7.centos.4.x86_64                       1/4
  検証中                  : apr-util-1.5.2-6.el7.x86_64                                    2/4
  検証中                  : httpd-2.4.6-40.el7.centos.4.x86_64                             3/4
  検証中                  : apr-1.4.8-3.el7.x86_64                                         4/4

インストール:
  httpd.x86_64 0:2.4.6-40.el7.centos.4

依存性関連をインストールしました:
  apr.x86_64 0:1.4.8-3.el7                             apr-util.x86_64 0:1.5.2-6.el7
  httpd-tools.x86_64 0:2.4.6-40.el7.centos.4

完了しました!
[root@BS-PUB-CENT7-01 test]# yum fssnap
読み込んだプラグイン:fastestmirror, fs-snapshot
Have 1 snapshots, using 4.6   space, from 1 origins.
fssnap summary done
[root@BS-PUB-CENT7-01 test]# yum fssnap list
読み込んだプラグイン:fastestmirror, fs-snapshot
List of 1 snapshosts:
Snapshot                         容量   Used   Free Origin Tags
centos/root_yum_20160910105209   44 M   4.7%    0     root yum_20160910105209
fssnap list done

 

実践!  CentOS 7 サーバー徹底構築 実践! CentOS 7 サーバー徹底構築

Linuxコンソール上で使えるバイナリエディタ「hexedit」

$
0
0

LinuxでCLIで使えるバイナリエディタを調べてたら、「hexedit」が結構使い勝手が良かったので忘れないように残しておく。
インストールは以下のコマンドで行える。

yum install hexedit #CentOS などRHEL系の場合
apt-get install hexedit # Debian/Ubuntu系の場合

インストールした後は、以下のコマンドでバイナリを編集できる。

hexedit バイナリファイル

20160911_172157000000

 

編集時の操作としては、とりあえず以下のショートカットキーを覚えておけば問題ないだろう。

  • F1 … ヘルプ
  • F2 … 保存
  • / … 検索
  • Ctrl + X … 保存して終了
  • Ctrl + C … 保存せずに終了

 

なんとなく、エディタとしてはvimとかよりnanoに似た操作感がある。
ちょっとバイナリをいじりたい、といったときには便利な感じだ。

0と1のコンピュータ世界 バイナリで遊ぼう! 0と1のコンピュータ世界 バイナリで遊ぼう!

CentOS 7にチームでのパスワード・鍵管理に便利な「RatticDB」をインストールする

$
0
0

仕事で、チームでのパスワードや鍵管理の方法について調べていて、それ専用のツールとして作られた「RatticDB」なるものを見かけたので、検証として使ってみることにした。
今回は、CentOS 7にこのツールをインストールして使ってみることにする。手順についてはこちらを参考に進め、DB用にデバイスを暗号化した領域を設ける(手順内で暗号化する)。

1.前提パッケージのインストール・設定

まずは、前提となるパッケージのインストールから…の前に、とりあえずSELinuxを無効化しておく。

setenforce 0
sed -i.bak "/SELINUX/s/enforcing/disabled/g" /etc/selinux/config

 

前提パッケージの導入のため、以下のコマンドを実行する。

yum install -y epel-release
yum update -y
yum upgrade -y
yum groupinstall -y development web-server
yum install -y gcc openldap-devel bzip2-devel sqlite-devel libxml2-devel libxslt-devel wget openssl-devel python-pip cryptsetup mod_wsgi vim tk-devel ncurses-devel readline-devel mysql-devel ntp httpd-devel python-devel mariadb-server mariadb
systemctl start ntpd
systemctl enable ntpd
systemctl start mariadb
systemctl enable mariadb

 

2.DBのセットアップ

次に、インストールしたMariaDBで最低限のセキュリティ設定を行う。

mysql_secure_installation
[root@BS-PUB-CENT7-01 ~]# mysql_secure_installation
/usr/bin/mysql_secure_installation: 行 379: find_mysql_client: コマンドが見つかりません

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n]
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

 

DBおよびユーザの作成を行う。

mysql -u root -p \
      -e 'CREATE DATABASE rattic;
          CREATE USER rattic@localhost identified by "RatticDB接続用パスワード";
          GRANT ALL ON rattic.* TO rattic;'

 

3.デバイスの暗号化&DBディレクトリの移動

次に、DBを格納するデバイスが暗号化されていないのはまずいので、ディスクを追加して暗号化する。

cryptsetup luksFormat /dev/sdb1
cryptsetup luksOpen /dev/sdb1 rattic_encrypted
pvcreate /dev/mapper/rattic_encrypted
vgcreate rattic_VG /dev/mapper/rattic_encrypted
lvcreate -l 100%VG -n rattic_LV rattic_VG
mkfs.ext4 /dev/mapper/rattic_VG-rattic_LV
mkdir -p /opt/apps/mysql
mount /dev/mapper/rattic_VG-rattic_LV /opt/apps/mysql
[root@BS-PUB-CENT7-01 ~]# cryptsetup luksFormat /dev/sdb1

WARNING!
========
This will overwrite data on /dev/sdb1 irrevocably.

Are you sure? (Type uppercase yes): YES
Enter passphrase:
Verify passphrase:
[root@BS-PUB-CENT7-01 ~]# cryptsetup luksOpen /dev/sdb1 rattic_encrypted
Enter passphrase for /dev/sdb1:
[root@BS-PUB-CENT7-01 ~]# pvcreate /dev/mapper/rattic_encrypted
  Physical volume "/dev/mapper/rattic_encrypted" successfully created
[root@BS-PUB-CENT7-01 ~]# vgcreate rattic_VG /dev/mapper/rattic_encrypted
  Volume group "rattic_VG" successfully created
[root@BS-PUB-CENT7-01 ~]# lvcreate -l 100%VG -n rattic_LV rattic_VG
  Logical volume "rattic_LV" created.
[root@BS-PUB-CENT7-01 ~]# mkfs.ext4 /dev/mapper/rattic_VG-rattic_LV
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
3276800 inodes, 13106176 blocks
655308 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2162163712
400 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

[root@BS-PUB-CENT7-01 ~]# mkdir -p /opt/apps/mysql
[root@BS-PUB-CENT7-01 ~]# mount /dev/mapper/rattic_VG-rattic_LV /opt/apps/mysql

 

デバイスを暗号化したら、先ほどインストールしたMariaDBの場所を移動する。

systemctl stop mariadb
cp -r /var/lib/mysql/* /opt/apps/mysql
mv /var/lib/mysql /var/lib/mysql.old
ln -s /opt/apps/mysql /var/lib/mysql
chown -R mysql:mysql /opt/apps/mysql/
sed -i.bk 's|/var/lib|/opt/apps|g' /etc/my.cnf
systemctl start mariadb

4.RatticDBをインストールする

さて、それではRatticDBをインストールしよう。
まず、以下のコマンドでソースのダウンロードと配置、その他細かい処理を行う。

cd /tmp
wget https://github.com/tildaslash/RatticWeb/archive/v1.3.1.tar.gz
tar -xzvf v1.3.1.tar.gz
mv RatticWeb-1.3.1 /opt/apps/RatticWeb
cd /opt/apps/RatticWeb
pip install -r requirements-mysql.txt -r requirements-dev.txt
mkdir -p /opt/apps/RatticWeb/static
useradd -c "RatticWeb" -m rattic
chown -R rattic:rattic /opt/apps/RatticWeb

 

次に、Ratticの設定ファイルを新規作成、編集する。

●/opt/apps/RatticWeb/conf/local.cfg

[ratticweb]
debug = False
secretkey = Ratticシークレットパスワード
hostname = ratticdb

[filepaths]
static = /opt/apps/RatticWeb/static

[database]
engine = django.db.backends.mysql
name = rattic
user = rattic
password = RatticDB接続用パスワード
host = localhost
port = 3306

 

ファイル作成後、所有者をratticにしておく。
また、環境にもよると思うが、念のためこちらの対応を行っておく。

chown rattic:rattic /opt/apps/RatticWeb/conf/local.cfg
rm -rf /usr/lib/python2.7/site-packages/kombu/transport/django/migrations/
rm -rf /usr/lib/python2.7/site-packages/djcelery/migrations/
mv /usr/lib/python2.7/site-packages/kombu/transport/django/{south_migrations,migrations}
mv /usr/lib/python2.7/site-packages/djcelery/{south_migrations,migrations}

 

最後に、以下のようにスクリプトを実行してデータベースへの書き込みなどの処理を行わせる。

cd /opt/apps/RatticWeb/
./manage.py syncdb --noinput
./manage.py migrate --all
./manage.py collectstatic -c --noinput
./manage.py demosetup

 

5.Apacheの設定

最後に、Apacheの設定をしておく。
以下のようなファイルを作成する。

●/etc/httpd/conf.d/rattic.conf

<VirtualHost *:80>
    ServerName ratticdb
    ServerAlias ratticdb
    Redirect permanent / https://ratticdb
</VirtualHost>
    Alias /robots.txt /opt/apps/RatticWeb/static/robots.txt
    Alias /favicon.ico /opt/apps/RatticWeb/static/favicon.ico
    AliasMatch ^/([^/]*\.css) /opt/apps/RatticWeb/static/styles/$1
    Alias /media/ /opt/apps/RatticWeb/media/
    Alias /static/ /opt/apps/RatticWeb/static/
<Directory /opt/apps/RatticWeb/static>
    #Order deny,allow
    #Allow from all
    Require all granted
</Directory>
<Directory /opt/apps/RatticWeb/media>
    #Order deny,allow
    #Allow from all
    Require all granted
</Directory>
#WSGISocketPrefix run/wsgi
WSGIScriptAlias / /opt/apps/RatticWeb/ratticweb/wsgi.py
WSGIPassAuthorization On
WSGIDaemonProcess rattic processes=2 threads=25 home=/opt/apps/RatticWeb/ python-path=/opt/apps/RatticWeb display-name=%{GROUP}
WSGIProcessGroup rattic
<Directory /opt/apps/RatticWeb/ratticweb>
    <Files wsgi.py>
        #Order deny,allow
        #Allow from all
        Require all granted
    </Files>
</Directory>

 

作業完了後、サービス再起動とファイアウォールのポートを開ける。

systemctl restart httpd
iptables -I INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT
iptables -I INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT

6.実際に使ってみる

さて、それでは実際にブラウザからアクセスしてみよう。
「https://サーバ名」にアクセスし、ログインしてみよう。初期のID/PWは「admin/rattic」となっている。

20160912_003938000000

 

ログインすると、パスワード一覧のページが表示される。

20160912_004041000000

 

あとは、ユーザやその所属するグループを作成し、それらに応じた鍵の暗号化処理を行わせて、随時鍵を登録していけばよい。そんなに迷うようなことはないだろう。
まずは、用意されているデモページを触ってみることをお勧めする。

一応、LDAPとの連携もできるようなので、悪くないと思う。
また、(あんまりメンテナンスされてなさそうだけど)CLIのツールも用意されてたりするので、結構使えそうな気がする。

 

IT管理者のための情報セキュリティガイド (NextPublishing) IT管理者のための情報セキュリティガイド (NextPublishing)

CentOS7にチームでのパスワード管理マネージャ「TeamPass」をインストールする

$
0
0

チームでのパスワード・鍵ファイルの管理に使えそうなツールを探していたところ、WebベースでAD連携もできるパスワード管理ツール「TeamPass」というものを見かけたので、インストールして少し触ってみることにした。インストール先にはCentOS 7を用いる。

1.前提パッケージのインストール

まずは前提となるパッケージのインストール。
以下のコマンドを実行する。

yum install -y epel-release
yum install -y http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum install -y --enablerepo=rpmforge,remi,remi-php55 httpd mariadb mariadb-server php php-mysql php-mcrypt php-json php-mbstring php-ldap php-bcmath php-iconv php-xml php-gd wget
systemctl start mariadb
systemctl enable mariadb
sed -i '/max_execution_time/s/30/60/g' /etc/php.ini

 

MariaDBで、最低限のセキュリティ設定を行っておく。

mysql_secure_installation

 

事前に、データベースの作成を行っておく。

mysql -u root -p \
      -e 'CREATE DATABASE teampass;
          CREATE USER teampass@localhost identified by "P@ssw0rd";
          GRANT ALL ON teampass.* TO teampass;'

 

最後に、検証なのでFirewalldとSELinuxについては適切な設定ではなく、プロセスの停止。
そのうえでhttpdの起動設定を行う。

setenforce 0
sed -i.bak "/SELINUX/s/enforcing/disabled/g" /etc/selinux/config
systemctl stop firewalld
systemctl disable firewalld
systemctl start httpd
systemctl enable httpd

 

2.TeamPassのインストール

次に、TeamPassのインストールを行う。
まず、こちらから最新版のダウンロードリンクを調べておく。
とりあえず今回は2.1.26を指定する。

cd /opt
wget https://github.com/nilsteampassnet/TeamPass/archive/2.1.26.19.tar.gz
tar xzvf 2.1.26.19.tar.gz
ln -s /opt/TeamPass-* /var/www/html/teampass
chown -R apache:apache /opt/TeamPass-*

3.TeamPassの初期設定

ブラウザで、「http://IPアドレス/teampass/」にアクセスする。
Welcomeページが表示されるので、そのまま「Next」で次のページへ。

20160913_000405000000

 

「Server checks」で何も問題がなければ、「LAUNCH」をクリックする。
何も問題がなければ「NEXT」に変わるのでそのまま進む。

20160913_000415000000

20160913_001825000000

 

DBへの接続情報を設定する。
先ほど作成したDB、ユーザやパスワードの情報を入力して「LAUNCH」をクリック。
接続に問題なければ「NEXT」に変わるのでそのまま進む。

20160913_002022000000

20160913_002129000000

 

TeamPassにおける暗号化や管理者ユーザのパスワード設定を行う。
ランダムな値を払い出すならば「Generate」をクリックすればよい。「LAUNCH」クリック後は今までと同じなので割愛。

20160913_002414000000

 

テーブル作成処理が行われる。
すべて終わったらそのまま次へ。

20160913_002554000000

 

処理がすべて完了したら、そのまま進めていく。

20160913_002654000000

20160913_002702000000

20160913_002705000000

 

4.TeamPassを使ってみる

インストール処理がすべて終わったら、ログイン画面が表示される。
あとは、IDはadmin、パスワードは先ほど設定した管理者パスワードでログインする。
一応、右上の国旗のトコを選択したら日本語にもできるみたいだけど…なんか翻訳が怪しいので英語のまま進める。

20160913_003008000000

 

ログイン直後の画面。
フォルダを作る必要があるようなので、上のフォルダのアイコンをクリックして作成する。

20160913_003353000000

20160913_003536000000

 

Rollを作成するため、フォルダの隣にある帽子アイコンをクリックして作成する。

20160913_003712000000

 

ロール作成後、設定からメンテナンスモードを無効化することで使えるようになる。

20160913_003922000000

 

で、いろいろと触ってみた感想だが…
うーん…正直、RatticDBのほうがいろいろと見やすくて操作や設定も簡単だった気がする。
Redmineがある環境であれば、Valutでもいいかも…

パスワードを共有するという機能だけなら、正直ちゃんとしたユーザであればアクセスしやすい方がいいわけで、どこに何があるんだかわかりにくいことでセキュリティ性を上げてるような印象を受けるというか…別のものを使える状況であれば、あまりいらないかなぁ…というのが感想。わざわざ新しいサーバ立ち上げてまで構築するかといわれると、ちょっと悩む。
LDAP連携とかもできるし、ワンタイムパスワードでの認証も可能など、機能はいっぱいあるみたいなんだけど。もうちょっとUIが見やすくなるとうれしいなぁ…

 

IT管理者のための情報セキュリティガイド (NextPublishing) IT管理者のための情報セキュリティガイド (NextPublishing)

CentOS 7にPython3.4・pip3をインストールする

UbuntuServer16.04にOSSのDCIM・IPAMツール「netbox」をインストールする

$
0
0

以前に紹介したDCIM・IPAMツール「netbox」を、Ubuntu Server 16.04にインストールしてみる。
インストール手順については、公式ドキュメントがあるのでそれを参考にする。

1.PostgreSQLのインストール・設定

まずはPostgreSQLのインストールから。
以下のコマンドを実行する。

sudo apt-get install -y postgresql libpq-dev python-psycopg2

 

PostgreSQLインストール後、DBの作成などを行わせる。
(この手順では、PostgreSQLでパスワードの設定とか行っていない。実際に利用する場合はちゃんと設定することを推奨する。パスワードも例として「P@ssw0rd」にしているが、これもちゃんとランダムなものを設定したほうがいいので、環境や状況に合わせて変更を。)

sudo -u postgres psql << EOF
CREATE DATABASE netbox;
CREATE USER netbox WITH PASSWORD 'P@ssw0rd';
GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox;
EOF

 

2.netboxのインストール・初期設定

Webサーバの前にnetboxのインストール。
まず、以下のコマンドを実行して前提パッケージを導入する。

sudo apt-get install -y python2.7 python-dev python-pip libxml2-dev libxslt1-dev libffi-dev graphviz libpq-dev libssl-dev git

 

次に、githubからnetboxのソースを持ってくる。

sudo mkdir -p /opt/netbox/
cd /opt/netbox/
sudo git clone -b master https://github.com/digitalocean/netbox.git .

 

pipで必要になるpythonパッケージのインストールを行う。

sudo pip install -r requirements.txt

 

設定ファイルをテンプレートをコピーして作成する。

cd netbox/netbox/
sudo cp configuration.example.py configuration.py

 

設定ファイルを編集して、DBの接続情報を記述する。

sudo sed -e "1i DEBUG=True" \
         -e "/^ALLOWED_HOSTS/s/\[\]/\[ 'アクセス可能なIPアドレス(*で指定も可能)' \]/g" \
         -e "/PostgreSQL username/s/''/'netbox'/g" \
         -e "/PostgreSQL password/s/''/'P@ssw0rd'/g" \
         -e "/^SECRET_KEY/s/''/'$(../generate_secret_key.py)'/g" \
         -i.bk ./configuration.py

DBへのテーブル作成などマイグレーションを実行する。

cd /opt/netbox/netbox/
./manage.py migrate

 

netboxの管理ユーザを作成する。

./manage.py createsuperuser
blacknon@BS-PUB-UBUNTU-01:/opt/netbox/netbox$ ./manage.py createsuperuser
Username (leave blank to use 'blacknon'):
Email address:
Password:
Password (again):
Superuser created successfully.

 

静的ファイルの設定。

./manage.py collectstatic
blacknon@BS-PUB-UBUNTU-01:/opt/netbox/netbox$ sudo ./manage.py collectstatic

You have requested to collect static files at the destination
location as specified in your settings:

    /opt/netbox/netbox/static

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes
Copying '/opt/netbox/netbox/project-static/bootstrap-3.3.6-dist/fonts/glyphicons-halflings-regular.svg'
Copying '/opt/netbox/netbox/project-static/bootstrap-3.3.6-dist/fonts/glyphicons-halflings-regular.woff2'
Copying '/opt/netbox/netbox/project-static/bootstrap-3.3.6-dist/fonts/glyphicons-halflings-regular.ttf'
Copying '/opt/netbox/netbox/project-static/bootstrap-3.3.6-dist/fonts/glyphicons-halflings-regular.woff'
Copying '/opt/netbox/netbox/project-static/bootstrap-3.3.6-dist/fonts/glyphicons-halflings-regular.eot'
Copying '/opt/netbox/netbox/project-static/bootstrap-3.3.6-dist/js/bootstrap.min.js'
...

 

最後に、テストでサーバを動作させる。

./manage.py runserver 0.0.0.0:8000 --insecure
blacknon@BS-PUB-UBUNTU-01:/opt/netbox/netbox$ ./manage.py runserver 0.0.0.0:8000 --insecure
Performing system checks...

System check identified no issues (0 silenced).
September 16, 2016 - 15:56:23
Django version 1.10, using settings 'netbox.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

 

無事、以下のようにページが表示されれば動作は問題ないだろう。

20160917_102152000000

 

3.Webサーバのインストール・設定

最後に、ちゃんとWebブラウザから接続できるようにWebサーバのインストール・設定を行う。
今回はWebサーバとしてNginxを用いる。

sudo apt-get install -y gunicorn supervisor nginx

 

Nginxの設定ファイルを配置する。

sudo sh -c 'cat <<"EOF" > /etc/nginx/sites-available/netbox
server {
    listen 80;

    server_name サーバ名(ドメインやIPアドレスなど);

    access_log off;

    location /static/ {
        alias /opt/netbox/netbox/static/;
    }

    location / {
        proxy_pass http://127.0.0.1:8001;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        add_header P3P '\''CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"'\'';
    }
}
EOF'

 

デフォルトの読み込みファイルを削除してシンボリックリンクを作成する。

cd /etc/nginx/sites-enabled/
sudo rm default
sudo ln -s /etc/nginx/sites-available/netbox

 

nginxの起動をする。

sudo service nginx restart

 

最後に、gunicornおよびsupervisordの設定ファイルを作成してサービスを起動させる。

sudo sh -c "cat <<EOF > /opt/netbox/gunicorn_config.py
command = '/usr/bin/gunicorn'
pythonpath = '/opt/netbox/netbox'
bind = '127.0.0.1:8001'
workers = 3
user = 'www-data'
EOF"
sudo sh -c "cat <<EOF > /etc/supervisor/conf.d/netbox.conf
[program:netbox]
command = gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi
directory = /opt/netbox/netbox/
user = www-data
EOF"
sudo chown -R www-data:www-data /opt/netbox/
sudo service supervisor restart

 

あとは、対象のサーバにブラウザからアクセスするだけだ。

20160917_135603000000

 

無事、インストールが完了した。

Data Center Handbook Data Center Handbook

Graylogで「Graylog Collector sidecar」を用いてログの転送を行わせる(Linux)

$
0
0

Graylogでは、Syslogのほかに「Graylog Collector Sidecar」というエージェントを使ってのログ収集方法がある。
この方式の場合、GELFというGraylogの独自フォーマットでのログ入力フォーマットを使えるため、Syslogに比べて詳細な情報を記録しておくことが可能になる。

今回は、この「Graylog Collector Sidecar」を用いてGELFでのログをGraylogに連携させる。このとき、エージェントからGraylogへのデータ転送にはFilebeat、Winlogbeat、NXLogが使えるのだが、今回はNXLogを利用する。手順については公式の手順が用意されているので、それを参考に行う。
なお、すでにGraylog Server側でGELFのインプットは作成済の状態とする(TCP)。

1.Graylog Collector Sidecarのインストール

まずはGraylog Collector Sidecarのパッケージのインストールから。
こちらから対象のディストリビューションに対応したパッケージをダウンロードして、以下のコマンドでインストールを行う。

sudo rpm -ihv https://github.com/Graylog2/collector-sidecar/releases/download/0.0.9/collector-sidecar-0.0.9-1.x86_64.rpm # CentOSなどRHEL系の場合
wget --quiet https://github.com/Graylog2/collector-sidecar/releases/download/0.0.9/collector-sidecar_0.0.9-1_amd64.deb && sudo dpkg -i collector-sidecar_0.0.9-1_amd64.deb && rm collector-sidecar_0.0.9-1_amd64.deb # Debian/Ubuntu系

 

2.NXLogのインストール

次に、Graylog Collector SidecarからGraylog Serverへログを転送させるために用いるNXLogのインストールを行う。
こちらのページからパッケージをダウンロードしてインストールする。

CentOS 7の場合

yum install https://nxlog.co/system/files/products/files/1/nxlog-ce-2.9.1716-1_rhel7.x86_64.rpm
sudo service nxlog stop
sudo chkconfig --del nxlog
sudo gpasswd -a nxlog root
sudo chown -R nxlog.nxlog /var/spool/collector-sidecar/nxlog

 

Ubuntu Server 16.04の場合

sudo apt-get install libapr1 libdbi1
wget --quiet https://nxlog.co/system/files/products/files/1/nxlog-ce_2.9.1716_ubuntu_1604_amd64.deb && sudo dpkg -i nxlog-ce_2.9.1716_ubuntu_1604_amd64.deb && rm nxlog-ce_2.9.1716_ubuntu_1604_amd64.deb
sudo /etc/init.d/nxlog stop
sudo update-rc.d -f nxlog remove
sudo gpasswd -a nxlog adm
sudo chown -R nxlog.nxlog /var/spool/collector-sidecar/nxlog

 

インストール完了後、以下のコマンドでGraylog Collector Sidecarのサービスを稼働させる。

sudo graylog-collector-sidecar -service install
sudo systemctl start collector-sidecar

 

3.設定ファイルの編集

最後に、Graylog Collector Sidecarの設定ファイルを編集してやる。
(とりあえずここではタグを「linux」にしているが、できればCentOSやUbuntuで分けて書いてやったほうがいいと思う。)

●/etc/graylog/collector-sidecar/collector_sidecar.yml

server_url: http://GraylogServerのホスト名orIPアドレス:12900
update_interval: 30
tls_skip_verify: true
send_status: true
list_log_files:
  - /var/log
node_id: エージェントのノード名(重複すると正常動作しないので注意)
collector_id: file:/etc/graylog/collector-sidecar/collector-id
log_path: /var/log/graylog/collector-sidecar
log_rotation_time: 86400
log_max_age: 604800
tags:
  - linux
backends:
    - name: nxlog
      enabled: true
      binary_path: /usr/bin/nxlog
      configuration_path: /etc/graylog/collector-sidecar/generated/nxlog.conf

 

最後に、Graylog Collector Sidecarを再起動する。

systemctl restart collector-sidecar

4.GraylogServerの管理画面からエージェントの設定を行う

Graylog Collector Sidecarの設定が終わったら、続きはGraylog Server側の管理画面で行う。
Graylog Serverのトップページから[System] > [Collector]を開き、先ほど設定したエージェントが表示されていることを確認する。

20160921_130958000000

 

右上にある「Manage Configuration」をクリックして、「Create Configuration」から新しいコンフィグを作成する。
(ここでは、とりあえず「Linux」というコンフィグを作っている)

20160921_131245000000

 

コンフィグの作成後、コンフィグ名をクリックして内容を編集する。
基本的には、以下の設定だけやってやればよい。

  • Tagの追加(Graylog Collector Sidecarで指定したタグ)
  • Configure Collector Outputsの設定(Graylog Collector SidecarからGraylog Serverへログを転送する際の設定)
  • Configure Collector Inputsの設定(Graylog Collector Sidecarで収集するログの設定)

20160921_140359000000

なお、このときにDebian系とRHEL系で同居させる場合は、「Define Snippets」のnxlog-defaultsを以下のように編集する必要がある。

{{if .Linux}}
User nxlog
Group nxlog
{{if eq .LinuxPlatform "debian"}}
Moduledir /usr/lib/nxlog/modules
{{end}}
{{if eq .LinuxPlatform "redhat"}}
Moduledir /usr/libexec/nxlog/modules
{{end}}
CacheDir /var/spool/collector-sidecar/nxlog
PidFile /var/run/graylog/collector-sidecar/nxlog.pid
define LOGFILE /var/log/graylog/collector-sidecar/nxlog.log
LogFile %LOGFILE%
LogLevel INFO

<Extension logrotate>
    Module  xm_fileop
    <Schedule>
        When    @daily
        Exec    file_cycle('%LOGFILE%', 7);
     </Schedule>
</Extension>
{{end}}
{{if .Windows}}
Moduledir %ROOT%\modules
CacheDir %ROOT%\data
Pidfile %ROOT%\data\nxlog.pid
SpoolDir %ROOT%\data
LogFile %ROOT%\data\nxlog.log
LogLevel INFO

<Extension logrotate>
    Module  xm_fileop
    <Schedule>
        When    @daily
        Exec    file_cycle('%ROOT%\data\nxlog.log', 7);
     </Schedule>
</Extension>
{{end}}

 

これで、Graylog Collector Sidecarでのログ転送が行えるようになった。

サーバ/インフラエンジニア養成読本 ログ収集〜可視化編 [現場主導のデータ分析環境を構築!] Software Design plus サーバ/インフラエンジニア養成読本 ログ収集〜可視化編 [現場主導のデータ分析環境を構築!] Software Design plus

sshdのauthorized_keysでできること

$
0
0

OpenSSHでは、デフォルトだと.ssh/authorized_keysというファイルに公開鍵を置いておく(別のファイルを指定することも可能)のだが、このファイルに設定を記述することで、その公開鍵の挙動を制限することができる。有名どころとしてはcommandとfromなのだが、そのほかにもいろいろと設定できるので便利そうなのをいくつかまとめておくことにする。
ここを見る限り、以下の項目について設定ができるようだ。

  • agent-forwarding
  • cert-authority
  • command=”command”
  • environment=”NAME=value”
  • no-agent-forwarding
  • no-port-forwarding
  • no-pty
  • no-user-rc
  • no-X11-forwarding
  • permitopen=”host:port”
  • port-forwarding
  • principals=”principals”
  • pty
  • restrict
  • tunnel=”n”
  • user-rc
  • X11-forwarding

 

1.ssh接続時に特定のコマンドが実行されるようにする

前にここでも書いたのだが、ssh接続と同時に指定したコマンドが実行されるようにするには、authorized_keysで以下のように記述すればよい。

command="コマンドPATH" ssh-rsa ..

 

例えば、authorized_keysに以下のように記述していたとする。

command="pwd;cat .ssh/auth*;ls -la" ssh-rsa AAAAB...

 

その場合、その鍵ファイルでログインすると以下のようにコマンドの実行結果が返され、処理終了と同時に接続が切断される。

[root@BS-PUB-CENT7-02 ~]# ssh test@BS-PUB-CENT7-01
command="cat .ssh/auth*;pwd;ls -la" ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC8CB356HRtiXR5RXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX+4BXUzTPf417jyUL3ktSJsmdVPAyyUFxpdrBpAKkG3pj4Tfhj3mYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYB3XWhllb5CyI7EcVM4dP7TTpBARzQBQpeaLsVp2gsbb4V8UuwdZyPbXahps90GadQgrDjR5647EGArOCIWRNuP+5ALK4tFFNEraG76cRRAs3lDLD462ZAcPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//9zszOtlDlr4GcgYJhX6px4Qtv1v/fjRn1 root@BS-PUB-CENT7-02.blacknon.local
/home/test
合計 16
drwx------. 3 test test  90  9月 21 23:48 .
drwxr-xr-x. 3 root root  17  9月 21 23:46 ..
-rw-------. 1 test test  25  9月 21 23:46 .bash_history
-rw-r--r--. 1 test test  18  8月  3 01:00 .bash_logout
-rw-r--r--. 1 test test 193  8月  3 01:00 .bash_profile
-rw-r--r--. 1 test test 231  8月  3 01:00 .bashrc
drwx------. 2 test test  28  9月 21 23:53 .ssh
Connection to bs-pub-cent7-01 closed.

 

このとき、環境変数として$SSH_ORIGINAL_COMMANDを用いることでsshコマンドの引数を実行コマンドに利用することも可能だ。
例えば、authorized_keysに以下のように記述していたとする。

command="echo $SSH_ORIGINAL_COMMAND;ls -la $SSH_ORIGINAL_COMMAND" ssh-rsa AAAAB...

 

その場合、接続時に引数を与えると以下のように結果が返ってくる。

[root@BS-PUB-CENT7-02 ~]# ssh test@BS-PUB-CENT7-01 .ssh/auth*
.ssh/authorized_keys
-rw-------. 1 test test 483  9月 22 00:02 .ssh/authorized_keys

 

なお、一応いろいろといじってみた限りでは指定されたコマンド以外は実行できないようになっている(引数でエスケープさせて任意のコマンドを実行させられない)ようだが、できればラッパーコマンドを別途用意しておくといいだろう。

[root@BS-PUB-CENT7-02 ~]# ssh test@BS-PUB-CENT7-01 '/tmp && echo aaa'
/tmp && echo aaa
/tmp:
合計 4
drwxrwxrwt.  7 root root   88  9月 21 21:34 .
dr-xr-xr-x. 17 root root 4096  1月  1  2016 ..
drwxrwxrwt.  2 root root    6  1月  1  2016 .ICE-unix
drwxrwxrwt.  2 root root    6  1月  1  2016 .Test-unix
drwxrwxrwt.  2 root root    6  1月  1  2016 .X11-unix
drwxrwxrwt.  2 root root    6  1月  1  2016 .XIM-unix
drwxrwxrwt.  2 root root    6  1月  1  2016 .font-unix
ls: && にアクセスできません: そのようなファイルやディレクトリはありません
ls: echo にアクセスできません: そのようなファイルやディレクトリはありません
ls: aaa にアクセスできません: そのようなファイルやディレクトリはありません
[root@BS-PUB-CENT7-02 ~]# ssh test@BS-PUB-CENT7-01 '/tmp || echo aaa'
/tmp || echo aaa
/tmp:
合計 4
drwxrwxrwt.  7 root root   88  9月 21 21:34 .
dr-xr-xr-x. 17 root root 4096  1月  1  2016 ..
drwxrwxrwt.  2 root root    6  1月  1  2016 .ICE-unix
drwxrwxrwt.  2 root root    6  1月  1  2016 .Test-unix
drwxrwxrwt.  2 root root    6  1月  1  2016 .X11-unix
drwxrwxrwt.  2 root root    6  1月  1  2016 .XIM-unix
drwxrwxrwt.  2 root root    6  1月  1  2016 .font-unix
ls: || にアクセスできません: そのようなファイルやディレクトリはありません
ls: echo にアクセスできません: そのようなファイルやディレクトリはありません
ls: aaa にアクセスできません: そのようなファイルやディレクトリはありません
[root@BS-PUB-CENT7-02 ~]# ssh test@BS-PUB-CENT7-01 "echo $(hostname)"
echo BS-PUB-CENT7-02.blacknon.local
ls: echo にアクセスできません: そのようなファイルやディレクトリはありません
ls: BS-PUB-CENT7-02.blacknon.local にアクセスできません: そのようなファイルやディレクトリはありません
[root@BS-PUB-CENT7-02 ~]# ssh test@BS-PUB-CENT7-01 "echo `hostname`"
echo BS-PUB-CENT7-02.blacknon.local
ls: echo にアクセスできません: そのようなファイルやディレクトリはありません
ls: BS-PUB-CENT7-02.blacknon.local にアクセスできません: そのようなファイルやディレクトリはありません

 

2.特定のIPアドレスからのみ接続を許可する

公開鍵で特定のIPアドレスやネットワークからの接続のみを許可する場合は、以下のように記述する。

from="ネットワークアドレス/CIDR or IPアドレス" ssh-rsa ...
from="ネットワークアドレス/CIDR or IPアドレス1,ネットワークアドレス/CIDR or IPアドレス2,..." ssh-rsa ... # 複数指定する場合はカンマで区切る

3.ポートフォワーディングやエージェントフォワーディングなどを無効にする

ポートフォワーディングやエージェントフォワーディングといった機能を使えないように設定することもできる。
以下、無効関係の設定の一覧。

  • no-agent-forwarding … エージェントフォワーディングを無効にする
  • no-port-forwarding … ポートフォワーディングを無効にする
  • no-pty … PTYを無効にする(対話形式の処理をできないようにする)
  • no-user-rc … ~/.ssh/rcの実行を無効にする
  • no-X11-forwarding … X11フォワーディングを無効にする

 

なお、これらに限らずだがそれぞれの設定はカンマ区切りにすることで複数指定することが可能だ。

no-agent-forwarding,no-port-forwarding,no-pty ssh-rsa ...

 

4.公開鍵に紐づいて環境変数を定義する

sshdで「PermitUserEnvironment」が有効になっている場合のみ、以下のように記述することで公開鍵に紐づき環境変数を定義することができる。

environment="環境変数=値" ssh-rsa ...

 

例えば、「NAME」という環境変数に「Value」という値を入れた場合。

[root@BS-PUB-CENT7-01 ~]# cat /home/test/.ssh/authorized_keys
environment="NAME=Value",command="echo $NAME" ssh-rsa ...

--------

[root@BS-PUB-CENT7-02 ~]# ssh test@BS-PUB-CENT7-01
Value
Connection to bs-pub-cent7-01 closed.

 

そのほか、sshトンネル(tunnel)やポートフォワード(permitopen)、証明書の信頼性について別リストから検証させるようにする(principals)など、いろいろと設定できるようだ。

OpenSSH[実践]入門 (Software Design plus) OpenSSH[実践]入門 (Software Design plus)

rbashで一部のユーザが実行できるコマンドを制限する(特定コマンドしか実行できないようにする)

$
0
0

一部のユーザで、特定のコマンドのみを利用できるようにして、そのほかのコマンドは実行できないようにしたい。
そんなときは、対象のユーザのログインシェルをrbashというシンボリックリンクにしてやることで実現可能だ。

まず、bashのシンボリックリンクとしてrbashを「/opt/bin」配下に作成する。

mkdir /opt/bin
ln -s /bin/bash /opt/bin/rbash

 

次に、rbash用のユーザを作成する。

useradd rbash_test -s /opt/bin/rbash
passwd rbash_test

 

rbash用のユーザが利用できるコマンド(シンボリックリンク)のみを格納したディレクトリを作成する。
ここでは、ping、lsのみを許可してみる。

mkdir /opt/bin/rbash_test/
ln -s $(which ping | grep bin) /opt/bin/rbash_test/
ln -s $(which ls | grep bin) /opt/bin/rbash_test/

 

最後に、.bash_profileを書き換えてPATHに先ほど作成したディレクトリのみを指定する。

chown root:root /home/rbash_test/.bash_profile
chmod 755 /home/rbash_test/.bash_profile
sed -i '/^PATH=/cPATH=/opt/bin/rbash_test/' /home/rbash_test/.bash_profile

あとはログインするだけだ。
対象のユーザでログインしてみよう。

20160925_092015000000

Last login: Sun Sep 25 09:19:23 2016 from XXX.XXX.XXX.XXX
[rbash_test@BS-PUB-CENT7-02 ~]$ ls -la
合計 16
drwx------. 2 rbash_test rbash_test  79  9月 25 09:18 .
drwxr-xr-x. 3 root       root        23  9月 25 09:16 ..
-rw-------. 1 rbash_test rbash_test 128  9月 25 09:19 .bash_history
-rw-r--r--. 1 rbash_test rbash_test  18  8月  3 01:00 .bash_logout
-rwxr-xr-x. 1 root       root       181  9月 25 09:18 .bash_profile
-rw-r--r--. 1 rbash_test rbash_test 231  8月  3 01:00 .bashrc
[rbash_test@BS-PUB-CENT7-02 ~]$ ls -la /opt/
合計 4
drwxr-xr-x.  3 root root   16  9月 25 09:16 .
dr-xr-xr-x. 17 root root 4096  1月  2  2016 ..
drwxr-xr-x.  3 root root   35  9月 25 09:17 bin
[rbash_test@BS-PUB-CENT7-02 ~]$ cd
-rbash: cd: 制限されています
[rbash_test@BS-PUB-CENT7-02 ~]$ ping -c 1 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=58 time=117 ms

--- 8.8.8.8 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 117.210/117.210/117.210/0.000 ms

 

確かにcdはできないし、制限はできてるようだ。
ただまぁ、chrootを行っているわけではないので、/homeより上は見えるようだ。ということはPATHを書き換えたり、「/bin」配下のコマンドを直接実行できるのか?というと

[rbash_test@BS-PUB-CENT7-02 ~]$ PATH=$PATH:/bin
-rbash: PATH: 読み取り専用の変数です
[rbash_test@BS-PUB-CENT7-02 ~]$ /bin/touch aaa
-rbash: /bin/touch: 制限されています:  `/' をコマンド名の中に指定できません

 

ちゃんと実行できないようになっているようだ。

[改訂新版]プロのためのLinuxシステム構築・運用技術 (Software Design plus) [改訂新版]プロのためのLinuxシステム構築・運用技術 (Software Design plus)

LinuxのCUIで管理するパスワードマネージャ「gpgpwd」コマンド

$
0
0

先日、いくつかチームでのパスワード管理マネージャについて紹介したが、今回は個人向けのパスワードマネージャ。
で、GUIで管理するタイプではなく、コンソールから利用できるタイプの「gpgpwd」というものを見かけたので、これについて紹介する。

1.インストール

まずはインストールの前に、前提となるパッケージを導入する。

apt-get install libjson-perl libtry-tiny-perl gnupg xclip git gnupg-agent # Debian/Ubuntu系の場合
yum install perl-JSON perl-Try-Tiny gnupg xclip git # CentOSなどRHEL系の場合

 

次に、以下のコマンドでgpgpwdをインストールする。

git clone https://github.com/zerodogg/gpgpwd
cd gpgpwd
sudo make install

 

これでインストールができた。

 

2.使ってみる

2-1.GPG鍵ファイルの作成

さて、それでは実際に使ってみよう。
まず、gpgpwdで保持するパスワードを暗号化するため、GnuPG鍵を作成する。

[root@BS-PUB-CENT7-01 ~]# gpg --gen-key
gpg (GnuPG) 2.0.22; Copyright (C) 2013 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

ご希望の鍵の種類を選択してください:
   (1) RSA と RSA (デフォルト)
   (2) DSA と Elgamal
   (3) DSA (署名のみ)
   (4) RSA (署名のみ)
あなたの選択は? [Enter]
RSA 鍵は 1024 から 4096 ビットの長さで可能です。
鍵長は? (2048) [Enter]
要求された鍵長は2048ビット
鍵の有効期限を指定してください。
         0 = 鍵は無期限
        = 鍵は n 日間で期限切れ
      w = 鍵は n 週間で期限切れ
      m = 鍵は n か月間で期限切れ
      y = 鍵は n 年間で期限切れ
鍵の有効期間は? (0) [Enter]
(null)は無期限です
これで正しいですか? (y/N) y

GnuPGはあなたの鍵を識別するためにユーザIDを構成する必要があります。

本名: TestID
電子メール・アドレス:
コメント:
次のユーザIDを選択しました:
    "TestID"

名前(N)、コメント(C)、電子メール(E)の変更、またはOK(O)か終了(Q)? O

 

その後、パスフレーズやランダムバイトの生成が行われるので、コンソールに表示されているメッセージに従って処理を行う。
(結構時間がかかる)

たくさんのランダム・バイトの生成が必要です。キーボードを打つ、マウスを動かす、
ディスクにアクセスするなどの他の操作を素数生成の間に行うことで、乱数生成器に
十分なエントロピーを供給する機会を与えることができます。
gpg: 鍵961B1619を絶対的に信用するよう記録しました
公開鍵と秘密鍵を作成し、署名しました。

gpg: 信用データベースの検査
gpg: 「ギリギリの信用」3、「全面的信用」1、PGP信用モデル
gpg: 深さ: 0  有効性:   1  署名:   0  信用: 0-, 0q, 0n, 0m, 0f, 1u
pub   2048R/961B1619 2016-09-21
   フィンガー・プリント = 2326 B78D 1278 DD34 0398  A082 7273 6E70 961B 1619
uid                  TestID
sub   2048R/07CBBA98 2016-09-21

2-2.パスワードの登録・確認

次に、実際にパスワードの登録をしてみよう。
以下のコマンドでパスワード登録が行える。なお確認時は指定されたパスワード名で部分一致で検索をかける。
それぞれのコマンドの実行前後にはGnuPG鍵のパスフレーズを求められる。

gpgpwd set パスワード名 # パスワードのセット
gpgpwd get パスワード名 # パスワードの確認(パスワード名で検索)

20160922_091227000000

[root@BS-PUB-CENT7-01 ~]# gpgpwd set TestPassword
Adding an entry for TestPassword

Random password: FqzZRLccV?3&bPx
  Enter /help for help.
  Enter a password to use a custom password.
  Just press enter to use the random password.
Password> TestPassword

  Enter a username for this entry.
  Just press enter to not store any username.
Username> root
[root@BS-PUB-CENT7-01 ~]# gpgpwd set PasswordTest
Adding an entry for PasswordTest

Random password: 06c5k_AX8q=2Va#
  Enter /help for help.
  Enter a password to use a custom password.
  Just press enter to use the random password.
Password> TeST

  Enter a username for this entry.
  Just press enter to not store any username.
Username> test
[root@BS-PUB-CENT7-01 ~]# gpgpwd get Password
Name                  Password                           Username
----                  --------                           --------
PasswordTest        : TeST                               test
TestPassword        : TestPassword                       root

 

うーん…個人利用であれば悪くなさそうな気がする。

暗号技術入門 第3版 秘密の国のアリス 暗号技術入門 第3版 秘密の国のアリス

標準出力・標準エラー出力の内容をターミナルとSyslogに出力する

$
0
0

標準出力・標準エラー出力の内容をターミナルとSyslog、どちらにも出力したいときには、以下のようにコマンドを実行すればよい。

コマンド 2>&1 | tee >(logger -i -t "タグ")

[root@BS-PUB-CENT7-01 ~]# echo "ABCDEFG" | tee >(logger -i -t "test")
ABCDEFG
[root@BS-PUB-CENT7-01 ~]# tail -3 /var/log/messages
Sep 24 14:15:55 BS-PUB-CENT7-01 systemd-logind: New session 281 of user root.
Sep 24 14:15:55 BS-PUB-CENT7-01 systemd: Starting Session 281 of user root.
Sep 24 14:30:11 BS-PUB-CENT7-01 test[7608]: ABCDEFG
rsyslog 実践ログ管理入門 rsyslog 実践ログ管理入門

Linux/Macのターミナルで使えるパスワードマネージャ「pass」

$
0
0

Linux/Macのターミナルで利用できるパスワードマネージャということで、インストールが簡単に行える「pass」というものを見かけたので紹介。
管理するパスワードをフォルダのように階層で管理できるので、CUIながらパスワード管理しやすそうだ。一応、gitでの管理も行えるようだ。

1.インストール

インストールは簡単で、以下のコマンドで行える。

sudo yum install pass # CentOSなどRHEL系
sudo apt-get install pass # Debian/Ubuntu系

 

Macの場合は以下のコマンドでインストールする。

brew install pass
echo "source /usr/local/etc/bash_completion.d/password-store" >> ~/.bashrc

2.使ってみる

さて、それでは実際にパスワードを登録してみよう。
まず、以下のコマンドでpassで管理するパスワードを暗号化するためのGPGキー名を入力する。
ここでは、GPGキーは前に紹介したgpgpwdで作成したものを指定する

pass init 暗号化キー名

 

2-1.パスワードの登録

次に、パスワードの登録を行う。
以下のコマンドで登録ができる。

pass insert 階層名/パスワード名

20160924_141055000000

 

パスワードを変更する場合は、再度同じコマンドを実行することで上書きできる。

 

2-2.パスワードの確認

登録したパスワードは、以下のコマンドで確認できる。

pass 階層名/パスワード

20160924_141831000000

 

なお、以下のコマンドでクリップボードに渡すこともできる。

pass -c 階層名/パスワード

 

と、こんな感じなのだが、CUIに慣れてる人なら結構便利かもしれない。

小さな会社のIT担当者のためのセキュリティの常識 小さな会社のIT担当者のためのセキュリティの常識

CentOS 7にOSSのWebベースAnsible操作・管理ツール「Ansible Semaphore」をインストールする

$
0
0

AnsibleのWebベースでの操作・管理ツールといえばAnsible Towerが有名だが、10ホスト以上になると有償となってしまうため、あまり気軽に入れられないところがある。
で、他に何かないかと探していたところ、「Ansible Semaphore」というものがあったので、すこし触ってみることにする。一応Dockerもあるようだが、今後のことも考えてCentOS 7に入れてみることにする。
OSS Alternative Ansible Towerと言っているので、なかなか期待できそうだ。

1.前提パッケージのインストール

まずは前提となるパッケージのインストールから。
以下のコマンドを実行する。

sudo yum -y install epel-release
sudo yum -y install ansible mariadb mariadb-server

 

MariaDBのサービス起動設定と、最低限のセキュリティ設定を行っておく。

systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation

 

データベースとそこにアクセスするユーザを作成する。

mysql -u root -p -e "
CREATE DATABASE semaphore;
CREATE USER 'semaphore'@'localhost' IDENTIFIED BY 'パスワード';
GRANT ALL PRIVILEGES ON semaphore.* TO 'semaphore'@'localhost' WITH GRANT OPTION;"

 

Playbookの配置先として、opt配下にディレクトリを作成しておく。

mkdir /opt/semaphore

 

2.Semaphoreのインストール

次に、Semaphoreのインストールを行う。
コンパイル済みのバイナリがあるので、ダウンロードしてセットアップさせる。

cd /usr/bin
wget https://github.com/ansible-semaphore/semaphore/releases/download/v2.0.4/semaphore_linux_amd64
mv semaphore_linux_amd64 semaphore
chmod a+x semaphore
semaphore -setup
[root@BS-PUB-CENT7-01 bin]# semaphore -setup

 Hello! You will now be guided through a setup to:

 1. Set up configuration for a MySQL/MariaDB database
 2. Set up a path for your playbooks (auto-created)
 3. Run database Migrations
 4. Set up initial seamphore user & password

 > DB Hostname (default 127.0.0.1:3306):
 > DB User (default root): semaphore
 > DB Password: P@ssw0rd
 > DB Name (default semaphore): semaphore
 > Playbook path: /opt/semaphore

 Generated configuration:
 {
        "mysql": {
                "host": "127.0.0.1:3306",
                "user": "semaphore",
                "pass": "P@ssw0rd",
                "name": "semaphore"
        },
        "port": "",
        "bugsnag_key": "",
        "tmp_path": "/opt/semaphore",
        "cookie_hash": "pi2crua6ZVIrUvbLAj8BLldw1uANvJwRWaS7pFCWmRw=",
        "cookie_encryption": "Sj1f/Z2XT592b/jeHue3K3pVBd9icJeubh1ixh58+yQ="
 }

 > Is this correct? (yes/no): yes
 Running: mkdir -p /opt/semaphore..
 Configuration written to /opt/semaphore/semaphore_config.json..
 Pinging database..

 Running DB Migrations..
Creating migrations table
Executing migration v0.0.0 (at 2016-09-25 00:09:01.221793369 +0900 JST)...
 [11/11]
Executing migration v1.0.0 (at 2016-09-25 00:09:01.53705146 +0900 JST)...
 [7/7]
Executing migration v1.1.0 (at 2016-09-25 00:09:01.858125307 +0900 JST)...
 [1/1]
Executing migration v1.2.0 (at 2016-09-25 00:09:01.930146649 +0900 JST)...
 [1/1]
Executing migration v1.3.0 (at 2016-09-25 00:09:01.963162965 +0900 JST)...
 [3/3]
Executing migration v1.4.0 (at 2016-09-25 00:09:02.123194544 +0900 JST)...
 [2/2]
Executing migration v1.5.0 (at 2016-09-25 00:09:02.238933666 +0900 JST)...
 [1/1]
Executing migration v0.1.0 (at 2016-09-25 00:09:02.268897766 +0900 JST)...
 [6/6]
Executing migration v1.6.0 (at 2016-09-25 00:09:02.410236064 +0900 JST)...
 [4/4]
Executing migration v1.7.0 (at 2016-09-25 00:09:02.601744445 +0900 JST)...
 [1/1]


 > Username: semaphore
 > Email:
 > Your name: semaphore
 > Password: P@ssw0rd

 You are all setup semaphore!
 Re-launch this program pointing to the configuration file

./semaphore -config /opt/semaphore/semaphore_config.json

 To run as daemon:

nohup ./semaphore -config /opt/semaphore/semaphore_config.json &

 You can login with  or semaphore.

 

最後に、以下のコマンドでsemaphoreを起動する。

nohup ./semaphore -config /opt/semaphore/semaphore_config.json &

3.ブラウザからアクセスする

無事インストールが完了したら、「http://IPアドレス:3000」へブラウザからアクセスする。
ログインには、先ほど設定したIDorメールアドレスとパスワードを用いる。

20160925_001401000000

 

ログイン直後の画面。まだ何もない状態。
まずは右上の「Projects」からプロジェクトを作成する。

20160925_001538000000

 

プロジェクト作成後、対象のプロジェクトを選択して「Key Store」からキーの登録をする。
このキーは、Ansibleでのデプロイでもそうだが、git cloneでも使用されるようだ。

20160925_002113000000

 

Playbookを登録しているgitリポジトリがあれば、左メニューの「Playbook Repositories」から登録できる。
ここで指定した場所にあるPlaybookを指定してタスクをキックすることになる。

 

20160925_004959000000

 

Inventoryを登録する。
このインベントリでAnsible hostsの役割も兼ねているようだ。右側にある「edit inventory contents」を編集し、グループとホストの情報を入力する。

 

20160925_012627000000

20160925_012704000000

 

最後に、Task Templeteからタスクを作成する。
このとき、実行したいPlaybookを指定してやる。

20160925_013201000000

 

あとは、対象のタスクをrunするだけだ。

 

20160925_013732000000

 

無事、Playbookを適用できた。
対象サーバもGUIで手軽に設定できるし、Playbookもgitで手軽に取得できる。
ユーザごとの権限管理を気を付ければ手軽にチームでも使えるだろうし、かなりいいと思う。

 

Linuxのコマンドラインでパスワード管理ができるCUIのパスワードマネージャ「mdp」

$
0
0

CUIでパスワード管理ができるツールをいくつか見つけたので何個か紹介していたが、今回は「mdp」というパスワードマネージャを紹介する。

1.インストール

まずは、前提となるパッケージのインストールから。
以下のコマンドを実行する。

yum install ncurses-devel ncurses gnupg git gcc # CentOSなどRHEL系の場合
apt-get install libncursesw5-dev libncursesw5 gnupg git gcc # Debian/Ubuntu系の場合

 

次に、mdpのインストールを行う。

git clone https://github.com/tamentis/mdp/
cd mdp
./configure
make
sudo make install

 

これでインストールが完了した。

2.使ってみる

mdpのインストール完了後、まず以下のコマンドで初期設定を行う。
なお、GPGのカギについては、以前に似たツールとして「gpgpwd」というものを紹介したのだが、そちらにて作成したものを使いまわしている。

mkdir ~/.mdp
chmod 600 ~/.mdp/
cat <<EOF > ~/.mdp/config
set gpg_key_id "GPG鍵"
set editor "/usr/bin/vim"
EOF
source ~/.mdp/config

 

次に、mdpコマンドで鍵の登録を行う。

mdp add キーワード

 

追加すると、4つほどの候補としてランダムなパスワードを提示してくるので、使うか変更するかエディタで編集する。

20160924_145805000000

 

追加後は以下のコマンドでパスワードを確認できる。

mdp get キーワード

20160924_151114000000

 

うーん、ちょっと使いづらい気もするけど…
これではただの暗号化したテキストファイルをいじってるだけなのでは…

サイバーセキュリティテスト完全ガイド ~Kali Linuxによるペネトレーションテスト~ サイバーセキュリティテスト完全ガイド ~Kali Linuxによるペネトレーションテスト~

CentOS 7にNW機器のコンフィグバックアップ自動化ツール「rancid」をインストールする

$
0
0

NW機器のコンフィグ自動バックアップ方法について調べていたところ、「rancid」というツールが良さそうだったので少し触ってみることにした。
比較的いろんなベンダーのNW機器に対応しており、CVSを用いての世代管理もできるようで、情報も豊富にありそうだ。一応、ViewVCを入れることでWebからコンフィグを管理させることも可能なようだ。というわけで、今回はCentOS7にこのrancid + ViewVCをインストールしてみる。こちらを見る限り、yumやapt-getでもインストールできそうだが、今回はソースをダウンロードしてインストールを行うことにした。手順については、こちらを参考に進めている。

なお、事前に以下のコマンドでSELinuxの無効化、firewalldで必要となるポートの開放を行っている。

setenforce 0
sed -i.bak "/SELINUX/s/enforcing/disabled/g" /etc/selinux/config
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

 

1.前提パッケージのインストール

まずは、以下のコマンドで前提となるパッケージ類の導入をする。

yum -y update
yum -y install vim wget ftp telnet mariadb-server mariadb perl tcl expect gcc cvs rcs httpd autoconf php-common php-gd php-pear php-pecl-memcache php-mysql php-xml mod_ssl MySQL-python

 

ViewVCを利用するために入れたhttpdのサービス起動設定をする。

systemctl enable httpd
systemctl start httpd

 

次に、グループおよびユーザの作成をする。

groupadd netadm
useradd -g netadm -c "Networking Backups User" -d /home/rancid rancid

 

2.rancidのインストール・初期設定を行う

次に、rancidのインストールを行う。
まず、以下のコマンドでソースのダウンロードを行う。
なお、最新版のパッケージについてはこちらからURLを確認しておくこと。

mkdir /home/rancid/tar
cd !$
wget ftp://ftp.shrubbery.net/pub/rancid/rancid-3.5.1.tar.gz

 

ダウンロード完了後、以下のコマンドでコンパイルしインストールを行う。

tar xzvf rancid-*.tar.gz
cd rancid-*/
./configure --prefix=/usr/local/rancid
make && make install

 

これで、rancidのインストールが完了した。
次に、バックアップ対象の機器情報やrancidのコンフィグについて、テンプレートなどをコピーしてファイルの作成を行う。

cp cloginrc.sample /home/rancid/.cloginrc
chmod 0600 /home/rancid/.cloginrc
chown -R rancid:netadm /home/rancid/.cloginrc
chown -R rancid:netadm /usr/local/rancid/
chmod 775 /usr/local/rancid/

 

設定ファイルを編集して、任意のグループ名を設定する。

sed -i.bk '/# list of rancid groups/,/^#LIST_OF_GROUPS/cLIST_OF_GROUPS="任意のグループ名(今回は「NetworkDevice」にしている)"' /usr/local/rancid/etc/rancid.conf

 

rancidユーザに切り替えてCVSの設定を行う。

su - rancid
/usr/local/rancid/bin/rancid-cvs
[root@BS-PUB-WEBTOOL-TEST01 rancid-3.5.1]# su - rancid
[rancid@BS-PUB-WEBTOOL-TEST01 ~]$ /usr/local/rancid/bin/rancid-cvs

No conflicts created by this import

cvs checkout: Updating Router
Directory /usr/local/rancid/var/CVS/Router/configs added to the repository
cvs commit: Examining configs
cvs add: scheduling file `router.db' for addition
cvs add: use 'cvs commit' to add this file permanently
RCS file: /usr/local/rancid/var/CVS/Router/router.db,v
done
Checking in router.db;
/usr/local/rancid/var/CVS/Router/router.db,v <-- router.db
initial revision: 1.1
done

No conflicts created by this import

cvs checkout: Updating And
Directory /usr/local/rancid/var/CVS/And/configs added to the repository
cvs commit: Examining configs
cvs add: scheduling file `router.db' for addition
cvs add: use 'cvs commit' to add this file permanently
RCS file: /usr/local/rancid/var/CVS/And/router.db,v
done
Checking in router.db;
/usr/local/rancid/var/CVS/And/router.db,v <-- router.db
initial revision: 1.1
done

No conflicts created by this import

cvs checkout: Updating Switches
Directory /usr/local/rancid/var/CVS/Switches/configs added to the repository
cvs commit: Examining configs
cvs add: scheduling file `router.db' for addition
cvs add: use 'cvs commit' to add this file permanently
RCS file: /usr/local/rancid/var/CVS/Switches/router.db,v
done
Checking in router.db;
/usr/local/rancid/var/CVS/Switches/router.db,v <-- router.db
initial revision: 1.1
done

3.ViewVCのインストール・初期設定を行う

3-1.ViewVCのインストール

次に、Webでコンフィグファイルを管理できるよう、ViewVCのインストールを行う。
ViewVCの最新版については、こちらからバージョンを確認してURLを適宜書き換えてもらいたい。

cd /home/rancid/tar/
wget http://viewvc.org/downloads/viewvc-1.1.25.tar.gz
tar xzvf viewvc-*.tar.gz
cd viewvc-*/
./viewvc-install
[root@BS-PUB-WEBTOOL-TEST01 viewvc-1.1.25]# ./viewvc-install
This is the ViewVC 1.1.25 installer.

It will allow you to choose the install path for ViewVC.  You will now
be asked some installation questions.  Defaults are given in square brackets.
Just hit [Enter] if a default is okay.

Installation path [/usr/local/viewvc-1.1.25]:

DESTDIR path (generally only used by package maintainers) []:

Installing ViewVC to /usr/local/viewvc-1.1.25:
   created   /usr/local/viewvc-1.1.25/bin/cgi/
   installed /usr/local/viewvc-1.1.25/bin/cgi/viewvc.cgi
   ....

 

これで、ViewVCのインストールが完了した。
次に、設定ファイルを編集してrancidのCVSを参照するように定義する。

sed -e '/^#root_parents =/croot_parents = /usr/local/rancid/var/CVS : cvs' \
    -e '/^#rcs_dir/crcs_dir = /usr/local/bin' \
    -e '/^#use_rcsparse/cuse_rcsparse = 1' \
    -i.bk /usr/local/viewvc-*/viewvc.conf

 

3-2.httpdまわりの設定を変更する

次に、以下のコマンドでcgiファイルをコピーする。

cp /usr/local/viewvc-*/bin/cgi/*.cgi /var/www/cgi-bin
chmod +x /var/www/cgi-bin/*.cgi
chown apache:apache /var/www/cgi-bin/*.cgi

 

httpdの設定ファイルを編集する。

cat <<EOF >> /etc/httpd/conf.d/rancid.conf
<VirtualHost *:80>
        DocumentRoot /var/www
        ScriptAlias /cgi-bin/ "/var/www/cgi-bin"
        ScriptAlias /viewvc /var/www/cgi-bin/viewvc.cgi
        ScriptAlias /query /var/www/cgi-bin/query.cgi
<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Order allow,deny
    Allow from all
</Directory>
</VirtualHost>
EOF

 

3-3.MariaDBまわりの設定を変更する

ViewVCで利用するMariaDBに関する設定を行う。
まず、以下のコマンドでサービスの起動設定と最低限のセキュリティ設定を行う。

systemctl enable mariadb
systemctl start mariadb
mysql_secure_installation

 

次に、以下のコマンドでViewVCで利用するためのユーザ(「VIEWVC」およびリードのみの権限を持つ「VIEWVCRO」ユーザ)の作成を行う。

mysql -u root -p -e "CREATE USER 'VIEWVC'@'localhost' IDENTIFIED BY 'パスワード';
GRANT ALL PRIVILEGES ON *.* TO 'VIEWVC'@'localhost' WITH GRANT OPTION;
CREATE USER 'VIEWVCRO'@'localhost' IDENTIFIED BY 'パスワード';
GRANT SELECT ON ViewVC.* TO 'VIEWVCRO'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
quit"

 

データベースの作成スクリプトを実行する。

/usr/local/viewvc-*/bin/make-database
[root@BS-PUB-WEBTOOL-TEST01 viewvc-1.1.25]# /usr/local/viewvc-*/bin/make-database
MySQL Hostname (leave blank for default):
MySQL Port (leave blank for default):
MySQL User: VIEWVC
MySQL Password: P@ssw0rd
ViewVC Database Name [default: ViewVC]:

Database created successfully.  Don't forget to configure the
[cvsdb] section of your viewvc.conf file.

 

ViewVCの設定ファイルを編集し、DBへの接続設定を行う。

sed -e '/^#host/chost = localhost' \
    -e '/^#port/cport = 3306' \
    -e '/^#database_name/cdatabase_name = ViewVC' \
    -e '/^#user/cuser = VIEWVC' \
    -e '/^#passwd/cpasswd = パスワード' \
    -i.bk /usr/local/viewvc-*/viewvc.conf

 

最後に、以下のコマンドを実行してデータベースをビルド、Apacheを再起動する。

/usr/local/viewvc-*/bin/cvsdbadmin rebuild /usr/local/rancid/var/CVS/CVSROOT/
systemctl restart httpd

 

あとは、ブラウザから「http://サーバのIPアドレス/viewvc」にアクセスすればよい。

20160924_193227000000

 

これで、ViewVCの設定が完了した。

 

4.各機器の設定・バックアップの実行

4-1.接続情報の設定ファイルを編集

接続情報について設定されているファイル「/home/rancid/.cloginrc」に、各NW機器の接続情報についてを記述する。
接続する各機器のIDやパスワードはワイルドカードでの指定もできる(ホスト名の部分一致で同じユーザIDやパスワードを指定できる)。

telnetおよびsshで、以下のように設定をする。
なお、事前にバックアップをとっておき、もともと入っていた設定については削除をすること。

●telnet

add password IPアドレスorホスト名(ワイルドカードあり) ログインパスワード Enableパスワード

 

●ssh

add user IPアドレスorホスト名(ワイルドカードあり) ユーザ名
add method IPアドレスorホスト名(ワイルドカードあり) ssh
add password IPアドレスorホスト名(ワイルドカードあり) ログインパスワード Enableパスワード

 

4-2.機器リストの設定

次に、各機器のリストを設定する。
リストは、「/usr/local/rancid/var/」配下にある「Router」および「Switches」フォルダにあるrouter.dbがそれに当たる。

機器の種類に応じて、それぞれのファイルに追加する。設定できるデバイスタイプについては、こちらを参照するとよいだろう。
(:(コロン)ではなく;(セミコロン)である)

ホスト名;デバイスタイプ(ベンダー);up

 

4-3.接続テストを行う

実際に、接続テストを行ってみよう。
以下のコマンドを実行する。

sudo -u rancid /usr/local/rancid/bin/clogin 対象ホスト
[root@BS-PUB-WEBTOOL-TEST01 viewvc-1.1.25]# sudo -u rancid /usr/local/rancid/bin/clogin 172.2X.XXX.XXX
172.2X.XXX.XXX
spawn telnet 172.2X.XXX.XXX
Trying 172.2X.XXX.XXX...
Connected to 172.2X.XXX.XXX.
Escape character is '^]'.


User Access Verification

Password:
BN-L2-T01>enable
Password:
BN-L2-T01#

 

無事ログインできるようであれば接続設定はちゃんとできている。

 

4-4.バックアップを取得してみる

ログインが無事行えたら、ちゃんとバックアップが行えるか確認をする。
以下のコマンドを実行する。

sudo -u rancid /usr/local/rancid/bin/rancid-run

 

無事に行えた場合は、ログに正常終了として残っている。
ログは、「/usr/local/rancid/var/logs」配下に出力されている。
“All routers sucessfully completed.”の一文があれば正常に終了している。

バックアップしたコンフィグファイルについては、「/usr/local/rancid/var/カテゴリ名/configs/」配下に配置されている。
ViewVCからも見れるので、そちらでも確認可能だ。

20160924_225713000000

 

定期的にバックアップを取得する場合は、先ほど実行したコマンドをcronで回してやるとよいだろう。

インフラ/ネットワークエンジニアのためのネットワーク技術&設計入門 インフラ/ネットワークエンジニアのためのネットワーク技術&設計入門

Graylogを2.0系から2.1系にアップグレードする(Ubuntu Server 16.04)

$
0
0

Graylog Serverのバージョンを2.0系から2.1系にアップグレードした際の手順について備忘として残しておく。
なお、サーバのOSはUbuntu Server 16.04を用いている。

まず、以下のコマンドで2.1系のリポジトリをインストールする。

wget https://packages.graylog2.org/repo/packages/graylog-2.1-repository_latest.deb
sudo dpkg -i graylog-2.1-repository_latest.deb

次に、パッケージ情報の更新をしてアップデートする。

sudo apt-get update
sudo apt-get install graylog-server

 

再起動後は、graylog-server(およびコレクターを使っているならばエージェント側のGraylog Collector Sidecar)の再起動を行うこと。
(サーバは再起動しないとCollectorなどの項目が見えない、Collectorは再起動しないとFailedになるので注意。)

サーバ/インフラエンジニア養成読本 ログ収集〜可視化編 [現場主導のデータ分析環境を構築!] Software Design plus サーバ/インフラエンジニア養成読本 ログ収集〜可視化編 [現場主導のデータ分析環境を構築!] Software Design plus

Graylog 2.1でGraylog Collector Sidecarで転送するログの暗号化を行う

$
0
0

Graylogで、転送するログの暗号化をしてパケットキャプチャをされても見れないようにする。
普通にただ設定すると、暗号化せずに転送されてしまうため、GELF形式で送ってもこんな感じで中身を見れてしまう。

blacknon@BS-PUB-GRAYLOG:~$ sudo tcpdump -i ens19 port 12201 -X
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens19, link-type EN10MB (Ethernet), capture size 262144 bytes
20:46:04.490369 IP 172.XXX.XXX.120.38600 > 172.XXX.XXX.187.12201: Flags [P.], seq 3796279135:3796279539, ack 1914087123, win 229, options [nop,nop,TS val 211603720 ecr 104066410], length 404
 0x0000: 4500 01c8 ea5f 4000 4006 2d74 ac14 6478 E...._@.@.-t..dx
 0x0010: ac14 64bb 96c8 2fa9 e246 9f5f 7216 a6d3 ..d.../..F._r...
 0x0020: 8018 00e5 e70e 0000 0101 080a 0c9c d108 ................
 0x0030: 0633 ed6a 7b22 7665 7273 696f 6e22 3a22 .3.j{"version":"
 0x0040: 312e 3122 2c22 5f45 7665 6e74 5265 6365 1.1","_EventRece
 0x0050: 6976 6564 5469 6d65 223a 2232 3031 362d ivedTime":"2016-
 0x0060: 3039 2d32 3520 3230 3a34 363a 3034 222c 09-25.20:46:04",
 0x0070: 225f 536f 7572 6365 4d6f 6475 6c65 4e61 "_SourceModuleNa
 0x0080: 6d65 223a 2235 3765 3230 6134 3666 3131 me":"57e20a46f11
 0x0090: 3135 3332 3063 3166 3636 3732 3122 2c22 15320c1f66721","
 0x00a0: 5f53 6f75 7263 654d 6f64 756c 6554 7970 _SourceModuleTyp
 0x00b0: 6522 3a22 696d 5f66 696c 6522 2c22 5f46 e":"im_file","_F
 0x00c0: 696c 654e 616d 6522 3a22 2f76 6172 2f6c ileName":"/var/l
 0x00d0: 6f67 2f73 7973 6c6f 6722 2c22 5f67 6c32 og/syslog","_gl2
 0x00e0: 5f73 6f75 7263 655f 636f 6c6c 6563 746f _source_collecto
 0x00f0: 7222 3a22 3337 3033 3232 3634 2d31 3564 r":"37032264-15d
 0x0100: 392d 3432 3863 2d61 3931 642d 3563 3437 9-428c-a91d-5c47
 0x0110: 6532 3833 6432 6233 222c 2268 6f73 7422 e283d2b3","host"
 0x0120: 3a22 4253 2d50 5542 2d55 4255 4e54 552d :"BS-PUB-UBUNTU-
 0x0130: 3031 2e42 4c41 434b 4e4f 4e2e 4c4f 4341 01.BLACKNON.LOCA
 0x0140: 4c22 2c22 7368 6f72 745f 6d65 7373 6167 L","short_messag
 0x0150: 6522 3a22 5365 7020 3235 2032 303a 3436 e":"Sep.25.20:46
 0x0160: 3a30 3020 4253 2d50 5542 2d55 4255 4e54 :00.BS-PUB-UBUNT
 0x0170: 552d 3031 2062 6c61 636b 6e6f 6e3a 206d U-01.blacknon:.m
 0x0180: 6573 7361 6765 2072 6570 6561 7465 6420 essage.repeated.
 0x0190: 3220 7469 6d65 733a 205b 2074 6573 7431 2.times:.[.test1
 0x01a0: 3233 345d 222c 2274 696d 6573 7461 6d70 234]","timestamp
 0x01b0: 223a 3134 3734 3830 3339 3634 2c22 6c65 ":1474803964,"le
 0x01c0: 7665 6c22 3a36 7d00 vel":6}.

 

これだとちょっとありがたくないので、ログを転送する際に暗号化してパケットキャプチャをされても見れないようにする。
なお、今回はサーバがUbuntu Server 16.04を用いたGraylogServer 2.1、クライアント側がGraylog Collector Sidecar + NXLogでログ転送(GELF形式)をしている環境においてのログ転送暗号化とする。
※Graylog Server 2.0系だとCollector SidecarのSSLが対応できてないので注意。

1.鍵ファイルの作成

まず、ログの通信を暗号化するための鍵ファイルを作成する。以下、rootユーザで処理を行う。
設置先は、「/opt/cert」というディレクトリにする。

mkdir -m 600 -p /opt/cert/
cd !$

 

鍵ファイルを作成するためにコマンドをインストールする。

sudo apt-get install gnutls-bin

 

鍵ファイルを作成する。

certtool --generate-privkey --outfile ca-key.pem --bits 2048
root@BS-PUB-GRAYLOG:/opt/cert# certtool --generate-privkey --outfile ca-key.pem --bits 2048
** Note: You may use '--sec-param Medium' instead of '--bits 2048'
Generating a 2048 bit RSA private key...

 

CA証明書を作成する。

certtool --generate-self-signed --load-privkey ca-key.pem --outfile ca.pem
root@BS-PUB-GRAYLOG:/opt/cert# certtool --generate-self-signed --load-privkey ca-key.pem --outfile ca.pem
Generating a self signed certificate...
Please enter the details of the certificate's distinguished name. Just press enter to ignore a field.
Common name: Graylog-GELF
UID:
Organizational unit name:
Organization name:
Locality name: Tokyo
State or province name: Tachikawa
Country name (2 chars): JP
Enter the subject's domain component (DC):
This field should not be used in new certificates.
E-mail:
Enter the certificate's serial number in decimal (default: 6334261173835579667):


Activation/Expiration time.
The certificate will expire in (days): 3650


Extensions.
Does the certificate belong to an authority? (y/N): y
Path length constraint (decimal, -1 for no constraint):
Is this a TLS web client certificate? (y/N): N
Will the certificate be used for IPsec IKE operations? (y/N): N
Is this a TLS web server certificate? (y/N): N
Enter a dnsName of the subject of the certificate:
Enter a URI of the subject of the certificate:
Enter the IP address of the subject of the certificate:
Enter the e-mail of the subject of the certificate:
Will the certificate be used to sign other certificates? (y/N): y
Will the certificate be used to sign CRLs? (y/N): N
Will the certificate be used to sign code? (y/N): N
Will the certificate be used to sign OCSP requests? (y/N): N
Will the certificate be used for time stamping? (y/N): N
Enter the URI of the CRL distribution point:
X.509 Certificate Information:
        Version: 3

        ~省略~

Is the above information ok? (y/N): y


Signing certificate...

サーバ証明書を作成する。

certtool --generate-privkey --outfile key.pem --bits 2048
certtool --generate-request --load-privkey key.pem --outfile request.pem
certtool --generate-certificate --load-request request.pem --load-ca-certificate ca.pem --load-ca-privkey ca-key.pem --outfile cert.pem
root@BS-PUB-GRAYLOG:/opt/cert# certtool --generate-privkey --outfile key.pem --bits 2048
** Note: You may use '--sec-param Medium' instead of '--bits 2048'
Generating a 2048 bit RSA private key...
root@BS-PUB-GRAYLOG:/opt/cert# certtool --generate-request --load-privkey key.pem --outfile request.pem
Generating a PKCS #10 certificate request...
Common name: Graylog-GELF
Organizational unit name:
Organization name:
Locality name: Tokyo
State or province name: Tachikawa
Country name (2 chars): JP
Enter the subject's domain component (DC):
UID:
Enter a dnsName of the subject of the certificate:
Enter a URI of the subject of the certificate:
Enter the IP address of the subject of the certificate:
Enter the e-mail of the subject of the certificate:
Enter a challenge password:
Does the certificate belong to an authority? (y/N): N
Will the certificate be used for signing (DHE ciphersuites)? (Y/n): N
Will the certificate be used for encryption (RSA ciphersuites)? (Y/n): N
Is this a TLS web client certificate? (y/N): y
Is this a TLS web server certificate? (y/N): y
Self signature: verified

root@BS-PUB-GRAYLOG:/opt/cert# certtool --generate-certificate --load-request request.pem --load-ca-certificate ca.pem --load-ca-privkey ca-key.pem --outfile cert.pem
Generating a signed certificate...
Enter the certificate's serial number in decimal (default: 6334263909407233153):


Activation/Expiration time.
The certificate will expire in (days): 3650

Expiration time: Wed Sep 23 22:39:09 2026
CA expiration time: Wed Sep 23 22:28:31 2026
Warning: The time set exceeds the CA's expiration time
Is it ok to proceed? (y/N): y


Extensions.
Do you want to honour the extensions from the request? (y/N): N
Does the certificate belong to an authority? (y/N): N
Is this a TLS web client certificate? (y/N): y
Will the certificate be used for IPsec IKE operations? (y/N): N
Is this a TLS web server certificate? (y/N): y
Enter a dnsName of the subject of the certificate:
Enter a URI of the subject of the certificate:
Enter the IP address of the subject of the certificate:
Will the certificate be used for signing (DHE ciphersuites)? (Y/n): N
Will the certificate be used for encryption (RSA ciphersuites)? (Y/n): N
X.509 Certificate Information:
        Version: 3

        ~省略~

Is the above information ok? (y/N): y


Signing certificate...

 

フォルダの所有者をgraylogに変更する。

chown graylog. -R /opt/cert

 

フォルダの所有者を変えたら、作成した鍵ファイルについてクライアント側に転送する。
具体的には、同じPATHに以下の3つのファイルを転送する。

  • /opt/cert/ca.pem
  • /opt/cert/cert.pem
  • /opt/cert/key.pem

 

2.Graylog Collector Sidecar側の設定変更

次に、Graylog Collector Sidecar(クライアント)側に対して暗号化を行うよう設定をする。
GraylogServerの管理画面にアクセスし、[System] > [Collector] > [Manage Configuration]を開き、編集対象のConfigurationを開く。

20160925_234901000000

 

画面下のタブで[NXLog]を選択し、設定変更をするOutputのEditボタンをクリックする。

20160925_235040000000

 

Typeを「[NXLog] GELF TCP/TLS output」に変更し、CAやCert、KeyファイルのPATHを指定する。
設定を確認して問題なければそのまま保存する。

20160926_001203000000

 

  • CA File … /opt/cert/ca.pem
  • Cert File … /ope/cert/cert.pem
  • Key File … /opt/cert/key.pem

 

これで、Collector側の設定は完了。

3.GraylogのInput側で設定を変更する

最後に、GraylogのInput側で設定の変更をする。
[System] > [Input]から対象のInputを編集する。

20160926_001435000000

  • TLS cert file … /opt/cert/cert.pem
  • TLS private key file … /opt/cert/key.pem
  • Enable TLS … チェック
  • TLS Client Auth Trusted Certs … /opt/cert/ca.pem
  • TLS client authentication … disabled

 

これで、転送されるログの暗号化ができた。

blacknon@BS-PUB-GRAYLOG:~$ sudo tcpdump -i ens19 port 12201 -X
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens19, link-type EN10MB (Ethernet), capture size 262144 bytes
00:18:21.935069 IP 172.XXX.XXX.120.47086 > 172.XXX.XXX.187.12201: Flags [P.], seq 2416711232:2416711637, ack 2081285395, win 240, options [nop,nop,TS val 214788092 ecr 107249662], length 405
        0x0000:  4500 01c9 2b3d 4000 4006 ec95 ac14 6478  E...+=@.@.....dx
        0x0010:  ac14 64bb b7ee 2fa9 900c 1640 7c0d e513  ..d.../....@|...
        0x0020:  8018 00f0 19af 0000 0101 080a 0ccd 67fc  ..............g.
        0x0030:  0664 7ffe 1703 0301 905d 391b a608 4b15  .d.......]9...K.
        0x0040:  574a f85e d275 422d bf96 a73a b73d 841b  WJ.^.uB-...:.=..
        0x0050:  a825 5dcb e563 d945 eae1 971e c64c d612  .%]..c.E.....L..
        0x0060:  aee3 b915 d1f5 4573 1ebf f6c5 8264 b439  ......Es.....d.9
        0x0070:  713e 39ec 29ae 1a6b feee 4e98 672c 086e  q>9.)..k..N.g,.n
        0x0080:  38ba 411a 2fea 5566 9013 6d35 8ce9 3d59  8.A./.Uf..m5..=Y
        0x0090:  375f 1bbd e05a f9cf 8e40 4849 fd15 2dec  7_...Z...@HI..-.
        0x00a0:  c804 f4c8 45a7 8e0b faa4 9391 6a96 8b8a  ....E.......j...
        0x00b0:  b6a6 b5f2 de59 21e8 06ab c87b 0b1c 75cc  .....Y!....{..u.
        0x00c0:  b2cf 0503 e909 4a98 bcc0 2452 1902 f823  ......J...$R...#
        0x00d0:  f111 693d 5701 a335 14f3 fc15 6873 0481  ..i=W..5....hs..
        0x00e0:  6544 9024 894c 5d54 9ea2 59e0 3407 89a1  eD.$.L]T..Y.4...
        0x00f0:  5d6e c609 d4e6 6023 8d40 b685 44d9 6bfa  ]n....`#.@..D.k.
        0x0100:  73a2 5a7a d03f 19a5 ef08 7eba c191 95ef  s.Zz.?....~.....
        0x0110:  2850 0f2a 6f7b bec9 e4df 7cf5 9313 2ba4  (P.*o{....|...+.
        0x0120:  6153 707d 5d65 bc48 bb3b aa3c 9e9d 2126  aSp}]e.H.;.<..!& 0x0130: 36ae 69c2 6585 5899 6ef7 d68a 6e33 ff8a 6.i.e.X.n...n3.. 0x0140: 8e92 32ee fe99 bc2f 2f31 d5da 7390 7de5 ..2....//1..s.}. 0x0150: d44c 5037 574a 6185 47aa 7148 838f 6b71 .LP7WJa.G.qH..kq 0x0160: 9713 868a 38b1 9bba 84bd 25cd 4e50 5850 ....8.....%.NPXP 0x0170: f2fc 44b5 1689 e8f0 3d98 c35e 5f78 e453 ..D.....=..^_x.S 0x0180: 94e5 4c43 5f73 929a e984 19e8 d01b f28a ..LC_s.......... 0x0190: 08cf c49f 9023 d8cb 0edc e502 f581 ec11 .....#.......... 0x01a0: 0593 f280 f664 8dce fc75 d59a 6397 0633 .....d...u..c..3 0x01b0: ff79 fd97 acd1 e00e 3509 9fbe 2db2 b1ea .y......5...-... 0x01c0: 7dcb 6311 c8b2 e7b3 b8 }.c...... 00:18:21.935095 IP 172.XXX.XXX.187.12201 > 172.XXX.XXX.120.47086: Flags [.], ack 405, win 49380, options [nop,nop,TS val 107252162 ecr 214788092], length 0
        0x0000:  4500 0034 b0cb 4000 4006 689c ac14 64bb  E..4..@.@.h...d.
        0x0010:  ac14 6478 2fa9 b7ee 7c0d e513 900c 17d5  ..dx/...|.......
        0x0020:  8010 c0e4 2183 0000 0101 080a 0664 89c2  ....!........d..
        0x0030:  0ccd 67fc                                ..g.

 

Graylogの画面上でも対象のログは確認できた。

サーバー管理者のためのイベントログ運用の基本 サーバー管理者のためのイベントログ運用の基本

仮想マシン(CentOS 7)でディスク拡張を行ったときのLVM拡張対応(PV領域の拡張)

$
0
0

仮想マシン(今回の場合CentOS 7)でディスクの拡張を行った際、特にISOブートとかさせることもなくLVM領域の拡張を行う必要があったので、備忘として残しておく。
(特に目新しいことをしたわけではなく、普通にやることやっただけなのだけど…よく忘れるので)

1.ディスクの拡張

まず、対象の仮想マシンのディスクを拡張する。ここについては各仮想基盤ごとに違うと思う。
ちなみに個人的にProxmoxVEを使っているので、対象の仮想マシンを選択して[ハードウェア]タブを開き、対象のハードディスクを選択して[Resize Disk]、ディスクに追加する容量を入力するだけだ。

20160927_070552000000

 

ディスクの拡張後、一度OSを再起動させる。

2.パーティションの拡張

次に、fdiskを使ってパーティションの拡張を行う。
やってることは簡単で、一度既存のパーティションを削除し、その次に各パーティションを作り直しさせているだけだ。

[root@BS-PUB-WEBTOOL-TEST01 ~]# df -h
ファイルシス            サイズ  使用  残り 使用% マウント位置
/dev/mapper/centos-root    36G  1.4G   35G    4% /
devtmpfs                  1.9G     0  1.9G    0% /dev
tmpfs                     1.9G     0  1.9G    0% /dev/shm
tmpfs                     1.9G  8.4M  1.9G    1% /run
tmpfs                     1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1                 497M  167M  330M   34% /boot
tmpfs                     380M     0  380M    0% /run/user/0
[root@BS-PUB-WEBTOOL-TEST01 ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


コマンド (m でヘルプ): p

Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
Disk label type: dos
ディスク識別子: 0x000cf688

デバイス ブート      始点        終点     ブロック   Id  システム
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    83886079    41430016   8e  Linux LVM

コマンド (m でヘルプ): d
パーティション番号 (1,2, default 2): 2
Partition 2 is deleted

コマンド (m でヘルプ): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
パーティション番号 (2-4, default 2):
最初 sector (1026048-125829119, 初期値 1026048):
初期値 1026048 を使います
Last sector, +sectors or +size{K,M,G} (1026048-125829119, 初期値 125829119):
初期値 125829119 を使います
Partition 2 of type Linux and of size 59.5 GiB is set

コマンド (m でヘルプ): w
パーティションテーブルは変更されました!

ioctl() を呼び出してパーティションテーブルを再読込みします。

WARNING: Re-reading the partition table failed with error 16: デバイスもしくはリソースがビジー 状態です.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
ディスクを同期しています。

 

これで、パーティションの変更処理が行えた。
といっても現時点ではマウントしている領域なので、一度再起動してから反映されることになる。
OSを再起動しよう。

 

3.LVM領域の拡張をする

再起動完了後、LVM領域の拡張をさせる。
まず、pvresizeコマンドでPhysical volumeの領域を変更する。

pvresize /dev/sda2
[root@BS-PUB-WEBTOOL-TEST01 ~]# pvresize /dev/sda2
  Physical volume "/dev/sda2" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized

 

今回の場合、Phycial Volumeをそのまま拡張したので、Volume Groupもそのまま拡張されている。
続いて、以下のコマンドで拡張されたVolume Groupの領域をすべてLogical Volumeに割り当てる。

lvextend -l +100%FREE /dev/centos/root
[root@BS-PUB-WEBTOOL-TEST01 ~]# lvextend -l +100%FREE /dev/centos/root
  Size of logical volume centos/root changed from 35.59 GiB (9111 extents) to 55.63 GiB (14242 extents).
  Logical volume root successfully resized.

 

最後に、ファイルシステムのリサイズをさせる。
CentOS 7の場合、「resize2fs」ではなく「xfs_growfs」を使う。

xfs_growfs /dev/centos/root
[root@BS-PUB-WEBTOOL-TEST01 ~]# # resize2fsだと以下のようなエラーがでる
[root@BS-PUB-WEBTOOL-TEST01 ~]# resize2fs /dev/centos/root
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/centos/root
Couldn't find valid filesystem superblock.
[root@BS-PUB-WEBTOOL-TEST01 ~]#
[root@BS-PUB-WEBTOOL-TEST01 ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=2332416 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=9329664, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=4555, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 9329664 to 14583808
[root@BS-PUB-WEBTOOL-TEST01 ~]# df -h
ファイルシス            サイズ  使用  残り 使用% マウント位置
/dev/mapper/centos-root    56G  1.4G   55G    3% /
devtmpfs                  1.9G     0  1.9G    0% /dev
tmpfs                     1.9G     0  1.9G    0% /dev/shm
tmpfs                     1.9G  8.3M  1.9G    1% /run
tmpfs                     1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1                 497M  167M  330M   34% /boot
tmpfs                     380M     0  380M    0% /run/user/0

 

これで処理は完了。

[改訂新版]プロのためのLinuxシステム構築・運用技術 (Software Design plus) [改訂新版]プロのためのLinuxシステム構築・運用技術 (Software Design plus)

sipcalcコマンドによるIPアドレスの計算

$
0
0

以前、コンソール上でのIPアドレスの計算方法について書いたけど、(名前からして)その際に紹介したipcalcの強化版?的なsipcalcというコマンドを見かけたので紹介する。

1.インストール

インストールだが、以下のコマンドで行える。

yum install -y epel-release;yum install sipcalc # CentOSなどRHEL系(CentOS 7にはまだパッケージが無い?)
sudo apt install sipcalc # Debian/Ubuntu系
brew install sipcalc # MacOS X

 

これでインストールができる。

2.コマンドを実行する

sipcalcは、基本的には以下のように実行して使用する。

sipcalc IPアドレス
blacknon@BS-PUB-UBUNTU-01:~$ sipcalc 192.168.0.100/26
-[ipv4 : 192.168.0.100/26] - 0

[CIDR]
Host address            - 192.168.0.100
Host address (decimal)  - 3232235620
Host address (hex)      - C0A80064
Network address         - 192.168.0.64
Network mask            - 255.255.255.192
Network mask (bits)     - 26
Network mask (hex)      - FFFFFFC0
Broadcast address       - 192.168.0.127
Cisco wildcard          - 0.0.0.63
Addresses in network    - 64
Network range           - 192.168.0.64 - 192.168.0.127
Usable range            - 192.168.0.65 - 192.168.0.126

-
blacknon@BS-PUB-UBUNTU-01:~$ sipcalc 192.168.0.100 255.255.255.240
-[ipv4 : 192.168.0.100 255.255.255.240] - 0

[CIDR]
Host address            - 192.168.0.100
Host address (decimal)  - 3232235620
Host address (hex)      - C0A80064
Network address         - 192.168.0.96
Network mask            - 255.255.255.240
Network mask (bits)     - 28
Network mask (hex)      - FFFFFFF0
Broadcast address       - 192.168.0.111
Cisco wildcard          - 0.0.0.15
Addresses in network    - 16
Network range           - 192.168.0.96 - 192.168.0.111
Usable range            - 192.168.0.97 - 192.168.0.110

-

 

インターフェイスを指定して、そこからIPアドレスの計算をさせることも可能だ。

sipcalc インターフェイス名
blacknon@BS-PUB-UBUNTU-01:~$ sipcalc eth0
-[int-ipv4 : eth0] - 0

[CIDR]
Host address            - 172.XXX.XXX.120
Host address (decimal)  - 2887516280
Host address (hex)      - AC1C0078
Network address         - 172.XXX.XXX.0
Network mask            - 255.255.255.0
Network mask (bits)     - 24
Network mask (hex)      - FFFFFF00
Broadcast address       - 172.28.0.255
Cisco wildcard          - 0.0.0.255
Addresses in network    - 256
Network range           - 172.XXX.XXX.0 - 172.XXX.XXX.255
Usable range            - 172.XXX.XXX.1 - 172.XXX.XXX.254

-

 

マスタリングTCP/IP 入門編 第5版 マスタリングTCP/IP 入門編 第5版
Viewing all 743 articles
Browse latest View live