Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #269270 > unrolled thread

Re: How to run automatically a script as soon root login

Started byGreg Wooledge <greg@wooledge.org>
First post2024-05-13 13:30 +0200
Last post2024-05-13 22:10 +0200
Articles 15 on this page of 35 — 11 participants

Back to article view | Back to linux.debian.user

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: How to run automatically a script as soon root login Greg Wooledge <greg@wooledge.org> - 2024-05-13 13:30 +0200
    Re: How to run automatically a script as soon root login Mario Marietto <marietto2008@gmail.com> - 2024-05-13 13:50 +0200
      Re: How to run automatically a script as soon root login Greg Wooledge <greg@wooledge.org> - 2024-05-13 14:00 +0200
      Re: How to run automatically a script as soon root login Erwan David <erwan@rail.eu.org> - 2024-05-13 14:00 +0200
      Re: How to run automatically a script as soon root login Nicolas George <george@nsup.org> - 2024-05-13 14:10 +0200
        Re: How to run automatically a script as soon root login Stefan Monnier <monnier@iro.umontreal.ca> - 2024-05-13 15:50 +0200
          Re: How to run automatically a script as soon root login Stefan Monnier <monnier@iro.umontreal.ca> - 2024-05-13 16:20 +0200
            Re: How to run automatically a script as soon root login Mario Marietto <marietto2008@gmail.com> - 2024-05-13 17:30 +0200
              Re: How to run automatically a script as soon root login Richmond <dnomhcir@gmx.com> - 2024-05-13 18:50 +0200
      Re: How to run automatically a script as soon root login Stefan Monnier <monnier@iro.umontreal.ca> - 2024-05-13 14:10 +0200
      Re: How to run automatically a script as soon root login Nicolas George <george@nsup.org> - 2024-05-13 14:20 +0200
        Re: How to run automatically a script as soon root login Erwan David <erwan@rail.eu.org> - 2024-05-13 14:50 +0200
          Re: How to run automatically a script as soon root login Richmond <dnomhcir@gmx.com> - 2024-05-13 15:20 +0200
            Re: How to run automatically a script as soon root login Erwan David <erwan@rail.eu.org> - 2024-05-13 15:20 +0200
            Re: How to run automatically a script as soon root login Greg Wooledge <greg@wooledge.org> - 2024-05-13 15:20 +0200
              Re: How to run automatically a script as soon root login <tomas@tuxteam.de> - 2024-05-13 15:30 +0200
                Re: How to run automatically a script as soon root login Mario Marietto <marietto2008@gmail.com> - 2024-05-13 19:10 +0200
        Re: How to run automatically a script as soon root login <tomas@tuxteam.de> - 2024-05-13 14:50 +0200
          Re: How to run automatically a script as soon root login <tomas@tuxteam.de> - 2024-05-13 15:00 +0200
            Re: How to run automatically a script as soon root login Mario Marietto <marietto2008@gmail.com> - 2024-05-13 15:20 +0200
              Re: How to run automatically a script as soon root login Nicolas George <george@nsup.org> - 2024-05-13 15:30 +0200
                Re: How to run automatically a script as soon root login Mario Marietto <marietto2008@gmail.com> - 2024-05-13 16:00 +0200
          Re: How to run automatically a script as soon root login Nicolas George <george@nsup.org> - 2024-05-13 15:00 +0200
          Re: How to run automatically a script as soon root login Nicolas George <george@nsup.org> - 2024-05-13 15:20 +0200
          Re: How to run automatically a script as soon root login Richmond <dnomhcir@gmx.com> - 2024-05-13 15:20 +0200
        Re: How to run automatically a script as soon root login Richmond <dnomhcir@gmx.com> - 2024-05-13 14:50 +0200
          Re: How to run automatically a script as soon root login Dan Ritter <dsr@randomstring.org> - 2024-05-13 15:10 +0200
      Re: How to run automatically a script as soon root login Dan Ritter <dsr@randomstring.org> - 2024-05-13 14:20 +0200
    Re: How to run automatically a script as soon root login Hans <hans.ullrich@loop.de> - 2024-05-13 18:10 +0200
      Re: How to run automatically a script as soon root login <tomas@tuxteam.de> - 2024-05-13 18:40 +0200
        Re: How to run automatically a script as soon root login Richard <rrosner5@gmail.com> - 2024-05-13 19:10 +0200
      Re: How to run automatically a script as soon root login Greg Wooledge <greg@wooledge.org> - 2024-05-13 21:10 +0200
        Re: How to run automatically a script as soon root login Mario Marietto <marietto2008@gmail.com> - 2024-05-13 21:20 +0200
          Re: How to run automatically a script as soon root login David Wright <deblis@lionunicorn.co.uk> - 2024-05-13 22:00 +0200
            Re: How to run automatically a script as soon root login Mario Marietto <marietto2008@gmail.com> - 2024-05-13 22:10 +0200

