How to change the hostname of a Linux system

 

 

 

另外:修改hosts文件 立即生效 linux ubuntu windows

windows参考:

做 web开发和测试的,难免需要经常修改hosts来指定某个域名访问某个特定的IP;在windows中,每次修改了C:\windows \system32\drivers\etc\hosts,都需要重启浏览器才生效。今天我就这个问题就行了学习,下面介绍三种方法,当然不一定对每个人 都有效,可以尝试一下吧。
大概原因是:应该是dnscache服务cache了dns查询结果。
方法一:命令行(cmd)运行:ipconfig /flushdns     #清除DNS缓存内容。
ps:ipconfig /displaydns    //显示DNS缓存内容
方法二: 修改注册表:HKeyCurrentUser\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings ,新建: DnsCacheEnabled  0x0 (REG_DWORD)
DnsCacheTimeout 0x0 (REG_DWORD)
ServerInfoTimeOut 0x0 (REG_DWORD) 这三个DWORD。
方法三:打开:控制面板 -管理工具– 服务, 在其中找到“DNS Client” 将其停用并改为手动模式(做了这个后,运行ipconfig /flushdns就没必要了,也运行不成功)

如果是使用Firefox浏览器,强烈推荐DNS Flusher插件,换了hosts绑定后,只需要使用一下这个插件,就会使HOST绑定立即生效;非常好用的!~

另外,在Linux下修改/etc/hosts,若想要它立刻生效,运行source /etc/hosts应该就可以了(这涉及到source命令的功能,下次再总结一下吧)[me补充:应该是立即生效的]

 

修改hostname

Linux 参考:

 

Normally we will set the hostname of a system during the installation process. Many peoples don’t care about this, and don’t change the hostname even if for example this was set to something really stupid by the datacenter that installed the system (most likely they will set this to “debian” on any debian installation, etc). For me, it is important to see on each one of the ssh screens I will have open at any time a different hostname that is relevant and will give me quickly the information on what system I am logged in.

Change the hostname on a running system

On any Linux system you can change its hostname with the command ‘hostname ‘ (surprised?)… Here are some quick usages of the command line hostname:

hostname

without any parameter it will output the current hostname of the system.

hostname --fqd

it will output the fully qualified domain name (or FQDN) of the system.

will set the hostname of the system to NEW_NAME. This is active right away and will remain like that until the system will be rebooted (because at system boot it will set this from some particular file configurations – see bellow how to set this permanently). You will most probably need to exit the current shell in order to see the change in your shell prompt.

Permanent hostname change on Debian based systems

Debian based systems use the file /etc/hostname to read the hostname of the system at boot time and set it up using the init script /etc/init.d/hostname.sh

/etc/hostname
输出:june-desktop

 

So on a Debian based system we can edit the file /etc/hostname and change the name of the system and then run:

/etc/init.d/hostname.sh start

to make the change active. The hostname saved in this file (/etc/hostname) will be preserved on system reboot (and will be set using the same script we used hostname.sh).

Permanent hostname change on RedHat based systems

RedHat based system use the file /etc/sysconfig/network to read the saved hostname at system boot. This is set using the init script /etc/rc.d/rc.sysinit

/etc/sysconfig/network
NETWORKING=yes
HOSTNAME="plain.domainname.com"
GATEWAY="192.168.0.1"
GATEWAYDEV="eth0"
FORWARD_IPV4="yes"

So in order to preserve your change on system reboot edit this file and enter the appropriate name using the HOSTNAME variable.

Use sysctl to change the hostname

Why would someone need a different method of doing the same thing as above? No idea, but here is anyway: use sysctl to change the variable kernel.hostname :
Use:

sysctl kernel.hostname

to read the current hostname, and

sysctl kernel.hostname=NEW_HOSTNAME

to change it.

 

//

http://hi.baidu.com/landnow/blog/item/a03c6989741aa2b30e244459.html

更改linux主机名
2007年01月29日 星期一 19:02

参考一:

在linux下修改主机名,只需修改/etc/sysconfig/network 文件就可以了 。
直接用hostname newname只能临时修改主机名,重启后就回到原来的主机名了。

参考二:

linux下修改主机名

                                      

修改主机名:
1./etc/sysconfig/network
内容:
NETWORKING=yes
HOSTNAME=lh //主机名(没有这行?那就添加这一行吧)
GATEWAY=192.168.1.1
2.运行命令:
#hostname lh
//lh为主机名
3./etc/hosts
内容:
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 localhost localhost.localdomain localhost
把第二localhost写为lh,即:
127.0.0.1 lh localhost.localdomain localhost
但是步骤3不是必须的。如果这三个步骤都做了,无论是否重起,
主机名都将修改成功

在Linux 系统中,默认的主机名是loalhost,如果想修改自己的主机名,可以使用命令:hostname xxx。不过如果你正在使用X Window窗口,用这个命令修改了主机名之后就打不开xterm了。所以,彻底一点的办法是直接修改/etc/network文件:HOSTNAME= xxx,然后重启计算机,主机名已经被修改了。

使用DHCP的系统就不能用上述方法了,因为启动network时主机名会被改成DHCP服务器提供的名称。怎么办呢?既然主机名被粗暴的修改了,那咱们也粗暴的改回来:编辑/etc/rc,在最后加上一行hostname xxx,重启系统就OK了。。

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值