12
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Nodejsでサーバーのホスト名を取得するコード

Posted at

Nodejsでサーバーのホスト名を取得するコード

環境

  • Node.js(4.2.4)
  • CentOS 7

ホスト名取得

Nodejs標準のOSモジュールを使用します。

nodejs
var os = require('os');
var hostname = os.hostname();
console.log(hostname);

> localhost.localdomain

hostnameコマンドの値をとっているようです。

bash
[root@localhost ~]# hostname
localhost.localdomain

ホスト名変更

CentOS7では、/etc/hostnameを変更するか、hostnameコマンドでホスト名を変更します。

/etc/hostname
yournewdomain.example.com

hostname yournewdomain.example.comは再起動するまで有効で、
/etc/hostnameは恒久的に変更したい際に使用します。

bash
[root@localhost ~]# hostname
yournewdomain.example.com

CentOS6では、/etc/sysconfig/networkのHOSTNAMEを設定するようです。

/etc/sysconfig/network
NETWORKING=yes
HOSTNAME=yournewdomain.example.com

たぶんservice network restartで反映されそうです。(確認してない)

参考

12
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?