在Linux中,以太网接口被命令为:eth0, eth1等, 0,1代表网卡编号

 通过lspci命令可以查看网上硬件信息(如果是usb接口,则可能要用lsusb)

 命令ifconfig命令用来查看接口信息

     ifconfig -a 查看所有接口

     ifconfig eth0 查看特定接口

在CentOS中可以用命令ifup,ifdown用来启用,禁用一个接口

在ubuntu中可以用ifconfig 网卡 up, ifconfig 网卡 down做同样的功能

     sudo ifconfig eth0 up,启用eth0网卡

     sudo ifconfig eth0 down,禁用eth0网卡

     ifup eth0,启用eth0网卡

     ifdown eth0,禁用eth0网卡



将Linux主机接入到网络中


依次填入以下信息:

IP/mask

路由:默认网关

DNS服务器

主DNS服务器

次DNS服务器

 第三DNS服务器


配置方式:

  静态指定:

    ifcfg, ifconfig, route, netstat

    ip: boject:{link, addr, route}, ss, tc

    配置文件:system-config-network-tui

    CentOS 7:nmcli, nmtui

    动态分配:DHCP

  配置网络接口:

    接口命名方式,CentOS 6,以太网:eth[0,1,2,…]

    ifconfig命令:

      ifconfig [interface]

        # ifconfig -a

        # ifconfig IFACE [up|down]

        ifconfig interface [aftype] options | address …

        # ifconfig IFACE IP/mask [up|down]

        # ifconfig IFACE IP netmask MASK

      注意:IP立即生效

网络配置:


两个命令:ip 和 ss 命令

ip命令

ip – show / manipulate routing, devices, policy routing and tunnels

ip [ OPTIONS ] OBJECT { COMMAND | help }

OBJECT := { link | addr | addrlabel | route | rule | neigh | tunnel | maddr | mroute | monitor }

常用选项:

  ip link set DEVICE { up | down }:激活或指定用户端口

  ip link show

    [dev IFACE]:指定接口

    [up]:仅显示处于激活状态的接口

  ip address:ip addr { add | del } IFADDR dev STRING

    [label LABEL]:添加地址时指明网卡别名

    [scope { global | link | host }]:指明作用域

        global:全局可用

        link:仅链接可用

        host:仅本机可用

    [broadcast ADDRESS]:指明广播地址

  ip address show

    [dev DEVICE]

    [label PATTERN]

    [primary and secondary]

  ip address flush 使用同 show 

  添加路由:ip route add TARGET via GW dev IFACE src SOURCE_IP

  添加网关:ip route add defalt via GW dev IFACE

  删除路由:ip route del TARGET

  显示路由:ip route [ show | flush ]


ss命令

  格式:ss [options] [ FILTER ]

  选项:

    -t:tcp协议相关

    -u:udp协议相关

    -w:裸套接字相关

    -x:unix sock相关

    -l:listen状态的链接

    -a:所有

    -n:数字格式

    -p:相关的程序及PID

    -e:扩展信息

    -m:内存用量

    -o:计时器信息

        

    FILTER := [ state TCP-STATE ] [ EXPRESSION ]

route命令:

  查看命令:route -n

  添加:route add [-net|-host] target [netmask Nm] [gw Gw] [[dev] If]

 示例如下   

[root@localhost ~]# route add -host 192.168.0.0 gw 192.168.103.1 dev eth0
[root@localhost ~]# route add -net 192.168.1.0/24 gw 192.168.103.1 dev eth0
[root@localhost ~]# route add -net 0.0.0.0 netmask 0.0.0.0 gw 192.168.103.1
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     192.168.103.1   255.255.255.255 UGH   0      0        0 eth0
192.168.103.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     192.168.103.1   255.255.255.0   UG    0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
0.0.0.0         192.168.103.1   0.0.0.0         UG    0      0        0 eth0
0.0.0.0         192.168.103.254 0.0.0.0         UG    0      0        0 eth0

       1添加本地地址192.168.0.0到eth0网卡上,网关为192.168.103.1

       2添加网路地址192.168.1.0到eth0网卡上,网关为192.168.103.1

       3添加默认路由,网关为192.168.103.1

      

删除命令:route del

  我要把刚刚添加的两条地址删除就可以用:route del [-net|-host] target [netmask Nm] [gw Gw] [[dev]      If]

 

         [root@localhost ~]# route del -net 192.168.1.0/24 gw 192.168.103.1
         [root@localhost ~]# route del -host 192.168.0.0 gw 192.168.103.1
         [root@localhost ~]# route -n
          Kernel IP routing table
          Destination     Gateway         Genmask        Flags  Metric  Ref    Use Iface
          192.168.103.0   0.0.0.0         255.255.255.0   U      0       0      0 eth0
          169.254.0.0     0.0.0.0         255.255.0.0     U      1002    0      0 eth0
          0.0.0.0         192.168.103.1   0.0.0.0         UG     0       0      0 eth0
          0.0.0.0         192.168.103.254 0.0.0.0         UG     0       0      0 eth0


netstat命令:

     显示网络连接:

    netstat [–tcp|-t] [–udp|-u] [–raw|-w] [–listening|-l] [–all|-a]        [–numeric|-n] [–extend|-e[–extend|-e]] [–program|-p]

    选项参数:

      -t:tcp协议相关

      -u:udp协议相关

      -w:raw socket相关

      -l:处于监听状态

      -a:所有状态

      -n:以数字显示IP和端口

      -e:扩展

      -p:显示相关的进程及PID

    常用组合:-tan,-uan,-tnl,-unl

    显示路由表:

    netstat {–route|-r} [–numeric|-n]

      -r:显示内核路由表

      -n:数字格式

    显示接口统计数据:

    netstat {–interfaces|-I|-i} [iface] [-all|-a] [–extend|-e] [–program|-p]                [–numeric|-n]

      # netstat -i

      # netstat -I IFACE