Generate summary with AI

Every Linux user hits the same wall eventually: you go to fix a config file, and either the permissions won’t let you touch it, or one wrong keystroke turns a quick edit into an afternoon of cleanup.

Open the wrong file without a backup, or get stuck in an editor you don’t know how to exit, and a two-minute change turns into lost time you didn’t budget for. The method you reach for should depend on the situation, such as a quick interactive tweak on a single endpoint, a scripted change you don’t want to babysit line by line, or the same fix running across every server in the fleet. Here is every method and the best situations to use them in.

What to do before you touch a file

Before you open an editor, there are two things you should check first, since they can determine whether that edit goes smoothly or not. Skipping either one is how a routine edit turns into a support ticket.

Confirm permissions and privileges

There are a few file permissions to check here:

  • w (write) for whichever class applies to you (user, group, or other)
  • x (search/execute) on every directory in the file’s path, or the system won’t let you reach the file at all, regardless of what the file’s own permissions say

You should also check root user for system-level configuration files since editing them requires elevated privileges via sudo. This is different from your own documents or working files, where you’re usually the owner and don’t need to escalate anything. If a sudo command fails outright, that’s a sign the account itself may be missing a group membership or sudoers entry, not just a simple permissions oversight on the file.

» Need help with this? See our guides to changing file permissions on Linux and fixing the permission denied error

Set your default editor

Two environment variables control which editor opens when a command-line tool like crontab -e needs one:

  • $VISUAL: This is for full-screen, cursor-based editors and requires a terminal that supports cursor movement.
  • $EDITOR: This is for simpler, line-based editors.

That distinction is mostly historical at this point (modern terminals all support cursor movement), but you should still check both variables for compatibility with older tools:

  1. Run export EDITOR=<desired_text_editor> to set your line-based default
  2. Run export VISUAL=<desired_text_editor> to set your full-screen default
  3. Add both lines to ~/.bashrc or ~/.profile if you want the setting to persist across sessions instead of resetting every time you open a new terminal

    Set default editor on Linux
  4. Git handles this separately from the shell: If you want a specific editor for commit messages, run git config --global core.editor <desired_text_editor>
  5. Git checks its own preference order before falling back to your shell variables: $GIT_EDITOR, then core.editor, then $VISUAL, then $EDITOR

    Set default editor for Git files

» Using Mint? Here’s how to update Linux Mint

Note: Don’t forget to back up before you edit

Before touching a critical system file like /etc/fstab, it’s worth it to make a copy you can restore from if the edit goes wrong:

  1. Run sudo cp -a /etc/fstab /etc/fstab.bak to create the backup. The -a flag preserves permissions, ownership, and links, so the backup is a true copy, not just a text dump
  2. Add a timestamp to the filename for traceability if you’re making repeated changes: sudo cp -a /etc/fstab /etc/fstab.bak.$(date +%Y%m%d-%H%M%S)

    Backup file in Linux
  3. Confirm the copy matches the original by running diff /etc/fstab /etc/fstab.bak.<timestamp>; No output means the files are identical

    Confirm copy matches original on Linux
  4. If an edit breaks something, restore the backup with sudo cp -a /etc/fstab.bak.<timestamp> /etc/fstab

6 ways to edit files in Linux

These methods are each suited to different situations, so pick the one that matches what you need the most.

Method 1: Editing interactively with nano

Use nano when you want a straightforward, screen-based edit without needing to learn a mode system first since it’s the lowest-friction option for a one-off change.

  1. Open the file and start typing at the cursor position. nano is in insert mode by default, so there’s no mode switch required to begin editing
  2. Press Ctrl + K to delete the current line, cutting it into the buffer
  3. To delete multiple lines, press Ctrl + ^ (or Alt + A) to start a selection (you will see a Mark Set message on the screen), then press Ctrl + K to cut the marked block

    Cut marked block in Linux
  4. Press Ctrl + W to open the search prompt, type your query, and press Enter

    Open search prompt in Linux
  5. Press Alt + W to jump to the next match

    Alt w to complete search
  6. Press Ctrl + (or Alt + R) to open search-and-replace. Type the search term, press Enter, then type the replacement and press Enter

    Search and replace in Linux
  7. For each match found, press Y to replace it, N to skip it, or A to replace every remaining instance at once

    Edit commands through nano

