Linux服务器联网

来源:互联网 发布:阿里巴巴菜鸟网络面试 编辑:程序博客网 时间:2024/05/16 07:08
一、linux网络配置相关文件
1.网络配置主要信息
  所在文件:/etc/sysconfig/network-scripts/ifcfg-eth0
[root@bes2 network-scripts]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"               ——网卡号
HWADDR="00:0C:29:E7:D3:D1"      ——mac地址
NM_CONTROLLED="yes"           ——是否被其他网络管理软件管理              
ONBOOT="yes"               ——是否在开机时启动该网卡
BOOTPROTO="static"    ——启动该网络接口时,使用何种协议? 如果是手动给予 IP 的环境,请输入 static 或 none ,如果是自动取得 IP 的时候, 请输入 dhcp
IPADDR="xxxxxx"               ——手动设定的ip
NETMASK="xxxxxx"     ——手动设定的子网掩码
GATEWAY="xxxxxx"          ——手动设定的网关

2.主机名信息
     所在文件:/etc/sysconfig/network
[root@bes2 network-scripts]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=xxxxxx

3.DNS IP信息
所在文件:/etc/resolv.conf
[root@bes2 network-scripts]# cat /etc/resolv.conf
nameserver xxxxxx

4.host文件信息
所在文件:/etc/hosts
[root@bes2 network-scripts]# cat /etc/hosts
xxxxxx   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

二、连上internet
1.设定IP/NETMASK/GATEWAY
1)设定
[root@bes2 network-scripts]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
HWADDR="xxxxxx"
NM_CONTROLLED="yes"               
ONBOOT="yes"              
BOOTPROTO="static"   
IPADDR="xxxxxx"
NETMASK="xxxxxx"
GATEWAY="xxxxxx"

2)重新启动网卡
[root@bes2 network-scripts]# /etc/init.d/network restart

3)观察
①查看ip相关参数是否正确
[root@bes2 network-scripts]# ifconfig eth0(网卡名称)
eth0      Link encap:Ethernet  HWaddr xxxxxx
          inet addr:xxxxxx  Bcast:xxxxxx  Mask:xxxxxx
          inet6 addr: fe80::20c:29ff:fee7:d3d1/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3849994 errors:0 dropped:0 overruns:0 frame:0
          TX packets:18953 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:274175113 (261.4 MiB)  TX bytes:2022322 (1.9 MiB)
②检查路由设定是否正确,应当包含上面设定的网关
[root@bes2 network-scripts]# route -n 
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

③检查与路由器是否联通
[root@bes2 network-scripts]# ping -c 3 XXXXX(网关地址)
PING XXXXX() 56(84) bytes of data.XXXXX
64 bytes from XXXXX: icmp_seq=1 ttl=255 time=3.06 ms
64 bytes from XXXXX: icmp_seq=2 ttl=255 time=1.63 ms
64 bytes from XXXXX: icmp_seq=3 ttl=255 time=1.78 ms

--- XXXXX ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2005ms
rtt min/avg/max/mdev = 1.637/2.163/3.065/0.642 ms

2.DNS服务IP设定
1)设定
[root@bes2 network-scripts]# vim /etc/resolv.conf 
nameserver XXXXX——所属网络的DNS服务器IP,需要询问网络管理员

2)检查,有正常返回即联通
[root@bes2 network-scripts]# dig www.baidu.com

; <<>> DiG 9.7.0-P2-RedHat-9.7.0-5.P2.el6 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 32475
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;www.baidu.com.               IN     A

;; ANSWER SECTION:
www.baidu.com.          602     IN     CNAME     www.a.shifen.com.
www.a.shifen.com.     45     IN     A     61.135.169.121
www.a.shifen.com.     45     IN     A     61.135.169.125

;; Query time: 7 msec
;; SERVER: 10.1.1.7#53(10.1.1.7)
;; WHEN: Thu Sep 18 23:34:33 2014
;; MSG SIZE  rcvd: 90


------上述设置完毕,且各项检查正常,则可以正常联网(前提是当前网络不需要代理,如果需要代理,还需对代理进行设置,代理设置见下方)

3.主机名修改
1)修改
[root@bes2 network-scripts]# vim /etc/sysconfig/network

NETWORKING=yes
HOSTNAME=XXXXX

2)检查主机名有没有对应的IP
[root@bes2 network-scripts]# ping -c 3 XXXXX
PING XXXXX (XXXXX) 56(84) bytes of data.
64 bytes from bt-199-034.bta.net.cn (202.106.199.34): icmp_seq=1 ttl=242 time=4.83 ms
64 bytes from bt-199-034.bta.net.cn (202.106.199.34): icmp_seq=2 ttl=242 time=4.76 ms
64 bytes from bt-199-034.bta.net.cn (202.106.199.34): icmp_seq=3 ttl=242 time=4.77 ms

--- XXXXX ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2008ms
rtt min/avg/max/mdev = 4.769/4.793/4.839/0.086 ms

问题:当前网络可以访问外网,但是从外网下载不行,可以ping通,但是wget不行
本质:其实还是没有联通外网
[root@bes2 ~]# ping -c 3 www.baidu.com      ——可以ping通
PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.
64 bytes from 61.135.169.125: icmp_seq=1 ttl=50 time=5.43 ms
64 bytes from 61.135.169.125: icmp_seq=2 ttl=50 time=5.11 ms
64 bytes from 61.135.169.125: icmp_seq=3 ttl=50 time=22.3 ms

--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2025ms
rtt min/avg/max/mdev = 5.116/10.957/22.320/8.036 ms
[root@bes2 ~]# wget www.baidu.com      ——不能下载
--2014-09-24 16:07:05--  http://www.baidu.com/
正在解析主机 www.baidu.com... 61.135.169.121, 61.135.169.125
正在连接 www.baidu.com|61.135.169.121|:80... 失败:拒绝连接。
正在连接 www.baidu.com|61.135.169.125|:80... 失败:拒绝连接。

原因:当前网络有代理,没有在服务器设置代理
[root@bes2 ~]# echo $http_proxy
查看代理设置为空,设置代理变量
[root@bes2 ~]# export http_proxy=http://代理ip:端口   ——这种方式是设置临时的环境变量,下次登录就失效了,设置环境变量永久生效的方法,见下方
[root@bes2 ~]# wget www.baidu.com
--2014-09-25 10:04:52--  http://www.baidu.com/
正在解析主机 proxy.asiainfo.com... 10.1.1.21, 10.1.1.20
正在连接 proxy.asiainfo.com|10.1.1.21|:8080... 已连接。
已发出 Proxy 请求,正在等待回应... 200 OK
长度:未指定 [text/html]
正在保存至: “index.html”

    [  <=>                                                                                        ] 80,077       113K/s   in 0.7s   

2014-09-25 10:04:53 (113 KB/s) - “index.html” 已保存 [80077]

设置环境变量永久生效的方法
[root@bes2 ~]# cp -r /etc/profile /etc/profile.bak    ——备份配置文件
[root@bes2 ~]# vim /etc/profile                    ——编辑配置文件
在配置文件的最下方加入下面这句话,然后保存文件
export http_proxy=http://代理ip:端口

0 0
原创粉丝点击