See all UNIX related articles/faq
I am trying to add entry to /etc/hosts file using ‘sudo echo ‘192.168.1.254 router’ >> /etc/hosts’ but getting an error that read as, -bash: /etc/hosts: Permission denied. How do I insert or append text to a file when running sudo command on Linux or Unix-like system?

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux or Unix terminal
Category Terminal/ssh
OS compatibility BSD Linux macOS Unix
Est. reading time 3 minutes
There are various ways to append a text or data to a file when using sudo command on Linux or Unix. You can use the tee command that opies input to standard output. Another option is to pass shell itself to the sudo command. This page includes examples of appending to a privileged file with the help of sudo and tee commands.
Fig.01: How to append/insert text into a file using sudo on Linux or Unix-like system?

Fig.01: How to append/insert text into a file using sudo on Linux or Unix-like system?

Let us see both methods.
Advertisement

Method 1: Use tee command to append to a file with sudo

The tee command read from standard input (such as keyboard) and write to standard output (such as screen) and files. The syntax is as follows
$ echo 'text' | sudo tee -a /path/to/file
$ echo '192.168.1.254 router' | sudo tee -a /etc/hosts

Sample outputs:

Password:
192.168.1.254   router

This solution is simple and you avoided running bash/sh shell with root privileges. Only append or write part needed root permission. Now use the cat command (or bat command if you want to see fancy outputs):
$ cat /etc/hosts

Bash: append to file with sudo and tee

Want to append text to more than one file while using sudo? Try:
$ echo 'data' | sudo tee -a file1 file2 fil3
Verify that you just appended to a file as sudo with cat command:
$ cat file1
$ cat file2

We can append to a file with the sudo command:
$ cat my_file.txt | sudo tee -a existing_file.txt > /dev/null
It is a good idea to redirect tee output to /dev/null when appending text. In other words, use >/dev/null when you don’t want tee command to write to the standard output such as screen.

Understanding the tee command options

  1. -a OR --append : Append to the given FILEs, do not overwrite
  2. -i OR --ignore-interrupts : Ignore interrupt signals
  3. -p : Diagnose errors writing to non pipes

See tee command man page by typing the following man command or help command:
$ man tee
$ tee --help # GNU/Linux

Method 2: Use bash/sh shell

The syntax is as follows when using bash/sh or any other modern shell that support appending text and passing option such as the -c. For instance:
$ sudo sh -c 'echo text >> /path/to/file'
$ sudo -- sh -c "echo 'text foo bar' >> /path/to/file"
$ sudo -- bash -c 'echo data >> /path/to/file'
$ sudo bash -c 'echo data text >> /path/to/file'

For example:
$ sudo sh -c 'echo "192.168.1.254 router" >> /etc/hosts'
The -c option is passed to the sh/bash to read commands from the argument string. Please note that you are running bash/sh shell with root privileges and redirection took place in that shell session. However, quoting complex command can be problem. Hence, the tee command method recommended to all.

Conclusion

As we learned that there are multiple ways to append text to a file using the sudo command. See the following manula pages using the help command or man/info command. For example:
$ info bash
$ man bash

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

6 comments… add one
  • Ren May 30, 2017 @ 9:14

    Hi guys, your T-Shirt link is broken, even without any adblocking.

  • Daniel Caruso II May 30, 2017 @ 9:37

    Why not just do
    sudo echo string >> file;
    This seems overdone.

  • Daniel Caruso II May 30, 2017 @ 9:40

    oh ignore me I didn’t read the first paragraph

  • Charles May 30, 2017 @ 17:33

    dd?

    • Charles May 30, 2017 @ 17:36

      echo 'text' | sudo dd oflags=append of=/path/to/file

  • Yordan Georgiev Sep 12, 2020 @ 12:17

    I missed the following example:
    # for example to disable ip6
    cat < /dev/null
    net.ipv6.conf.all.disable_ipv6 = 1
    net.ipv6.conf.default.disable_ipv6 = 1
    net.ipv6.conf.lo.disable_ipv6 = 1
    EOF

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.