centos初装系统-无法上网

运行ping时,发现ping不通

[root@www ~]# ping www.baidu.com
ping: www.baidu.com: Name or service not known

如果ping域名的时候出现ping:unknown host  xxx.xxx
但是ping IP地址的时候可以通的话
可知是dns服务器没有配置好,
查看一下配置文件/etc/resolv.conf,里面是否有nameserver xxx.xxx.xxx.xxx,比如使用dns服务器 nameserver 8.8.8.8,如果有,修改一个可用的dns服务器,如8.8.8.8或者4.4.4.4,保存退出即可!

当然,如果连ip都ping 8.8.8.8都ping不通的话,那么就说明网络配置有问题:可以这样解决:

1、编辑网络配置

vi /etc/sysconfig/network-scripts/ifcfg-enp0s3  #注 网络配置文件名可能会有不同,在输入到ifcfg时,可以连续按两下tab键,获取提示,比如我的机器 为 ifcfg-enp0s3

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s3
UUID=098aaea2-65b0-49b7-821d-1fe636293b46
DEVICE=enp0s3
ONBOOT=yes    #### 刚打开这一行是 no ,我编辑改成了yes,只需要按 i 就能编辑,编辑完按esc退出编辑模式,然后按:wq保存文件 [注意:是英文的冒号]
ZONE=public

2、重启网络服务

centos6系统下  
service network restart

centos7系统下[速度有点慢2秒钟左右]
systemctl restart network

3、测试效果

[root@localhost ~]# ping baidu.com
PING baidu.com (39.156.69.79) 56(84) bytes of data.
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=53 time=39.6 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=53 time=78.5 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=53 time=71.8 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=4 ttl=53 time=136 ms


### 按 contrl键 + c键  取消

还可以自定义DNS

vi /etc/resolv.conf 
nameserver 8.8.8.8
nameserver 8.8.4.4

4、查看本机IP

ifconfig 安装

##### 如果查看本机,发现没有 ifconfig 命令行
[root@localhost ~]# ifconfig
-bash: ifconfig: command not found
[root@localhost ~]# 


##### 首先确认下是否是环境变量没有ifconfig 引起。 
[root@localhost ~]# ls /sbin/ifconfig
ls: cannot access /sbin/ifconfig: No such file or directory
[root@localhost ~]# 


##### 以上确定了系统是没有安装ifconfig,下面我们来安装     
##### locale是本地语言设置问题可忽略
[root@localhost ~]# yum install ifconfig
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: centosx4.centos.org
 * updates: mirrors.huaweicloud.com
No package ifconfig available.
Error: Nothing to do
[root@localhost ~]# 


### 提示没有ifconfig安装包。我们再使用yum search ifconfig来搜索下ifconfig的相关
[root@localhost ~]# yum search ifconfig
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirror-hk.koddos.net
 * updates: mirrors.huaweicloud.com
=================================== Matched: ifconfig ====================================
net-tools.i686 : Basic networking tools
[root@localhost ~]# 



##### 查看ifconfig匹配的是net-tools.i686包                     
 yum install net-tools.i686 -y

ifconfig使用

### 使用ifconfig命令,可以查看本地IP,下面这个是 没有联网时的
[root@localhost ~]# ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 08:00:38:56:29:2c  txqueuelen 1000  (Ethernet)
        RX packets 95  bytes 9319 (9.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 104  bytes 9383 (9.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 152  bytes 13024 (12.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 152  bytes 13024 (12.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


### 下面这个是 联网 成功的 本机IP是 192.168.1.128

enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.128  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fd38::3d2:a2b7:39cb:278a  prefixlen 64  scopeid 0x20<link>
        ether 08:00:38:56:29:2c  txqueuelen 1000  (Ethernet)
        RX packets 111  bytes 11112 (10.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 124  bytes 11293 (11.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 156  bytes 13368 (13.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 156  bytes 13368 (13.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ip addr 命令(也可以查看本机ip)

[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:38:56:29:2c brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.128/24 brd 192.168.1.255 scope global noprefixroute dynamic enp0s3
       valid_lft 5226sec preferred_lft 5226sec
    inet6 fd38::3d2:a2b7:39cb:278a/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@localhost ~]# 

netstat和ss命令(也可以查看本机ip)

  • centos6 可以使用 netstat
  • centos7 可以使用 ss -ant

5、centos6下编辑ifcfg-xxx 仍无法上网

输入ifconfig命令查看是否有内网ip地址,如果没有,那么配置一下: eth0是设备名,具体看ifconfig 显示的设备名。

ifconfig eth0 192.168.1.100  netmask 255.0.0.0 up

另外使用route命令查看一下当前的默认网关,如果没有,也配置一个,

route add default gw 192.168.1.1

配置好这两个以后,就可以正常的ping通网络了,如果还不能ping www.baidu.com,那么就要去查看dns服务器啦


参考:

https://blog.csdn.net/qq_30180559/article/details/79535596
https://jingyan.baidu.com/article/eb9f7b6d42636d869364e8c9.html

https://blog.csdn.net/menlinshuangxi/article/details/7968955

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments