博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CentOS配置网络(手动设置和自动获取)
阅读量:7141 次
发布时间:2019-06-28

本文共 4000 字,大约阅读时间需要 13 分钟。

  hot3.png

下面是两种设置网络连接的方法,在说怎么设置前,
一定要做好备份工作,特别是对于新手来说,如果修改的地方多了,你也会搞晕的,所以修改一个文件前最好要备份(bak)。

主要配置文件:

/etc/sysconfig/network-scripts/ifcfg-eth0

/etc/sysconfig/network

/etc/resolv.conf

/etc/hosts

重启网络配置,配置生效:

service network restart
一、修改文件来设置网络连接

1、自动获取IP的方法

自动获取根4个文件有关系

a)/etc/sysconfig/network-scripts/ifcfg-eth0

[zhangy network-scripts]$ cat ifcfg-eth0

# Marvell Technology Group Ltd. 88E8039 PCI-E Fast Ethernet Controller

DEVICE=eth0                               //由eth0来启动

BOOTPROTO=dhcp                     //获取IP的方式是自动获取,static是固定IP,none是手动

HWADDR=00:16:D3:A2:F6:09       //网卡的物理地址

IPV6INIT=yes                              //是否支持IP6

IPV6_AUTOCONF=yes                //IP6是否自动配置

ONBOOT=yes                           //启动时网络接口是否有效

b)/etc/sysconfig/network

[zhangy network-scripts]$ cat /etc/sysconfig/network

NETWORKING=yes                                 //网络是否可用

NETWORKING_IPV6=yes

HOSTNAME=localhost.localdomain //主机名,主机名在/etc/hosts里面配置

c)/etc/hosts

[zhangy network-scripts]$ cat /etc/hosts

# Do not remove the following line, or various programs

# that require network functionality will fail.

127.0.0.1               localhost.localdomain localhost     //根/etc/resolv.conf中search要对应,localhost.localdomain

::1             localhost6.localdomain6 localhost6

d)/etc/resolv.conf

[zhangy network-scripts]$ cat /etc/resolv.conf

; generated by /sbin/dhclient-script

nameserver 192.168.1.1      //DNS服务器对应的IP

search localdomain //搜索要找的域名,在/etc/hosts里面设定的有

2、手动设定

手动设定只要修改三个文件就可以了

a)/etc/sysconfig/network

[zhangy network-scripts]$ cat /etc/sysconfig/network

NETWORKING=yes

NETWORKING_IPV6=yes

#HOSTNAME=localhost.localdomain        //为什么要把主机名注释掉,一般先解析主机名或者域名,再解析DNS

GATEWAY=192.168.1.1                            //加上网关

b)/etc/sysconfig/network-scripts/ifcfg-eth0

[zhangy network-scripts]$ cat ifcfg-eth0

# Marvell Technology Group Ltd. 88E8039 PCI-E Fast Ethernet Controller

DEVICE=eth0

BOOTPROTO=none                         //启动为手动

BROADCAST=192.168.1.255

HWADDR=00:16:D3:A2:F6:09

IPADDR=192.168.1.108                   //设置的IP

NETMASK=255.255.255.0                //子网掩码

NETWORK=192.168.1.0

IPV6INIT=yes

IPV6_AUTOCONF=yes

ONBOOT=yes

TYPE=Ethernet                                //网络类型

c)/etc/resolv.conf

[zhangy network-scripts]$ cat /etc/resolv.conf

#; generated by /sbin/dhclient-script

#nameserver 192.168.1.1

#search localdomain

nameserver 116.228.111.118             //加上主DNS

nameserver 180.168.255.18              //加上次DNS

nameserver 8.8.8.8  #Google域名服务器

nameserver 8.8.4.4  #Google域名服务器

二、命令方法

netconfig是网络管理工具,RH装完机后,系统中就会有netconfig。虽然centos也是由RH演化来的,但是没有啊。不过没关系我们可能自己来装。

[zhangy download]$ wget ftp://ftp.nluug.nl/pub/os/Linux/distr/CentOS/5.0/os/i386/CentOS/netconfig-0.8.24-1.2.2.1.i386.rpm

[zhangy download]$ rpm -ivh netconfig-0.8.24-1.2.2.1.i386.rpm

error: can't create transaction lock on /var/lib/rpm/__db.000

[zhangy download]$ su root                   //别忘了要用root账户,不然就会报上面的错误

口令:

[root download]# rpm -ivh netconfig-0.8.24-1.2.2.1.i386.rpm

Preparing...                ########################################### [100%]

1:netconfig              ########################################### [100%]

[root download]# whereis netconfig     //查看一下netconfig在什么地方

netconfig: /usr/sbin/netconfig

[root@localhost]# netconfig --help --bootproto=(dhcp|bootp|none) Boot protocol to use //获取IP的方式,dhcp是自,bootp固定IP,none手动 --gateway=STRING Network gateway //指定网关 --ip=STRING IP address //指定IP地址 --nameserver=STRING Nameserver  //指定DNS --netmask=STRING Netmask //指定子网掩码 --hostname=STRING Hostname  //指定主机名 --domain=STRING Domain name  //指定域名 -d, --device=STRING Network device  //指定网络设备 --nodns No DNS lookups  //没有DNS查询 --hwaddr=STRING Ethernet hardware address //指定网卡的物理地址 --description=STRING Description of the device //描述 Help options: -?, --help Show this help message --usage Display brief usage message

1、自动获取IP

[root download]#/usr/sbin/netconfig -d eth0 --bootproto=dhcp

2、手动设置IP

[root download]#/usr/sbin/netconfig -d eth0 --bootproto=none --ip=192.168.1.108 --netmask=255.255.255.0 --gateway=192.168.1.1 --nameserver=116.228.111.118

三、配置中遇到的一些问题

1、重启网络不起作用

service network restart    重启后还是无法连接网络,我可以确定我配置的没问题,重新启动一下电脑就可以了。

2,备份ifcfg-eth0文件的时候要注意,前面不要带有ifcfg-eth0

上图中我把自动获取IP的配置文件ifcfg-eth0作了备份,名子为ifcfg- eth0_dhcp,重新启动网络的时候,这个文件依然被执行了,开始的时候,我并没注意到这一点,等我ifconfig查看网络的时候,发现我做的修改 根本没有启作用,并且/etc/resolv.conf里面做的修改又被盖了。所以备份的时候,前面不要带有ifcfg-eth0

转载于:https://my.oschina.net/junn/blog/153575

你可能感兴趣的文章
Jquery Validform使用入门datatype参数详解
查看>>
phpdoc
查看>>
hdu1874
查看>>
linux学习笔记十一:srpm软件包(*.src.rpm)的安装
查看>>
关于android sensor架构
查看>>
Ghost使用方法
查看>>
LAMP安装步骤(操作系统CentOS6)
查看>>
MFA 经验分享
查看>>
备份恢复软件哪个更好用
查看>>
JMeter名词解析说明
查看>>
How to change window manager to OpenBox in Linux Mint 13 Mate
查看>>
iframe自适应内容高度
查看>>
Apache 的 httpd.conf 详解
查看>>
在eclipse上面搭建hbase环境
查看>>
curl json串
查看>>
nagios监控(二)
查看>>
linux和windows的文件共享系统samba的安装
查看>>
Aspose.Words使用教程之如何写入纯文本(TXT)文件
查看>>
Hadoop源码之(一)源码编译
查看>>
yum安装postgresql8.4
查看>>