Linux 下设置 hostname

40 篇文章 0 订阅

Linux系统安装后,想个性定制一下自己的系统,如主机名字的修改。本文将介绍,如何修改Linux的主机名(hostname).

 

方法一: 永久修改,重启后生效。

修改配置文件: /etc/hostname  在该文件中,用你喜欢的主机名字替换原来的主机名字。系统重启后,生效。

 

方法二: 利用hostname命令临时修改,系统的hostname。系统后重启后,恢复 /etc/hostname中指定的主机名。

具体操作如下:

wzb@310Lab:~$ cat /etc/hostname
310Lab
wzb@310Lab:~$ cat /proc/sys/kernel/hostname
310Lab
wzb@310Lab:~$ which hostname
/bin/hostname
wzb@310Lab:~$ hostname Lab
hostname: you must be root to change the host name
wzb@310Lab:~$ sudo hostname Lab
wzb@310Lab:~$ cat /etc/hostname
310Lab
wzb@310Lab:~$ cat /proc/sys/kernel/hostname
Lab
wzb@310Lab:~$ hostname -v
Lab
wzb@310Lab:~$

可见hostname已经改变,而hostname的配置文件/etc/hostname 没有发生变化,但系统重启后,系统仍用配置文件/etc/hostname的主机名来初始化系统。故没有永久地改变。

可见, /bin/hostname这个命令每次都读取和设置的文件是系统的动态文件:/proc/sys/kernel/hostname 中读取hostname的值。我们知道 /proc 文件系统是Linux系统的运行时的虚拟文件系统。重启后,其内容将被重新初始化。


换一个终端,重新登陆: 

Last login: Fri Mar 23 16:28:20 2012 from 211.87.18.109
wzb@Lab:~$ clear
wzb@Lab:~$

可见,从另一终端登陆,已经发现系统的hostname已经改变了。

 

方法三: 将方法一 与方法二结合,即用hostname newhostname 同时,用newhostname替代/etc/hostname中的oldhostname。

具体操作如下:

wzb@310Lab:~$ clear
wzb@310Lab:~$ cat /etc/hostname
310Lab
wzb@310Lab:~$ cat /proc/sys/kernel/hostname
310Lab
wzb@310Lab:~$ which hostname
/bin/hostname
wzb@310Lab:~$ hostname Lab
hostname: you must be root to change the host name
wzb@310Lab:~$ sudo hostname Lab
wzb@310Lab:~$ cat /etc/hostname
310Lab
wzb@310Lab:~$ cat /proc/sys/kernel/hostname
Lab
wzb@310Lab:~$ hostname -v
Lab
wzb@310Lab:~$
wzb@310Lab:~$ cat /etc/hostname
310Lab
wzb@310Lab:~$ cat /proc/sys/kernel/hostname
Lab
wzb@310Lab:~$ sudo vi /etc/hostname
sudo: unable to resolve host Lab
[sudo] password for wzb:
wzb@310Lab:~$ cat /etc/hostname
Lab
wzb@310Lab:~$

即使系统重启后,修改也能保持。


 

 

2. hostname命令的其他用途:

wzb@310Lab:~$ hostname -v
310Lab
wzb@310Lab:~$ hostname -h
Usage: hostname [-v] {hostname|-F file}      set host name (from file)
       domainname [-v] {nisdomain|-F file}   set NIS domain name (from file)
       hostname [-v] [-d|-f|-s|-a|-i|-y]     display formated name
       hostname [-v]                         display host name

       hostname -V|--version|-h|--help       print info and exit

    dnsdomainname=hostname -d, {yp,nis,}domainname=hostname -y

    -s, --short           short host name
    -a, --alias           alias names
    -i, --ip-address      addresses for the host name
    -f, --fqdn, --long    long host name (FQDN)
    -d, --domain          DNS domain name
    -y, --yp, --nis       NIS/YP domain name
    -F, --file            read host name or NIS domain name from given file

   This command can get or set the host name or the NIS domain name. You can
   also get the DNS domain or the FQDN (fully qualified domain name).
   Unless you are using bind or NIS for host lookups you can change the
   FQDN (Fully Qualified Domain Name) and the DNS domain name (which is
   part of the FQDN) in the /etc/hosts file.
wzb@310Lab:~$ hostname -s
www
wzb@310Lab:~$ hostname -a
310Lab
wzb@310Lab:~$ hostname -i
211.87.18.162
wzb@310Lab:~$ hostname -f
www.es.sdu.edu.cn
wzb@310Lab:~$ hostname -d
es.sdu.edu.cn
wzb@310Lab:~$ hostname -y
(none)
wzb@310Lab:~$ cat /etc/hosts
127.0.0.1       localhost
211.87.18.162  www.es.sdu.edu.cn 310Lab

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
wzb@310Lab:~$

 

