Creating and editing a file via SSH

Popular:
LEVEL UP YOUR SERVER SETUP! APPLY AVA AND LAUNCH WITH A 15% DISCOUNT
USE PROMO:

How to Create and Edit Files Over SSH

Secure Shell (SSH) offers a secure, encrypted way to manage remote servers, making file creation and editing a key skill for sysadmins and developers. This guide simplifies file management over SSH using command-line editors, with practical examples and tips for efficient server administration on AvaHost or similar Linux-based hosting platforms.

Managing files via SSH is essential for configuring servers, editing scripts, or logging data. Whether you’re setting up a website or maintaining a VPS, mastering tools like

nano

,

vi

, and

touch

streamlines your workflow securely.

Connecting to the server via SSH

Before you can create or edit a file, you must establish an SSH connection:

ssh username@your-server-ip

  • username: Your SSH user (e.g. root or admin).

  • your-server-ip: The IP address of your remote server.

If you are using your own port, add -p port_number:

ssh -p 2222 username@your-server-ip

Creating a file

To create a new file, you can use one of several commands:

With

touch

touch myfile.txt

This creates an empty file called myfile.txt in the current directory.

With echo

echo "Initial content" > myfile.txt

This creates a file and adds a line of text.

Editing a file

You can edit files with command line text editors. Here are the most common options:

nano (beginner-friendly)

nano myfile.txt
  • Easy to use, with on-screen commands.

  • Use Ctrl O to save, Ctrl X to exit.

vi / vim (advanced users)

vi myfile.txt
  • Press

    i

    to enter insert mode.

  • Enter your content.

  • Press

    Esc

    , then type

    :wq

    to save and exit.

cat (quick edits)

To display or append content:

cat myfile.txt # Display
echo "One more line" >> myfile.txt # Append

Changing file permissions (optional)

After creating/editing, you may want to change the permissions:

chmod 644 myfile.txt

Or change the ownership:

chown user:user myfile.txt

Conclusion

Managing files via SSH is an essential part of remote server management. Whether you’re setting up configuration files or logging system data, tools like

nano

,

vi

and

touch

make it easy to get the job done. Once you have mastered these basics, you can work efficiently on any Linux-based system via SSH.