2012年2月16日

更新dhclient.conf以防resolv.conf被改

有时使用ssh登录FreeBSD时会很慢,为什么呢?
是因为ssh要进行"DNS反查(查询主机IP所对应的域名)",所以在登录时候会很慢.有两个方法可以解决:
1) 编辑 /etc/ssh/sshd_conf , 将 #UseDNS yes 取消注释,设为no ,重启sshd
2) 编辑 /etc/resolv.conf(没有就新建),在里面写入DNS,如 nameserver 8.8.8.8.

使用2)的重启之后你会觉得第二个方法行不通,因为ssh登录还是很慢.这又为什么呢?
当再一次查看/etc/reslov.conf文件的时候就会发现,里面的内容不是你的内容.
你可以vim /sbin/dhclient-script文件的add_new_resolv_conf()函数时会发现:/etc/resolv.conf里面的内容是被替换的.
那解决的方法也是的,还不止一个,有二个!
1) 直接将 /sbin/dhclient-script文件的add_new_resolv_conf函数注释.
2) 在/etc/dhclient.conf文件里加上一句:

prepend domain-name-servers 8.8.8.8, 8.8.4.4;
man dhclient.conf里prepend小节的内容是
prepend { [option declaration] [, ... option declaration] } If for some set of options the client should use a value you sup‐ ply, and then use the values supplied by the server, if any, these values can be defined in the prepend statement. The prepend statement can only be used for options which allow more than one value to be given. This restriction is not enforced - if violated, the results are unpredictable.
重启之后查看/etc/resolv.conf,会发现多了两行!