liunx 網絡基礎管理

 在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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章