# How to get the device name "hostname"

**URL:** https://discourse.nodered.org/t/how-to-get-the-device-name-hostname/33907
**Category:** General
**Created:** [7 October 2020 22:18 UTC](https://discourse.nodered.org/t/how-to-get-the-device-name-hostname/33907 "2020-10-07T22:18:32Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![fabiobrent](https://avatars.discourse-cdn.com/v4/letter/f/bb73d2/32.png) [@fabiobrent](https://discourse.nodered.org/u/fabiobrent)
#### Post date: [7 October 2020 22:18 UTC](https://discourse.nodered.org/t/how-to-get-the-device-name-hostname/33907/1 "2020-10-07T22:18:33Z")

</div>

Hello I need to get the name of my device "hostname" (is a raspberry) where is loaded Node-red.  
So I need to create a payload with the name of the device (set by Pi configuration form -\> "hostname" field)

Thank you

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [7 October 2020 22:30 UTC](https://discourse.nodered.org/t/how-to-get-the-device-name-hostname/33907/2 "2020-10-07T22:30:34Z")

</div>

an exec node set to run `hostname` ?  
(and don't append the payload from the incoming message.)

```auto
[{"id":"da669b10.7253d8","type":"debug","z":"e9b4dddd.70115","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":60,"wires":[]},{"id":"3078162b.0ab55a","type":"exec","z":"e9b4dddd.70115","command":"hostname","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":300,"y":60,"wires":[["da669b10.7253d8"],[],[]]},{"id":"6238f928.ad4ca8","type":"inject","z":"e9b4dddd.70115","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":60,"wires":[["3078162b.0ab55a"]]}]

```

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [7 October 2020 22:32 UTC](https://discourse.nodered.org/t/how-to-get-the-device-name-hostname/33907/3 "2020-10-07T22:32:49Z")

</div>

By "hostname", do you mean "myDeviceName"?

That is in the `settings.js` file.

---

<div class="post-metadata">

### Author: ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)
#### Post date: [8 October 2020 01:39 UTC](https://discourse.nodered.org/t/how-to-get-the-device-name-hostname/33907/4 "2020-10-08T01:39:21Z")

</div>

Add 'os:requrie('os') item to the global context... example of this is in the settings file how to do this.

Then you just do a global get to the 'os' node to get the hostname() function results. For example I do this at times in a function node...

```auto
var theHost = global.get('os').hostname().split('.')[0];

```

The above gets just 'host' name, ignoring any domain name element of the host FQDN (Full Qualified Doman Name) returned. So 'host' not '[host.domain.com](http://host.domain.com)' for example.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [22 October 2020 01:39 UTC](https://discourse.nodered.org/t/how-to-get-the-device-name-hostname/33907/5 "2020-10-22T01:39:45Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
