Welcome to the Linux Foundation Forum!

Lab 25.2 the echo method requires you to be root; sudo won’t work.

Hey guys,

There is a note in 25.2 "In the below we are going to use two methods, one involvingsysctl, the other directly echoing values to/proc/sys/kernel/pid_max. Note that theechomethod requires you to be root;sudowon’t work. We’ll leave it to you to figureout why, if you don’t already know!"

The method mentioned is:
$ echo 24000 > /proc/sys/kernel/pid_max # This must be done as root

I can't figure out why. Can someone help?

Comments

  • coop
    coop Posts: 916

    if you don't do it as root (through su say) you have to do:

    sudo sh -c "echo ......pid_max"

    it is because you cannot do something like sudo echo "something" > /root/something" because you don't have permission to write something in that directory. echo has to start a new shell to redirect the output to a privileged directory; thus you must start a new shell before you do the echo which is what "sh -c" does

  • I get it. in fact, the shell does the redirect, not the 'echo' command even under sudo, and the shell is running under the regular user. Thanks.

  • Endraya
    Endraya Posts: 4

    You can try adding the following alias, which passes your environment to the sudo'ed command (simply put), to your .bashrc file: alias sudo='sudo '

    However, i doubt it would work for the echo command, but it's quite handy in general, but you can use the print command:
    $ sudo printf "%s\n" "24000" > /proc/sys/kernel/pid_max
    Works without issues here.

    Did a quick test using:
    sudo printf "%s\n" "24000" > /home/USER/file.stringtest (since i've no interest in changing the maximal amount of PIDs allowed at this time).
    Output:
    [user@ ~]$ cat file.stringtest
    24000
    [user@ ~]$

    As which was expected, it created the file with 24000 written as a string, ending with a newline to emulate echo's default behaviour.
    The printf command is usually a bit more flexible than the echo command (in this case you could drop the string and newline option for the command but for general files, it is a better example).

  • KonstantinA
    KonstantinA Posts: 29
    edited April 2020

    When you have something like the following:

    echo "something" > /dir/file

    the shell opens first the ">", before executing the command (in a sub-shell).

    If you don't have permissions to write to that file, you get the error "...permission denied...".

    The above is valid even if you execute the command as "sudo".

    Another way, except those already suggested above can be:

    echo "something" | sudo tee /dir/file > /dev/null

  • coop
    coop Posts: 916

    That's a relatively simple trick I've never seen and it works. Just to confirm:

    c8:/usr/bin>echo hello > hello
    bash: hello: Permission denied
    c8:/usr/bin>echo hello | sudo tee hello
    hello
    c8:/usr/bin>cat hello
    hello
    c8:/usr/bin>ls -l hello
    -rw-r--r-- 1 root root 6 Apr  9 08:01 hello
    c8:/usr/bin>
    

    I'm actually kind of surprised "tee" does not open a new shell in the
    way echo does. Go figure :) I'll ask a couple of experts and strace myself.

Categories

Upcoming Training