How to append text to a file when using sudo command on Linux or Unix

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?

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.

ADVERTISEMENTS

Method 1: Use tee command

The tee command read from standard input (such as keyboard) and write to standard output (such as screen) and files. The syntax is:
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.

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 sudo:
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 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 man tee

Method 2: Use bash/sh shell

The syntax is:
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'
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, tee method recommended to all.

Conclusion

As we learned that there are multiple ways to append text to a file using the sudo command.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
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.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.