How can I get an environment variable representing my machine ip?












1















I'm not sure how the scope of variables in /etc/environment works, nor when they get created, but I need a "global" environment variable that is populated with the ip address after a DHCP address is assigned so I can use that variable just like any variable defined in /etc/environment



For example, I assign hard-coded variables in /etc/environment such as:



TZ="America/New_York"


I can then use ${TZ} in **yml* configuration files and they will be populated with value assigned in /etc/environment



I need to be able to use ${ipv4} in my yml files which will have the machine's ip address (which is assigned via DHCP). How can I create this, keeping in mind I might not log into the machine.



I've done a little research and I know I can populate a value with the ip address by running the following executed from a bash prompt will populate foo with the correct ip.



foo=$(/sbin/ip -o -4 addr list enp0s3 | awk '{print $4}' | cut -d/ -f1)


ETA: Since my machine gets assigned an ip from my router's MAC address filtering option, I won't expect my ip to ever change unless I do so from the router, which I tend to do from time to time. So this is why I'm not hard coding the actual value into the file










share|improve this question







New contributor




Mike Forman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • Look into using the /etc/profile file or creating a script in /etc/profile.d/ folder that will run your command to set the foo as a global variable.

    – Terrance
    yesterday













  • Don't those only get executed upon login?

    – Mike Forman
    yesterday











  • This all depends on why you need to know what your IP address is. The host is already named for localhost. Localhost is a global variable already to the system. And yes, the profile would be only run at login. Or if you added it to ~/.bashrc would run on every time you launched a terminal window, but that is not global and is only used in the present terminal session. If you added it to your ~/.profile file, it would have to be sourced every time you wish to use it. You can source any file at any time. . /etc/profile would source the file at that time.

    – Terrance
    yesterday











  • Your ~/.profile file is sourced at login as well.

    – Terrance
    yesterday











  • I need to know the ip address the DHCP server hands out to this machine, and I need it after it is assigned and not before waiting for someone to login, much less having a kludge to auto-login. So after the login window appears, I'd like to have XXX=192.168.1.46

    – Mike Forman
    yesterday
















1















I'm not sure how the scope of variables in /etc/environment works, nor when they get created, but I need a "global" environment variable that is populated with the ip address after a DHCP address is assigned so I can use that variable just like any variable defined in /etc/environment



For example, I assign hard-coded variables in /etc/environment such as:



TZ="America/New_York"


I can then use ${TZ} in **yml* configuration files and they will be populated with value assigned in /etc/environment



I need to be able to use ${ipv4} in my yml files which will have the machine's ip address (which is assigned via DHCP). How can I create this, keeping in mind I might not log into the machine.



I've done a little research and I know I can populate a value with the ip address by running the following executed from a bash prompt will populate foo with the correct ip.



foo=$(/sbin/ip -o -4 addr list enp0s3 | awk '{print $4}' | cut -d/ -f1)


ETA: Since my machine gets assigned an ip from my router's MAC address filtering option, I won't expect my ip to ever change unless I do so from the router, which I tend to do from time to time. So this is why I'm not hard coding the actual value into the file










share|improve this question







New contributor




Mike Forman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • Look into using the /etc/profile file or creating a script in /etc/profile.d/ folder that will run your command to set the foo as a global variable.

    – Terrance
    yesterday













  • Don't those only get executed upon login?

    – Mike Forman
    yesterday











  • This all depends on why you need to know what your IP address is. The host is already named for localhost. Localhost is a global variable already to the system. And yes, the profile would be only run at login. Or if you added it to ~/.bashrc would run on every time you launched a terminal window, but that is not global and is only used in the present terminal session. If you added it to your ~/.profile file, it would have to be sourced every time you wish to use it. You can source any file at any time. . /etc/profile would source the file at that time.

    – Terrance
    yesterday











  • Your ~/.profile file is sourced at login as well.

    – Terrance
    yesterday











  • I need to know the ip address the DHCP server hands out to this machine, and I need it after it is assigned and not before waiting for someone to login, much less having a kludge to auto-login. So after the login window appears, I'd like to have XXX=192.168.1.46

    – Mike Forman
    yesterday














1












1








1








I'm not sure how the scope of variables in /etc/environment works, nor when they get created, but I need a "global" environment variable that is populated with the ip address after a DHCP address is assigned so I can use that variable just like any variable defined in /etc/environment



For example, I assign hard-coded variables in /etc/environment such as:



TZ="America/New_York"


I can then use ${TZ} in **yml* configuration files and they will be populated with value assigned in /etc/environment



I need to be able to use ${ipv4} in my yml files which will have the machine's ip address (which is assigned via DHCP). How can I create this, keeping in mind I might not log into the machine.



I've done a little research and I know I can populate a value with the ip address by running the following executed from a bash prompt will populate foo with the correct ip.



foo=$(/sbin/ip -o -4 addr list enp0s3 | awk '{print $4}' | cut -d/ -f1)


ETA: Since my machine gets assigned an ip from my router's MAC address filtering option, I won't expect my ip to ever change unless I do so from the router, which I tend to do from time to time. So this is why I'm not hard coding the actual value into the file










share|improve this question







New contributor




Mike Forman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I'm not sure how the scope of variables in /etc/environment works, nor when they get created, but I need a "global" environment variable that is populated with the ip address after a DHCP address is assigned so I can use that variable just like any variable defined in /etc/environment



