注:基于Ubuntu20.04环境验证测试。

一、ping(测试另台主机是否可达)

1.1 安装
sudo apt install iputils-ping
1.2 命令参数
Usage
  ping [options] <destination>

Options:
  <destination>      dns name or ip address
  -a                 use audible ping
  -A                 use adaptive ping
  -B                 sticky source address
  -c <count>         stop after <count> replies
  -D                 print timestamps
  -d                 use SO_DEBUG socket option
  -f                 flood ping
  -h                 print help and exit
  -I <interface>     either interface name or address
  -i <interval>      seconds between sending each packet
  -L                 suppress loopback of multicast packets
  -l <preload>       send <preload> number of packages while waiting replies
  -m <mark>          tag the packets going out
  -M <pmtud opt>     define mtu discovery, can be one of <do|dont|want>
  -n                 no dns name resolution
  -O                 report outstanding replies
  -p <pattern>       contents of padding byte
  -q                 quiet output
  -Q <tclass>        use quality of service <tclass> bits
  -s <size>          use <size> as number of data bytes to be sent
  -S <size>          use <size> as SO_SNDBUF socket option value
  -t <ttl>           define time to live
  -U                 print user-to-user latency
  -v                 verbose output
  -V                 print version and exit
  -w <deadline>      reply wait <deadline> in seconds
  -W <timeout>       time to wait for response

IPv4 options:
  -4                 use IPv4
  -b                 allow pinging broadcast
  -R                 record route
  -T <timestamp>     define timestamp, can be one of <tsonly|tsandaddr|tsprespec>

IPv6 options:
  -6                 use IPv6
  -F <flowlabel>     define flow label, default is random
  -N <nodeinfo opt>  use icmp6 node info query, try <help> as argument

For more details see ping(8).
1.3 命令实例
# ping baidu.com 检查是否可以访问外网
ping baidu.com

# 根据ip地址检查主机是否可达
ping 192.168.3.161

# 根据ipv6地址检查主机是否可达
ping fd80:1234:5678::100

二、ifconfig(经典的网络设备查询、配置命令)

在Linux环境下,ifconfig是常用的网络配置工具之一,可以用于配置和管理网络接口,包括设置IP地址、子网掩码、网络广播地址等。
在最新版本的Linux系统中,ifconfg逐渐被ip addr和ip link慢慢取代,但仍是一个经典的网络配置工具。

2.1 安装
sudo apt install net-tools
2.2 命令参数
Usage:
  ifconfig [-a] [-v] [-s] <interface> [[<AF>] <address>]
  [add <address>[/<prefixlen>]]
  [del <address>[/<prefixlen>]]
  [[-]broadcast [<address>]]  [[-]pointopoint [<address>]]
  [netmask <address>]  [dstaddr <address>]  [tunnel <address>]
  [outfill <NN>] [keepalive <NN>]
  [hw <HW> <address>]  [mtu <NN>]
  [[-]trailers]  [[-]arp]  [[-]allmulti]
  [multicast]  [[-]promisc]
  [mem_start <NN>]  [io_addr <NN>]  [irq <NN>]  [media <type>]
  [txqueuelen <NN>]
  [[-]dynamic]
  [up|down] ...

  <HW>=Hardware Type.
  List of possible hardware types:
    loop (Local Loopback) slip (Serial Line IP) cslip (VJ Serial Line IP)
    slip6 (6-bit Serial Line IP) cslip6 (VJ 6-bit Serial Line IP) adaptive (Adaptive Serial Line IP)
    ash (Ash) ether (Ethernet) ax25 (AMPR AX.25)
    netrom (AMPR NET/ROM) rose (AMPR ROSE) tunnel (IPIP Tunnel)
    ppp (Point-to-Point Protocol) hdlc ((Cisco)-HDLC) lapb (LAPB)
    arcnet (ARCnet) dlci (Frame Relay DLCI) frad (Frame Relay Access Device)
    sit (IPv6-in-IPv4) fddi (Fiber Distributed Data Interface) hippi (HIPPI)
    irda (IrLAP) ec (Econet) x25 (generic X.25)
    eui64 (Generic EUI-64)
  <AF>=Address family. Default: inet
  List of possible address families:
    unix (UNIX Domain) inet (DARPA Internet) inet6 (IPv6)
    ax25 (AMPR AX.25) netrom (AMPR NET/ROM) rose (AMPR ROSE)
    ipx (Novell IPX) ddp (Appletalk DDP) ec (Econet)
    ash (Ash) x25 (CCITT X.25)
2.3 命令实例
# 查看所有网卡信息
ifconfig

# 启动/关闭指定网卡
ifconfig <net_name> up
ifconfig <net_name> down

# 指定网卡添加/删除ip
ifconfig <net_name> add <ip_addr>
ifconfig <net_name> del <ip_addr>

三、ip(功能更强大的网络配置命令)

ip命令是linux上更为强大网络管理命令,可以代替ifconfig管理linux网络环境。

3.1 安装
sudo apt-get install iproute2
3.2 命令参数
Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }
       ip [ -force ] -batch filename
where  OBJECT := { link | address | addrlabel | route | rule | neigh | ntable |
                   tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm |
                   netns | l2tp | fou | macsec | tcp_metrics | token | netconf | ila |
                   vrf | sr | nexthop }
       OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |
                    -h[uman-readable] | -iec | -j[son] | -p[retty] |
                    -f[amily] { inet | inet6 | mpls | bridge | link } |
                    -4 | -6 | -I | -D | -M | -B | -0 |
                    -l[oops] { maximum-addr-flush-attempts } | -br[ief] |
                    -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |
                    -rc[vbuf] [size] | -n[etns] name | -N[umeric] | -a[ll] |
                    -c[olor]}
3.3 命令实例
# 查看所有网卡信息
ip a

# 启动/关闭指定网卡
ip link set <net_name> up
ip link set <net_name> down

# 指定网卡添加/删除ip
ip addr add <ip_addr> dev <net_name>
ip addr del <ip_addr> dev <net_name>

# 添加/删除路由表
ip route add <ip_addr> dev <net_name>
ip route del <ip_addr> dev <net_name>

四、vlan网络

VLAN是虚拟局域网的缩写。

# 挂载vlan内核支持
sudo modprobe 8021q

# 创建vlan网卡
sudo ip link add link <net_name> name <net_name>.<vlan_id> type vlan id <vlan_id>

# 分配ip
sudo ip addr add <ip_addr> dev <net_name>.<vlan_id>

# 使能网卡
sudo ip link set <net_name>.<vlan_id> up
08-13 07:21