How do I create a service on CentOS running as a different user than root?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I'm using CentOS 7. I want to run a nodeJS script as a service but run the service as the "rails" user. As root, I created the file /usr/lib/systemd/system/myservice.service, with the contents



[Unit]
Description=mydir nodejs server

[Service]
ExecStart=/home/rails/mydir/start.sh
ExecStop=/home/rails/mydir/stop.sh

[Install]
WantedBy=multi-user.target


What else do I need to do to get the service to run as the user I want?







share|improve this question





















  • It looks like a quick Google would produce some useful examples, including some right here on U&LSE.
    – John Bollinger
    May 7 at 20:05











  • Yeah but those are dealing with older versions of CentOS than what I listed in my question. E.g. top Google result is centos.org/forums/viewtopic.php?t=54865 but that deals with CenteOS 5.
    – Dave
    May 7 at 20:44














up vote
0
down vote

favorite












I'm using CentOS 7. I want to run a nodeJS script as a service but run the service as the "rails" user. As root, I created the file /usr/lib/systemd/system/myservice.service, with the contents



[Unit]
Description=mydir nodejs server

[Service]
ExecStart=/home/rails/mydir/start.sh
ExecStop=/home/rails/mydir/stop.sh

[Install]
WantedBy=multi-user.target


What else do I need to do to get the service to run as the user I want?







share|improve this question





















  • It looks like a quick Google would produce some useful examples, including some right here on U&LSE.
    – John Bollinger
    May 7 at 20:05











  • Yeah but those are dealing with older versions of CentOS than what I listed in my question. E.g. top Google result is centos.org/forums/viewtopic.php?t=54865 but that deals with CenteOS 5.
    – Dave
    May 7 at 20:44












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm using CentOS 7. I want to run a nodeJS script as a service but run the service as the "rails" user. As root, I created the file /usr/lib/systemd/system/myservice.service, with the contents



[Unit]
Description=mydir nodejs server

[Service]
ExecStart=/home/rails/mydir/start.sh
ExecStop=/home/rails/mydir/stop.sh

[Install]
WantedBy=multi-user.target


What else do I need to do to get the service to run as the user I want?







share|improve this question













I'm using CentOS 7. I want to run a nodeJS script as a service but run the service as the "rails" user. As root, I created the file /usr/lib/systemd/system/myservice.service, with the contents



[Unit]
Description=mydir nodejs server

[Service]
ExecStart=/home/rails/mydir/start.sh
ExecStop=/home/rails/mydir/stop.sh

[Install]
WantedBy=multi-user.target


What else do I need to do to get the service to run as the user I want?









share|improve this question












share|improve this question




share|improve this question








edited May 8 at 14:50









cherdt

6341414




6341414









asked May 7 at 19:57









Dave

363827




363827











  • It looks like a quick Google would produce some useful examples, including some right here on U&LSE.
    – John Bollinger
    May 7 at 20:05











  • Yeah but those are dealing with older versions of CentOS than what I listed in my question. E.g. top Google result is centos.org/forums/viewtopic.php?t=54865 but that deals with CenteOS 5.
    – Dave
    May 7 at 20:44
















  • It looks like a quick Google would produce some useful examples, including some right here on U&LSE.
    – John Bollinger
    May 7 at 20:05











  • Yeah but those are dealing with older versions of CentOS than what I listed in my question. E.g. top Google result is centos.org/forums/viewtopic.php?t=54865 but that deals with CenteOS 5.
    – Dave
    May 7 at 20:44















It looks like a quick Google would produce some useful examples, including some right here on U&LSE.
– John Bollinger
May 7 at 20:05





It looks like a quick Google would produce some useful examples, including some right here on U&LSE.
– John Bollinger
May 7 at 20:05













Yeah but those are dealing with older versions of CentOS than what I listed in my question. E.g. top Google result is centos.org/forums/viewtopic.php?t=54865 but that deals with CenteOS 5.
– Dave
May 7 at 20:44




Yeah but those are dealing with older versions of CentOS than what I listed in my question. E.g. top Google result is centos.org/forums/viewtopic.php?t=54865 but that deals with CenteOS 5.
– Dave
May 7 at 20:44










1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










You can specify the user and group under the [Service] heading:



User=rails
Group=rails


See the credentials section of systemd.exec for details.



Also, use /etc/systemd/system/myservice.service as the path for service files created by the system administrator instead of /usr/lib/systemd/system. See Creating and Modifying systemd Unit Files






share|improve this answer





















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f442394%2fhow-do-i-create-a-service-on-centos-running-as-a-different-user-than-root%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    You can specify the user and group under the [Service] heading:



    User=rails
    Group=rails


    See the credentials section of systemd.exec for details.



    Also, use /etc/systemd/system/myservice.service as the path for service files created by the system administrator instead of /usr/lib/systemd/system. See Creating and Modifying systemd Unit Files






    share|improve this answer

























      up vote
      0
      down vote



      accepted










      You can specify the user and group under the [Service] heading:



      User=rails
      Group=rails


      See the credentials section of systemd.exec for details.



      Also, use /etc/systemd/system/myservice.service as the path for service files created by the system administrator instead of /usr/lib/systemd/system. See Creating and Modifying systemd Unit Files






      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        You can specify the user and group under the [Service] heading:



        User=rails
        Group=rails


        See the credentials section of systemd.exec for details.



        Also, use /etc/systemd/system/myservice.service as the path for service files created by the system administrator instead of /usr/lib/systemd/system. See Creating and Modifying systemd Unit Files






        share|improve this answer













        You can specify the user and group under the [Service] heading:



        User=rails
        Group=rails


        See the credentials section of systemd.exec for details.



        Also, use /etc/systemd/system/myservice.service as the path for service files created by the system administrator instead of /usr/lib/systemd/system. See Creating and Modifying systemd Unit Files







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered May 8 at 14:28









        cherdt

        6341414




        6341414






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f442394%2fhow-do-i-create-a-service-on-centos-running-as-a-different-user-than-root%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay