zoukankan      html  css  js  c++  java
  • Linux网络基本配置

    一.Linux网络配置文件

    1.  /etc/sysconfig/network-scripts/ifcfg-eth0 文件

      在Red Hat系统中,系统网络设备的配置文件保存在/etc/sysconfig/network-scripts目录下,其中文件ifcfg-eth0包含第一块网卡的配置信息,文件ifcfg-eth1包含第二块网卡的配置信息,文件ifcfg-lo包含回路IP地址信息。

    [root@redhat2 ~]# ls /etc/sysconfig/network-scripts

    ifcfg-eth0   ifdown-post    ifup-eth    ifup-routes

    ifcfg-lo     ifdown-ppp     ifup-ippp   ifup-sit

    ifdown       ifdown-routes  ifup-ipv6   ifup-tunnel

    ifdown-bnep  ifdown-sit     ifup-isdn   ifup-wireless

    ifdown-eth   ifdown-tunnel  ifup-plip   init.ipv6-global

    ifdown-ippp  ifup           ifup-plusb  net.hotplug

    ifdown-ipv6  ifup-aliases   ifup-post   network-functions

    ifdown-isdn  ifup-bnep      ifup-ppp    network-functions-ipv6

    [root@redhat2 ~]#

    下面是/etc/sysconfig/network-scripts/ifcfg-eth0文件内容的示例。

    [root@redhat2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0

    DEVICE="eth0"                           //表示物理设备的名字,如eth0,eth1

    BOOTPROTO=static                //表示为网卡配置静态还是动态IP地址

    IPADDR=192.168.12.11         //表示赋给该网卡的IP地址

    NETMASK=255.255.255.0    //表示子网掩网

    ONBOOT=yes                          //表示启动系统时是否激活该卡,yes激活,no不激活

    TYPE="Ethernet"

    [root@redhat2 ~]#

    可以为BOOTPROTO设置4种选项。

    ● none:表示无需启动协议。

    ● bootp:表示使用bootp协议。

    ● dhcp: 表示使用dhcp协议动态获取IP地址。

    ● static:表示手工设置静态IP地址。

    1. /etc/resolv.conf文件

    文件/etc/resolv.conf是由域名解析器(resolver,一个根据主机名解析IP地址的库)使用的配置文件,下面是/etc/resolv.conf文件内容的示例。

          search openarch.com

         nameserver 202.96.209.5

         nameserver 202.96.209.6

    该文件中包含的内容描述如下。

    Search:表示DNS搜索路径,即解析不完整名称时默认的附件域名后缀,这样可以在解析名称时用简短的主机名而不是完全合格域名(FQDN)。

    Nameserver:表示解析域名时使用该地址指定的主机为域名服务器,其中域名服务器是按照文件中出现的顺序来查询的。

    1. /etc/host.conf文件

    文件/etc/host.conf指定如何解析主机名,redhat系统通过解析器库来获得主机名对应的IP地址,下面是/etc/host.conf文件内容的示例。

    Order bind,hosts

    Multi on

    Nospoof on

         该文件中包含的内容描述如下:

     order bind,hosts:指定主机名查询顺序,这里规定先使用DNS来解析域名,然后再查询/etc/hosts文件。

    Multi on

    Nospoof on

    该文件中包含的内容描述如下。

    Order bind,hosts:指定主机名查询顺序,这里规定先使用DNS来解析域名,然后再查询/etc/hosts文件。

    Multi on:表示在/etc/hosts文件中指定的主机是否可以有多个地址,拥有多个IP地址的主机一般称为多穴主机。

    Nospoof on:表示不允许对该服务器进行IP地址欺骗。IP欺骗是一种攻击系统安全的手段,通过把IP地址伪装成别的计算机,来取得其他计算机的信任。

    4./etc/sysconfig/network文件

        文件/etc/sysconfig/network用来指定服务器上的网络配置信息,下面是/etc/sysconfig/network文件内容的示例。

    [root@redhat2 ~]# cat /etc/sysconfig/network

    NETWORKING=yes  //是否配置网络

    HOSTNAME=redhat2  //表示计算机的主机名

    5./etc/hosts文件

       当计算机启动时,在可以查询DNS以前,计算机需要查询一些主机名到IP地址的匹配。这些匹配信息存放在/etc/hosts文件中。在没有域名服务器的情况下,系统上的所有网络程序都通过查询该文件来解析对应于某个主机名的IP地址。

    [root@redhat2 ~]# cat /etc/hosts

    192.168.12.11   redhat2

    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

    [root@redhat2 ~]#

      最左边一列是计算机IP地址,中间一列是主机名,最后面的列都是该主机的别名。一旦配置完计算机的网络配置文件,应该重新启动网络以使修改生效。

    6./etc/services文件。

      文件/etc/services定义了Linux系统中所有服务的名称、协议类型、服务的端口等信息。/etc/services文件是一个服务名和服务端口对应的数据库文件,下面是/etc/services文件内容的示例。

    二.Linux网络命令

      在Linux系统中提供了大量的网络命令用于网络配置、网络测试以及网络诊断,如ifconfig,ping,netstat,traceroute,arp以及tcpdump等。

    1.traceroute

      使用tracerout命令可以显示数据包到目标主机之间的路径。

    Traceroute  [-dFlnrvx] [-f<存活数值>] [-g<网关>]…[-I <网络界面>] [-m<存活数值>] [-p<通信端口>] [-s<来源地址>][-t<服务类型>][-w<超时秒数>][主机名称或IP地址][数据包大小]。

    2.ifconfig

      使用ifconfig命令可以显示或设置计算机网卡的IP地址。

     命令语法:

    [root@redhat2 ~]# ifconfig --help

    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>]  [metric <NN>]  [mtu <NN>]

      [[-]trailers]  [[-]arp]  [[-]allmulti]

      [multicast]  [[-]promisc]

      [mem_start <NN>]  [io_addr <NN>]  [irq <NN>]  [media <type>]

      [txqueuelen <NN>]

      [[-]dynamic]

      [up|down] …

    例1:配置网卡eth3的IP地址,同时激活该设备。

      [root@redhat2 ~]# ifconfig eth3 192.168.12.121 netmask 255.255.255.0 up

    例2:配置网卡eth3别名设备eth3:1的IP的地址。

      [root@redhat2 ~]# ifconfig eth3:1 192.168.12.0

    例3:激活网卡eth3:1设备。

     [root@redhat2 ~]# ifconfig eth3:1 up

    例4:查看所有的网卡网络接口配置。

    [root@redhat2 ~]# ifconfig

    eth2      Link encap:Ethernet  HWaddr 00:0C:29:53:BB:56 

              inet addr:192.168.12.11  Bcast:192.168.12.255  Mask:255.255.255.0

              inet6 addr: fe80::20c:29ff:fe53:bb56/64 Scope:Link

              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

              RX packets:873 errors:0 dropped:0 overruns:0 frame:0

              TX packets:915 errors:0 dropped:0 overruns:0 carrier:0

              collisions:0 txqueuelen:1000

              RX bytes:91453 (89.3 KiB)  TX bytes:414896 (405.1 KiB)

    eth3      Link encap:Ethernet  HWaddr 00:0C:29:53:BB:60 

              inet addr:192.168.12.121  Bcast:192.168.12.255  Mask:255.255.255.0

              inet6 addr: fe80::20c:29ff:fe53:bb60/64 Scope:Link

              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

              RX packets:124 errors:0 dropped:0 overruns:0 frame:0

              TX packets:134 errors:0 dropped:0 overruns:0 carrier:0

              collisions:0 txqueuelen:1000

              RX bytes:12196 (11.9 KiB)  TX bytes:17381 (16.9 KiB)

    eth3:1    Link encap:Ethernet  HWaddr 00:0C:29:53:BB:60 

              inet addr:192.168.12.0  Bcast:192.168.12.255  Mask:255.255.255.0

              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

    lo        Link encap:Local Loopback 

              inet addr:127.0.0.1  Mask:255.0.0.0

              inet6 addr: ::1/128 Scope:Host

              UP LOOPBACK RUNNING  MTU:16436  Metric:1

              RX packets:914 errors:0 dropped:0 overruns:0 frame:0

              TX packets:914 errors:0 dropped:0 overruns:0 carrier:0

              collisions:0 txqueuelen:0

              RX bytes:45860 (44.7 KiB)  TX bytes:45860 (44.7 KiB)

    [root@redhat2 ~]#

    例5:禁用网卡eth3:1设备。

    [root@redhat2 ~]# ifconfig eth3:1 down

    3.ping

    使用ping命令可用于测试与目标计算机之间的连通性。

    [root@redhat2 ~]# ping --help

    ping: invalid option -- '-'

    Usage: ping [-LRUbdfnqrvVaA] [-c count] [-i interval] [-w deadline]

                [-p pattern] [-s packetsize] [-t ttl] [-I interface or address]

                [-M mtu discovery hint] [-S sndbuf]

                [ -T timestamp option ] [ -Q tos ] [hop1 ...] destination

    [root@redhat2 ~]#

    例1:测试与 192.168.12.121计算机的连通性,要求返回4个ICMP数据包。

    [root@redhat2 ~]# ping -c 4 192.168.12.121

    PING 192.168.12.121 (192.168.12.121) 56(84) bytes of data.

    64 bytes from 192.168.12.121: icmp_seq=1 ttl=64 time=0.158 ms

    64 bytes from 192.168.12.121: icmp_seq=2 ttl=64 time=0.069 ms

    64 bytes from 192.168.12.121: icmp_seq=3 ttl=64 time=0.067 ms

    64 bytes from 192.168.12.121: icmp_seq=4 ttl=64 time=0.037 ms

    --- 192.168.12.121 ping statistics ---

    4 packets transmitted, 4 received, 0% packet loss, time 3002ms

    rtt min/avg/max/mdev = 0.037/0.082/0.158/0.046 ms

    [root@redhat2 ~]#

    4.netstat

      使用netstat命令可用于显示网络状态的信息。

    [root@redhat2 ~]# netstat --help

    usage: netstat [-veenNcCF] [<Af>] -r         netstat {-V|--version|-h|--help}

           netstat [-vnNcaeol] [<Socket> ...]

           netstat { [-veenNac] -I[<Iface>] | [-veenNac] -i | [-cnNe] -M | -s } [delay]

            -r, --route                display routing table

            -I, --interfaces=<Iface>   display interface table for <Iface>

            -i, --interfaces           display interface table

            -g, --groups               display multicast group memberships

            -s, --statistics           display networking statistics (like SNMP)

            -M, --masquerade           display masqueraded connections

            -v, --verbose              be verbose

            -n, --numeric              don't resolve names

            --numeric-hosts            don't resolve host names

            --numeric-ports            don't resolve port names

            --numeric-users            don't resolve user names

            -N, --symbolic             resolve hardware names

            -e, --extend               display other/more information

            -p, --programs             display PID/Program name for sockets

            -c, --continuous           continuous listing

            -l, --listening            display listening server sockets

            -a, --all, --listening     display all sockets (default: connected)

            -o, --timers               display timers

            -F, --fib                  display Forwarding Information Base (default)

            -C, --cache                display routing cache instead of FIB

            -T, --notrim               stop trimming long addresses

            -Z, --context              display SELinux security context for sockets

      <Iface>: Name of interface to monitor/list.

    例1:显示网络接口状态信息。

    [root@redhat2 ~]# netstat -i

    Kernel Interface table

    Iface       MTU Met    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg

    eth2       1500   0      883      0      0      0      916      0      0      0 BMRU

    eth3       1500   0      366      0      0      0      309      0      0      0 BMRU

    lo        16436   0     1034      0      0      0     1034      0      0      0 LRU

    [root@redhat2 ~]#

    例2:显示内核路由表信息。

    [root@redhat2 ~]# netstat -r

    Kernel IP routing table

    Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface

    192.168.12.0    *               255.255.255.0   U         0 0          0 eth3

    192.168.12.0    *               255.255.255.0   U         0 0          0 eth2

    [root@redhat2 ~]#

    三.chkconfig和service命令

        在Linux系统上可以使用chkconfig和service命令控制服务的启动、停止和重新启动,两者之间的区别在于使用service命令控制服务可以马上生效,而使用chkconfig命令控制服务需要等计算机重新启动后才会生效。

    1.chkconfig命令

      使用chkconfig命令主要用来设置下次重启计算机以后启动、停止服务,使用chkconfig命令不会立即启动或停止一项服务。

    [root@redhat2 ~]# chkconfig --help

    chkconfig version 1.3.49.3 - Copyright (C) 1997-2000 Red Hat, Inc.

    This may be freely redistributed under the terms of the GNU Public License.

    usage:   chkconfig [--list] [--type <type>] [name]

             chkconfig --add <name>

             chkconfig --del <name>

             chkconfig --override <name>

             chkconfig [--level <levels>] [--type <type>] <name> <on|off|reset|resetpriorities>

    [root@redhat2 ~]#

    例1:查看各种不同的运行等级中各项服务的状况。

    [root@redhat2 ~]# chkconfig --help

    chkconfig version 1.3.49.3 - Copyright (C) 1997-2000 Red Hat, Inc.

    This may be freely redistributed under the terms of the GNU Public License.

    usage:   chkconfig [--list] [--type <type>] [name]

             chkconfig --add <name>

             chkconfig --del <name>

             chkconfig --override <name>

             chkconfig [--level <levels>] [--type <type>] <name> <on|off|reset|resetpriorities>

    [root@redhat2 ~]# chkconfig --list

    NetworkManager  0:off   1:off   2:on    3:on    4:on    5:on    6:off

    abrt-ccpp       0:off   1:off   2:off   3:on    4:off   5:on    6:off

    abrt-oops       0:off   1:off   2:off   3:on    4:off   5:on    6:off

    abrtd           0:off   1:off   2:off   3:on    4:off   5:on    6:off

    acpid           0:off   1:off   2:on    3:on    4:on    5:on    6:off

    atd             0:off   1:off   2:off   3:on    4:on    5:on    6:off

    auditd          0:off   1:off   2:on    3:on    4:on    5:on    6:off

    avahi-daemon    0:off   1:off   2:off   3:on    4:on    5:on    6:off

    bluetooth       0:off   1:off   2:off   3:on    4:on    5:off   6:off

    cpuspeed        0:off   1:on    2:on    3:on    4:on    5:on    6:off

    crond           0:off   1:off   2:on    3:on    4:on    5:on    6:off

    例2:列出named服务在各个运行级别上的运行状态。

    [root@redhat2 ~]# chkconfig --list bluetooth

    bluetooth       0:off   1:off   2:off   3:on    4:on    5:off   6:off

    [root@redhat2 ~]#

     例3:在运行级别5,6上启动bluetooth服务。  

    [root@redhat2 ~]# chkconfig --list bluetooth

    bluetooth       0:off   1:off   2:off   3:on    4:on    5:off   6:off

    [root@redhat2 ~]# chkconfig --level 56 bluetooth  on

    [root@redhat2 ~]# chkconfig --list bluetooth 

    bluetooth       0:off   1:off   2:off   3:on    4:on    5:on    6:on

    [root@redhat2 ~]#

    2.service命令

      使用service命令可以启动或停止守护进程,service命令执行后会立即生效。

    [root@redhat2 ~]# service --help

    Usage: service < option > | --status-all | [ service_name [ command | --full-restart ] ]

    [root@redhat2 ~]#

      例1:启动 bluetooth服务。

    [root@redhat2 ~]# service  bluetooth start

    Enabling Bluetooth devices:

    [root@redhat2 ~]#

    例2:查看 bluetooth服务运行状态。

    [root@redhat2 ~]# service  bluetooth status

    Usage: /etc/init.d/bluetooth {start|stop}

    例3:重启bluetooth服务。 

    [root@redhat2 ~]# service bluetooth restart

    Usage: /etc/init.d/bluetooth {start|stop}

    [root@redhat2 ~]#

    例4:停止bluetooth服务。  

    [root@redhat2 ~]# service bluetooth stop

    Stopping Bluetooth services:

    [root@redhat2 ~]#

  • 相关阅读:
    15款经典图表软件推荐 创建最漂亮的图表
    CSS+JS打造的自适应宽度的滑动门和选项卡
    兼容多浏览器的加入收藏代码
    指针与引用深层次的区别
    反编译winform资源文件
    程序创业必过三关
    自动ping博客服务程序
    C#批量加水印程序
    C#应用程序随机启动
    失败降临是命中注定
  • 原文地址:https://www.cnblogs.com/numberone/p/4264816.html
Copyright © 2011-2022 走看看