Linux基础-网络管理

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

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

1.1 安装
shell 复制代码
sudo apt install iputils-ping
1.2 命令参数
shell 复制代码
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 命令实例
shell 复制代码
# 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 安装
bash 复制代码
sudo apt install net-tools
2.2 命令参数
bash 复制代码
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 命令实例
shell 复制代码
# 查看所有网卡信息
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 安装
bash 复制代码
sudo apt-get install iproute2
3.2 命令参数
bash 复制代码
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 命令实例
shell 复制代码
# 查看所有网卡信息
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是虚拟局域网的缩写。

bash 复制代码
# 挂载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
相关推荐
NiNg_1_23436 分钟前
使用Docker Compose一键部署
运维·docker·容器
萠哥啥都行41 分钟前
Linux安装Docker以及Docker入门操作
运维·docker·容器
王哲晓41 分钟前
Linux通过yum安装Docker
java·linux·docker
小江湖19941 小时前
元数据保护者,Caesium压缩不丢重要信息
运维·学习·软件需求·改行学it
gopher95111 小时前
linux驱动开发-中断子系统
linux·运维·驱动开发
码哝小鱼1 小时前
firewalld封禁IP或IP段
linux·网络
鼠鼠龙年发大财1 小时前
【x**3专享】安装SSH、XFTP、XShell、ARM Linux
linux·arm开发·ssh
nfgo1 小时前
快速体验Linux发行版:DistroSea详解与操作指南
linux·ubuntu·centos
吃面不喝汤662 小时前
如何配置和使用自己的私有 Docker Registry
运维·docker·容器
Rookie_explorers2 小时前
Linux下go环境安装、环境配置并执行第一个go程序
linux·运维·golang