Most key combinations are listed along the bottom of the nano window if you forget one mid-edit.

Method 2: Editing with vi or vim

Reach for vi/vim when you want a keyboard-driven workflow that doesn’t rely on arrow keys or a mouse, especially over a slow or minimal SSH session.

  1. Open the file. It loads in normal (command) mode by default, so you’re not editing text yet
  2. Press i to enter insert mode before the cursor, or a to insert after it
  3. Press Esc to return to normal mode from insert mode
  4. From normal mode, press dd to delete the current line, or dw to delete a single word starting from the cursor
  5. Press : to open the command prompt at the bottom of the screen, then type the following commands:
  • w and press Enter to save
  • q and press Enter to quit
  • wq (or x) and press Enter to save and quit in one step
Editing commands in vi or vim

If you need a reminder of what a command does, run :help, followed by the command name (for example :help insert), then :q to return to your file.

Help command in vi or vim

Method 3: Editing without opening an editor

Use echo with redirection when you need to make a small, predictable change (like appending a line to a config file) without opening an interactive session at all:

  1. echo "new_text" > /path/to/file overwrites the file’s existing contents
  2. echo "new_text" >> /path/to/file appends to the end of the file instead

    Echo command on Linux
  3. For a file that requires elevated privileges, redirection alone won’t work with sudo the way you’d expect; pipe through tee instead: echo "config_param=new_value" | sudo tee -a /etc/system_file

    Echo command for elevated privileges

Method 4: Scripted search-and-replace with sed

Use sed when the change is a find-and-replace you want to apply consistently, without manually stepping through matches one at a time.

The right syntax is sed -i "s/old_value/new_value/g" path/to/file, where:

  • s means substitute
  • old_value is the text to match
  • new_value is the replacement
  • g replaces every occurrence on a line instead of just the first
  • -i modifies the file in place
Correct syntax for sed command on Linux
  1. Run sed -i.bak "s/old_value/new_value/g" path/to/file to make the change and automatically create a backup with the .bak extension in the same step

    Sed command with automatic backup
  2. To target a single line instead of the whole file, run sed -i '3s/old_value/new_value/' path/to/file, replacing 3 with the line number you want

    Target a single line with sed command

Method 5: Editing with a GUI text editor

Use a GUI editor like gedit when you’re working directly on a desktop Linux environment and prefer a visual interface over a terminal-based one.

  1. Run gedit /path/to/file to open the file. The terminal stays attached to the editor process until you close the window

    gedit command in GUI editor
  2. Run gedit /path/to/file & instead if you want the process to run detached, freeing up your terminal for other commands while the editor stays open

    gedit command to run unattached

Method 6: Scaling edits across multiple servers

Once a fix needs to land on more than a couple of machines, editing each one by hand stops being practical, which is why technicians prefer to script the same change across a fleet instead.

For a smaller number of hosts, a bash loop over ssh is usually enough. Here’s an example script you can paste:

The Script:

Atera does not guarantee the integrity, availability, security, virus-free, safety, lawfulness, non-infringement, rights’ status, or functionality of the scripts. The use of the shared scripts is at your own risk. Scripts are provided “AS IS”. *

#!/bin/bash
#disable root login on all targeted hosts
HOSTS="server1 server2 server3"
for host in $HOSTS; do
  ssh "$host" "sudo sed -i.bak 's/^PermitRootLogin.*/PermitRootLogin no/' /etc/ssh/sshd_config"
done
Script copied to clipboard

For a more involved change, copy a script to each host and run it remotely instead of trying to fit the whole edit into a single inline command:

The Script:

Atera does not guarantee the integrity, availability, security, virus-free, safety, lawfulness, non-infringement, rights’ status, or functionality of the scripts. The use of the shared scripts is at your own risk. Scripts are provided “AS IS”. *

#!/bin/bash
HOSTS="server1 server2 server3"
for host in $HOSTS; do
  scp update_config.sh "$host":/tmp/
  ssh "$host" "sudo bash /tmp/update_config.sh"
