How to edit files while in ssh using nano

There are several programs available for you to use to edit files while in the Linux command line. 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 nano.

When editing files with nano, the first thing you’ll need to do is make sure that you’ve connected to your server via SSH. If you need assistance with doing this, please see How to login to your server via SSH.

We’re currently logged in, so let’s run pwd to see what directory we’re in, and then ls to see what files are in this directory:

[email protected] [~/testa]# pwd
[email protected] [~/testa]# ls -alh
total 8.0K
drwxr-xr-x 2 user5 user5 4.0K Dec 7 18:43 ./
drwx–x–x 10 user5 user5 4.0K Dec 6 08:16 ../
-rw-r–r– 1 user5 user5 0 Dec 7 18:43 file1.txt
-rw-r–r– 1 user5 user5 0 Dec 7 18:43 file2.txt
-rw-r–r– 1 user5 user5 0 Dec 7 18:43 file3.txt

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:

[email protected] [~/testa]# nano file2.txt

After running this command, the nano file editor will open and load the contents of file2.txt:

GNU nano 1.2.4                   File: file2.txt

                                [ Read 0 lines ] ^G Get Help ^O WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos
^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Txt ^T To Spell

What you’re looking at above is a blank file loaded into nano. The top line shows the version of nano you’re using and the name of the file you’re editing. The bottom part of the screen includes all of the commands that you can run.

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.

Then what?

The commands at the bottom of the page are executed by holding down Ctrl on your keyboard followed by the letter listed. For example, if you wanted to save the file (referred to as “WriteOut”), hold Ctrl + O on your keyboard. When you’re ready to exit nano, hold Ctrl + X and you will be exited from the application.

Looking For Dedicated Hosting?

Using SSH provides a faster method for managing a dedicated server hosting environment. You can learn more about what makes else makes dedicated servers so fast here.

Was this article helpful? Let us know!