To solve the problem of command permission, you can expand the scope of permission.
Next, we will introduce it to you with an example.
For example, there is a root file (admin.log) in the current directory, and we want to write content to it.
Usually, we directly execute the write action with the sudo command, such as:
➜ sudo echo "test content" > admin.log
At this point, you will find an error, “permission denied: admin.log“
This is because we have insufficient permissions.
The redirection symbols “>” (and “> >”) are bash commands.
When we use sudo, we only make the echo command have root permission, but we do not make the “>” command also have root permission.
Therefore, bash will think that these two commands are not like admin.log file to write information.
At this point, we can use the SH -c command.
➜ sudo sh -c 'echo "test content" > admin.log'