tail program output to file in Linux

Behind the pipe, the sudo doesn't work. I don't know why you can't write to your home - maybe the file belongs to root?

 sudo tail /var/log/apache2/error.log | sudo tee ~/errors.txt

Maybe you need a different user behind the pipe. For sure, you don't need a preexisting file.


When you write sudo somecommand > ~/errors.txt, the shell that's calling sudo (and is running as you) is the one performing the redirection and opening ~/errors.txt. See Redirecting stdout to a file you don't have write permission on. Usually the problem in that case is that you want root to write to the file; see the linked question for ways to do this.

Here, it's odd that you cannot write to a file in your home directory. Chances are that you previously saved some output as root in /home/user/errors.txt, and that file now exists and belongs to root. Remove the file (you can do that as long as you have write permission on /home/user, and then you'll be able to create it as your user.

rm ~/errors.txt
sudo tail /var/log/apache2/error.log > ~/errors.txt

If the file truly doesn't exist, then you don't have write permission in your home directory. While technically possible, and actually occasionally useful for some restricted users, it's very unusual.