done
Script copied to clipboard

A bash loop like this is a reasonable starting point once you’re past a handful of servers you can reasonably script and verify by hand. Past that point (or in a production environment where you need built-in idempotency, rollback, and audit history), you’ll often need a dedicated tool for that job.

Atera’s RMM platform lets you deploy scripts remotely to specific endpoints or groups on your network, schedule them to run automatically, and check the status of endpoints all from a central location without having to touch each endpoint. And you don’t even need to know how to code. Just ask AI Copilot to write the specific script you need.

» Learn more about monitoring Linux servers at scale

Recovering when an edit goes wrong

Even with a backup in hand, things can still go wrong, such as a file saved without the privileges to write it or an editor that stops responding.

For example, many editors don’t write changes directly into the original file. Instead, they save a new file and rename it over the original. That’s usually invisible to you, but it can change the file’s inode, which can break the relationship between hard links, and any process that already had the old file open will keep using the old inode rather than picking up your edit.

Here are the two most common problems and how to fix them.

Forcing a save on a restricted file

If you opened a system file without sudo and have already made changes you don’t want to lose, you don’t have to start over and can force the save through with elevated privileges.

In vim or vi, run :w !sudo tee % > /dev/null. This writes the buffer’s contents through sudo tee, which updates the current filename (%) with root privileges, without you needing to quit and reopen the file with sudo from scratch.

Force save on restricted file in vim or vi

nano doesn’t support this trick directly, so the workaround takes a few more steps:

  1. Press Ctrl + O to save, then type a path you have write access to (such as /tmp/fstab_tmp) instead of the original file’s path, and press Enter
  2. If prompted to confirm saving under a different name, press Y

    Force save on restricted file in nano
  3. Press Ctrl + X to exit the editor
  4. Move the temporary file into place with elevated privileges: sudo mv /path/to/temp_file /etc/original_file

    Move temporary with elevated privileges

Escaping an unresponsive vim session

If vim locks up or stops responding to input, work through these in order rather than force-closing the terminal outright.

  1. Press Esc a few times to make sure you’re back in normal mode, then try :q! to quit without saving, or :qa! if multiple buffers are open
  2. If that doesn’t respond, press Ctrl + Z to suspend the process and return to the shell, then run fg to bring it back to the foreground and try :q! again

    Suspend process in unresponsive vim session
  3. If vim is still unresponsive, find the process with ps aux | grep [v]im and force it closed with kill -9 <vim_PID>

    Kill process in vim session

A force-kill doesn’t necessarily mean your changes are gone. vim maintains a swap file (.filename.swp) that can recover unsaved work after a crash. Run vim -r filename to recover from it.

Editing files carefully still matters

None of these methods are complicated on their own. The skill is in matching the method to the moment and protecting yourself before you commit to a change. That discipline holds up whether you’re patching one server or fifty.

SSHing into each box one at a time stops being realistic after a certain point. Atera’s remote scripting lets IT teams and MSPs push the same verified command, sed pattern, or script across selected devices or device groups on demand, no manual per-server login required.

» Learn more about installing Atera’s Linux Agent or start your free trial today

Was this helpful?

* Scripts are provided for your benefit. You understand and acknowledge that when downloading and/or copying and/or using the Scripts: (i) you may be exposed to Scripts from a variety of sources, (ii) Atera is not responsible and takes no liability for the accuracy, usefulness, integrity, lawfulness, title or infringement, security, functionality or Intellectual Property Rights of, or relating to, such Scripts; and (iii) the Scripts are provided “AS IS” and “AS AVAILABLE”, and may have errors, and may not be malware-free, and that your interactions with, and use of, the Scripts is at your sole risk and free will. You hereby agree to waive, and hereby do waive, any legal or equitable rights or remedies you may have against Atera with respect to the Scripts.

Related Articles

How to close a tmux session

Read now

How to activate VENV Python

Read now

How to monitor Linux servers at scale

Read now

How to detach a tmux session

Read now

Endless IT possibilities

Boost your productivity with Atera’s intuitive, centralized all-in-one platform