网络设置

1.ip

ifconfig

临时型添设置ip地址

ifconfig  [ethX  1.1.1.1/24]  [up|down]:配置哪块网卡的IP地址、子网掩码、开启、关闭;不加任何参数代表查看在工作的网卡信息。 只有管理管可以配置
  -a:显示所有网卡接口信息。

2.路由

route:默认不带任何参数显示路由信息
  -n:以数字的形式显示路由信息

[root@localhost ~]# route 
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     *               255.255.255.0   U     1      0        0 eth0
default         192.168.0.1     0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 eth0

临时型添加、删除路由:

  route add  -net|-host  目的地/掩码  gw  下一跳   添加去往目的地为XX的网络或者本地路由下一跳为XX 
  route add  default gw 下一跳        添加默认路由

[root@localhost ~]# route add -net 1.0.0.0/8 gw 192.168.0.1
[root@localhost ~]# route add default gw 192.168.0.1
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0
1.0.0.0         192.168.0.1     255.0.0.0       UG    0      0        0 eth0
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 eth0

  route del -net|-host  目的地/掩码    删除一条路由
  route del default            删除默认路由

[root@localhost ~]# route del -net 1.0.0.0/8
[root@localhost ~]# route del default
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0

 3.配置文件

在配置文件中指定参数是永久保存的,同样可以使用setup启用图形界面进行配置

★ /etc/sysconfig/network-scripts/ifcfg-接口名称:网卡配置文件。重启网络服务或主机时才生效。

[root@bogon ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
BOOTPROTO="dhcp"
HWADDR="00:0C:29:3F:B2:71"
MTU="1500"
NM_CONTROLLED="yes"
ONBOOT="yes"

DEVICE="":关联的设备名称,要与文件名的后半部分保持一致,也就是ifcfg-____。
BOOTPROTO="":{static|none|dhcp|bootp},引导协议,静态地址static或none。动态获取dhcp
IPADDR="":IP地址
NETMASK="":子网掩码
GATEWAY="":设定默认网关
ONBOOT="":开机时是否自动激活网络接口
HWADDR="":硬件地址,要与硬件中的地址保持一致,可以省略
USERCTL="":{yes|no}是否允许普通用户控制此接口
PEERDNS="":{yes|no}是否在BOOTPROTO为dhcp时接收由dhcp服务器指定的dns地址

一块网卡可以有多个地址,但是子地址不能使用DHCP获取,以eth0为例。配置方法为

[root@bogon ~]# ifconfig eth0:0 1.1.1.1/10
[root@bogon ~]# ifconfig eth0:0
eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:3F:B2:71  
          inet addr:1.1.1.1  Bcast:1.63.255.255  Mask:255.192.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

永久配置方法为↓。新建类似这个文件

[root@bogon ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0:0
DEVICE=eth0:0
IPADDR="1.1.1.1"
NETMASK="255.0.0.0"
GATEWAY="1.1.1.254"
BOOTPROTO="static"
ONBOOT="yes"

/etc/sysconfig/network-scripts/route-ethX:路由配置文件
添加方法:
    目的地/掩码  via  下一跳地址
例    1.1.1.1/10  via  2.2.2.2

/etc/resolv.conf:DNS配置文件,最多3个
添加方法:
         nameserver  DNS_IP
         nameserver  DNS_IP
         nameserver  DNS_IP
例  nameserver  202.99.96.68

/etc/hosts:指定本地解析
添加方法:
       主机IP  主机名  别名
例    1.1.1.1   www.a.com  www

/etc/sysconfig/network:网络服务配置文件
HOSTNAME="":配置主机名
NETWORKING="":{yes|no}是否启用网络功能


4.iproute2

iproute2同样是网络管理工具

ip
  link:网络接口属性
    show:查看当前网络接口状况;ip link show
    set:设置设备状态;ip link set ethX {up|down}
  addr:地址
    add:设定网卡地址;ip addr add ip地址/掩码  dev  ethX
    del:删除ip地址;ip addr del ip地址/掩码  dev  ethX
    show:查看接口;ip addr show dev  ethX  [to 1.1/24] 这样可以查看以1.1开头掩码为24位的ip有哪些
    flush:清空地址;ip addr flush dev  ethX  [to  1.1/24]同样可以清空掉以1.1开头,掩码为24位的ip  
  route:路由
    add:添加路由;ip route add to  ip地址/掩码 dev  ethX  via  下一跳地址
    flush:删除路由;ip route flush to  ip地址/掩码
 

初学linux,每学到一点东西就写一点,如有不对的地方,恳请包涵!
原文地址:https://www.cnblogs.com/forlive/p/8531111.html