How to Upload Files to Ubuntu Server Using SFTP

This guide provides a straightforward, step-by-step walkthrough on how to securely upload a local file to a remote Ubuntu Linux server using the Secure File Transfer Protocol (SFTP) command-line interface. You will learn how to establish a secure connection, navigate your directories, and execute the transfer command quickly and efficiently.

Step 1: Connect to Your Remote Ubuntu Server

To start an SFTP session, open your local terminal (Linux/macOS) or command prompt (Windows) and run the following command, replacing user with your remote username and server_ip with your Ubuntu server’s IP address:

sftp user@server_ip

If your server uses a custom SSH port (other than the default port 22), use the -P flag:

sftp -P custom_port user@server_ip

You will be prompted to enter your remote user password or SSH key passphrase to establish the connection.

Step 2: Navigate Your Local and Remote Directories

Once connected, your terminal prompt will change to sftp>. You can now navigate both your local machine and the remote server.

Step 3: Upload the File

To upload a file from your local machine to the current directory on your remote Ubuntu server, use the put command:

put filename.txt

If you want to upload a file and save it to a specific remote path, specify both the local file path and the remote directory destination:

put /path/to/local/filename.txt /path/to/remote/directory/

To upload an entire directory and its contents, use the recursive flag -r:

put -r local_folder

Step 4: Close the Connection

Once the file transfer is complete, close the SFTP session by typing:

exit