Interacting with a remote server from the command line
Published - 2020-03-20

Alright.

Working with a remote server is something that most developers have to deal with at some point, and lately I find myself doing it almost every day. Whether I'm downloading log files, updating my php.ini or deploying a new site, these are the tools and commands that I fall back on most often. 

Connect using the SSH client

If you need full access to the remote machine, this is the option you'll want. To connect, you'll need to provide the remote user name and either the hostname or IP address:

ssh username@hostname_or_ip

If the connection is successful, you'll get a shell prompt with full access to the remote machine as if you were sitting in front of it. You can install updates, access your database or nearly anything else you need to do. 

Alternatively, it's possible to run a command directly on the remote server without actually logging in to a shell prompt...simply connect, run a command and then disconnect. For example: 


# list all files and directories in the remote web root
ssh username@hostname_or_ip ls /var/www/html

This will print the output from your ls command, then return you directly to a local shell prompt.

Mount a remote directory locally using SSHFS

This is the option that I use often when I'm working with websites or applications that live on a cloud server.

The connection syntax is similar to SSH client connection, but you'll also need to specify the local mount point as well as the remote directory that you would like to mount. For example:


# mount your remote web root to local directory /projects/mysite
sshfs username@hostname_or_ip:/var/www/html /projects/mysite/

Now if you cd into /projects/mysite and execute ls you'll see all of the files from the remote directory.

The interesting thing about this is that you can do things like git pull just as if this were a normal local directory. The benefit to this is that I don't have to bother with deploy keys on the production server, which can sometimes be a pain.

Transferring files with SCP

SCP is a secure file protocol that is used to transfer files. If you just need to move a single file or directory between your local and remote machines, this is a good option. The syntax is very similar to what we've seen before. You'll make the connection, name the file or directory to move and where you want to put it.


# download file from remote
scp username@hostname_or_ip:/var/www/html/index.php /sites/mysite/
# upload file to remote
scp /sites/mysite/index.php username@hostname_or_ip:/var/www/html/

...for directories, simply add the recursive flag...


# download directory from remote
scp -r username@hostname_or_ip:/var/www/html /sites/
# upload to remote
scp -r /sites username@hostname_or_ip:var/www/html

This is a quick and easy way to transfer files and I use it often, but sometimes you need to be a little more interactive. For that, there's SFTP.

Transferring and managing files with SFTP

SFTP is another secure file protocol that is used to transfer files, but it also gives you loads of other options. Where SCP simply moves a file or directory between machines, SFTP will also let you navigate the directory structure and do basic file management.

Establishing a connection is very much the same as the other SSH commands:


sftp username@hostname_or_ip
# or...specify a remote directory
sftp username@hostname_or_ip:/var/www/html

When the connection is established you'll get an interactive sftp prompt.


sftp>

Most of the commands available from this prompt are similar to those available in a standard Linux shell prompt. For a full list, type help. 

To navigate around the remote or local machine:


# get current directory
#remote
pwd
#local
lpwd
# list files and directories
# remote
ls
# local 
lls
# change directory
# remote
cd /remote_dir
#local
lcd /local_dir

Uploads and downloads are handled by the put and get commands. Add the recursive flag (-r) to either of these to transfer directories.


# upload a file
put localfile.sh
# upload a directory
put -r localdir/
# download a file
get remotefile.sh
# download a directory
get -r remotedir/

You can also handle file ownership and permissions.


chgrp group_id remote.sh
chown user_id remote.sh
chmod mode remote.sh

 

This is just a very high level overview of a few of the tools and commands that I tend to use most often. You'll find tons of information in the man pages and I highly recommend reading those. It's time well spent. 

Cheers.