/etc/hosts 是本地的域名解析文件,对域名查找,首先查找的本地文件就是/etc/hosts, 其次,去DNS服务器上查找。

hostname 命令 加选项 能够通过/etc/hosts文件获得系统的网络配置信息。

hostname  newhostname:  设置系统的hostname为newhostname。

hostname -v   :显示系统的主机名(hostname)。

hostname -s  :显示系统域名(在/etc/hosts中设置的)简短形式,即开头。

hostname -f   :显示系统域名(在/etc/hosts中设置的)全称形式。

hostname -a  : 显示系统域名(在/etc/hosts中设置的)的别名(alias)。

hostname -i   :显示系统ip(在/etc/hosts中设置的)。
hostname -d :显示系统域名(在/etc/hosts中设置的)。

hostname -F :从指定文件中读取hostname。

 

简要说一下:/etc/hosts  文件的格式:

ip              domainname    alias

ip地址        域名                    别名

wzb@310Lab:~$ man hosts
wzb@310Lab:~$ man hosts | cat
HOSTS(5)                   Linux Programmer’s Manual                  HOSTS(5)



NAME
       hosts - The static table lookup for hostnames

SYNOPSIS
       /etc/hosts

DESCRIPTION
       This  manual  page  describes  the format of the /etc/hosts file.  This
       file is a simple text file that associates IP addresses with hostnames,
       one line per IP address.  For each host a single line should be present
       with the following information:

              IP_address canonical_hostname [aliases...]

       Fields of the entry are separated by any number of  blanks  and/or  tab
       characters.   Text  from a "#" character until the end of the line is a
       comment, and is ignored.  Host  names  may  contain  only  alphanumeric
       characters, minus signs ("-"), and periods (".").  They must begin with
       an  alphabetic  character  and  end  with  an  alphanumeric  character.
       Optional aliases provide for name changes, alternate spellings, shorter
       hostnames, or generic hostnames (for example, localhost).

       The Berkeley Internet Name Domain (BIND) Server implements the Internet
       name  server  for Unix systems.  It augments or replaces the /etc/hosts
       file or hostname lookup, and frees a host from  relying  on  /etc/hosts
       being up to date and complete.

       In  modern  systems,  even though the host table has been superseded by
       DNS, it is still widely used for:

       bootstrapping
              Most systems have a small host table  containing  the  name  and
              address  information  for  important hosts on the local network.
              This is useful when DNS is not running, for example during  sys‐
              tem bootup.

       NIS    Sites  that  use NIS use the host table as input to the NIS host
              database.  Even though NIS can be used with DNS, most NIS  sites
              still  use the host table with an entry for all local hosts as a
              backup.

       isolated nodes
              Very small sites that are isolated from the network use the host
              table  instead of DNS.  If the local information rarely changes,
              and the network is not connected to  the  Internet,  DNS  offers
              little advantage.

FILES
       /etc/hosts

NOTES
       Modifications  to this file normally take effect immediately, except in
       cases where the file is cached by applications.

   Historical Notes
       RFC 952 gave the original format for the  host  table,  though  it  has
       since changed.

       Before  the advent of DNS, the host table was the only way of resolving
       hostnames on the fledgling Internet.  Indeed, this file could  be  cre‐
       ated  from the official host data base maintained at the Network Infor‐
       mation Control Center (NIC), though local changes were  often  required
       to  bring  it  up  to  date regarding unofficial aliases and/or unknown
       hosts.  The NIC no longer maintains the hosts.txt files, though looking
       around  at  the  time  of  writing  (circa  2000), there are historical
       hosts.txt files on the WWW.  I just found three, from 92, 94, and 95.

EXAMPLE
       127.0.0.1       localhost
       192.168.1.10    foo.mydomain.org       foo
       192.168.1.13    bar.mydomain.org       bar
       146.82.138.7    master.debian.org      master
       209.237.226.90  www.opensource.org

SEE ALSO
       hostname(1), resolver(3), resolver(5), hostname(7), named(8),  Internet
       RFC 952

COLOPHON
       This  page  is  part of release 3.05 of the Linux man-pages project.  A
       description of the project, and information about reporting  bugs,  can
       be found at http://www.kernel.org/doc/man-pages/.



Linux                             2002-06-16                          HOSTS(5)
wzb@310Lab:~$

 

 3. Linux系统中系统提示符(prompt sign)  中的\h 指代就是hostname。

wzb@310Lab:~$ echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$
wzb@310Lab:~$ echo $PS2
>
wzb@310Lab:~$


 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值