How To Save A File In Vim / Vi Without Root Permission With Sudo On ...

Nano GPU - Affordable solution to Accelerate your AI journey. LayerStack
  • Help Center
  • Product Docs
  • API Docs
  • Technical Tutorials
  • FAQ
  • Community
Sign Up
  1. Help Center
  2. Tutorials
  3. How to save a file in Vim / Vi without Root Permission with sudo on Linux Cloud Servers
How to save a file in Vim / Vi without Root Permission with sudo on Linux Cloud Servers 2020-06-03 By Ryan 42679 Views linux sudo 17 reviews

On a Linux system, vim and vi are the most popular text editors. We can edit the content of the files and able save it using vi/vim editor.

By default the server configuration files are owned by the root user, hence the normal user can not write the changes due to insufficient Permission.

This article will guide you to the basic commands about how to save a file after writing or modifying its content without Root Permission using sudo option.

  1. Steps to edit the file /etc/hosts as a normal user.

    # vi /etc/hosts

    sudosave1

    After making some changes (E.g Adding a new entry) in the file, save it by typing the following normal command which you can see that vim/vi CANNOT open the file due to the permission. :

    :w!

    and then, Enter.

    sudosave2

    sudosave3

  2. Now you try to save the file by followingpressing,

    :w !sudo tee %

    :w – Write a file

    !sudo – Call shell with sudo command

    tee – The output of write command redirected using tee

    % - Indicate the Current filename. Here it is /etc/hosts

    And then, Enter.

    sudosave4

    sudosave5

  3. Enter the password of the logged-in user and then Enter.

    sudosave6

  4. Press L to load changes in vim itself.

  5. Now open the file again to see the changes.

    # cat /etc/hosts

    sudosave7

Related Tutorials

  • Creating Sudo User on Linux Cloud Servers

  • How to use sudo or sudoers to start, stop & restart Apache on Linux Cloud Servers

What do you think about this article?

Rate this article 5 4 3 2 1 LayerStack Promotion Need assistance? Chat with us now!

Try this guide to receive free bundled services at signup on a new free account.

Sign Up

Your Feedback Is Important

We hope you’ll give the new products and updates a try. If you have an idea for improving our products or want to vote on other user ideas so they get prioritized, please submit your feedback on our Community platform. And if you have any questions, please feel free to ask in the Community or contact our Technical Support team.

Powerful Arm Virtual Machines

New Powerful Arm Virtual Machines

  • Ultra-high performance Ampere® Altra® Max cloud-native processors
  • Provide as much as 30% better price performance than x86 VMs
  • 2 CPU / 4GB RAM*, Ultra-fast SSD storage

Nano GPU: Affordable solution to Accelerate your AI journey. Promote code: GP15TW: 15% off discount on all General Purpose cloud servers in Taiwan region. LayerStack's Chinese New Year Promotion: Up to 50% Off on all Premium Compute VMs Year End Special Sale: Big 50% OFF on General Purpose & Premium Compute VMs! Use Promo Code: HAPPY25 LayerStack X'mas Sale!: Enjoy 30% OFF all virtual machines Cloud GPU: All existing clients can receive 30% off promo code to start the AI journey with NVIDIA A40 GPUs today! Limited Offer: Use code PRO20SO to enjoy 20% off Premium Compute cloud servers. Valid until Sept 22, get yours now. Black Friday Sale: Use code "BLACK15BC" to enjoy 15% off for Basic cloud servers. Starting from US3.8/mo, deploy now! Product Docs | API Docs | Technical Tutorial | FAQ | Community | LayerStack Main Site ©2026 LayerStack Limited.

Tag » How To Save In Vi