How to edit or create a file over SSH using Nano

Help | Dedicated Servers

Si está utilizando la línea de comandos de Linux, hay varios programas que puede usar para editar archivos en SSH. Esta es una forma conveniente de modificar archivos, ya que puede cambiarlos rápidamente en la terminal sin usar un editor de texto separado.

 

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.
  3. Run the following command and replace example.html with the name of the file you want to create.
  4. The nano editor will then open up the file and you can begin entering text.
  5. Once you have finished editing click Ctrl+O then the Enter key to save and create the file

cd public_html

nano example.html

Edit File in SSH

 

  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

     
  3. Now we’ll run the “ls” command to see what files are in this directory.

     
  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:
    After running this command, the nano file editor will open and load the contents of file2.txt.

    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.


    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.

    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

nano file2.txt

ls

pwd

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.
 

Exit Nano

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

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.

Ctrl + X

Ctrl + O

Need help? Contact us