ubuntu 下的网络工具
当管理很多linux主机时,为了方便了解linux下的网络情况,可以安装一些网络工具,通过命令行或者web界面 进行查看。 ubuntu因为提供了相比而言比较方便的安装包,所以以ubuntu为主。Centos等linux发行版可以参考或者搜索百度,谷歌来得到相应的软件安装与使用过程。
本文所提到的相关工具的安装和使用的说明也是基于互联网上搜索相关关键词得到, 根据实际ubuntu 16.04 的版本执行所编写。
1. 命令行简单测量主机间的网络带宽
iperf 会在端口5001进行监听,需要在防火墙上打开这个端口的访问权限。
服务监听端执行
iperf -s
客户端执行,将ip换成服务端的ip.
iperf -c ip
改变端口,以及在nat server之后测试
server 位于 nat server之后, 例如nat router 映射2702 为12702
server:
iperf -s -p 2702
------------------------------------------------------------
Server listening on TCP port 2702
TCP window size: 85.3 KByte (default)
------------------------------------------------------------
[ 4] local 10.1.36.100 port 2702 connected with 47.100.201.247 port 54630
[ ID] Interval Transfer Bandwidth
[ 4] 0.0- 1.5 sec 15.4 MBytes 83.9 Mbits/sec
client 执行:
#iperf -c 123.122.10.54 -p 12702 -r
-----------------------------------------------------------
Server listening on TCP port 12702
TCP window size: 85.3 KByte (default)
------------------------------------------------------------
------------------------------------------------------------
Client connecting to 123.122.10.54, TCP port 12702
TCP window size: 45.0 KByte (default)
------------------------------------------------------------
[ 3] local 172.19.222.139 port 54634 connected with 123.122.10.54 port 12702
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.0 sec 113 MBytes 94.5 Mbits/sec
2. 终端查看具体某个程序的实时网络带宽流量
2.1 安装
centos:
yum -y install nethogs
ubuntun:
apt install nethogs
2.2 执行
sudo nethogs
PID USER PROGRAM DEV SENT RECEIVED
8848 root ctorrent eth0 296.486 1061.492 KB/sec
8195 root sshd:root@pts/3 eth0 1.04 0.053 KB/sec
6717 root synchting eth0 0.000 0.000 KB/sec
定时刷新模式:
Refreshing:
ctorrent/8848/0 492.183 2439.79
sshd: root@pts/3/8195/0 1.2625 0.084375
unknown TCP/0/0 0 0
3. 端口监听接收工具
- 一端使用nc -l 127.0.0.1 6000启动监听程序,另一端使用nc 127.0.0.1 6000 启动连接程序,则两端能互相看到对方的输入。
- ctrl+c可以退出监听。
- 一端使用nc -l 127.0.0.1 6000启动监听程序,另一端运行自己的程序,则可以被监听到。
- 监听测试 nc -l 127.0.0.1 6000
4 免密登录
4.1. 生成密钥
ssh-keygen
生成2个文件,位于~/.ssh/下,分别是公共密钥id_rsa.pub,和私有密钥id_rsa。
4.2.发送自己的公共密钥给服务器端
ssh-copy-id user@host
输入密码
以上命令是给远程服务器发送自己的公共密钥,服务器端会要求输入密码。服务端拿到这个密钥后会在.ssh/下保存,文件名为authorized_keys;在mac平台下没有该命令,可以通过scp命令将公共密钥文件copy至服务端改名即可。
4.3. 实现免密登录
【注】解释访问过程:
在登陆服务端,无需输入密码,登陆过程如下:
客户端先将自己的公共密钥发送给服务器端存储起来
客户端发起登录请求,服务器端接受到后,发送一段随机的字符串给客户端
客户端接受该随机字符串后使用自己的私有密钥进行加密,然后发送给服务端
服务端接收后,使用存储的客户端公共密钥进行解密,解密成功,客户端即可登录,就无须输入密码
4.4. 简易登录
在本地配置config文件,位于~/ssh/config,在该文件加入如下几行配置:
host huabei1
HostName 39.106.122.67
User root
host xitian
Hostname 123.122.10.54
User root
Port 10022
那么我们登陆时,输入如下命令,无须用户名和密码
ssh xitian
5. 快速大批量构建远程登录
远程登录时,或者用到ssh时,由于之前未建立连接,则提示
rsync -av file1M.dat huadong:/root/test2/
The authenticity of host '47.100.201.247 (47.100.201.247)' can't be established.
ECDSA key fingerprint is SHA256:8kQlvH6reWFRxIb5vAsFW8MnRTcMX18ep/7BNksNavg.
Are you sure you want to continue connecting (yes/no)?
为了避免提示,自动建立公私钥,可以使用如下命令:
本机生成公私钥对。
ssh-keygen
输入密码,不用交互,直接执行命令,本机与其他主机建立联系,其余部分参考免密登录
sshpass -p XXX ssh-copy-id -o StrictHostKeyChecking=no huabei3
6 基于web界面查看网络流量情况
6.1. ntop
安装与启动
apt install ntop
sudo /etc/init.d/ntop start
查看
http://39.106.122.67:3000/thptStats.html
6.2. ntopng
ntopng是原ntop下一代版本,网络流量实时监控显示。
安装与启动
sudo apt-get install ntopng
service ntopng start
## or
systemctl restart ntopng
vim /etc/ntopng.conf
# * Interfaces to sniff on: one interface per line, prefix with -i=
-i=eth0
#-i=wlan0
# If none is specified, ntopng will try to auto-detect the best interface.
#
# * Port on which ntopng will listen for the web-UI.
-w=3000
访问与查看
http://127.0.0.1:3000
用户名/密码: admin/admin