AIoT.IO.VN
🌐 SONG NGỮ

Pi.Alert trong Docker: phát hiện thiết bị lạ trên mạng LAN

Ng Nghia Phan • • ⚡ 3 phút đọc
VI EN
MỤC LỤC BÀI VIẾT (Nhấn để xem)

Pi.Alert theo dõi thiết bị Wi-Fi/LAN mới xuất hiện và cảnh báo thiết bị lạ hoặc thiết bị “luôn phải online” bị mất. Mình chạy nó trên Linux Docker host bằng host networking để ứng dụng nhìn được mạng local; vì thế cần cân nhắc quyền truy cập và không mở UI thẳng ra Internet.1

Cài Docker trên Linux

Đăng nhập thiết bị Linux, chạy đúng block source:

# install prerequisites
sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg-agent -y
# add docker gpg key
curl -fsSL https://download.docker.com/linux/$(awk -F'=' '/^ID=/{ print $NF }' /etc/os-release)/gpg | sudo apt-key add -
# add docker software repository
sudo add-apt-repository "deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/$(awk -F'=' '/^ID=/{ print $NF }' /etc/os-release) $(lsb_release -cs) stable"
# install docker
sudo apt install docker-ce docker-compose containerd.io -y
# enable and start docker service
sudo systemctl enable docker && sudo systemctl start docker
# add the current user to the docker group
sudo usermod -aG docker $USER
# reauthenticate for the new group membership to take effect
su - $USER

Sau khi re-authenticate để nhận group Docker, kiểm tra docker version và quyền user trước khi đi tiếp.

Chạy Pi.Alert

Tạo thư mục, chạy container tạm để lấy config/database mặc định, copy hai thư mục ra host, xóa container tạm rồi chạy container chính:

# create working directory
mkdir ~/docker/pialert -p
# set ownership on the working directories
sudo chown "$USER":"$USER" ~/docker -R
# run temp pialert container
docker run -d --name=pialert --net=host -e TZ=America/New_York jokobsk/pi.alert
# copy default config files to host
docker cp pialert:/home/pi/pialert/config ~/docker/pialert/
# copy db to host
docker cp pialert:/home/pi/pialert/db ~/docker/pialert/
# remove the temp container
docker rm pialert --force
# run the pialert container
docker run -d --name=pialert --net=host -e TZ=America/New_York -v ~/docker/pialert/db:/home/pi/pialert/db -v ~/docker/pialert/config/:/home/pi/pialert/config/ --restart=unless-stopped jokobsk/pi.alert

--net=host giúp Pi.Alert nhìn các interface của host nhưng cũng mở rộng phạm vi ứng dụng; mình chỉ chạy trên máy tin cậy và kiểm tra docker inspect pialert.

Tài trợ / Quảng cáo

Mở giao diện và kiểm tra

Mở:

http://DNSorIP:20211/pialert

Trang chào mừng xác nhận container hoạt động. Nguồn lưu ý cron của Pi.Alert mặc định chạy mỗi 5 phút để tìm thiết bị mới; mình kiểm tra log, database và danh sách thiết bị trước khi coi cảnh báo là đúng.

Backup và giới hạn

Backup ~/docker/pialert/config và ~/docker/pialert/db trước khi nâng image. Host networking không tự tạo firewall boundary, và nhận diện thiết bị theo MAC/IP có thể thay đổi khi client dùng random MAC. Pi.Alert là tín hiệu giám sát, không thay thế firewall, IDS hoặc VLAN isolation.

Nguồn