How to Edit & Create a File in SSH Using Nano

Learn How to Edit & Create a File in SSH Using Nano

If you are using the linux command line, there are several programs for you to use to edit files in SSH. This is a convenient way to modify files since you can quickly change them in terminal without using a separate text editor.

Here at InMotion Hosting, our System Administration team primarily uses programs called nano and vi. Vi is a very sophisticated program with somewhat of a high learning curve. Because of this, we’re going to show you how to edit files using the nano SSH editor. The nano editor should be included with all major versions of Linux and Unix.

Create a File in SSH

  1. Open a terminal or connect to your server vis SSH.
  2. Use the cd command to change to the directory you want to create the file in. In this example, I’m creating a file in the public_html folder.
    cd public_html
  3. Run the following command and replace example.html with the name of the file you want to create.
    nano example.html
  4. The nano editor will then open up the file and you can begin entering text.
    Creating a File via SSH with Nano
  5. Once you have finished editing click Ctrl+O then the Enter key to save and create the file.

Edit File in SSH

In this section, we’ll walk you through the process of editing a file via SSH using nano.

  1. When editing files with nano, the first thing you’ll need to do is connect to your server via SSH.
  2. We’re currently logged in, so let’s run “pwd” command to see what directory we’re in
    Edit File in SSH - pwd
  3. Now we’ll run the “ls” command to see what files are in this directory.
    Edit File in SSH - ls Command
  4. As you can see, we’re in the testa folder and there are currently three files: file1.txt, file2.txt, and file3.txt. If we wanted to edit file2.txt using nano, enter the following command:
    nano file2.txt
    After running this command, the nano file editor will open and load the contents of file2.txt.
    Editing a File in Nano

    What you’re looking at above is me editing a file using nano. The top line shows the version of nano you’re using and the name of the file you’re editing.
    Top of the Nano Editor

    The bottom part of the screen includes all of the commands that you can run.
    You can then run the ls command to view the new file.Bottom of the Nano Editor

    You can move the cursor around with your keyboard arrows, but the mouse will not work in the nano editor. Here I am deleting a line of text in the file.
    View of the Cursor in Nano

So what do you do now? Start editing the file! You can edit the file as you would using any other editor, except you don’t have the option to use your mouse to point and click on items.

How to Save & Exit Nano

The commands at the bottom of the nano page are executed by holding down Ctrl on your keyboard followed by the letter listed. Here are the steps for saving a file and exiting the nano editor.

Save File in Nano

  • To save the file (referred to as “WriteOut”), hold Ctrl + O on your keyboard.
    Ctrl + O

Exit Nano

  • When you’re ready to exit nano, hold Ctrl + X and you will be exited from the application.
    Ctrl + X

Congratulations, now you know how to edit a file in SSH using nano, as well as how to save and exit. Check out our Server section for more helpful tutorials to help you manage your environment.

No matter if you’re a developer, system administrator, or simply a fan of SSH and command line, InMotion’s Cloud Hosting plans provide a fast, scalable environment that is budget-friendly.
John-Paul Briones Content Writer II

John-Paul is an Electronics Engineer that spent most of his career in IT. He has been a Technical Writer for InMotion since 2013.

More Articles by John-Paul

Was this article helpful? Join the conversation!

Shop the Black Friday Sale
Hurry! These Deals Won't Last Long