HHeLiBeXの日記 正道編

日々の記憶の記録とメモ‥

hostnameの変更

CentOSにおけるホスト名、永続的に変更しようと思うと、バージョンによってやり方が異なるので簡単にまとめておくメモ。

CentOS 5/6

ホスト名は「/etc/sysconfig/network」ファイルに書いてあるので、hostnameコマンドで変更しても永続的には変更されません。

# vi /etc/sysconfig/network
HOSTNAME=<new-hostname>
# sync;sync;sync;shutdown -r now

「service network restarrt」で反映されると書いてあるサイトを山ほど見つけたのだけど、ダメだよね?その認識で合ってるよね?

中には「/etc/hosts」も編集すると書いてあるサイトもあって、なんかもう‥

CentOS 7/8

hostnamectlコマンドを使います。

# hostnamectl set-hostname <new-hostname>

これで即時反映されます。

logwatchをアンインストールせずにメール通知を止める

logwatchを使ってログの集計情報をメールで飛ばすということをやってきたけど、毎日毎日何通も来るのでだんだんウザくなって読まなくなってしまった。

そんなわけで、アンインストールするのもアレだったので、とりあえずメール通知を止めようということになった。

まずCentOS 6の環境で設定。

[hhelibex@centos6 ~]$ logwatch --version
Logwatch 7.3.6 (released 05/19/07)
[hhelibex@centos6 ~]$ cat /etc/logwatch/conf/logwatch.conf
# Local configuration options go here (defaults are in /usr/share/logwatch/default.conf/logwatch.conf)
Detail = High
Archives = Yes
Save = /tmp/logwatch
[hhelibex@centos6 ~]$ 

これで様子を見てみると、無事に指定したファイルに結果が保存された。よしよし。

次はCentOS 7の環境。

[hhelibex@centos7 ~]$ logwatch --version
Logwatch 7.4.0 (released 03/01/11)
[hhelibex@centos7 ~]$ 

・・なのだが、CentOS 6環境と同じように設定してもダメで、相変わらずメールが飛んでくる・・ そこで「/usr/share/logwatch/default.conf/logwatch.conf」を見てみると、v7.3.6(CentOS 6)では存在した「Save」パラメータがv7.4.0(CentOS 7)では無くなっている。

なぬ!?と思って「logwatch --help」してみる。

[hhelibex@centos6 ~]$ logwatch --help

Usage: /usr/sbin/logwatch [--detail <level>] [--logfile <name>]
   [--print] [--mailto <addr>] [--archives] [--range <range>] [--debug <level>]
   [--save <filename>] [--help] [--version] [--service <name>]
   [--numeric] [--output <output_type>]
   [--splithosts] [--multiemail] [--no-oldfiles-log]

--detail <level>: Report Detail Level - High, Med, Low or any #.
--logfile <name>: *Name of a logfile definition to report on.
--logdir <name>: Name of default directory where logs are stored.
--service <name>: *Name of a service definition to report on.
--print: Display report to stdout.
--mailto <addr>: Mail report to <addr>.
--archives: Use archived log files too.
--save <filename>: Save to <filename>.
--range <range>: Date range: Yesterday, Today, All, Help
                             where help will describe additional options
--numeric: Display addresses numerically rather than symbolically and numerically
           (saves  a  nameserver address-to-name lookup).
--debug <level>: Debug Level - High, Med, Low or any #.
--splithosts: Create a report for each host in syslog.
--multiemail: Send each host report in a separate email.  Ignored if
              not using --splithosts.
--output <output type>: Report Format - mail, html or unformatted#.
--encode: Use base64 encoding on output mail.
--no-oldfiles-log: Suppress the logwatch log, which informs about the
                   old files in logwatch tmpdir.
--version: Displays current version.
--help: This message.
* = Switch can be specified multiple times...

[hhelibex@centos6 ~]$
[hhelibex@centos7 ~]$ logwatch --help

