1、Linux修改本机别名/etc/hosts的hostName后经常不生效解决
Linux修改本机别名/etc/hosts的hostName后经常不生效, 比如我们/etc/hosts的内容如下:
#192.68.1.10 message.xxx.com
192.68.1.11 message.xxx.com
但是ping
message.xxx.com还是指向192.68.1.10。
一般2种解决方案:
1). 重启,这是最直接最可靠稳定的一种方法,如果有些时候不方便重启,则可以使用第二种方法。
2). 修改/etc/sysconfig/network文件,将HOSTNAME修改成对应的别名,如下:
NETWORKING=yes
HOSTNAME=host1
1
2
NETWORKING=yes
HOSTNAME=host1
修改后重启网络服务
service network restart (本质是/etc/init.d/network) 3).
如果重启服务器后,还是不起作用,应该是本机的dns缓存引起的。
查看nscd是否启用: ps -ef|grep nscd
直接关闭Linux nscd 缓存服务: /etc/init.d/nscd stop
2、Linux的DNS
linux本身是没有dns缓存的,想使用dns缓存的话需要自己安装一个服务程序NSCD(name service cache daemon).
nscd缓存三种服务passwd group hosts,所以它会记录三个库,分别对应源/etc/passwd, /etc/hosts 和 /etc/resolv.conf每个库保存两份缓存,一份是找到记录的,一份是没有找到记录的。每一种缓存都保存有生存时间(TTL).
安装:
yum install nscd
修改配置文件/etc/nscd.conf,开启dns缓存,修改这一行
enable-cache hosts yes。
如果启用了nscd 服务,才有dns 缓存,否则是没有dns缓存的。
开启 、停止、 重启服务 service nscd start | stop | restart
nscd的配置:
通过编辑/etc/nscd.conf文件,在其中增加如下一行可以开启本地DNS cache:
enable-cache hosts yes
阿里云主机上的配置如下:
[root@iZ2571ykq ~]# cat /etc/nscd.conf
logfile /var/log/nscd.log
threads 6
max-threads 128
server-user nscd
debug-level 5
paranoia no
enable-cache passwd no
enable-cache group no
enable-cache hosts yes
positive-time-to-live hosts 5
negative-time-to-live hosts 20
suggested-size hosts 211
check-files hosts yes
persistent hosts yes
shared hosts yes
max-db-size hosts 33554432
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@iZ2571ykq ~]# cat /etc/nscd.conf
logfile /var/log/nscd.log
threads 6
max-threads 128
server-user nscd
debug-level 5
paranoia no
enable-cache passwd no
enable-cache group no
enable-cache hosts yes
positive-time-to-live hosts 5
negative-time-to-live hosts 20
suggested-size hosts 211
check-files hosts yes
persistent hosts yes
shared hosts yes
max-db-size hosts 33554432
相关参数的解释如下:
logfile debug-file-name:指定调试信息写入的文件名。
debug-level value:设置希望的调试级别。
threads number:这是启动的等待请求的线程数。最少将创建5个线程。
server-user user:如果设置了该选项,nscd将作为该用户运行,而不是作为root。如果每个用户都使用一个单独的缓存(-S参数),将忽略该选项
enable-cache service