Linux 系统管理员在接手一台新的服务器的时候,如果没有好的交接文档,我们可以得到的主机信息就要靠我们自己了。很多信息是可以直接通过命令从主机上获得的,下面就和大家分享 5 个获取 Linux 主机信息的命令。(注:我使用的是 Ubuntu 13.04)
1. 查看或改变 Linux 主机名命令
$ hostname
mylinuxbook-Inspiron-1525
从命令结果上您可以看到我的 Ubuntu 主机名是 mylinuxbook-Inspiron-1525。您可能会注意到,主机名通常也出现在命令提示符下,所以很多时候我们将主机名改短一些可能会更方便使用。
通过如下命令来改变它:
$ sudo hostname mylinuxbook
注意,改变主机名需要 root 权限,所以我在上面使用了 sudo 命令来提升权限。如果您执行了上面的命令,那么您可以立即打开一个新的终端,在终端中您就能查看到您新的主机名,同时您会看到新的名称的命令提示符。
不过通过这个命令修改的主机名是一个临时功能,一旦您重启您的服务器,这个名称将不会存在。如果您需要永久的修改主机名,那么您需要修改 /etc/hostname 文件。
2. Linux 主机命令
这个命令可以让您来查看主机的 IP 信息,
$ host google.com
google.com has address 74.125.236.72
google.com has address 74.125.236.78
google.com has address 74.125.236.73
google.com has address 74.125.236.71
google.com has address 74.125.236.65
google.com has address 74.125.236.69
google.com has address 74.125.236.66
google.com has address 74.125.236.64
google.com has address 74.125.236.67
google.com has address 74.125.236.68
google.com has address 74.125.236.70
google.com has IPv6 address 2404:6800:4007:801::1004
google.com mail is handled by 10 aspmx.l.google.com.
google.com mail is handled by 20 alt1.aspmx.l.google.com.
google.com mail is handled by 30 alt2.aspmx.l.google.com.
google.com mail is handled by 40 alt3.aspmx.l.google.com.
google.com mail is handled by 50 alt4.aspmx.l.google.com.
从命令的运行结果我们可以看到主机的 IP (IPV4 和 IPV6)以及邮件服务器相关的信息。
如果您还想查看 DNS 记录,可以使用 -a 参数
$ host -a google.com
Trying "google.com"
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 34210
;; flags: qr rd ra; QUERY: 1, ANSWER: 21, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;google.com. IN ANY
;; ANSWER SECTION:
google.com. 211 IN AAAA 2404:6800:4007:800::1009
google.com. 299 IN MX 10 aspmx.l.google.com.
google.com. 299 IN MX 50 alt4.aspmx.l.google.com.
google.com. 299 IN MX 20 alt1.aspmx.l.google.com.
google.com. 299 IN MX 30 alt2.aspmx.l.google.com.
google.com. 299 IN MX 40 alt3.aspmx.l.google.com.
google.com. 267 IN A 74.125.236.168
google.com. 267 IN A 74.125.236.165
google.com. 267 IN A 74.125.236.166
google.com. 267 IN A 74.125.236.167
google.com. 267 IN A 74.125.236.164
google.com. 267 IN A 74.125.236.174
google.com. 267 IN A 74.125.236.163
google.com. 267 IN A 74.125.236.169
google.com. 267 IN A 74.125.236.162
google.com. 267 IN A 74.125.236.161
google.com. 267 IN A 74.125.236.160
google.com. 136467 IN NS ns3.google.com.
google.com. 136467 IN NS ns1.google.com.
google.com. 136467 IN NS ns4.google.com.
google.com. 136467 IN NS ns2.google.com.
Received 412 bytes from 127.0.1.1#53 in 30 ms
如果您需要查看域名服务器或者 SOA 信息,可以使用 -c 参数
$ host -C google.com
Nameserver 216.239.38.10:
google.com has SOA record ns1.google.com. dns-admin.google.com. 2013061800 7200 1800 1209600 300
Nameserver 216.239.34.10:
google.com has SOA record ns1.google.com. dns-admin.google.com. 2013061800 7200 1800 1209600 300
Nameserver 216.239.36.10:
google.com has SOA record ns1.google.com. dns-admin.google.com. 2013061800 7200 1800 1209600 300
Nameserver 216.239.32.10:
google.com has SOA record ns1.google.com. dns-admin.google.com. 2013061800 7200 1800 1209600 300
或者您可以使用 -d 或者 -v 参数来输出详细信息
$ host -v google.com
Trying "google.com"
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52674
;; flags: qr rd ra; QUERY: 1, ANSWER: 11, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;google.com. IN A
;; ANSWER SECTION:
google.com. 77 IN A 74.125.236.40
google.com. 77 IN A 74.125.236.38
google.com. 77 IN A 74.125.236.34
google.com. 77 IN A 74.125.236.37
google.com. 77 IN A 74.125.236.36
google.com. 77 IN A 74.125.236.41
google.com. 77 IN A 74.125.236.35
google.com. 77 IN A 74.125.236.33
google.com. 77 IN A 74.125.236.46
google.com. 77 IN A 74.125.236.32
google.com. 77 IN A 74.125.236.39
Received 204 bytes from 127.0.1.1#53 in 33 ms
Trying "google.com"
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 31473
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;google.com. IN AAAA
;; ANSWER SECTION:
google.com. 28 IN AAAA 2404:6800:4007:800::1002
Received 56 bytes from 127.0.1.1#53 in 28 ms
Trying "google.com"
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2871
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;google.com. IN MX
;; ANSWER SECTION:
google.com. 13 IN MX 40 alt3.aspmx.l.google.com.
google.com. 13 IN MX 50 alt4.aspmx.l.google.com.
google.com. 13 IN MX 10 aspmx.l.google.com.
google.com. 13 IN MX 20 alt1.aspmx.l.google.com.
google.com. 13 IN MX 30 alt2.aspmx.l.google.com.
Received 136 bytes from 127.0.1.1#53 in 29 ms
这个命令可以让您获得大量的主机信息。
3. Linux arch 命令
这个命令可以让您得到系统的处理器系统构架信息。
$ arch
x86_64
从命令执行结果来看,我的主机是 64 位的 x86 系列处理器。
4. uname 命令