Usage: /usr/sbin/logwatch [--detail <level>] [--logfile <name>] [--output <output_type>]
   [--format <format_type>] [--encode <enconding>] [--numeric] [--no-oldfiles-log]
   [--mailto <addr>] [--archives] [--range <range>] [--debug <level>]
   [--filename <filename>] [--help|--usage] [--version] [--service <name>]
   [--hostformat <host_format type>] [--hostlimit <host1,host2>] [--html_wrap <num_characters>]

--detail <level>: Report Detail Level - High, Med, Low or any #.
--logfile <name>: *Name of a logfile definition to report on.
--logdir <name>: Name of default directory where logs are stored.
--service <name>: *Name of a service definition to report on.
--output <output type>: Report Output - stdout [default], mail, file.
--format <formatting>: Report Format - text [default], html.
--encode <encoding>: Enconding to use - none [default], base64.
--no-oldfiles-log: Suppress the logwatch log, which informs about the
                   old files in logwatch tmpdir.
--mailto <addr>: Mail report to <addr>.
--archives: Use archived log files too.
--filename <filename>: Used to specify they filename to save to. --filename <filename> [Forces output to file].
--range <range>: Date range: Yesterday, Today, All, Help
                             where help will describe additional options
--numeric: Display addresses numerically rather than symbolically and numerically
           (saves  a  nameserver address-to-name lookup).
--debug <level>: Debug Level - High, Med, Low or any #.
--hostformat: Host Based Report Options - none [default], split, splitmail.
--hostlimit: Limit report to hostname - host1,host2.
--hostname: overwrites hostname
--html_wrap <num_characters>: Default is 80.
--version: Displays current version.
--help: This message.
--usage: Same as --help.
* = Switch can be specified multiple times...

[hhelibex@centos7 ~]$

unified diffも取ってみる。

--- centos6
+++ centos7
@@ -1,31 +1,33 @@
-Usage: /usr/sbin/logwatch [--detail <level>] [--logfile <name>]
-   [--print] [--mailto <addr>] [--archives] [--range <range>] [--debug <level>]
-   [--save <filename>] [--help] [--version] [--service <name>]
-   [--numeric] [--output <output_type>]
-   [--splithosts] [--multiemail] [--no-oldfiles-log]
+Usage: /usr/sbin/logwatch [--detail <level>] [--logfile <name>] [--output <output_type>]
+   [--format <format_type>] [--encode <enconding>] [--numeric] [--no-oldfiles-log]
+   [--mailto <addr>] [--archives] [--range <range>] [--debug <level>]
+   [--filename <filename>] [--help|--usage] [--version] [--service <name>]
+   [--hostformat <host_format type>] [--hostlimit <host1,host2>] [--html_wrap <num_characters>]
--detail <level>: Report Detail Level - High, Med, Low or any #.
--logfile <name>: *Name of a logfile definition to report on.
--logdir <name>: Name of default directory where logs are stored.
--service <name>: *Name of a service definition to report on.
---print: Display report to stdout.
+--output <output type>: Report Output - stdout [default], mail, file.
+--format <formatting>: Report Format - text [default], html.
+--encode <encoding>: Enconding to use - none [default], base64.
+--no-oldfiles-log: Suppress the logwatch log, which informs about the
+                   old files in logwatch tmpdir.
--mailto <addr>: Mail report to <addr>.
--archives: Use archived log files too.
---save <filename>: Save to <filename>.
+--filename <filename>: Used to specify they filename to save to. --filename <filename> [Forces output to file].
--range <range>: Date range: Yesterday, Today, All, Help
                              where help will describe additional options
--numeric: Display addresses numerically rather than symbolically and numerically
            (saves  a  nameserver address-to-name lookup).
