How can I create an environment variable that contains the IP address of an adapter that is set via DHCP?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















I am using docker and my scripts reference an environment variable that I'm manually setting in user's foo account /etc/environment like:



SERVER_IP=172.16.16.29


I'm using in my docker-compose file (which I'm running under as foo)



environment:
- ServerIP=${SERVER_IP}


I would like to be able to have a variable DHCP_IP that will be populated when adapter enp0s3 has it's IP address set so that I can use DHCP_IP in place of SERVER_IP in the docker-compose above.



I'm not concerned about the IP address changing often as I'm using MAC address filtering in my router to assign the same IP. But I don't want to have to set the IP address manually in a file like I'm doing now.



So how can I put the value of the ip address of enp0s3 into a variable called DHCP_IP, and how can I reference that at the command line or in a file?



Or if you know of an alternative, I'm open to suggestions.










share|improve this question


















  • 1





    DHCP_IP="$(ip addr show enp0s3 | grep "inet " | awk '{print $2}' | cut -d/ -f1)"

    – waltinator
    Mar 25 at 17:45











  • I can get the IP already. How do I put it to a variable?

    – WhiskerBiscuit
    Mar 25 at 18:05


















0















I am using docker and my scripts reference an environment variable that I'm manually setting in user's foo account /etc/environment like:



SERVER_IP=172.16.16.29


I'm using in my docker-compose file (which I'm running under as foo)



environment:
- ServerIP=${SERVER_IP}


I would like to be able to have a variable DHCP_IP that will be populated when adapter enp0s3 has it's IP address set so that I can use DHCP_IP in place of SERVER_IP in the docker-compose above.



I'm not concerned about the IP address changing often as I'm using MAC address filtering in my router to assign the same IP. But I don't want to have to set the IP address manually in a file like I'm doing now.



So how can I put the value of the ip address of enp0s3 into a variable called DHCP_IP, and how can I reference that at the command line or in a file?



Or if you know of an alternative, I'm open to suggestions.










share|improve this question


















  • 1





    DHCP_IP="$(ip addr show enp0s3 | grep "inet " | awk '{print $2}' | cut -d/ -f1)"

    – waltinator
    Mar 25 at 17:45











  • I can get the IP already. How do I put it to a variable?

    – WhiskerBiscuit
    Mar 25 at 18:05














0












0








0








I am using docker and my scripts reference an environment variable that I'm manually setting in user's foo account /etc/environment like:



SERVER_IP=172.16.16.29


I'm using in my docker-compose file (which I'm running under as foo)



environment:
- ServerIP=${SERVER_IP}


I would like to be able to have a variable DHCP_IP that will be populated when adapter enp0s3 has it's IP address set so that I can use DHCP_IP in place of SERVER_IP in the docker-compose above.



I'm not concerned about the IP address changing often as I'm using MAC address filtering in my router to assign the same IP. But I don't want to have to set the IP address manually in a file like I'm doing now.



So how can I put the value of the ip address of enp0s3 into a variable called DHCP_IP, and how can I reference that at the command line or in a file?



Or if you know of an alternative, I'm open to suggestions.










share|improve this question














I am using docker and my scripts reference an environment variable that I'm manually setting in user's foo account /etc/environment like:



SERVER_IP=172.16.16.29


I'm using in my docker-compose file (which I'm running under as foo)



environment:
- ServerIP=${SERVER_IP}


I would like to be able to have a variable DHCP_IP that will be populated when adapter enp0s3 has it's IP address set so that I can use DHCP_IP in place of SERVER_IP in the docker-compose above.



I'm not concerned about the IP address changing often as I'm using MAC address filtering in my router to assign the same IP. But I don't want to have to set the IP address manually in a file like I'm doing now.



So how can I put the value of the ip address of enp0s3 into a variable called DHCP_IP, and how can I reference that at the command line or in a file?



Or if you know of an alternative, I'm open to suggestions.







server 18.04 environment-variables






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 25 at 16:29









WhiskerBiscuitWhiskerBiscuit

1386




1386








  • 1





    DHCP_IP="$(ip addr show enp0s3 | grep "inet " | awk '{print $2}' | cut -d/ -f1)"

    – waltinator
    Mar 25 at 17:45











  • I can get the IP already. How do I put it to a variable?

    – WhiskerBiscuit
    Mar 25 at 18:05














  • 1





    DHCP_IP="$(ip addr show enp0s3 | grep "inet " | awk '{print $2}' | cut -d/ -f1)"

    – waltinator
    Mar 25 at 17:45











  • I can get the IP already. How do I put it to a variable?

    – WhiskerBiscuit
    Mar 25 at 18:05








1




1





DHCP_IP="$(ip addr show enp0s3 | grep "inet " | awk '{print $2}' | cut -d/ -f1)"

– waltinator
Mar 25 at 17:45





DHCP_IP="$(ip addr show enp0s3 | grep "inet " | awk '{print $2}' | cut -d/ -f1)"

– waltinator
Mar 25 at 17:45













I can get the IP already. How do I put it to a variable?

– WhiskerBiscuit
Mar 25 at 18:05





I can get the IP already. How do I put it to a variable?

– WhiskerBiscuit
Mar 25 at 18:05










1 Answer
1






active

oldest

votes


















0














Add the following to /etc/bash.bashrc



The adapter in question is: enp0s3, so change that accordingly



IP="$(ip addr show enp0s3  | awk '$1 == "inet" { print $2 }' | cut -d/ -f1)"
export SERVER_IP=${IP}





share|improve this answer
























    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
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1128587%2fhow-can-i-create-an-environment-variable-that-contains-the-ip-address-of-an-adap%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Add the following to /etc/bash.bashrc



    The adapter in question is: enp0s3, so change that accordingly



    IP="$(ip addr show enp0s3  | awk '$1 == "inet" { print $2 }' | cut -d/ -f1)"
    export SERVER_IP=${IP}





    share|improve this answer




























      0














      Add the following to /etc/bash.bashrc



      The adapter in question is: enp0s3, so change that accordingly



      IP="$(ip addr show enp0s3  | awk '$1 == "inet" { print $2 }' | cut -d/ -f1)"
      export SERVER_IP=${IP}





      share|improve this answer


























        0












        0








        0







        Add the following to /etc/bash.bashrc



        The adapter in question is: enp0s3, so change that accordingly



        IP="$(ip addr show enp0s3  | awk '$1 == "inet" { print $2 }' | cut -d/ -f1)"
        export SERVER_IP=${IP}





        share|improve this answer













        Add the following to /etc/bash.bashrc



        The adapter in question is: enp0s3, so change that accordingly



        IP="$(ip addr show enp0s3  | awk '$1 == "inet" { print $2 }' | cut -d/ -f1)"
        export SERVER_IP=${IP}






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 25 at 18:57









        WhiskerBiscuitWhiskerBiscuit

        1386




        1386






























            draft saved

            draft discarded




















































            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%2f1128587%2fhow-can-i-create-an-environment-variable-that-contains-the-ip-address-of-an-adap%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

            數位音樂下載

            When can things happen in Etherscan, such as the picture below?

            格利澤436b