目录

前提

1. 机器IP地址查询

1.1 ifconfig

1.1.1 安装包

1.1.2 执行命令

1.1.3 拓展-ifconfig的其它用法

1.1.4 常用的属性说明

1.2 ip addr

1.2.1 查看IP地址

1.2.2 其它用法

1.3 hostname -I

1.3.1 查看IP地址

1.3.2 其它用法

2 网络通信状态查询

2.1 ping

2.2 traceroute

3 DNS解析

3.1 nslookup

3.2 dig

4 端口状态查询

4.1 netstat

4.2 ss

4.3 lsof

4.4 nc

4.4.1 监听端口

4.4.2 扫描端口

4.4.3 传输文件 聊天

5 其它

5.1 curl

5.2 tcpdump

6 Linux的网络相关的文件


前提

Linux系统,不同的版本可能会有差异,下面是本人用的版本。

ubuntu版本

root@node2:~# cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.3 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

centos版本

[root@centos ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

1. 机器IP地址查询

确认自己机器的IP地址 ,有下面的几种方式

1.1 ifconfig

跟windows的ipconfig很像,可以查看自己机器的ip地址,但是实际上列出的是机器上所有网卡的信息,系统安装的版本原因,可能系统本身不会带有这个命令。

1.1.1 安装包

  • centos可以用yum provides ifconfig查找这个命令属于哪个包
#查找ifconfig所在的package
[root@centos ~]# yum provides ifconfig
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile* base: ftp.iij.ad.jp* extras: ftp.iij.ad.jp* updates: ftp.iij.ad.jp
net-tools-2.0-0.25.20131004git.el7.x86_64 : Basic networking tools
Repo        : @base
Matched from:
Filename    : /usr/sbin/ifconfig#安装
[root@centos ~]# yum install net-tools -y
  • ubuntu可以直接执行ifconfig,然后系统会提示如何安装
root@node2:~# ifconfigCommand 'ifconfig' not found, but can be installed with:apt install net-tools# 安装
root@node2:~# apt install net-tools

1.1.2 执行命令

可以看到两个网卡,一个是ens33 也就是本地的网卡,本机的IP地址是192.168.0.203,子网掩码是255.255.255.0,广播地址是192.168.0.255。还有一个是lo,也就是本地回环地址,IP是127.0.0.1。

root@node2:~# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 192.168.0.203  netmask 255.255.255.0  broadcast 192.168.0.255inet6 240f:73:1e6d:1:20c:29ff:fe42:c352  prefixlen 64  scopeid 0x0<global>inet6 fe80::20c:29ff:fe42:c352  prefixlen 64  scopeid 0x20<link>ether 00:0c:29:42:c3:52  txqueuelen 1000  (Ethernet)RX packets 247578  bytes 235754869 (235.7 MB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 59994  bytes 5649270 (5.6 MB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536inet 127.0.0.1  netmask 255.0.0.0inet6 ::1  prefixlen 128  scopeid 0x10<host>loop  txqueuelen 1000  (Local Loopback)RX packets 849  bytes 78675 (78.6 KB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 849  bytes 78675 (78.6 KB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

1.1.3 拓展-ifconfig的其它用法

下面的这些命令慎用,如果是用ssh登录的情况下,可能会切断你的连接。

# 启动ens33网卡
ifconfig ens33 up
# 关闭ens33网卡
ifconfig ens33 down
# 给ens33网卡分配IP地址
ifconfig ens33 192.168.0.xxx# ens33网卡追加新的IP地址
ifconfig add ens33 192.168.0.xxx
# ens33网卡删除IP地址
ifconfig del ens33 192.168.0.xxx

1.1.4 常用的属性说明

  • 操作用的属性

通过

man ifconfig

或者

ifconfig -h

可以查看到下面的属性用法

属性 解释
up 启动网卡
down 停止网卡
add 增加IP地址到网卡
del 从网卡删除IP地址
mtu  设置网卡的mtu(最大转送单位)
netmask 设置网卡的子网掩码
hw ether  设置物理地址(MAC地址)
  • 表示用的属性
属性 解释
-a 表示所有的网卡信息(包括已经down的网卡)
-s 简单表示网卡的通信状况

1.2 ip addr

ip命令是现在大多数版本linux自带的命令,被用来替代ifconfig。

1.2.1 查看IP地址

[root@centos ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000link/ether 00:0c:29:07:cf:68 brd ff:ff:ff:ff:ff:ffinet 192.168.0.211/24 brd 192.168.0.255 scope global noprefixroute ens33valid_lft forever preferred_lft foreverinet6 240f:73:1e6d:1:8441:716a:d14:3b9c/64 scope global noprefixroute dynamic valid_lft 266sec preferred_lft 266secinet6 fe80::ba3e:8d0:5cc3:c7e3/64 scope link noprefixroute valid_lft forever preferred_lft forever

1.2.2 其它用法

用法

ip [option] object [subcommand]

详细的内容用下面的命令去查询

man ip

  • 属性说明
短属性 长属性 解释
-4 IPv4
-6 IPv6
-B bridge
-b file -batch file 从文件读取命令
-s -stats、-statistics 显示详细情报
-o -oneline 显示内容变成一行  用/ 替代换行 
-r -resolve 显示dns名

例如

[root@centos ~]# ip -4 -o a
1: lo    inet 127.0.0.1/8 scope host lo\       valid_lft forever preferred_lft forever
2: ens33    inet 192.168.0.211/24 brd 192.168.0.255 scope global noprefixroute ens33\       valid_lft forever preferred_lft forever

对象说明

网络相关的内容(待整理)

对象 别名 解释
link l 网络设备
addr a、address ip地址
addrlabel addrl
route r
rule rul
neigh n、neighbour
ntable nt、ntb
tunnel t
tuntap tu
maddr m、ma、mad
mroute mr
mrule mru
monitor mon
xfrm x、xf
netns net
l2tp l2
tcp_metrics tcp_m、tcp_metrics、tcpm
token tok

ip command的命令很强大,有兴趣懂网络的可以自己研究一下。

比如跟ifconfig一样的功能

# ens33网卡追加新的IP地址
ip addr add 192.168.0.xxx dev ens33# 从ens33网卡删除IP地址
ip addr del 192.168.0.xxx dev ens33

1.3 hostname -I

hostname

就是看本机的机器名的。还附带别的功能。

注意:这个命令 不像前两个命令可以看到网卡信息,只是单纯的看IP地址

1.3.1 查看IP地址

[root@centos ~]# hostname -i
fe80::ba3e:8d0:5cc3:c7e3%ens33 240f:73:1e6d:1:8441:716a:d14:3b9c%2 192.168.0.211
[root@centos ~]# hostname -I
192.168.0.211 240f:73:1e6d:1:8441:716a:d14:3b9c

1.3.2 其它用法

临时设定hostname,重启后失效。

[root@centos ~]# hostname
centos
[root@centos ~]# hostname centos02
[root@centos ~]# hostname
centos02

用法比较简单,用hostname -h 就可以看到所有的用法,或者用man hostname就可以用到更多的信息。

如果要永久修改hostname的话,用hostnamectl, 可以看到连配置文件都发生变更了

[root@centos ~]# hostnamectl set-hostname centos02
[root@centos ~]# hostname
centos02
[root@centos ~]# cat /etc/hostname
centos02
[root@centos ~]#

2 网络通信状态查询

2.1 ping

ping 跟windows 一样 ,可以用来确认网络连接的状态和应答速度。

但是要注意的是,可以ping通,不代表你可以访问对方机器,ping不通,也不代表你肯定可以访问对方机器。

ping命令走的是ICMP协议,是网络层的协议。跟端口什么的没有关系。

[root@centos ~]# ping 192.168.0.1 -c 4
PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
64 bytes from 192.168.0.1: icmp_seq=1 ttl=255 time=2.48 ms
64 bytes from 192.168.0.1: icmp_seq=2 ttl=255 time=3.38 ms
64 bytes from 192.168.0.1: icmp_seq=3 ttl=255 time=8.78 ms
64 bytes from 192.168.0.1: icmp_seq=4 ttl=255 time=3.32 ms--- 192.168.0.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3012ms
rtt min/avg/max/mdev = 2.480/4.492/8.782/2.502 ms

2.2 traceroute

查询网络的路由路径

这个命令不是自带命令,centos用yum provides traceroute, ubuntu自带提示。

安装完后可以执行命令了,可以看到从本机到google经过的路由地址。

[root@centos ~]# traceroute google.com -4
traceroute to google.com (172.217.26.238), 30 hops max, 60 byte packets1  gateway (192.168.0.1)  2.985 ms  3.900 ms  3.738 ms2  sjkBBAR006-1.bb.kddi.ne.jp (111.87.254.17)  10.775 ms  14.363 ms  14.200 ms3  sjkBBAC07.bb.kddi.ne.jp (27.85.214.221)  11.480 ms sjkBBAC07.bb.kddi.ne.jp (182.248.174.125)  13.813 ms sjkBBAC07.bb.kddi.ne.jp (27.86.126.237)  13.630 ms4  27.85.132.69 (27.85.132.69)  15.890 ms 27.80.241.125 (27.80.241.125)  15.739 ms 27.80.241.133 (27.80.241.133)  15.506 ms5  27.86.120.218 (27.86.120.218)  12.840 ms 106.139.192.102 (106.139.192.102)  15.053 ms 27.86.120.22 (27.86.120.22)  14.908 ms6  72.14.242.21 (72.14.242.21)  14.755 ms  11.571 ms  10.071 ms7  * * *8  72.14.233.34 (72.14.233.34)  10.347 ms 142.250.226.60 (142.250.226.60)  13.171 ms 108.170.242.193 (108.170.242.193)  13.081 ms9  108.170.242.209 (108.170.242.209)  13.003 ms 108.170.242.208 (108.170.242.208)  11.917 ms 108.170.242.176 (108.170.242.176)  12.787 ms
10  bom05s09-in-f14.1e100.net (172.217.26.238)  12.704 ms 209.85.249.241 (209.85.249.241)  13.103 ms bom05s09-in-f14.1e100.net (172.217.26.238)  12.993 ms

3 DNS解析

3.1 nslookup

跟windows的nslookup一样,可以把domain名解析成IP地址。

使用方法很简单。

可以看到通过DNS服务器[2001:268:fd07:4::1] 把www.baidu.com的域名解析成IP地址。

  1. 根据CNAME Record把www.baidu.com解析成www.a.shifen.com
  2. 根据CNAME Record把www.a.shifen.com解析成www.wshifen.com
  3. 根据A Record把www.wshifen.com解析成119.63.197.151和119.63.197.139
[root@centos ~]# nslookup www.baidu.com
Server:     2001:268:fd07:4::1
Address:    2001:268:fd07:4::1#53Non-authoritative answer:
www.baidu.com   canonical name = www.a.shifen.com.
www.a.shifen.com    canonical name = www.wshifen.com.
Name:   www.wshifen.com
Address: 119.63.197.151
Name:   www.wshifen.com
Address: 119.63.197.139

注意:有时候能ping通IP地址,却不能解析域名,就是因为DNS服务器的设置有问题。

/etc/resolv.conf  文件里 就是DNS服务器的设置。

但是要注意不能直接修改这个文件,而且ubuntu和centos的修改方式不一样。

[root@centos ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 2001:268:fd07:4::1
nameserver 2001:268:fd08:4::1

3.2 dig

这个命令不是系统自带的,需要自己安装。安装方式跟前面的几个命令一样,不再赘述。

 使用方式

[root@centos ~]# dig www.baidu.com; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.8 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 5053
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 1;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;www.baidu.com.         IN  A;; ANSWER SECTION:
www.baidu.com.      917 IN  CNAME   www.a.shifen.com.
www.a.shifen.com.   223 IN  CNAME   www.wshifen.com.
www.wshifen.com.    170 IN  A   119.63.197.151
www.wshifen.com.    170 IN  A   119.63.197.139;; Query time: 15 msec
;; SERVER: 2001:268:fd07:4::1#53(2001:268:fd07:4::1)
;; WHEN: Wed Jan 05 23:07:27 JST 2022
;; MSG SIZE  rcvd: 127

4 端口状态查询

4.1 netstat

Netstat 命令用于打印网络连接(network connections),路由表(routing tables),接口统计信息 (Interface Statistics),伪装连接(masquerade connections) ,多播成员资格 (Multicast Memberships) 。

端口占用情况调查

命令格式:

netstat   [address_family_options]   [--tcp|-t]   [--udp|-u]   [--udplite|-U]   [--sctp|-S]  [--raw|-w]  [--listening|-l]  [--all|-a]
       [--numeric|-n] [--numeric-hosts] [--numeric-ports] [--numeric-users] [--symbolic|-N] [--extend|-e[--extend|-e]] [--timers|-o] [--pro‐
       gram|-p] [--verbose|-v] [--continuous|-c] [--wide|-W] [delay]

下面的协议,有很多可能系统不支持,主要记住ipv4和ipv6就可以了。

address_family_options:

[-4|--inet] --------------------------------------------------------------------------------------IPv4

[-6|--inet6]  -----------------------------------------------------------------------------------IPv6

[--protocol={inet,inet6,unix,ipx,ax25,netrom,ddp, ... } ]-----------------------各种协议

[--unix|-x] [--inet|--ip|--tcpip] [--ax25] [--x25][--rose] [--ash] [--ipx] [--netrom] [--ddp|--appletalk] [--econet|--ec]-----------------------各种协议

tcp

# 属性[-t] 代表tcp 已经确立连接的tcp端口列表
[root@centos ~]# netstat -t
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0     36 centos:ssh              192.168.0.13:58026      ESTABLISHED# 属性[-l] 监听中的tcp端口列表
[root@centos ~]# netstat -tl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN
tcp        0      0 localhost:smtp          0.0.0.0:*               LISTEN
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN
tcp6       0      0 localhost:smtp          [::]:*                  LISTEN # 属性[-n] 端口和host全部变成数字(跟上面的列表一样,但是表示的是数字)
# 监听中的tcp端口列表
[root@centos ~]# netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN
tcp6       0      0 ::1:25                  :::*                    LISTEN  

udp

[root@centos ~]# netstat -nul
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
udp        0      0 127.0.0.1:323           0.0.0.0:*
udp6       0      0 ::1:323                 :::*    

还有很多别的用法

比如说 -4 只显示ipv4,-6 只显示ipv6,上面的命令格式可以自由组合来打印自己需要的内容

[root@centos ~]# netstat -ntl4
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN [root@centos ~]# netstat -ntl6
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp6       0      0 :::22                   :::*                    LISTEN
tcp6       0      0 ::1:25                  :::*                    LISTEN  

 打印NIC信息

netstat {--interfaces|-I|-i} [--all|-a] [--extend|-e]  [--verbose|-v]  [--program|-p]  [--numeric|-n]  [--numeric-hosts]  [--numeric-
       ports] [--numeric-users] [--continuous|-c] [delay]

比如查看NIC信息跟ifconfig一样

[root@centos ~]# netstat -ia
Kernel Interface table
Iface             MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
ens33            1500    40264      0      0 0         12113      0      0      0 BMRU
lo              65536       79      0      0 0            79      0      0      0 LRU
[root@centos ~]#
[root@centos ~]# ifconfig -s
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
ens33            1500    40311      0      0 0         12137      0      0      0 BMRU
lo              65536       79      0      0 0            79      0      0      0 LRU

打印路由信息

netstat    {--route|-r}   [address_family_options]   [--extend|-e[--extend|-e]]   [--verbose|-v]   [--numeric|-n]   [--numeric-hosts]
       [--numeric-ports] [--numeric-users] [--continuous|-c] [delay]

查看路由表,跟route 命令一样

[root@centos ~]# netstat -ra
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         gateway         0.0.0.0         UG        0 0          0 ens33
192.168.0.0     0.0.0.0         255.255.255.0   U         0 0          0 ens33
[root@centos ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens33
192.168.0.0     0.0.0.0         255.255.255.0   U     100    0        0 ens33

打印伪装连接和多播成员资格的信息等等。

4.2 ss

ss在端口占用检查方面跟netstat很像。是一个打印网络的socket情报的命令。

ss [options] [ FILTER ]

ss  is used to dump socket statistics. It allows showing information similar to netstat.  It can display more TCP and state information than other tools.

经常使用的用法

[root@centos ~]# ss -nta
State      Recv-Q Send-Q                                    Local Address:Port                                                   Peer Address:Port
LISTEN     0      128                                                   *:22                                                                *:*
LISTEN     0      100                                           127.0.0.1:25                                                                *:*
ESTAB      0      36                                        192.168.0.211:22                                                     192.168.0.13:58026
LISTEN     0      128                                                [::]:22                                                             [::]:*
LISTEN     0      100                                               [::1]:25                                                             [::]:*                  [root@centos ~]# ss -ntl
State      Recv-Q Send-Q                                    Local Address:Port                                                   Peer Address:Port
LISTEN     0      128                                                   *:22                                                                *:*
LISTEN     0      100                                           127.0.0.1:25                                                                *:*
LISTEN     0      128                                                [::]:22                                                             [::]:*
LISTEN     0      100                                               [::1]:25                                                             [::]:*

注意:ss命令的属性跟netstat很像。学会一个另外一个也很快能理解。

4.3 lsof

列出所有打开的文件。

我们知道Linux一起皆文件。也就是说lsof可以调查端口的占用情况,当然强大的lsof可以查询很多东西。像进程process, 用户user等等。

详细内容可以用 man lsof 命令查询。这个命令的说明 简直就是长篇论文。

这个命令也不是系统自带的,需要安装。安装方法跟上面的命令一样。

打印所有端口状态

# 打印ipv4的所有socket
[root@centos ~]# lsof -i 4 -a
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
chronyd  690 chrony    5u  IPv4  16930      0t0  UDP localhost:323
sshd    1010   root    3u  IPv4  20080      0t0  TCP *:ssh (LISTEN)
master  1115   root   13u  IPv4  20914      0t0  TCP localhost:smtp (LISTEN)
sshd    2468   root    3u  IPv4  30517      0t0  TCP centos:ssh->192.168.0.13:58026 (ESTABLISHED)# 打印ipv6的所有socket
[root@centos ~]# lsof -i 6
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
chronyd  690 chrony    6u  IPv6  16931      0t0  UDP localhost:323
sshd    1010   root    4u  IPv6  20082      0t0  TCP *:ssh (LISTEN)
master  1115   root   14u  IPv6  20915      0t0  TCP localhost:smtp (LISTEN)

指定的端口状态查询

# 指定端口22的状态查询
[root@centos ~]# lsof -i:22
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd    1010 root    3u  IPv4  20080      0t0  TCP *:ssh (LISTEN)
sshd    1010 root    4u  IPv6  20082      0t0  TCP *:ssh (LISTEN)
sshd    2468 root    3u  IPv4  30517      0t0  TCP centos:ssh->192.168.0.13:58026 (ESTABLISHED)

 指定host的连接查询

# 来自192.168.0.13 的网络连接
[root@centos ~]# lsof -i@192.168.0.13
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd    2468 root    3u  IPv4  30517      0t0  TCP centos:ssh->192.168.0.13:58026 (ESTABLISHED)

指定host和端口的连接查询

# centos机器的端口22的状态查询
[root@centos ~]# lsof -i@centos:22
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd    2468 root    3u  IPv4  30517      0t0  TCP centos:ssh->192.168.0.13:58026 (ESTABLISHED)

指定协议的情况下查询

# ipv4 端口22
[root@centos ~]# lsof -i4:22
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd    1010 root    3u  IPv4  20080      0t0  TCP *:ssh (LISTEN)
sshd    2468 root    3u  IPv4  30517      0t0  TCP centos:ssh->192.168.0.13:58026 (ESTABLISHED)# ipv6 端口22
[root@centos ~]# lsof -i6:22
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd    1010 root    4u  IPv6  20082      0t0  TCP *:ssh (LISTEN)

上面的属性 都可以自由组合来打印自己需要的内容,只是要注意先后顺序。

除了端口状态查询以外,lsof还具有ps命令的功能

查询user lihg和nginx的所有process

[root@centos ~]# lsof -u lihg,nginx
COMMAND  PID USER   FD   TYPE DEVICE  SIZE/OFF     NODE NAME
bash    2911 lihg  cwd    DIR  253,0       188 33574977 /root
bash    2911 lihg  rtd    DIR  253,0       224       64 /
bash    2911 lihg  txt    REG  253,0    964536 50553748 /usr/bin/bash
bash    2911 lihg  mem    REG  253,0     61560    15691 /usr/lib64/libnss_files-2.17.so
bash    2911 lihg  mem    REG  253,0 106172832 50553739 /usr/lib/locale/locale-archive
bash    2911 lihg  mem    REG  253,0   2156272    15673 /usr/lib64/libc-2.17.so
bash    2911 lihg  mem    REG  253,0     19248    15679 /usr/lib64/libdl-2.17.so
bash    2911 lihg  mem    REG  253,0    174576    30658 /usr/lib64/libtinfo.so.5.9
bash    2911 lihg  mem    REG  253,0    163312    15666 /usr/lib64/ld-2.17.so
bash    2911 lihg  mem    REG  253,0     26970    30627 /usr/lib64/gconv/gconv-modules.cache
bash    2911 lihg    0u   CHR  136,1       0t0        4 /dev/pts/1
bash    2911 lihg    1u   CHR  136,1       0t0        4 /dev/pts/1
bash    2911 lihg    2u   CHR  136,1       0t0        4 /dev/pts/1
bash    2911 lihg  255u   CHR  136,1       0t0        4 /dev/pts/1

lsof的最基本的功能,打印打开的文件。

打印指定目录下打开的文件

# 查询目录/home/lihg/下打开的文件
[root@centos ~]# lsof /home/lihg/
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF     NODE NAME
bash    2911 lihg  cwd    DIR  253,0      120 17674103 /home/lihg
vi      2934 lihg  cwd    DIR  253,0      120 17674103 /home/lihg

4.4 nc

nc命令可以用于涉及到TCP,UDP,UNIX套接字的任何事情。

比如说打开TCP连接,发送UDP数据包,监听端口,扫描端口。

不是系统自带命令,需要安装。

centos 这里要特别注意要添加yum源epel

然后再用命令 yum install netcat -y 来安装netcat。

如果是nmap-ncat 包的话,可能没有端口扫描功能。

4.4.1 监听端口

 监听端口(默认是TCP)

[lihg@centos ~]$ nc -l 8090[root@centos ~]# lsof -i:8090
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nc      2948 lihg    3u  IPv6  36465      0t0  TCP *:8090 (LISTEN)
nc      2948 lihg    4u  IPv4  36466      0t0  TCP *:8090 (LISTEN)

 监听端口(UDP)

[lihg@centos ~]$ nc -lu 8090[root@centos ~]# lsof -i:8090
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nc      2993 lihg    3u  IPv6  37237      0t0  UDP *:8090
nc      2993 lihg    4u  IPv4  37238      0t0  UDP *:8090

只监听IPv4

[lihg@centos ~]$ nc -l4 8090[root@centos ~]# lsof -i:8090
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nc      2995 lihg    3u  IPv4  37271      0t0  TCP *:8090 (LISTEN)

同理也可以只监听IPv6。

4.4.2 扫描端口

centos的IP地址是192.168.0.211 ,node2的IP地址是192.168.0.203。

[root@centos ~]# hostname -I
192.168.0.211 root@node2:~# hostname -I
192.168.0.203

从cenos去扫描node2的端口8090。(注意防火墙是否开启,开启的情况下要把测试的端口打开)

# ubuntu机器未开启8090端口监听的情况下
[root@centos ~]# nc -v -w 2 192.168.0.203 -z 8090
nc: connect to 192.168.0.203 port 8090 (tcp) failed: Connection refused# ubuntu机器开启8090端口监听的情况下
root@node2:~# nc -l 8090[root@centos ~]# nc -v -w 2 192.168.0.203 -z 8090
Connection to 192.168.0.203 8090 port [tcp/*] succeeded!

要注意host,port,protocol三个一起才可以确定一个连接。

# ubuntu机器监听UDP的8090端口
root@node2:~# nc -lu 8090# centos机器去扫描ubuntu的TCP的8090端口
[root@centos ~]# nc -v -w 2 192.168.0.203 -z 8090
nc: connect to 192.168.0.203 port 8090 (tcp) failed: Connection refused# centos机器去扫描ubuntu的UDP的8090端口
[root@centos ~]# nc -vu -w 2 192.168.0.203 -z 8090
Connection to 192.168.0.203 8090 port [udp/*] succeeded!

4.4.3 传输文件 聊天

借助监听端口和端口扫描的功能 可以实现两台机器之间传递信息,甚至是文件

 聊天

root@node2:~# nc -l 8090[root@centos ~]# nc 192.168.0.203 8090# 这样就可以建立一个TCP的连接,然后在centos这边输入内容,node2那边就可以看到,同理反过来一样。
root@node2:~# nc -l 8090
hi this is ubuntu[root@centos ~]# nc 192.168.0.203 8090
hi this is ubuntu

 传送文件

# node2的家目录下的文件
root@node2:~# ls -l
total 8
drwxr-xr-x 3 root root 4096 Dec  8 09:47 snap
drwxr-xr-x 2 root root 4096 Dec 17 13:08 test
-rw-r--r-- 1 root root    0 Jan  6 12:50 ubuntu.txt
root@node2:~# cat ubuntu.txt
this is ubuntu#centos的家目录下的文件
[root@centos ~]# ll
total 4
-rw-------. 1 root root 1461 Dec 18 11:02 anaconda-ks.cfg
drwxr-xr-x. 3 root root   54 Dec 19 20:47 mysql# 通过8090端口传送文件
root@node2:~# nc -l 8090 < ubuntu.txt [root@centos ~]# nc 192.168.0.203 8090 > centos.txt# 可以看到文件已经传送到centos
[root@centos ~]# ll
total 8
-rw-------. 1 root root 1461 Dec 18 11:02 anaconda-ks.cfg
-rw-r--r--. 1 root root   15 Jan  6 21:53 centos.txt
drwxr-xr-x. 3 root root   54 Dec 19 20:47 mysql
[root@centos ~]# cat centos.txt
this is ubuntu

还有很多其它的用法,可以用man nc 来查看帮助文档

5 其它

5.1 curl

一般开发者用这个命令来测试api,实际上命令很强大,有很多功能。

curl  is a tool to transfer data from or to a server, using one of the supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS,IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP). The command is designed to work without user interaction.

root@node2:~# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>body {width: 35em;margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif;}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>
root@node2:~#

5.2 tcpdump

抓包工具

# 通过tcp的8090 端口传输
root@node2:~# nc -l 8090 < ubuntu.txt [root@centos ~]# nc 192.168.0.203 8090
this is ubuntu# 针对端口8090 可以抓取到包(默认是TCP)
root@node2:~# tcpdump -i ens33 port 8090
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
13:19:13.043666 IP 192.168.0.211.55112 > node2.8090: Flags [S], seq 3599715317, win 29200, options [mss 1460,sackOK,TS val 3688998 ecr 0,nop,wscale 7], length 0
13:19:13.043795 IP node2.8090 > 192.168.0.211.55112: Flags [S.], seq 3772181634, ack 3599715318, win 65160, options [mss 1460,sackOK,TS val 3170940051 ecr 3688998,nop,wscale 7], length 0
13:19:13.044143 IP 192.168.0.211.55112 > node2.8090: Flags [.], ack 1, win 229, options [nop,nop,TS val 3688998 ecr 3170940051], length 0
13:19:13.142334 IP node2.8090 > 192.168.0.211.55112: Flags [P.], seq 1:16, ack 1, win 510, options [nop,nop,TS val 3170940149 ecr 3688998], length 15
13:19:13.143560 IP 192.168.0.211.55112 > node2.8090: Flags [.], ack 16, win 229, options [nop,nop,TS val 3689097 ecr 3170940149], length 0

还可以根据fromIP,fromPort,toIP,toPort,protocol来进行组合抓包。

5.3 nmcli(centos)

man nmcli

修改dns, ip地址用

修改后要用

systemctl restart NetworkManager

以后补充

6 Linux的网络相关的文件

  • /etc/services
  • /etc/networks
  • /etc/hostname
  • /etc/hosts
  • /etc/host.conf
  • /etc/nsswitch.conf
  • /etc/resolv.conf
  • /etc/sysconfig/network
  • /etc/sysconfig/network-scripts/

Linux学习整理-网络命令集相关推荐

  1. Linux学习整理-网络防火墙ufw

    Linux的防火墙的术语 先整理一下他们之间的关系 netfilter ​netfilter 项目是一个社区驱动的协作 FOSS 项目,为Linux 2.4.x 和更高版本的内核系列提供包过滤软件.n ...

  2. Linux学习整理-网络防火墙firewalld

    1 概念 1-1. firewall简介 firewalld是Linux系统的一款防火墙管理工具.通过充当netfilter的前端来提供防火墙功能,默认后端是iptables/ntftables.开发 ...

  3. Linux学习整理-网络防火墙iptables-实践篇1

    目录 1 实验环境 2 iptables常用用法 2.1 查看iptables链 2.2 规则追加 2.3 规则删除 2.4 规则全删除 2.5 自定义链(chain) 2.6 删除自定义链 2.7 ...

  4. linux学习查看日志命令

    linux学习查看日志命令 tail -f linux学习日 志 文 件 说 明 /var/log/maillog 与邮件相关的日志信息 /var/log/secure 与安全相关的日志信息 /var ...

  5. Linux学习之网络相关命令

    Linux的核心,网络相关 很多工具也是通过分析加工 /proc./sys 下的数据来工作的,而那些更加细致.专业的性能监测和调优,可能还需要更加专业的工具(perf.systemtap等)和技术才能 ...

  6. Java设计模式(学习整理)---命令模式

    设计模式之Command(学习整理) 1.Command定义 不少Command模式的代码都是针对图形界面的,它实际就是菜单命令,我们在一个下拉菜单选择一个命令时,然后会执行一些动作. 将这些命令封装 ...

  7. linux终端刷新网络命令,在Ubuntu Linux操作系统中重新启动网络的方法

    你使用的是基于Ubuntu的Linux操作系统,然而有时似乎无法连接到网络,其实简单的重启可以修复一些问题.在本文中将介绍在Ubuntu和其他Linux发行版中重新启动网络的方法,以便你可以使用任何适 ...

  8. linux fls函数,Linux学习笔记- find 命令详解

    前言 find命令是我们日常工作中比较常用的Linux命令.全面的掌握这个命令可以使很多操作达到事半功倍的效果. 使用find命令常常会有以下这些疑惑: find命令的格式是什么? 参数中出现+或-号 ...

  9. Linux的常用网络命令

    rsh命令  rsh是"remote shell"(远程 shell)的缩写. 该命令在指定的远程主机上启动一个shell并执行用户在rsh命令行中指定的命令.如果用户没有给出要执 ...

最新文章

  1. python检查目录是否存在,如果不存在则创建
  2. 把文件夹下的所有文件打包成一个zip包
  3. 在Java中衡量执行时间– Spring StopWatch示例
  4. 工作103:组装查询
  5. WCF中如何用nettcp协议进行通讯
  6. linux命令大全私房菜,linux命令大全(自己制作,基于鸟书私房菜以及man)-D
  7. DTD(Document Type Definition) 简介
  8. 在Ubuntu20.04上安装ros
  9. MPEG TS流简介
  10. javascript玩转ElasticSearch(一)
  11. 矿泉水瓶勿重复使用易得癌病
  12. 比亚迪芯片BF7615BMxx芯片怎么烧录
  13. 计算二维紧束缚模型费米面和nesting程序新思路
  14. Python - 随机生成英文字母
  15. git revert 之后 找回原来的代码
  16. 1分钟7张图:后视镜判断车距,非常实用
  17. 周杰伦讲给快手的“独家秘密”
  18. ElasticSearch——手写一个ElasticSearch分词器(附源码)
  19. (四十二):Aligning Linguistic Words and Visual Semantic Units for Image Captioning
  20. echarts按照时间显示柱状图_echarts 不连续的柱状图 在线时间分布离散图

热门文章

  1. x270 运行linux,商用范儿十足 ThinkPad X270深度评测
  2. Java读取文件内容,返回字符串
  3. Solid Edge 放样使用引导曲线
  4. 360/腾讯/网易有道/CSDN版ChatGPT上线;看!AIGC艺术第一个场景落地北京;AI狂飙的时代,人还有价值吗 | ShowMeAI日报
  5. 全国青少年信息素养大赛图形化编程初赛·模拟四卷,含答案解析
  6. Git 命令之stash
  7. Python 正则替换字符串
  8. linux 网卡重新扫描,在ubuntu桌面配置一个网络打印机和扫描仪的方法,
  9. 【ae调整图层】调整图层上的特效会影响到他下面的所有图层
  10. day15-re与正则表达式