Page 2 of 2 — ← Prev page 1 [2]


#269290

FromNicolas George <george@nsup.org>
Date2024-05-13 15:30 +0200
Message-ID<IDDUB-d4Js-3@gated-at.bofh.it>
In reply to#269287
Mario Marietto (12024-05-13):
> The command iptables -A POSTROUTING -t nat -s 192.168.1.5 -j MASQUERADE
> doesn't work if invoked as a user,it says "you must be root". So,as
> user,the script seems to be working fine like this :
> 
> function jumpto
> {
>         label=$1
>         cmd=$(sed -n "/$label:/{:a;n;p;ba};" $0 | grep -v ':$')
>         eval "$cmd"
>         exit
> }

You seem to be investing a lot of effort into a fragile solution to
emulate a deprecated execution control primitive.

Regards,

-- 
  Nicolas George

[toc] | [prev] | [next] | [standalone]


#269293

FromMario Marietto <marietto2008@gmail.com>
Date2024-05-13 16:00 +0200
Message-ID<IDEnE-d4Tf-7@gated-at.bofh.it>
In reply to#269290

[Multipart message — attachments visible in raw view] — view raw

I've found that solution on the Internet. It wasn't the only solution that
I found,but that form won the challenge because it has found my mind ready
to detect that it could have worked. Maybe I could have used while,but
after 1 hour of thinking I didn't understand how and I resigned. The same
for until and for. Sorry for that,but I'm not an experienced coder. I try
to do my best with what I find on the Internet. I can adapt an already good
enough solution to fit my needs,but I'm not able to write a script from
scratch. It is also true that I found it good because it reminded me of my
old good times when I was a good basic and turbo pascal programmer.

On Mon, May 13, 2024 at 3:24 PM Nicolas George <george@nsup.org> wrote:

> Mario Marietto (12024-05-13):
> > The command iptables -A POSTROUTING -t nat -s 192.168.1.5 -j MASQUERADE
> > doesn't work if invoked as a user,it says "you must be root". So,as
> > user,the script seems to be working fine like this :
> >
> > function jumpto
> > {
> >         label=$1
> >         cmd=$(sed -n "/$label:/{:a;n;p;ba};" $0 | grep -v ':$')
> >         eval "$cmd"
> >         exit
> > }
>
> You seem to be investing a lot of effort into a fragile solution to
> emulate a deprecated execution control primitive.
>
> Regards,
>
> --
>   Nicolas George
>
>

-- 
Mario.

[toc] | [prev] | [next] | [standalone]


#269282

FromNicolas George <george@nsup.org>
Date2024-05-13 15:00 +0200
Message-ID<IDDrz-d4ko-3@gated-at.bofh.it>
In reply to#269279
tomas@tuxteam.de (12024-05-13):
> That's like slicing your morning baguette with the chainsaw.

Worse than that, it will only work from an X11 environment. Certainly
not at boot.

Regards,

-- 
  Nicolas George

[toc] | [prev] | [next] | [standalone]


#269285

FromNicolas George <george@nsup.org>
Date2024-05-13 15:20 +0200
Message-ID<IDDKW-d4Gq-1@gated-at.bofh.it>
In reply to#269279
Richmond (12024-05-13):
> sudo bash -c "echo 1 > hello"

Use sh for that.

Regards,

-- 
  Nicolas George

[toc] | [prev] | [next] | [standalone]


#269288

