2025 Nov. 02.
2025 Oct. 25.
2025 May 06.
2025 Apr. 26.
2025 Mar. 30.
2024 Sep. 29.
2024 May. 04.
2024 Feb. 25.
2022 Feb. 11.
2021 Nov. 21.
2021 Jun. 05.
2021 May 02.
2021 Apr. 27.
2021 Mar. 28.
2021 Jan. 31.
2020 Sep. 21.
参考元
Ubuntu Desktop 18.04でClamAVによるウィルスチェックを実行する!
Ubuntu 20.04 LTS : Clamav アンチウィルス : Server World
https://www.hiroom2.com/2018/05/06/ubuntu-1804-clamav-ja/
【Ubuntu 20.04/18.04 LTS Server】ClamAVで定期的にウイルスチェックし、メール通知する | The modern stone age.
clamscanによるウイルススキャンは低速だが高機能。
clamdデーモンを使うclamdscanによるウイルススキャンは高速だが低機能。
この記事ではclamdscanを利用する。
準備作業
パッケージをインストール
(Ubuntu)
$ sudo apt update && sudo apt install clamav clamav-daemon
(Arch系Manjaro Linux)
# pacman -S clamav
ウイルス定義更新
出典 【Ubuntu 20.04/18.04 LTS Server】ClamAVで定期的にウイルスチェックし、メール通知する | The modern stone age.
$ sudo freshclam
上記freshclamで次のエラーが出たら、下記のログ設定を行う
(エラー出力例)
$ sudo freshclam WARNING: Ignoring deprecated option SafeBrowsing at /etc/clamav/freshclam.conf:22 ERROR: /var/log/clamav/freshclam.log is locked by another process ERROR: Problem with internal logger (UpdateLogFile = /var/log/clamav/freshclam.log). ERROR: initialize: libfreshclam init failed. ERROR: Initialization error!
(エラー対応ログ設定)
$ sudo rm /var/log/clamav/freshclam.log $ sudo touch /var/log/clamav/freshclam.log $ sudo chown clamav:clamav /var/log/clamav/freshclam.log /etc/logrotate.d/clamav-freshclam 、 /etc/logrotate.d/clamav-daemo 、 /etc/logrotate.d/clamav の下記create行を修正する 変更前:create 640 clamav adm 変更後:create 640 clamav clamav
(確認)
$ sudo freshclam
ウイルス定義の自動更新起動状況確認
$ systemctl status clamav-freshclam.service (clamav-freshclam.serviceが動いていなかったら) $ sudo systemctl enable clamav-freshclam.service $ sudo systemctl start clamav-freshclam.service
clamdscanに必要なclamav-daemonの設定
ウイルスを移動する設定の時の移動先ディレクトリの作成
$ sudo mkdir /var/moved_virus $ sudo chmod 777 /var/moved_virus
/etc/clamav/clamd.conf を編集
設定項目リスト CentOS6:ClamAV(Clam AntiVirus)のインストール及び設定(1) | Engineer Log
## rootなファイルをスキャンできるようにユーザーをrootにする # User clamav User root ### スキャンするファイルサイズを無制限にする ## スキャンされるデータの最大量 #MaxScanSize 100M MaxScanSize 0 ## 通常のファイルと、アーカイブから展開されたファイルの最大サイズ #MaxFileSize 25M MaxFileSize 0 ## ディレクトリ深さ #MaxDirectoryRecursion 15 MaxDirectoryRecursion 20 ## スキャンしないファイル・ディレクトリ ExcludePath ^/dev/ ExcludePath ^/proc/ ExcludePath ^/sys/ ExcludePath ^/var/moved_virus/ ## ScanOnAccessは廃止されたので設定しない。clamAVでのオンアクセススキャンにはfanotifyを利用する。
(Ubuntuでは)ClamAV をrootで実行できるようにAppArmorによる制限を外す。
参考元 Linuxでウイルス対策 – ClamAVのインストールと初期設定 (2018年版) | 如是我聞 - a blog about life and tech
(Ubuntu)
AppArmor Utilsをインストールし、ClamAVへのAppArmorによる制限を外す
$ sudo apt install apparmor-utils $ sudo aa-complain clamd
(Arch系Manjaro Linux)
# aa-complain clamd
CPUへの高負荷対策
(1)/etc/clamav/clamd.confに下記を設定する。
# clamdがスキャン時に使用する最大スレッド数(CPUコアを制限)
MaxThreads 2
# スキャン対象サイズの上限を下げる(例:50MB)
MaxFileSize 50M
# スキャン対象のファイル数上限(例:1000ファイル)
MaxFiles 1000
(2)systemdの設定でcpu使用率を制限する
参考元:Debian 9.3 ClamAV高負荷問題の対処2 (Linux自作PC)
/lib/systemd/system/clamav-daemon.service の [Service]セクションに "MemoryLimit=512M" "CPUQuota=50%" を記述する。
(3)clamd デーモンを起動する際に、nice コマンドを使ってプロセスの優先度を下げる。
clamd の起動スクリプト内で、niceコマンドを付けてclamdを起動する。
nice +10 /usr/sbin/clamd -c /etc/clamav/clamd.conf
+10 は優先度を下げる度合いを指定する。値が大きいほど優先度が低くなる。
設定反映
$ sudo systemctl daemon-reload $ systemctl status clamav-daemon.service (動いていなかったら) $ sudo systemctl enable clamav-daemon.service $ sudo systemctl start clamav-daemon.service $ systemctl status clamav-daemon.service 稼働状況を確認する。 (動いていたら) $ sudo systemctl restart clamav-daemon.service $ systemctl status clamav-daemon.service 稼働状況を確認する。
確認
"systemctl status clamav-daemon.service"コマンドの出力の"Memory:"を確認する。
$ systemctl status clamav-daemon.service
● clamav-daemon.service - Clam AntiVirus userspace daemon
Loaded: loaded (/usr/lib/systemd/system/clamav-daemon.service; enabled; preset: enabled)
Drop-In: /etc/systemd/system/clamav-daemon.service.d
└─extend.conf
Active: active (running) since Sun 2025-03-30 18:45:13 JST; 5min ago
TriggeredBy: ● clamav-daemon.socket
Docs: man:clamd(8)
man:clamd.conf(5)
https://docs.clamav.net/
Process: 1026357 ExecStartPre=/bin/mkdir -p /run/clamav (code=exited, status=0/SUCCESS)
Process: 1026359 ExecStartPre=/bin/chown root /run/clamav (code=exited, status=0/SUCCESS)
Main PID: 1026362 (clamd)
Tasks: 1 (limit: 4515)
Memory: 511.9M (limit: 512.0M peak: 512.0M swap: 939.4M swap peak: 939.4M)
CPU: 35.317s
CGroup: /system.slice/clamav-daemon.service
└─1026362 /usr/sbin/clamd --foreground=true
データベースを更新する
$ sudo systemctl stop clamav-freshclam.service $ sudo freshclam
上記での出力を確認しておく
データベースの自動更新設定
/etc/clamav/freshclam.conf内のChecks設定が1日の更新回数
$ sudo systemctl enable clamav-freshclam.service $ systemctl status clamav-freshclam.service (動いていなかったら) $ sudo systemctl start clamav-freshclam.service
clamav-daemonを利用したclamdscanによるスキャンを試しに実行する
無害ウイルスをダウンロードして、スキャンしてみて、リターンコードを確認する。
$ sudo chmod 744 /var/log/clamav/clamav.log $ cd YOUR/WORKING/DIR $ wget https://secure.eicar.org/eicar.com.txt clamdの稼働状況を確認する。 $ ps ax | grep clamd | grep -v grep clamdが稼働していたら、次のコマンドでclamdを停止する。 $ sudo systemctl stop clamav-daemon.socket $ sudo systemctl stop clamav-daemon.service クライアントツールのclamdscanが接続するデーモンclamdを実行する $ sudo clamd -c /etc/clamav/clamd.conf スキャン実行 $ sudo clamdscan --multiscan --fdpass --move=/var/moved_virus YOUR/WORKING/DIR > OUTPUT.txt ## --multiscan 複数のスレッドを使用するため、処理速度が向上 ## --move=VIRUS/MOVED/DIR ウイルスファイルの移動先ディレクトリ ## YOUR/WORKING/DIR スキャンするディレクトリ。子ディレクトリもスキャンされる ## --remove ウイルスファイルを削除する ## --quiet 出力を抑制 ## --log=FILE ファイルに保存。デフォルトは/etc/clamav/clamd.conf内のLogFile指定 ## --fdpass ## 他のユーザが実行しているclamdデーモンでスキャン。 ## clamav-daemon.serviceを実行するclamdユーザーとしてファイルをスキャン。 ## Pass the file descriptor permissions to clamd. This is useful if clamd is running as a different user as it is faster than streaming the file to clamd. Only available if connected to clamd via local(unix) socket. $ echo $? ## clamdscanの返り値(リターンコード) ## 0 : No virus found ## 1 : Virus(es) found ## 2 : An error occurred
返り値を確認する
OUTPUT.txtと/var/log/clamav/clamav.logでeicar.com.txt(無害ウイルス)の検知を確認する。
ログ(OUTPUT.txt)で「ウイルスファイルのrenameに失敗」「ハードリンクはできない」とか出力されるが、ウイルスファイルが移動先ディレクトリに移動されていた。
(OUTPUT.txt)
eicar.com.txt: Win.Test.EICAR_HDB-1 FOUND
traverse_rename: Failed to rename: eicar.com.txt
to: moved_virus/eicar.com.txt.001
Error:Invalid cross-device link
eicar.com.txt: moved to 'moved_virus/eicar.com.txt.001'
----------- SCAN SUMMARY -----------
Infected files: 1
Time: 1.165 sec (0 m 1 s)
Start Date: 2024:02:25 18:29:04
End Date: 2024:02:25 18:29:05 2021 Apr. 23. Ubuntuで、--multiscanを付けるとリターンコードが139となり動作しなくなった。
2022 Feb. 10. Ubuntuで、--multiscanを付けても動作した。
systemd/タイマーへの定期実行設定
/etc/systemd/system/clamdscan-weekly.service を次の内容で作成する
[Unit] Description=Weekly clamdscan (with flock) After=network.target clamd.service [Service] Type=oneshot # lockfile は /var/lock/clamdscan.lock にする(ディレクトリ権限に注意) ExecStart=/usr/bin/flock -n /var/lock/clamdscan.lock /usr/bin/clamdscan --log=/var/log/clamav/clamdscan-weekly.log --fdpass --multiscan --quiet --move=/var/moved_virus / # 戻り値をそのまま反映させる RemainAfterExit=no User=root Group=root [Install] WantedBy=multi-user.target
/etc/systemd/system/clamdscan-weekly.timerを次の内容で作成する
[Unit] Description=Timer for weekly clamdscan [Timer] OnCalendar=Sun 03:00:00 Persistent=true [Install] WantedBy=timers.target
設定読み込み
$ sudo systemctl daemon-reload
タイマー有効化
$ sudo systemctl enable --now clamdscan-weekly.timer
タイマー稼働状況確認
$ systemctl status clamdscan-weekly.timer
タイマーが有効なら次回実行予定が出力内に書かれている。
ログローテンション設定する
次の内容の /etc/logrotate.d/clamdscan-weekly を作成する。
/var/log/clamav/clamdscan-weekly.log {
weekly
rotate 4
compress
missingok
notifempty
create 640 root adm
}
この設定の意味:
weekly: 週に1回ローテーション
rotate 4: 最大4世代保持(1ヶ月分)
compress: 古いログは gzip 圧縮
missingok: ファイルがなくてもエラーにしない
notifempty: 空ファイルはローテーションしない
create: 新しいログファイルを作成(パーミッション指定)
作成した設定の構文をチェックする
$ sudo logrotate --debug /etc/logrotate.conf
手動でローテーションを試す
$ sudo logrotate -f /etc/logrotate.conf
-f は強制的にローテーションを実行する(通常の周期を無視)
ローテーション後、/var/log/clamav/clamdscan-weekly.log.1 や .gz が生成されているか確認する。
以下は旧
参考ページ
systemdでtimerの作り方(最小限のサンプル) #systemd - Qiita
systemd/タイマー - ArchWiki
systemd .timerについて調べた事を記事にしておく | そう備忘録
clamd (clamav-daemon.service)をサービスとして起動させる。
"systemctl status clamav-daemon.service"でclamav-daemon.serviceが稼働していなかったら次のコマンドを実行する。
$ sudo systemctl enable --now clamav-daemon.service
clamav-daemon.serviceの稼働を確認する。
$ systemctl status clamav-daemon.service
systemdサービスファイル作成
/etc/systemd/system/myclamdscan.service
[Unit] Description=Run clamdscan once after clamav-daemon is ready Requires=clamav-daemon.service After=clamav-daemon.service Wants=network-online.target After=network-online.target [Service] Type=oneshot ExecStartPre=/usr/bin/bash -c 'timeout 30 bash -c "until [ -S /var/run/clamav/clamd.ctl ]; do sleep 1; done"' ExecStart=/usr/bin/clamdscan --log=/var/log/clamav/clamdscan.log --multiscan --fdpass --quiet --move=/var/moved_virus / RemainAfterExit=yes [Install] WantedBy=multi-user.target
myclamdscan.serviceの有効化
$ sudo systemctl enable myclamdscan.service
systemdタイマーファイル作成 (PC起動時にサービスとして起動すればよいので これは不要)
/etc/systemd/system/myclamdscan.timer
[Unit] Description=Runs myclamdscan.service [Timer] # ブート後、この時間後にこのタイマーを開始する。1minでは動作しなかったので2minにした。 OnBootSec=2min # 実行する時間の間隔 OnUnitActiveSec=4d Unit=myclamdscan.service [Install] WantedBy=multi-user.target
myclamdscan.timerの有効化
$ sudo systemctl enable myclamdscan.timer $ sudo systemctl start myclamdscan.timer
clamdscanの返り値をメールするようにしたらよいかも。
clamdscanをcron設定するのは、稼働中のclamdscanに追加で多重実行するようになるので不可
デスクトップPCで、システム起動時の実行としてcronで@reboot設定にすると
ERROR: Could not connect to clamd on LocalSocket /var/run/clamav/clamd.ctl: No such file or directory
となったので、下記の "systemd/タイマーへの定期実行設定" に変更した。
@reboot clamdscan --log=/var/log/clamav/clamdscan.log --multiscan --fdpass --quiet --move=/var/moved_virus /
/var/log/clamav/clamav.logの属性が744になっていたので644に変更した。
以下は参考程度に
ウイルス定義ファイル更新設定変更
/etc/clamav/freshclam.conf を修正する
ウイルス定義ファイル更新時のclamdサービスへの通知を非通知に設定する
# NotifyClamd // NotifyClamd行をコメント行にする
DatabaseMirror のドメインをjp に(db.XY.clamav.net→ db.jp.clamav.net)
ウイルス定義ファイル更新時にclamav-freshclam(ウイルス定義ファイル自動更新プログラム)が稼働しているとエラーを吐くので止める
# systemctl stop clamav-freshclam.service
ウイルス定義ファイル更新
# freshclam
画面出力とログ/var/log/clamav/freshclam.logを確認する
手動ウイルススキャン設定
ウイルス定義ファイル自動更新を起動
# systemctl start clamav-freshclam.service
デフォルトでは各種ファイル属性がclamavなのでrootに変更(この属性変更は不要か)
# chown root:clamav /var/log/clamav/freshclam.log
# chown root:clamav /var/lib/clamav
# chown root:clamav /var/lib/clamav/*
手動動作確認
お試し無害ウィルスをダウンロード
$ cd YOUR/WORKING/DIR
$ wget http://www.eicar.org/download/eicar.com
汚染ファイル隔離先ディレクトリを作成
$ mkdir ~/virus
手動ウイルススキャン実行
$ clamscan --infected --remove --recursive --move=${HOME}/virus --exclude-dir="^/dev/" --exclude-dir="^/proc/" --exclude-dir="^/sys/" DIR/TO/BE/SCANNED
結果が画面出力(標準出力か標準エラー出力か未確認)
clamdデーモンによる高速手動ウイルススキャン設定
clamdはサーバとして動作し、clamdscanコマンドを用いてソケット通信を介してウイルス検査を実行する。
clamscanよりも実行速度は速い
( 【Ubuntu 20.04/18.04 LTS Server】ClamAVで定期的にウイルスチェックし、メール通知する | The modern stone age. より )
データベース更新の間隔設定
/etc/clamav/clamd.confのSelfCheckの値の秒数間隔で更新される
スキャン実行者をrootにする
/etc/clamav/clamd.confのUser設定行をコメント行にする
# User clamav (clamavユーザーのままだとroot属性のファイルをスキャンできない)
ウイルススキャン時に"Directory recursion limit reached"と出るなら
/etc/clamav/clamd.confのMaxDirectoryRecursionを25にしてみる
MaxDirectoryRecursion 25
( Debian 9.3 ClamAV高負荷問題の対処 (Linux自作PC) より)
システムに高負荷がかかっているようなら
/lib/systemd/system/clamav-daemon.service の [Service]セクションに CPUQuota=50% と設定してみる
( Debian 9.3 ClamAV高負荷問題の対処2 (Linux自作PC) より)
ウイルススキャン
参考元
https://tutorialmore.com/questions-300013.htm
オープンソースのアンチウイルスソフトウェア、ClamAVをCentOS 7にインストールする #clamav - Qiita
設定
/etc/clamav/clamd.confの編集(正規表現可能)
# Example // Exampleはコメント行にする User root ExcludePath ^/dev/ ExcludePath ^/proc/ ExcludePath ^/sys/
ウイルススキャン実行とオプション
# clamdscan --multiscan --fdpass --quiet /DIR/TO/BE/SCANNED > OUTPUT.txt
--multiscan 複数のスレッドを使用するため、処理速度が向上
--quiet 感染ファイルを除くすべての出力を抑制
--log=FILE ファイルに保存(出力リダイレクトでも可能)
--fdpass
clamav-daemon.serviceを実行するclamdユーザーとしてファイルをスキャン。
他のユーザが実行しているclamdデーモンでスキャン。
Pass the file descriptor permissions to clamd. This is useful if clamd is running as a different user as it is faster than streaming the file to clamd. Only available if connected to clamd via local(unix) socket.
clamdscanの返り値(リターンコード)
0 : No virus found.
1 : Virus(es) found.
2 : An error occurred.
参考 clamscanとclamdscanの使用方法 - tech.farend
clamdscan lstat() failed: Permission denied | まあのぶつぶつ
Configure clamdscan to scan all files on a system on Ubuntu 12.04 - Stack Overflow
https://tutorialmore.com/questions-300013.htm
clamdscanで再起的なスキャン実行
# ls -R DIR/TO/BE/SCANNED | grep / | tr -d : | xargs clamdscan
( clamscanとclamdscanの使用方法 - tech.farend より)