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
-
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
1 -
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.
0 -
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).0 -
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
0 -
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.0
Categories
- All Categories
- 178 LFX Mentorship
- 178 LFX Mentorship: Linux Kernel
- 775 Linux Foundation IT Professional Programs
- 384 Cloud Engineer IT Professional Program
- 175 Advanced Cloud Engineer IT Professional Program
- 75 DevOps IT Professional Program - Discontinued
- 7 DevOps & GitOps IT Professional Program
- 103 Cloud Native Developer IT Professional Program
- 7.6K Training Courses & Learning Paths
- 13 AI & ML Training
- 1 Blockchain & Decentralized Identity Training
- 34 Cloud & Containers Training
- 3 Cybersecurity Training
- 3 DevOps & Site-Reliability Training
- 2 Linux Kernel Development Training
- 2 Networking Training
- 2 Open Source Best Practice Training
- 5 System Administration Training
- 1 System Engineering Training
- 6 Web & Application Development Training
- 798 Hardware
- 202 Drivers
- 68 I/O Devices
- 37 Monitors
- 96 Multimedia
- 173 Networking
- 91 Printers & Scanners
- 92 Storage
- 772 Linux Distributions
- 81 Debian
- 68 Fedora
- 24 Linux Mint
- 13 Mageia
- 24 openSUSE
- 151 Red Hat Enterprise
- 31 Slackware
- 13 SUSE Enterprise
- 356 Ubuntu
- 470 Linux System Administration
- 31 Cloud Computing
- 73 Command Line/Scripting
- Github systems admin projects
- 101 Linux Security
- 79 Network Management
- 102 System Management
- 46 Web Management
- 166 Mobile Computing
- 32 Android
- 119 Development
- 1.2K New to Linux
- 1K Getting Started with Linux
- 407 Off Topic
- 128 Introductions
- 35 Study Material
- 1K Programming and Development
- 310 Kernel Development
- 721 Software Development
- 1K Software
- 418 Applications
- 182 Command Line
- 5 Compiling/Installing
- 71 Games
- 320 Installation
- Archived
- 183 Small Talk
- 2 LFD140 Class Forum
- 1.4K LFS258 Class Forum
Upcoming Training
-
August 20, 2018
Kubernetes Administration (LFS458)
-
August 20, 2018
Linux System Administration (LFS301)
-
August 27, 2018
Open Source Virtualization (LFS462)
-
August 27, 2018
Linux Kernel Debugging and Security (LFD440)