For example, I assign hard-coded variables in /etc/environment such as:



TZ="America/New_York"


I can then use ${TZ} in **yml* configuration files and they will be populated with value assigned in /etc/environment



I need to be able to use ${ipv4} in my yml files which will have the machine's ip address (which is assigned via DHCP). How can I create this, keeping in mind I might not log into the machine.



I've done a little research and I know I can populate a value with the ip address by running the following executed from a bash prompt will populate foo with the correct ip.



foo=$(/sbin/ip -o -4 addr list enp0s3 | awk '{print $4}' | cut -d/ -f1)


ETA: Since my machine gets assigned an ip from my router's MAC address filtering option, I won't expect my ip to ever change unless I do so from the router, which I tend to do from time to time. So this is why I'm not hard coding the actual value into the file







boot 18.04 environment-variables






share|improve this question







New contributor




Mike Forman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Mike Forman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Mike Forman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Mike FormanMike Forman

61




61




New contributor




Mike Forman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Mike Forman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Mike Forman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • Look into using the /etc/profile file or creating a script in /etc/profile.d/ folder that will run your command to set the foo as a global variable.

    – Terrance
    yesterday













  • Don't those only get executed upon login?

    – Mike Forman
    yesterday











  • This all depends on why you need to know what your IP address is. The host is already named for localhost. Localhost is a global variable already to the system. And yes, the profile would be only run at login. Or if you added it to ~/.bashrc would run on every time you launched a terminal window, but that is not global and is only used in the present terminal session. If you added it to your ~/.profile file, it would have to be sourced every time you wish to use it. You can source any file at any time. . /etc/profile would source the file at that time.

    – Terrance
    yesterday











  • Your ~/.profile file is sourced at login as well.

    – Terrance
    yesterday











  • I need to know the ip address the DHCP server hands out to this machine, and I need it after it is assigned and not before waiting for someone to login, much less having a kludge to auto-login. So after the login window appears, I'd like to have XXX=192.168.1.46

    – Mike Forman
    yesterday



















  • Look into using the /etc/profile file or creating a script in /etc/profile.d/ folder that will run your command to set the foo as a global variable.

    – Terrance
    yesterday













  • Don't those only get executed upon login?

    – Mike Forman
    yesterday











  • This all depends on why you need to know what your IP address is. The host is already named for localhost. Localhost is a global variable already to the system. And yes, the profile would be only run at login. Or if you added it to ~/.bashrc would run on every time you launched a terminal window, but that is not global and is only used in the present terminal session. If you added it to your ~/.profile file, it would have to be sourced every time you wish to use it. You can source any file at any time. . /etc/profile would source the file at that time.

    – Terrance
    yesterday











  • Your ~/.profile file is sourced at login as well.

    – Terrance
    yesterday











  • I need to know the ip address the DHCP server hands out to this machine, and I need it after it is assigned and not before waiting for someone to login, much less having a kludge to auto-login. So after the login window appears, I'd like to have XXX=192.168.1.46

    – Mike Forman
    yesterday

















Look into using the /etc/profile file or creating a script in /etc/profile.d/ folder that will run your command to set the foo as a global variable.

– Terrance
yesterday







Look into using the /etc/profile file or creating a script in /etc/profile.d/ folder that will run your command to set the foo as a global variable.

– Terrance
yesterday















Don't those only get executed upon login?

– Mike Forman
yesterday





Don't those only get executed upon login?

– Mike Forman
yesterday













This all depends on why you need to know what your IP address is. The host is already named for localhost. Localhost is a global variable already to the system. And yes, the profile would be only run at login. Or if you added it to ~/.bashrc would run on every time you launched a terminal window, but that is not global and is only used in the present terminal session. If you added it to your ~/.profile file, it would have to be sourced every time you wish to use it. You can source any file at any time. . /etc/profile would source the file at that time.

– Terrance
yesterday





This all depends on why you need to know what your IP address is. The host is already named for localhost. Localhost is a global variable already to the system. And yes, the profile would be only run at login. Or if you added it to ~/.bashrc would run on every time you launched a terminal window, but that is not global and is only used in the present terminal session. If you added it to your ~/.profile file, it would have to be sourced every time you wish to use it. You can source any file at any time. . /etc/profile would source the file at that time.

– Terrance
yesterday













Your ~/.profile file is sourced at login as well.

– Terrance
yesterday





Your ~/.profile file is sourced at login as well.

– Terrance
yesterday













I need to know the ip address the DHCP server hands out to this machine, and I need it after it is assigned and not before waiting for someone to login, much less having a kludge to auto-login. So after the login window appears, I'd like to have XXX=192.168.1.46

– Mike Forman
yesterday





I need to know the ip address the DHCP server hands out to this machine, and I need it after it is assigned and not before waiting for someone to login, much less having a kludge to auto-login. So after the login window appears, I'd like to have XXX=192.168.1.46

– Mike Forman
yesterday










0






active

oldest

votes











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Mike Forman is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1116444%2fhow-can-i-get-an-environment-variable-representing-my-machine-ip%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes








Mike Forman is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Mike Forman is a new contributor. Be nice, and check out our Code of Conduct.













Mike Forman is a new contributor. Be nice, and check out our Code of Conduct.












Mike Forman is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1116444%2fhow-can-i-get-an-environment-variable-representing-my-machine-ip%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How did Captain America manage to do this?

迪纳利

南乌拉尔铁路局