Linux系统基础网络配置老鸟精华篇

    /    2018-09-30

对于linux高手看似简单的网络配置问题,也许要说出所以然来也并不轻松,因此仍然有太多的初学者徘徊在门外就不奇怪了,这里,老男孩老师花了一些时间总结了这个文档小结,也还不够完善,欢迎大家补充,交流。

一、配置修改主机名

临时方法:

hostname oldboy

退出当前shell重新登陆即可生效。此法只能临时修改生效,重起系统后失效。

提示:很多人使用hostname 主机名来修改,其实这个只是做为暂时的,重启后将恢复到配置前的主机名.

永久方法:

法一:

步骤1:

vi /etc/sysconfig/network

将里面的HOSTNAME=XX改成HOSTNAME=oldboy,然后保存。

提示:这里改完后,执行/etc/init.d/network restart或 source /etc/sysconfig/network等做法都不生效

如果要单一的修改这个文件可能就需要重起服务器了。

题外话:这里应该是有一个直接生效的方法的系统自带的当前及永久生效方法的,就是就是加载下hostname调用下配置文件的命令执行下就好,

把这个问题留给大家了!

步骤2:

然后通过命令行继续修改:hostname oldboy ,完成后退出重新登陆即生效

提示:这样就能保证临时生效,重起系统系统也生效了。

法二:

即时永久都生效的另外改法:执行setup-网络配置-- Edit DNS configuration-->

DNS configuration                  
 x                               x 
 x Hostname      oldboy_______   x ===========>修改这里
 x Primary DNS   202.106.0.20___ x 
 x Secondary DNS 211.147.6.3____ x 
 x Tertiary DNS  _______________ x 
 x Search        _______________ x

提示:在这里改完保存后会及时生效,重起也生效,从这就可以看出来,系统还是有直接临时永久都生效的方法。

特别说明:使用以上的方法都可能导致/etc/hosts中的127.0.0.1对应的主机名未修改,此时,应该手动修改:如:

[root@oldboy ~]# cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1              oldboy localhost.localdomain localhost ===========>修改这里的oldboy
::1             localhost6.localdomain6 localhost6
10.0.0.190  www.oldboyedu.com

提示:如果/etc/hosts不改,以后会遇到一些问题,如sendmail启动缓慢,ldap服务解析缓慢,sudo切用户缓慢等等都是主机名和/etc/hosts中的解析不对应导致的。

拓展:有关重起系统设置主机名的脚本:

[root@oldboy ~]# grep -i hostname /etc/rc.d/rc.sysinit 
HOSTNAME=`/bin/hostname`
if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; then
    HOSTNAME=localhost
# Set the hostname.
update_boot_stage RChostname
action $"Setting hostname ${HOSTNAME}: " hostname ${HOSTNAME} ===========>这里就是系统重起后执行rc.sysinit后修改机器名的地方。
        # Use the network address to set the hostname of the client.  This
        if [ "$HOSTNAME" = "localhost" -o "$HOSTNAME" = "localhost.localdomain" ]; then
                        hostname ${HOSTNAME}
                mount -t nfs $CLIENTSTATE/$HOSTNAME $STATE_MOUNT -o rw,nolock
        # Reset the hostname.
        action $"Resetting hostname ${HOSTNAME}: " hostname ${HOSTNAME}
[root@oldboy ~]# grep -i hostname /etc/rc.d/init.d/network ===========>这里没有和主机名相关的信息。

二、网卡配置文件说明

[root@oldboy ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 ==》这里是eth0网卡设备的默认配置文件
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth0 ================》这里是网卡名称第一块网卡为eth0,第二块为eth1...
BOOTPROTO=static============》static为固定IP地址,非动态DHCP获取
BROADCAST=10.0.0.255 =======》这里是广播地址,一般为网络地址的最后一个地址。
HWADDR=00:0C:29:0E:5F:63 ===》这里mac地址,不同的机器不能重复,尤其是在刻隆虚拟机或复制网卡配置时要注意。
IPADDR=10.0.0.161 ==========》这里是IP地址
NETMASK=255.255.255.0=======》子网掩码/24
NETWORK=10.0.0.0   =========》网段设置
ONBOOT=yes =================》开机网卡自启动
GATEWAY=10.0.0.254==========》网关的配置,也可以命令行通过route添加删除。
TYPE=Ethernet===============》类型

提示:第一块网卡为ifcfg-eth0,第二块为ifcfg-eth1...

三、配置修改ip地址

配置服务器IP及DNS等网络配置的方法:

1)setup-网络配置,然后修改。

2)直接编辑或手写上面配置文件。

提示:以上两个方法一般需要重起或者重新加载/etc/init.d/network restart 或/etc/init.d/network reload才能生效。

四、配置修改网关gw

法一:修改/etc/sysconfig/network

[root@oldboy network-scripts]# cat /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=oldboy
GATEWAY=10.0.0.254 ====》这个就是网关的配置

提示:需要执行/etc/init.d/network reload 即可生效。

法二:修改/etc/sysconfig/network-scripts/ifcfg-eth0

[root@oldboy network-scripts]# cat ifcfg-eth0
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth0
BOOTPROTO=static
BROADCAST=10.0.0.255
HWADDR=00:0C:29:0E:5F:63
IPADDR=10.0.0.161
NETMASK=255.255.255.0
NETWORK=10.0.0.0
ONBOOT=yes
GATEWAY=10.0.0.254====》这个就是增加网关的配置,默认没有