FromRichmond <dnomhcir@gmx.com>
Date2024-05-13 15:20 +0200
Message-ID<IDDKW-d4Gq-3@gated-at.bofh.it>
In reply to#269279
<tomas@tuxteam.de> writes:

> On Mon, May 13, 2024 at 01:36:23PM +0100, Richmond wrote:
>> I was experimenting, and found this works:
>> 
>> sudo xterm -e "echo 1 > hello"
>
> That's like slicing your morning baguette with the chainsaw.

I do that too.

>
> But if it works for you... hey :-)
>
> Cheers

This also works:

sudo bash -c "echo 1 > hello"

[toc] | [prev] | [next] | [standalone]


#269280

FromRichmond <dnomhcir@gmx.com>
Date2024-05-13 14:50 +0200
Message-ID<IDDhT-d4gY-3@gated-at.bofh.it>
In reply to#269276
I was experimenting, and found this works:

sudo xterm -e "echo 1 > hello"

It created a file owned by root. But I found I was able to remove it
without being root even though group and world permissions were read
only.

[toc] | [prev] | [next] | [standalone]


#269283

FromDan Ritter <dsr@randomstring.org>
Date2024-05-13 15:10 +0200
Message-ID<IDDBf-d4D5-1@gated-at.bofh.it>
In reply to#269280
Richmond wrote: 
> I was experimenting, and found this works:
> 
> sudo xterm -e "echo 1 > hello"
> 
> It created a file owned by root. But I found I was able to remove it
> without being root even though group and world permissions were read
> only.

The owner of a directory can delete any file in it.


-dsr-

[toc] | [prev] | [next] | [standalone]


#269277

FromDan Ritter <dsr@randomstring.org>
Date2024-05-13 14:20 +0200
Message-ID<IDCOR-d47e-3@gated-at.bofh.it>
In reply to#269271
Mario Marietto wrote: 
> --> If they only want this thing to happen when root logs in directly on a
> console or ssh, then .profile may indeed be the correct answer.
> 
> Yes,I don't need to run xorg and a desktop environment,since warp-cli
> disconnect and warp-cli connect do not require them.
> I wouldn't to login as root automatically,but I've realized that this
> command :
> 
> echo 1 > /proc/sys/net/ipv4/ip_forward
> 
> work only if I'm root. It does not work using sudo. So,in the end I've
> chosen to be root instead of a normal user that can use sudo.


If you run 

sudo echo 1 > /proc/sys/net/ipv4/ip_forward

then the shell you are running it from will run "sudo echo 1"
and then try to put the output in that file. That fails because
the running process is owned by you, not root.

If you put the echo command into a script, and then run the
script with sudo, this will work.

-dsr-


then 

[toc] | [prev] | [next] | [standalone]


#269296

FromHans <hans.ullrich@loop.de>
Date2024-05-13 18:10 +0200
Message-ID<IDGpr-d6i9-9@gated-at.bofh.it>
In reply to#269270
Am Montag, 13. Mai 2024, 13:24:17 CEST schrieb Greg Wooledge:
> On Mon, May 13, 2024 at 07:36:07AM +0200, Richard wrote:
> > .profile

Sorry, dumb question: Depending of the shell, the user is using (let's say, he 
will use bash), can the script not be added into ~/.bashrc?