--debug <level>: Debug Level - High, Med, Low or any #.
---splithosts: Create a report for each host in syslog.
---multiemail: Send each host report in a separate email.  Ignored if
-              not using --splithosts.
---output <output type>: Report Format - mail, html or unformatted#.
---encode: Use base64 encoding on output mail.
---no-oldfiles-log: Suppress the logwatch log, which informs about the
-                   old files in logwatch tmpdir.
+--hostformat: Host Based Report Options - none [default], split, splitmail.
+--hostlimit: Limit report to hostname - host1,host2.
+--hostname: overwrites hostname
+--html_wrap <num_characters>: Default is 80.
--version: Displays current version.
--help: This message.
+--usage: Same as --help.
* = Switch can be specified multiple times...

・・おいおい、変わりすぎでしょう。

まぁ、とりあえず「Output = file」「Filename = /tmp/logwatch」とすれば良さそう・・・ダメだった・・なぜにメールが飛ぶ!?・・

ということで、今度はcronの設定を疑ってみる。

[hhelibex@centos7 ~]$ cat /etc/cron.daily/0logwatch
#!/bin/sh

#Set logwatch location
LOGWATCH_SCRIPT="/usr/sbin/logwatch"
#Add options to this line. Most options should be defined in /etc/logwatch/conf/logwatch.conf,
#but some are only for the nightly cronrun such as --output mail and should be set here.
#Other options to consider might be "--format html" or "--encode base64", man logwatch for more details.
OPTIONS="--output mail"

#Call logwatch
$LOGWATCH_SCRIPT $OPTIONS

exit 0
[hhelibex@centos7 ~]$ 

・・ちょっと待て!!設定ファイルを無視して強制的にメール送信かよ!!

(疲れたので中略)

結局、辿り着いた答えは以下だった。

[hhelibex@centos7 ~]$ cat /etc/logwatch/conf/logwatch.conf
# Local configuration options go here (defaults are in /usr/share/logwatch/default.conf/logwatch.conf)
Detail = High
Archives = Yes
mailer = "cat > /tmp/logwatch"
[hhelibex@centos7 ~]$ 

ちなみに余談だけど、CentOS 6環境のcron設定も覗いてみると・・・

[hhelibex@centos6 ~]$ cat /etc/cron.daily/0logwatch
#!/bin/bash

DailyReport=`grep -e "^[[:space:]]*DailyReport[[:space:]]*=[[:space:]]*" /usr/share/logwatch/default.conf/logwatch.conf | head -n1 | sed -e "s|^\s*DailyReport\s*=\s*||"`

if [ "$DailyReport" != "No" ] && [ "$DailyReport" != "no" ]
then
    logwatch
fi
[hhelibex@centos6 ~]$ 

・・「/etc/logwatch/conf/logwatch.conf」で「DailyReport」を設定しても無視ですか・・orz

hostnamectlとsystemctlでの情報表示

今まで、パラメータを渡して実行することしかしなかったので、パラメータなしで実行すると情報が出てくるっていうのをつい最近になって知りました(汗)。

hostnamectlコマンドは、ホスト名やOS名等々が表示される。

[hhelibex@centos7 ~]$ hostnamectl
   Static hostname: centos7
   Pretty hostname: CentOS7
         Icon name: computer-vm
           Chassis: vm
        Machine ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
           Boot ID: yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1062.9.1.el7.x86_64
      Architecture: x86-64
[hhelibex@centos7 ~]$ 

systemctlコマンドは、操作できるUNITの一覧などを参照することができる。

[hhelibex@centos7 ~]$ systemctl
UNIT                                                                                     LOAD   ACTIVE SUB       DESCRIPTION
proc-sys-fs-binfmt_misc.automount                                                        loaded active running   Arbitrary Executable File Formats File System Automount Point
 :
sys-subsystem-net-devices-eth0.device                                                    loaded active plugged   Virtio network device
sys-subsystem-net-devices-eth1.device                                                    loaded active plugged   Virtio network device
-.mount                                                                                  loaded active mounted   /
boot.mount                                                                               loaded active mounted   /boot
 :