提示:需要执行/etc/init.d/network restart 或reload 即可生效。

特别注意:

1)/etc/sysconfig/network-scripts/ifcfg-eth0中的网关配置优先于/etc/init.d/network

2)注意配置的大小写,gATEWAY=10.0.0.254这样的小写格式就不对。

法三:通过命令修改默认网关:

[root@oldboy network-scripts]# route del default gw 10.0.0.254 ==》首先删除之
[root@oldboy network-scripts]# route -n ==》查看命令
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
[root@oldboy network-scripts]# route add default gw 10.0.0.254 ==》添加默认网关
[root@oldboy network-scripts]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
0.0.0.0         10.0.0.254      0.0.0.0         UG    0      0        0 eth0

注意:此修改为临时修改,重新加载或启动网卡后就会失效,解决方法就是尽量在配置文件里配置,实在有需求就把route add default gw 10.0.0.254放到rc.local自启动文件里/etc/init.d/network网卡restart/reload段脚本配置里实现开机自启动或重起网卡也生效,这也有点太复杂了,咱就别这样做了,好不?

五、修改主机DNS配置

法一:setup--网络配置图形操作

法二:修改/etc/resolv.conf ==》这里主机DNS配置文件的路径

[root@oldboy network-scripts]# cat /etc/resolv.conf 
nameserver 202.106.0.20
nameserver 8.8.8.8
nameserver 211.147.6.3

提示:每个配置一行这个resolv.conf修改后直接生效,注意,不是nameservers,

另,这个配置文件也是有知识的,同学们可以man resolv.conf查看。

六、查看ip、网关或路由、DNS配置

1.查看ip配置方法

[root@oldboy ~]# ifconfig ==》直接命令名表示显示所有网卡IP
eth0      Link encap:Ethernet  HWaddr 00:0C:29:1D:28:D5  
          inet addr:10.0.0.190  Bcast:10.0.0.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe1d:28d5/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4154 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3107 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:367589 (358.9 KiB)  TX bytes:481210 (469.9 KiB)
          Interrupt:185 Base address:0x1400

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:36 errors:0 dropped:0 overruns:0 frame:0
          TX packets:36 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:3744 (3.6 KiB)  TX bytes:3744 (3.6 KiB)

[root@oldboy ~]# ifconfig eth0 ==》显示指定网卡IP
eth0      Link encap:Ethernet  HWaddr 00:0C:29:1D:28:D5  
          inet addr:10.0.0.190  Bcast:10.0.0.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe1d:28d5/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4170 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3130 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:368687 (360.0 KiB)  TX bytes:484496 (473.1 KiB)
          Interrupt:185 Base address:0x1400

2.查看网关和路由方法

[root@oldboy network-scripts]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
0.0.0.0         10.0.0.254      0.0.0.0         UG    0      0        0 eth0

[root@oldboy ~]# netstat -rn  ==》此法可以不记,记route -n即可。
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
10.0.0.0        0.0.0.0         255.255.255.0   U         0 0          0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0

查看linux命令帮助的命令man or info or命令 --help:

例:查看route用法

man route 和 info route 或route --help

route功能强大,同学们要熟练掌握,上面仅是一个简单的用法。

3.查看DNS配置方法

[root@oldboy ~]# cat /etc/resolv.conf ==》这是常见的检查方法
nameserver 202.106.0.20
[root@oldboy ~]# ping g.cn ==》这是常见的检查方法,返回ip地址了说明通的。
PING g.cn (74.125.71.160) 56(84) bytes of data.
64 bytes from hx-in-f160.1e100.net (74.125.71.160): icmp_seq=1 ttl=49 time=51.7 ms

[root@oldboy ~]# host www.oldboyedu.com ==》这是常见的检查方法,返回ip地址了说明dns通的。
www.oldboyedu.com has address 211.100.98.99

--- g.cn ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 51.710/51.710/51.710/0.000 ms
[root@oldboy ~]# dig www.oldboyedu.com

; <<>> DiG 9.3.6-P1-RedHat-9.3.6-4.P1.el5 <<>> www.oldboyedu.com
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 45736
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

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

;; ANSWER SECTION:
www.oldboyedu.com.      600     IN      A       211.100.98.99

;; Query time: 90 msec
;; SERVER: 202.106.0.20#53(202.106.0.20)
;; WHEN: Mon Feb 20 11:50:57 2012
;; MSG SIZE  rcvd: 51

[root@oldboy ~]# nslookup
> www.oldboyedu.com
Server:         202.106.0.20
Address:        202.106.0.20#53

Non-authoritative answer:
Name:   www.oldboyedu.com
Address: 211.100.98.99

七、如果win32和linux服务器无法连接网络,分别描述排查方法?

此题留给大家了。大家可以评论或单独和我交流。

基本检查思路:

1. 物理链路是否通畅

2. 本机IP,路由,DNS的设置情况是否达标。

3. 测试到网关或路由器的通畅情况。

4. 测试ping公网ip的通常情况(平时要记几个外部IP)

5. 测试DNS的通畅,可以ping etiantian.org看是否出对应IP。

根据每级的结果来排查,如果是系统管理员,以上问题都检查了后,还可以在网关路由服务器上向上向下测试。

逐级排查。

(2)

分享至