码迷,mamicode.com
首页 > 系统相关 > 详细

Linux之网络配置详解

时间:2015-12-26 23:38:43      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:ip地址   互联网   

Linux之网络配置详解


    在安装好一台linux过后,我们需要做些什么让主机能够连上互联网?能够和其它主机进行通信呢?下面我们一起来看看需要配置哪些:

        

    1. ip地址配置:

            版本:简单来说现在主流版本是IPv4,但是由于互联网的普及,IP地址十分紧缺,所以才出现了IPv6,下面我们主要讲解的是IPv4

                IPv4:32bits

                 IPv6:128bits

            分类:

                A类:1-127

                B类:128-191

                C类:192-223

                D类:224-239,用于组播

                E类:240-255,用于科研

     2.子网掩码:

            子网掩码用于识别IP地址的类别

                A类:255.0.0.0 = IP地址/8

                B类:255.255.0.0 = IP地址/16

                C类:255.255.255.0 = IP地址/24




    3.网关:

            配置完IP地址和子网掩码后你就可以在同网段之内进行通信了,网关的配置可以让你能够访问其它网段的主机,网关一般性设定为网络内路由器地址

            


    4.DNS:

            Domain Name System,域名系统;主机如果需要连上互联网,访问网络上的资源的话,需要设置DNS。





下面我们来一起看下 ifcfg命令家族/ip命令/ss命令 的使用方法:


    ifcfg命令家族:ifconfig、route、netstat

        

        ifconfig:查看和配置网卡信息

        技术分享            

            ifconfig [选项] [接口名]

                选项:

                    -a:显示所有接口

            例如:配置接口IP为192.168.0.1/24=192.168.0.1 netmask 255.255.255.0

             技术分享



        route:查看管理路由信息

                类型:

                    主机路由:单个IP地址

                    网络路由:一个网段

                    默认路由:任意网络

            

            route -n:查看路由条目

            技术分享

            添加路由条目:

                 route add [-net|-host] ip/mask [gw|dev]    

            删除路由条目:

                  route del [-net|-host] ip/mask [gw|dev]

            添加默认路由:

                 route add default [gw|dev]

                         -net:表示后面接的路由是一个网络

                         -host:表示后面接的路由是一个主机

                         ip/mask:制定目标ip地址和子网掩码

                         gw:gateway的简写,一般指向路由器IP

                         dev:从哪块网卡出去;可省略

            例如:添加一条指向从eno33554984出去到192.168.1.113的路由   

            技术分享        

            例2:把上面添加的条目删除

            技术分享      

            



     netstat命令:命令用于显示各种网络相关信息,如网络连接,路由表,接口状态等

                                    

                             常用参数:                                                    

                                                -a (all)显示所有选项,默认不显示LISTEN相关
                                                -t (tcp)仅显示tcp相关选项
                                                -u (udp)仅显示udp相关选项
                                                -n 拒绝显示别名,能显示数字的全部转化成数字
                                                -l 仅列出有在 Listen (监听) 的服务状态

                                                -p 显示建立相关链接的程序名
                                                -r 显示路由信息,路由表
                                                -e 显示扩展信息,例如uid等
                                                -s 按各个协议进行统计
                                                -c 每隔一个固定时间,执行该netstat命令

                        上面的参数可以结合使用:

                                               如下:                                        

                                            netstat  -rn                                  

                                            netstat  -anpt

                                  netstat  -tan



        


    ip命令家族:用来显示或管理Linux主机的路由、网络设备、策略路由等功能

                        用法:

                                ip [选项] OBJECT [参数]

                                              选项:

                                                        -s:输出更详细的信息

                                                        -4:制定使用的网络层协议是IPv4

                                                

                                                OBJECT:

                                                        link:网络设备配置

                                                                ip link set:设置设备属性

                                                                            dev 接口名:指明要管理的设备,dev可省略

                                                                            up 和 down:启动和禁用设备

                                                                            name 接口名:更改接口名

                                                         

                                           # ip link set dev 接口名 down

                                                            技术分享

                                                                 

                                                                 ip link help:帮助信息

                                                                 ip link show:设备属性

                                                            技术分享


                                                        addr:ip地址管理

                                                                ip addr add :添加新地址

                                                                       label 标签名:备用ip地址接口名,例如:eth1:0

                       # ip addr add 192.168.0.2/24 label eno16777736:0

                                                             或者

                                                          ifconfig eno16777736:0 192.168.0.2/24  

                   技术分享 

                                                   

                                                                  



                                                                  ip addr del:删除地址

                                                                  ip addr show:查看地址

                                                                 ip addr flush:清空所有地址

                                  


                  route:路由表管理

                                                                ip route add:添加新路由

                                                            #设置网络    

                                                                ip route change:修改路由

    

                                                                ip route delete:删除路由


                                                                ip route show:查看路由

        

    

        


        

    ss命令:查看网络状态

        用法:

                        ss [选项] [filter]

            

                          选项:                

                            -t:tcp协议的相关连接

                    -u:UDP相关的连接                

                    -l:监听状态的连接

                    -a:所有状态的连接

                    -n:数字格式

                    -p:相关的程序及其PID

                    -m:内存用量

                    -o:显示计数器信息

                  filter:

                      state:

                        LISTEN:监听

                        ESTALISEHD:建立的链接

                        CLOSING:关闭

                        ...

    

                      expression:过滤器

                        dport=                    

                        sport=

    

                示例:

                1.显示80端口的tcp连接

                技术分享

                



    




        配置文件:

        

    接口地址:

    /etc/sysconfig/network-scripts/ifcfg-接口名

            技术分享

            

DEVICE:此配置文件对应的设备的名称

ONBOOT:在服务引导过程中,是否激活此接口

UUID:此设备的唯一标识

BOOTPROTO:使用什么方式获取IP;dhcp,static

TYPE:接口类型:Ethernet,Bridge

DNS1:第一DNS服务器

DNS2:备用DNS服务器

GATEWAY:默认网关

IPADDR:IP地址

PREFIX(NETMASK):子网掩码

                

    路由:

    /etc/sysconfig/network-scripts/route-接口名

           

            技术分享   

        




以上是简单介绍

Linux之网络配置详解

标签:ip地址   互联网   

原文地址:http://luoj5.blog.51cto.com/9389984/1728661

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!