postfix.service                                                                          loaded active running   Postfix Mail Transport Agent
postgresql.service                                                                       loaded active running   PostgreSQL database server
 :
sshd.service                                                                             loaded active running   OpenSSH server daemon
 :
network.target                                                                           loaded active active    Network
 :
systemd-tmpfiles-clean.timer                                                             loaded active waiting   Daily Cleanup of Temporary Directories

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

101 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
[hhelibex@centos7 ~]$ 

ふと叩いてみた結果として気付いたんだけど、何がきっかけで知識を得られるか分からない。

findコマンドで特定のユーザ/グループのファイル/ディレクトリを探す

ある時、特定のユーザ/グループのファイル/ディレクトリをchownしたい要件が出てきた。 開発環境で、最初は「apache/apache」で動かしていたhttpd、だんだんとめんどくさくなって、httpdのUser/Groupを変えたいけど、ファイルの所有者が‥という感じ。

まぁ探す手段はあるんだろうと思いながら、findコマンドのヘルプを見てみる。

$ cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
$ find --version
find (GNU findutils) 4.5.11
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
有効になっている機能: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS(FTS_CWDFD) CBO(level=2)
$ find --help
使用法: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

デフォルトのパスはカレントディレクトリです。デフォルトの評価式 (expression) は -print です。評価式は演算子、オプション、テストおよびアクションで構成さ
れます。

演算子 (優先順位は降順です。特に指定がない場合は -and が暗黙的に使用されます):
( EXPR ) ! EXPR -not EXPR EXPR1 -a EXPR2 EXPR1 -and EXPR2
EXPR1 -o EXPR2 EXPR1 -or EXPR2 EXPR1 , EXPR2

positional options (always true): -daystart -follow -regextype

normal options (always true, specified before other expressions):
-depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
--version -xautofs -xdev -ignore_readdir_race -noignore_readdir_race

テスト (N は +N、-N または Nで指定します): -amin N -anewer FILE -atime N -cmin N
-cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME
-ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN
-links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE
-nouser -nogroup -path PATTERN -perm [-/]MODE -regex PATTERN
-readable -writable -executable
-wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
-used N -user NAME -xtype [bcdpfls]
-context CONTEXT


アクション: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print
-fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit
-exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;
-execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;

バグを発見した場合は findutils バグ報告ページ http://savannah.gnu.org/ を
使用して報告 (および修正の進捗を追跡) してください。Web を利用できない場合
は <bug-findutils@gnu.org> に E-mail を送ってください。

覚えていた定型的なものを使っていただけなんだなと改めて思う。

今回は、テスト「-user NAME」と「-group NAME」を使えばよさげ。

例えば以下のような感じになるだろう。

# find / -user apache -group apache -exec chown hhelibex.hhelibex {} \; -o -user apache -exec chown hhelibex {} \; -o -gro
up apache -exec chown .hhelibex {} \;

ユーザ/グループが両方ともapacheであるケースを忘れてはいけないことに注意。

2020年の行動指針(努力目標)

今更感ですが、明けましておめでとうございます。

昨年どころか、ここ数年というもの、いろいろとあって、何も進展しておらずにただただ日々の諸々に追われるだけの毎日になっている気がしています。

この流れを断ち切るべく、また続々と現れる若い力に飲み込まれないために、何ができるだろうかと考えてみました。

実のところ、このブログ自体をどうしていこうかなというのも思うところありなのですが、いったんリセットして、コンテンツは何らかの形で継承できるようにとか、ゆっくりですが、考えているところです。

とりあえず、2020年は「何かを知ったら何かを書こう」というのを行動指針(努力目標)にしていきたいなと思っています。

例えば「コマンドのオプションを1つ知ったからブログ記事を一つ書こう」とか、そんな軽いノリにしようかと思っています。

