Skip to main content

How to use sudo and output redirection

It often happens that you need to echo or cat something to some config file, or maybe a log, or something else owned by root. It also often happens that you try to run it as a regular user and get a permission denied error. And normally you'd just prepend your command with sudo and be done with it, but output redirects are not that easy: current shell will handle them itself, they will not be passed to sudo and you will still have your permission problem.

Luckily, the fix is easy: instead of prepending the command, you just replace > somefile with | sudo tee somefile and >> somefile with | sudo tee -a somefile whatever the command before was. You may also add > /dev/null if you don't want if to echo everything.
Example:

echo "truncated" | sudo tee /var/log/toolong.log
echo "additional log record" | sudo tee -a /var/log/toolong.log

Of course, this is not the only way and it's not always the best but it works all the time, it's quite straight-forward (doesn't require any escaping or changing the original command in any way), and it doesn't require any additional programs to be installed.

Feel free to write about your preferred method in comments.

And a bonus tip about tee, not sudo-related:

wget -O - http://example.com/dvd.iso \
  | tee >(sha1sum > dvd.sha1) \
        >(md5sum > dvd.md5) \
  > dvd.iso

Popular posts from this blog

「この番組は、ご覧のスポンサーの提供でお送りします」Inference Labs

It's official, I have corporate sponsors now (which will hopefully be closer to strategic partnership soon, at least I'd like that). Thanks for the steaks and wine and equipment and all other resources for my distributed hacker team (I'm hoping for a cool demo for ETHOnline hackathon), and local Web3 meatup group I facilitate in Tbilisi (steaks and gas money for everyone participating in the ecosystem), and my Web3 community support army in training. A large bag of kudos for our general sponsor: Inference Labs! If anybody needs help onboarding into the decentralised world, reach me privately and we'll help you navigating the options, we have the expenses covered and good people for this (better points of contact are being established, things are being worked on, stay tuned). I should probably tell a few words about what we actually do at Inference Labs but things change so often I hope we're still bringing decentralized AI to the Web3 world when you read this (maybe...

New horizons

I originally started this blog with ideas of reviewing devices and services and hoping that eventually if it gets popular enough somebody starts sending me stuff to review. A lot has changed since, I stopped obsessing that much about new gadgets and got into vintage electronics, many of things that were new and interesting a few years ago are a commodity now. I thought about reviewing the phone I finally got to refreshing last year (S23 Ultra is ok upgrade for Note 8, I'm glad that new ultras will finally have flat screen again, I might upgrade next year or so just for that) but I don't really feel like it or think it would mean much for the readers. Most of my vintage electronics is at home where I haven't been for a couple years and it's not something I can currently do something about, I touched a soldering iron like once or twice this year. I might post something work-related once I get the hang of what I'm actually doing there and have some rough ideas wen dece...

Using virtualenv for more than Python projects

Sorry, it's not a complete instruction, just a thought. It occurred to me (some time ago) that Python's virtualenv is, essentially, a simplified version of system "prefix", it has bin, lib, include, and can have more stuff when needed. If you're willing to experiment (you'll probably have to set a few additional environment variables and/or build flags but that's no big deal), you can install various other tools there up until you have a complete system with its own compiler and complete set of libraries although it's much simpler to keep using system compiler and libraries only complimenting them when needed. Granted, prefixes are nothing new, people were using /opt (and their home directory) this way since the beginning of time. But with little help of virtualenv-wrapper or pyenv you can easily switch between them and isolate environments better. Binaries and stuff installed in virtualenv would override system defaults but only when venv is activat...