How to easily edit files over SSH with nano

How to Easily Edit Files Over SSH with Nano

You occasionally may need to edit existing files or create new files on a remote server using a text-based terminal interface via secure shell (SSH). In this article, I’ll go over how to access a remote server via SSH and create or edit files with the Nano text editor, a standard text editor available on most Linux servers. It’s a process that’s simple for beginners: connect to the server, open Nano, edit the file, save the file, and exit.

In your server’s operating system, it’s helpful to remember that everything is a file. Applications run on executable files with configuration files that can affect their behavior. Your user account is a file. Even directories are files that point to other files. Knowing how to manipulate these files gives you great power over your server environment.

Why Remote Editing

There are many reasons why you might need to be able to edit a file remotely via SSH. You may need to carry out some necessary maintenance and configuration of server-side applications and installations. Editing files via SSH is often the fastest and most efficient way to edit a file remotely. Otherwise, you would have to download the files via FTP, SCP, or some other file transfer protocol and then re-upload the files.

It’s also good practice to be comfortable on the command line. For example, if you want to make unique configurations to your web server, or create Git repositories, logging into your server and doing it yourself is often the fastest, most efficient way. As we go over in our Beginner’s Guide to SSH for WordPress Users, interacting with a live server is inherently risky. In that guide, we share tips to minimize your risk by backing up your site files and database, restricting SSH access to essential users only and more.

How To Edit Files Via SSH Using Nano

First, you will need to navigate to the directory in which you will be editing files, then open the file with Nano, you can run the following command:

nano <filename>

Be sure to substitute <filename> with the name of the file you want to edit. (If the file does not exist, this command will create it when saved.)

Once inside the file, you will be able to start typing. You can use your arrow keys to move within the file.

How To Edit Files Via SSH Using Nano

Nano Shortcuts

Some key commands you should know for editing files with Nano include saving, searching, and exiting. Just like using keyboard shortcuts in your operating system, Nano accepts key commands bound to certain key combinations on your keyboard. There are many key commands, but these are the most common:

  • Search: Ctrl + w
  • Save (“Write out”): Ctrl + o
  • Exit: Ctrl + x

Once you’re done editing your file, you can jump right to exit, Ctrl + x, and you will be prompted to save. Answer Y (for yes) and you will be prompted to confirm the file output.

If all is well, press Enter.

That’s it! You just edited and saved a file using the Nano text editor. Well done.

Important Error Notice

If you see this message in your file [ File '<filename>' is unwritable]', that means you do not have write privileges to that file. You will need to make sure that you have a sudo user configured on your server and start a new session as that user.

Important error notice from nano

When invoking `sudo`, or any user with root privilege, it’s critical to adhere to best practices and double-check your input. One wrong command with root privilege can take down a server.

To activate your sudo privilege, add sudo to the beginning of your Nano editor command:

sudo nano <filename>

Fill in your sudo user password and proceed to edit your file as usual.

Other Editors

Nano is not the only option for editing files via SSH. You also have other popular editors like Vim, Emacs, and variants thereof. These editors are noteworthy for their extended functionality. They each have dedicated scripting languages that allow for virtually unlimited configuration.

Vim

Vim, also known as vi, is noteworthy for speed and cross-platform compatibility. It leverages speed through its legendary key commands, which mostly center on the home row of the standard QWERTY keyboard.

Vim is a modal editor, which means it has two different modes of operation; these include the “normal” mode and the “insert” mode. Normal mode is for navigation, running commands, and other functions, while insert mode inserts and edits the text itself. While Vim includes more options to edit files compared to Nano, it’s usually better for advanced users with prior experience working in the Linux shell.

You can enter Vim the same way you did Nano.

vim <filename>

You will start in normal mode. To enter “insert” mode press i on your keyboard.

This might seem unusual if you’re accustomed to standard desktop text editors. But with practice, these commands become second nature, and there are many ways to speed up and fine-tune the process.

To get back to normal mode you can press the Esc key or Ctrl-[.

Here are some basic Vim navigation keys you can use to jump around your file while in normal mode:

  • Move left, down, up, and right using h, j, k, and l respectively
  • Go to the beginning of the line using 0
  • Go to the end of the line using $
  • Move forward and backward by word using w and b

There are many other shortcuts you can use in Vim to get around your document with great speed and accuracy. To get the full tutorial, you can enter the Vim tutor by typing vimtutor on your command line.

Now you’ll need to make sure you can save and exit your file. To save the file, switch to normal mode and type :w, then press Enter. This saves the files without quitting Vim. To save and exit with one command, you can type :wq and press Enter. To quit without saving type :q! and press Enter.

Emacs

Emacs, like Vim, is a popular modal editor, but it has a lot more modes than Vim. There are special modes for editing different kinds of files. This makes file editing fast and intuitive, but it has a steep learning curve for new users.

While available in a terminal variant, it’s worth noting that Emacs is most commonly used as a local text editor. It has built-in functionality for connecting to your server with SSH, so you can edit remote files within your local configuration. This is a good option to have because most Emacs users have a personalized configuration, written in Emacs lisp, tailored to how they like to work. Editing remote files from Emacs means you can carry your configuration to any remote computer or server.

Also of note, unlike Nano or Vim, Emacs does not come bundled with most operating systems. So if you want to use it remotely, you may need to install it on whatever servers you’ll be logging into. Or, as mentioned above, you can use the SSH connection tools within Emacs on your local workstations.

Effortless File Editing

Getting comfortable with file editing over SSH opens up new possibilities for managing your server and favorite apps. The Nano text editor is the most accessible way to start, but you can always test-drive more advanced options like Vim and Emacs. Picking a text editor is a personal choice that reflects how you like to work. Investing time in learning new strategies and more advanced software always pays dividends. The results will become evident over time as you learn how to effortlessly manage files with precision and speed.

Alternative advanced options for text editors

What’s your preferred command line editor for working over SSH? Nano? Emacs? Something else? Let us know in the comments below.

Author

Chris Maiorana, Technical Writer

Chris is a tech enthusiast and writer with 10+ years of experience. His passion shines through on his website and YouTube channel, catering to the DIY power user. Focused on Linux, open-source software, and WordPress, Chris simplifies complex tech topics, making them approachable to all.

Want your face and bio here? ☝

Write an article like this and get paid well. Check out our writers program

Start Your 7-Day Free Trial

Begin your SpinupWP journey today and spin up your first server within minutes.

Subscribe to get the latest news, updates and optimizations in performance and security.

You are already logged in

It looks like you are already logged in to SpinupWP.

Please log out of this account to continue.

Registration Successful

Thanks for registering for a new SpinupWP account.

Before getting started, could you verify your email address by clicking on the link we just emailed to you?

Start Your 7-Day Free Trial

No credit card required. All features included.

By signing up to SpinupWP, you agree to our Terms and Conditions.
For privacy related information, view our Privacy Policy.