If yes, second dumb question: Coiuld it be ANY script or command? 
(also running as non-rootuser, like adding "runuser -u myuser 
command_whatever").

Just some thoughts....

Best

Hans 

[toc] | [prev] | [next] | [standalone]


#269297

From<tomas@tuxteam.de>
Date2024-05-13 18:40 +0200
Message-ID<IDGSt-d6rs-3@gated-at.bofh.it>
In reply to#269296

[Multipart message — attachments visible in raw view] — view raw

On Mon, May 13, 2024 at 06:06:37PM +0200, Hans wrote:
> Am Montag, 13. Mai 2024, 13:24:17 CEST schrieb Greg Wooledge:
> > On Mon, May 13, 2024 at 07:36:07AM +0200, Richard wrote:
> > > .profile
> 
> Sorry, dumb question: Depending of the shell, the user is using (let's say, he 
> will use bash), can the script not be added into ~/.bashrc?

Yes. Consult your shell's manual. And it will also depend on whether
it is considered to be a "login shell". And on a couple of other
factors.

> If yes, second dumb question: Coiuld it be ANY script or command? 
> (also running as non-rootuser, like adding "runuser -u myuser 
> command_whatever").

Root can do this, yes.

Cheers
-- 
t

[toc] | [prev] | [next] | [standalone]


#269303

FromRichard <rrosner5@gmail.com>
Date2024-05-13 19:10 +0200
Message-ID<IDHlx-d6Ra-23@gated-at.bofh.it>
In reply to#269297

[Multipart message — attachments visible in raw view] — view raw

>> If yes, second dumb question: Coiuld it be ANY script or command?
>> (also running as non-rootuser, like adding "runuser -u myuser
>> command_whatever").

>Root can do this, yes.
Or to be more precise, .bashrc (and any file that's read from it like
.bash_aliases) can run anything the bash CLI can run with the same
privileges. Of course, anything requiring user input like entering
passwords won't work. The same is true for .profile, but with the twist
that depending on which shell is reading it, other syntaxes may be
possible/required, while .bashrc and similarly named files are pretty much
bash-exclusive. So as long as it's something that should run upon login,
like setting user-wide environment variables.

> Sorry, dumb question: Depending of the shell, the user is using (let's
say, he will use bash), can the script not be added into ~/.bashrc?
.profile is simply more universal. E.g. I'm mounting cloud storage drives
through rclone. Since I put the command into .profile, it won't just be run
when I log in to the bash shell, but also when I log in to Gnome. I don't
think that would be possible with .bashrc.


Best
Richard

Am Mo., 13. Mai 2024 um 18:34 Uhr schrieb <tomas@tuxteam.de>:

> On Mon, May 13, 2024 at 06:06:37PM +0200, Hans wrote:
> > Am Montag, 13. Mai 2024, 13:24:17 CEST schrieb Greg Wooledge:
> > > On Mon, May 13, 2024 at 07:36:07AM +0200, Richard wrote:
> > > > .profile
> >
> > Sorry, dumb question: Depending of the shell, the user is using (let's
> say, he
> > will use bash), can the script not be added into ~/.bashrc?
>
> Yes. Consult your shell's manual. And it will also depend on whether
> it is considered to be a "login shell". And on a couple of other
> factors.
>
> > If yes, second dumb question: Coiuld it be ANY script or command?
> > (also running as non-rootuser, like adding "runuser -u myuser
> > command_whatever").
>
> Root can do this, yes.
>
> Cheers
> --
> t
>

[toc] | [prev] | [next] | [standalone]


#269308

FromGreg Wooledge <greg@wooledge.org>
Date2024-05-13 21:10 +0200
Message-ID<IDJdE-d88c-7@gated-at.bofh.it>
In reply to#269296
On Mon, May 13, 2024 at 06:06:37PM +0200, Hans wrote:
> Am Montag, 13. Mai 2024, 13:24:17 CEST schrieb Greg Wooledge:
> > On Mon, May 13, 2024 at 07:36:07AM +0200, Richard wrote:
> > > .profile
> 
> Sorry, dumb question: Depending of the shell, the user is using (let's say, he 
> will use bash), can the script not be added into ~/.bashrc?

The context has been snipped out.  The context for this was "OP is trying
to run a command when root logs in".  The method of login was not stated.
First responder said ".profile works for every method of login".  I said
that this is incorrect: it doesn't work for many GUI login setups.

In those same GUI login setups, .bashrc is *also* not read when the
user logs in.  None of the shell startup files are read at all.

All of this is a tangent to the actual problem, though.

> If yes, second dumb question: Coiuld it be ANY script or command? 
> (also running as non-rootuser, like adding "runuser -u myuser 
> command_whatever").

We're several layers deep into an X-Y problem here.  The *actual* problem
is that the system's networking configuration is not correct/complete.

The *workaround* is that the OP is logging in and running commands to
change the networking configuration temporarily.

The question resulting from the workaround (the Y in the X-Y) was "How
can I automate these commands that I keep having to type?"

The proper question should have been "How can I fix my system's networking
configuration permanently?"

[toc] | [prev] | [next] | [standalone]


#269309

FromMario Marietto <marietto2008@gmail.com>
Date2024-05-13 21:20 +0200
Message-ID<IDJnj-d8bj-7@gated-at.bofh.it>
In reply to#269308

[Multipart message — attachments visible in raw view] — view raw

---> The context has been snipped out....

nope. Read well what I said on my first post :


*[Forgot to say that I switched boot target to text with this command :*

*sudo systemctl set-default multi-user.target]*

What does this mean for you ? The context is that I was not using any
desktop manager.


On Mon, May 13, 2024 at 9:05 PM Greg Wooledge <greg@wooledge.org> wrote:

> On Mon, May 13, 2024 at 06:06:37PM +0200, Hans wrote:
> > Am Montag, 13. Mai 2024, 13:24:17 CEST schrieb Greg Wooledge:
> > > On Mon, May 13, 2024 at 07:36:07AM +0200, Richard wrote:
> > > > .profile
> >
> > Sorry, dumb question: Depending of the shell, the user is using (let's
> say, he
> > will use bash), can the script not be added into ~/.bashrc?
>
> The context has been snipped out.  The context for this was "OP is trying
> to run a command when root logs in".  The method of login was not stated.
> First responder said ".profile works for every method of login".  I said
> that this is incorrect: it doesn't work for many GUI login setups.
>
> In those same GUI login setups, .bashrc is *also* not read when the
> user logs in.  None of the shell startup files are read at all.
>
> All of this is a tangent to the actual problem, though.
>
> > If yes, second dumb question: Coiuld it be ANY script or command?
> > (also running as non-rootuser, like adding "runuser -u myuser
> > command_whatever").
>
> We're several layers deep into an X-Y problem here.  The *actual* problem
> is that the system's networking configuration is not correct/complete.
>
> The *workaround* is that the OP is logging in and running commands to
> change the networking configuration temporarily.
>
> The question resulting from the workaround (the Y in the X-Y) was "How
> can I automate these commands that I keep having to type?"
>
> The proper question should have been "How can I fix my system's networking
> configuration permanently?"
>
>

-- 
Mario.

[toc] | [prev] | [next] | [standalone]


#269310

FromDavid Wright <deblis@lionunicorn.co.uk>
Date2024-05-13 22:00 +0200
Message-ID<IDK01-d8ow-3@gated-at.bofh.it>
In reply to#269309
On Mon 13 May 2024 at 21:18:30 (+0200), Mario Marietto wrote:
> On Mon, May 13, 2024 at 9:05 PM Greg Wooledge <greg@wooledge.org> wrote:
> > On Mon, May 13, 2024 at 06:06:37PM +0200, Hans wrote:
> > > Am Montag, 13. Mai 2024, 13:24:17 CEST schrieb Greg Wooledge:
> > > > On Mon, May 13, 2024 at 07:36:07AM +0200, Richard wrote:
> > > > > .profile
> > >
> > > Sorry, dumb question: Depending of the shell, the user is using (let's
> > say, he
> > > will use bash), can the script not be added into ~/.bashrc?
> >
> > The context has been snipped out.  The context for this was "OP is trying
> > to run a command when root logs in".  The method of login was not stated.
> > First responder said ".profile works for every method of login".  I said
> > that this is incorrect: it doesn't work for many GUI login setups.
> >
> > In those same GUI login setups, .bashrc is *also* not read when the
> > user logs in.  None of the shell startup files are read at all.
> >
> > All of this is a tangent to the actual problem, though.
> >
> > > If yes, second dumb question: Coiuld it be ANY script or command?
> > > (also running as non-rootuser, like adding "runuser -u myuser
> > > command_whatever").
> >
> > We're several layers deep into an X-Y problem here.  The *actual* problem
> > is that the system's networking configuration is not correct/complete.
> >
> > The *workaround* is that the OP is logging in and running commands to
> > change the networking configuration temporarily.
> >
> > The question resulting from the workaround (the Y in the X-Y) was "How
> > can I automate these commands that I keep having to type?"
> >
> > The proper question should have been "How can I fix my system's networking
> > configuration permanently?"
> > 
> ---> The context has been snipped out....
> 
> nope. Read well what I said on my first post :
> 
> 
> *[Forgot to say that I switched boot target to text with this command :*
> 
> *sudo systemctl set-default multi-user.target]*
> 
> What does this mean for you ?

To me, it means just one more change while trying to get your
networking to work. Your OP finished with "It does not work and anyway
it does not seem to be what I want..." and "I suspect that the
solution is easier than what I'm trying to do...". Again, to me,
that suggests that after settling on a better, permanent solution
to your problem, you would roll back the other changes that you
made along the way.

> The context is that I was not using any
> desktop manager.

My understanding of this statement is that "the context" is what
is described in your OP. As you said you "switched boot target to
text", I would assume you originally had a different target, likely
a DM, and that you might revert back to it after solving the problem.

Cheers,
David.

[toc] | [prev] | [next] | [standalone]


#269311

FromMario Marietto <marietto2008@gmail.com>
Date2024-05-13 22:10 +0200
Message-ID<IDK9H-d8H5-1@gated-at.bofh.it>
In reply to#269310

[Multipart message — attachments visible in raw view] — view raw

yeah at the beginning i used xorg + xfce but then i realized that i did not
need them,so the context became the textual mode.

Il lun 13 mag 2024, 21:52 David Wright <deblis@lionunicorn.co.uk> ha
scritto:

> On Mon 13 May 2024 at 21:18:30 (+0200), Mario Marietto wrote:
> > On Mon, May 13, 2024 at 9:05 PM Greg Wooledge <greg@wooledge.org> wrote:
> > > On Mon, May 13, 2024 at 06:06:37PM +0200, Hans wrote:
> > > > Am Montag, 13. Mai 2024, 13:24:17 CEST schrieb Greg Wooledge:
> > > > > On Mon, May 13, 2024 at 07:36:07AM +0200, Richard wrote:
> > > > > > .profile
> > > >
> > > > Sorry, dumb question: Depending of the shell, the user is using
> (let's
> > > say, he
> > > > will use bash), can the script not be added into ~/.bashrc?
> > >
> > > The context has been snipped out.  The context for this was "OP is
> trying
> > > to run a command when root logs in".  The method of login was not
> stated.
> > > First responder said ".profile works for every method of login".  I
> said
> > > that this is incorrect: it doesn't work for many GUI login setups.
> > >
> > > In those same GUI login setups, .bashrc is *also* not read when the
> > > user logs in.  None of the shell startup files are read at all.
> > >
> > > All of this is a tangent to the actual problem, though.
> > >
> > > > If yes, second dumb question: Coiuld it be ANY script or command?
> > > > (also running as non-rootuser, like adding "runuser -u myuser
> > > > command_whatever").
> > >
> > > We're several layers deep into an X-Y problem here.  The *actual*
> problem
> > > is that the system's networking configuration is not correct/complete.
> > >
> > > The *workaround* is that the OP is logging in and running commands to
> > > change the networking configuration temporarily.
> > >
> > > The question resulting from the workaround (the Y in the X-Y) was "How
> > > can I automate these commands that I keep having to type?"
> > >
> > > The proper question should have been "How can I fix my system's
> networking
> > > configuration permanently?"
> > >
> > ---> The context has been snipped out....
> >
> > nope. Read well what I said on my first post :
> >
> >
> > *[Forgot to say that I switched boot target to text with this command :*
> >
> > *sudo systemctl set-default multi-user.target]*
> >
> > What does this mean for you ?
>
> To me, it means just one more change while trying to get your
> networking to work. Your OP finished with "It does not work and anyway
> it does not seem to be what I want..." and "I suspect that the
> solution is easier than what I'm trying to do...". Again, to me,
> that suggests that after settling on a better, permanent solution
> to your problem, you would roll back the other changes that you
> made along the way.
>
> > The context is that I was not using any
> > desktop manager.
>
> My understanding of this statement is that "the context" is what
> is described in your OP. As you said you "switched boot target to
> text", I would assume you originally had a different target, likely
> a DM, and that you might revert back to it after solving the problem.
>
> Cheers,
> David.
>
>

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | linux.debian.user


csiph-web