Do you want to access local files on your PC from another machine? Here is a simple demonstration to login on your PC using SSH. SSH stands for Secure Socket Shell and it allows a user to access one computer from another on an unsecured network. You can configure your Linux PC to enable SSH and access it’s files on another Windows/Linux/Mac PC. Follow the below commands to setup your SSH server:
First of all, make sure your software is up to date by running the following commands:
$ sudo apt update$ sudo apt upgrade
Next step is to install the SSH server, run the command:
$ sudo apt install openssh-server
When the installation finishes, you can check the status of your server using following command:
$ sudo systemctl status ssh
If it says “active: (running)”, you are good to go. It in case it is inactive, use the following command to start your server:
$ sudo systemctl start ssh
You can stop/restart your server anytime using following commands:
$ sudo systemctl stop ssh$ sudo systemctl restart ssh
If you are using Ubuntu, you need to open up an SSH port due to firewall configuration tool, The following command will allow you to ssh into the machine:
$ sudo ufw allow ssh
Now, your machine is ready for SSH access. Grab another machine with any OS. I will be using a mac machine to ssh into my Linux PC. You will not require any extra configuration for Linux and Mac, but if you are on Windows, you will require to download an ssh client. What I recommend is install puTTY which is the most popular free SSH client.
On successful download and installation, you will see the following screen. The port should be at 22, enter IP Address of your Linux Server, and connect. It will prompt you if you trust the host, just click yes. Next step is Login. It will ask you for username and password.
Your username is the name of the root user and password is the root password of your Linux machine. If your login is successful, you will see username@machinename at bottom of terminal.
If you are on Linux/Mac just open the terminal and type the following command to ssh into your server:
$ sudo username@ipaddress
username is the name of root user of your server and ipaddress is also of your server. Next it will prompt you for password which is server’s root user’s password. Again, if you are successful, your prompt will change to username@machinename and now you will be able to access all your files of server PC into the client PC.
To know the IP address of your Linux Server, you can use the following command. The highlighted portion displays the required IP address:
$ ip route
So, our remote login is successful. We can access all our files from server machine on our own PC. Next, we will look at how can we transfer files from local PC to remote server and vice-versa.
Firstly, lets look at the procedure to copy a file from local PC to remote Server. For this purpose, we use SCP command which stands for Secure Copy. To transfer a file, make sure you have exited from your SSH login, if not then type,
Now, use the following command:
$ scp path_to_file remoteuser@remoteserver:/path_to_remote
Here, path_to_file is the complete path of your file in your local PC. remoteuser is the username of root user in remote PC and remoteserver is the IP address of the remote PC. If the path_to_remote is not specified, it will copy the file to the home directory.
To copy multiple files at a time, you can specify path to multiple files in a single command:
$ scp path1 path2 path3 remoteuser@remoteserver:/path_to_remote
To copy all files inside a folder recursively, use the argument -r:
$ scp -r path1 remoteuser@remoteserver:/path_to_remote
Now, to transfer files from remote server to local PC, we use the same command but the order of arguments gets reversed:
$ scp remoteuser@remoteserver:/path_to_remote path_to_file
That’s it!! It is this simple to access your Linux machine remotely and use it for several purposes. But remember by following this tutorial, your Linux machine will be exposed to the internet and someone having your root password will be able to access all files stored on your personal PC. To avoid such threat, a good practice is to generate access keys. Details about the same will be covered in next article. Read my previous articles here. And stay tuned for more tech articles :)