Linux网络基本配置
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.基本网络配置概述
将Linux主机接入到网络,需要配置网络相关设置。一般包括如下内容: 主机名 IP/netmask 路由:默认网关 DNS服务器 主DNS服务器 次DNS服务器 第三DNS服务器
1>.Linux操作戏他中DNS解析次序
[root@node101.yinzhengjie.org.cn ~]# grep hosts /etc/nsswitch.conf | grep -v ^# hosts: files dns myhostname [root@node101.yinzhengjie.org.cn ~]#
温馨提示:
files:
指定的是本地文件"/etc/hosts"。
dns:
指的是网络上的DNS服务器,一般情况会直接配置在网卡或是"/etc/resolv.conf"文件中。一般DNS会配置多个,以防DNS损坏故障。
myhostname:
顾名思义,如果上面2个都解析不了就用本地主机名来进行解析,最简单的办法就是使用网络层ICMP协议来ping当前主机名,你会发现尽管没有在"/etc/hosts"和DNS中有响应的解析,但的确会ping通,而且ping通的地址解析往往为当前主机IP地址。
2>.修改主机名
[root@node101.yinzhengjie.org.cn ~]# hostnamectl status #显示当前主机状态 Static hostname: node101.yinzhengjie.org.cn Icon name: computer-vm Chassis: vm Machine ID: 9ccdf621e5984f3b8626614f6fa15a48 Boot ID: c8b891684b394b2b88b9f06b91658368 Virtualization: kvm Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-957.el7.x86_64 Architecture: x86-64 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cat /etc/hostname node101.yinzhengjie.org.cn [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# hostname node101.yinzhengjie.org.cn [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# hostnamectl set-hostname centos7.yinzhengjie.org.cn #改命令可以同时修改主机名和配置文件。 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# hostname centos7.yinzhengjie.org.cn [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cat /etc/hostname centos7.yinzhengjie.org.cn [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# exec bash #当然我们得重新启用一个bash才能让主机名修改立即生效,或者退出当前终端重新登录也可以生效。 [root@centos7.yinzhengjie.org.cn ~]#
3>.CentOS 6.x 操作系统建议修改主机名后顺手添加本地解析文件,而CentOS 7.x则不用
[root@centos7.yinzhengjie.org.cn ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.30.1.101 node101.yinzhengjie.org.cn 172.30.1.102 node102.yinzhengjie.org.cn 172.30.1.103 node103.yinzhengjie.org.cn 172.30.1.104 node104.yinzhengjie.org.cn 172.30.1.105 node105.yinzhengjie.org.cn 172.30.1.106 node106.yinzhengjie.org.cn 172.30.1.107 node107.yinzhengjie.org.cn 172.30.1.108 node108.yinzhengjie.org.cn [root@centos7.yinzhengjie.org.cn ~]# [root@centos7.yinzhengjie.org.cn ~]# vim /etc/hosts [root@centos7.yinzhengjie.org.cn ~]# [root@centos7.yinzhengjie.org.cn ~]# cat /etc/hosts #在CentOS6.x版本的操作系统建议修改主机名后将对应的主机名加入"hosts"解析文件中。否则可能造成无法解析的情况。 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 centos7.yinzhengjie.org.cn ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.30.1.101 node101.yinzhengjie.org.cn 172.30.1.102 node102.yinzhengjie.org.cn 172.30.1.103 node103.yinzhengjie.org.cn 172.30.1.104 node104.yinzhengjie.org.cn 172.30.1.105 node105.yinzhengjie.org.cn 172.30.1.106 node106.yinzhengjie.org.cn 172.30.1.107 node107.yinzhengjie.org.cn 172.30.1.108 node108.yinzhengjie.org.cn [root@centos7.yinzhengjie.org.cn ~]# [root@centos7.yinzhengjie.org.cn ~]#