整理しないと取り留めなくなってしまうという恐れもあるのですが、書かなければ記憶の中だけにとどまっていずれ忘れ去ってしまうので、「学習ログ」という意味合いでよいかな、と考えて、とりあえず数をこなしていきたいなと思います。

本年もどうぞよろしくお願いします。

【注:ネタ】Ruby言語における整数型の最大値と最小値の変遷(しません)

諸事情でかくかくしかじか、とにかく飲み込んでくれ、という状況なので、まぁ今のうちに2019年を振り返っておきますか、ということで。

(※先に断っておきますと、「子どもじゃあるまいし・・」という突っ込みをしたいと思いますが、『心は永遠の幼児』ですので。)

振り返ってみると、面白い画像が出てきました。

f:id:hhelibex:20191224232635p:plain

まさかねぇ、とツイート検索してみたら、未だに残ってるんですねぇ(2019/12/25 00:22現在)‥ということで、以下が元ツイート。

これの参照元の記事は以下。

hhelibex.hatenablog.jp

単に整数型の最大値と最小値を調べているだけなんですけどね。確かに、2017年にRuby 2.0.0というバージョンは「古いもの」なのでしょうが、CentOS 7のベースリポジトリに含まれる正統派バージョンなのですよね。

Ruby 2.0.0というバージョン番号に脊髄反射しただけ」なのか、「突っ込む先を間違えただけ」なのか、「全世界35兆人(!?)のCentOSerを敵に回したいだけ」なのか、はたまた「常に新しいバージョンを実運用で維持し続けられるほどのヒ・マ・人」なのか、まぁ真意は知りませんけど。

Rubyがバージョンによって整数型の最大値と最小値が違うクソ言語ではないことを願って、時間もないので、一気に行ってみましょうか。

使うプログラムは前回のものをちょっと弄って以下です。

if defined?(RUBY_DESCRIPTION)
    print RUBY_DESCRIPTION,"\n"
else
    print RUBY_VERSION,"\n"
end
a = 1
ct = 255
a <<= ct
while ct < 256 && (a << 1) + 1 > a
    a <<= 1
    a += 1
    ct += 1
end
print "    int:max? = ",a,"\n"
a += 1
print "          +1 = ",a,"\n"

a = -1
ct = 255
a <<= ct
while ct < 256 && (a << 1) < a
    a <<= 1
    ct += 1
end
print "    int:min? = ",a,"\n"
a -= 1
print "          -1 = ",a,"\n"

ウザくなるので、256ビットシフト位で止めておいてあげましょうか(十分ウザい)。

では結果。

続きを読む

Apache httpd 2.4用mod_cidr_lookupパッチ

ものすごく今更感はありますが。

CentOS 6⇒7で、Apache httpdが2.2.15⇒2.4.6に変わっています。

そんなわけで、とりあえず、CentOS 7でApache httpd用に作られたmod_cidr_lookupをインストールするためのパッチを投げておきますね。

curl -O -L http://downloads.sourceforge.net/modcidrlookup/mod_cidr_lookup-1.2.tar.gz
tar zxf mod_cidr_lookup-1.2.tar.gz
cd  mod_cidr_lookup-1.2/apache2
cp -vp mod_cidr_lookup.c mod_cidr_lookup.c.bak
cat <<__EOD__ | patch -u -o mod_cidr_lookup.c mod_cidr_lookup.c.bak -
--- mod_cidr_lookup-1.2/apache2/mod_cidr_lookup.c
+++ mod_cidr_lookup-1.2/apache2/mod_cidr_lookup.c
@@ -368,7 +368,7 @@
   apr_sockaddr_t *sockaddr;
   uint8_t        *addr;
 
-  sockaddr = r->connection->remote_addr;
+  sockaddr = r->connection->client_addr;
 
 #if APR_HAVE_IPV6
   if (sockaddr->family == AF_INET6 &&
__EOD__
make
make install

単に/usr/include/httpd/httpd.hで定義されているconn_rec構造体のメンバー変数の名前が変わっただけなんですが。