很久没有更新了,水一篇。

本文介绍如何安装 Vnstat 实现监测 VPS 实时流量。以下教程基于 Debian 系统

编译安装

源安装版本比较旧,所以直接编译安装吧。

编译可能会遇到各种错误,这时候就需要自己搜索解决了。这里也记录一下通常会遇到的问题。

首先安装 git :

apt-get install git -y

拉取源码:

git clone https://github.com/vergoh/vnstat.git

进入 vnstat 目录:

cd vnstat

编译文件:

./configure --prefix=/usr --sysconfdir=/etc && make

这个步骤可能就会遇到各种错误。比如:

configure: error: in `/root/vnstat':
configure: error: no acceptable C compiler found in $PATH

此时需要安装 ggc,运行以下命令:

apt-get install gcc -y

之后再重新运行编译文件的命令 ./configure --prefix=/usr --sysconfdir=/etc && make

但是你可能还会遇到以下错误:

configure: error: could not find required sqlite3 library

解决以上错误,运行以下命令:

apt-get install -y libsqlite3-dev

之后再重新运行编译文件的命令 ./configure --prefix=/usr --sysconfdir=/etc && make

然而,你可能还会遇到错误提示:-bash: make: command not found

再安装 make 就可以了:

apt-get install make -y

然后再次运行编译文件的命令:

./configure --prefix=/usr --sysconfdir=/etc && make

最后进行安装:

make install

到这一步就安装成功了

常用命令

这里记录下常用的命令

vnstat -l    #显示实时流量
vnstat -h    #按小时查询
vnstat -d    #按天数查询
vnstat -m    #按月数查询
vnstat -w    #按周数查询
vnstat -t    #查询TOP10

设置网卡

一般来说,默认就已经设置好。

但是如果运行以上常用命令,发现错误,那么就需要设置网卡

首先,使用 ifconfig 命令查看网卡

一般来说,都是 eth0

接着,修改 vnstat 监测的网卡

nano /etc/vnstat.conf

修改 Interface 选项:

Interface "eth0"

之后保存退出

重启 vnstat 以及设置开机启动:


systemctl restart vnstat.service 
systemctl enable vnstat.service

简洁安装版

Debian:

apt-get install git gcc libsqlite3-dev make -y
git clone https://github.com/vergoh/vnstat.git && cd vnstat && ./configure --prefix=/usr --sysconfdir=/etc && make && make install

Centos:

yum install git gcc sqlite sqlite-devel make -y
git clone https://github.com/vergoh/vnstat.git && cd vnstat && ./configure --prefix=/usr --sysconfdir=/etc && make && make install
Last modification:October 2, 2020
如果觉得我的文章对你有用,请随意赞赏