File sharing is an important aspect of utilizing any operating system. You might use a USB drive for local file sharing. However, you may also accomplish this by using remote desktop access, especially if you are on the same network as another PC at work. In this part, we will go over some of the fundamentals of file sharing as well as some tools to do this.
Secure Copy (SCP)
One of the useful tools for file sharing is definitely scp
which works almost similar to the cp
command. It utilizes the ssh protocol so you need to username and password for accessing the remote machine. Using the scp
, you can transfer any file from a local to a remote machine or remote to the local machine, and between two remote machines as well. When used scp
to send data, both the files and the password are encrypted to ensure that anyone listening on the network does not obtain anything important.
Syntax:
$ scp file.txt [email protected]:/remote/dir
Here, file.txt
is the name of the file you wish to share, and remote_username
and 10.10.0.100
are the remote machine’s username and IP address, respectively. Finally, /remote/directory
specifies the location of the file in the remote system.
There are a few options that you can pass also. These are listed below:
-c
: compresses the file-q
: hide the non-error messages and progress bar-r:
recursive coping-p
: preserves file modification and access time
Before using scp
command, you need to know that you must provide the username and password of the remote machine. You also need to have read permission of the source file and write permission of the destination. The colon (:) is used by scp
to differentiate between local and remote directories.
To copy from a remote machine to a local machine,
$ scp [email protected]:/remote/file.txt /local/dir
To copy from a remote machine to another remote machine,
$ scp [email protected]:/files/file.txt [email protected]:/files
In this case, you need to provide the username and password of both machines.
Remote Synchronization (rsync)
It is another tool for copying files between local and remote machines, similar to the scp
command but with significant differences. Assume you’re transferring a huge file and it is stopped for whatever reason. If you want to copy the file, you must restart the process with scp
. However, there is a benefit for you if you use the rsync
command. It features a checking algorithm that verifies the file’s size or time to confirm the modifications. Then it just copies the rest of the file. As a result, its checking methodology makes it a quick, versatile, and secure substitute for the command scp
.
To copy files on the same host,
$ rsync -zvr /local/dir/one /local/dir/two
To copy files from a local host to a remote machine,
$ rsync /local/dir [email protected]:/remote/dir
To copy a file from a remote machine to the local host,
$ rsync [email protected]:/remote/dir /local/dir
There are lots of options that you can pass with rsync
which can be found using the command below:
$ rsync --help
Some of the options are given here for your ease.
-h
: humans readable output-v
: verbose output-z
: compresses the file-r
: recursive copying
HTTP Server using Python
So far, we talked about how we can communicate between servers and how to transfer files from the local host to remote servers, and the other way around. In this section, I will give you a brief overview of how you can set up your own server and share your desired directory with others. For this purpose, python has a great module named http.server
which is pretty simple and easy to use. Most of cases, it comes by default if have installed python 3
it on your system.
To enable the server, first, choose a directory you want to share with others. For me it is /home/ohid/desktop/server/
. Now, go to your terminal from this directory and run the command below.
$ python3 -m http.server
You can see that after enabling http.server
, it starts serving itself through port 8000
. Now, go to the http://0.0.0.0:8000
link from your browser. You can see your directory like this.
This directory now can be accessed through the IP address of other machines. Additionally, html
files like index.html can be added to customize the webpage. A demo index.html
file is shown below.
<html>
<header>
<title>EnableGeek Tutorials</title>
</header>
<body>
<h1>Welcome to EnableGeek tutorials! </h1>
<p> Visit our home page</p>
<a href="https://www.enablegeek.com"/>EnableGeek</a>
</body>
</html>
In that case, the Python interpreter will detect it automatically and serve it to the IP address instead of serving the files. If you enable the python server now, you will see in the browser something like the image below.
You can also set a different port if you wish to http.server
offer more functionality also for this you may need to write a python script. The http.server
is named as SimpleHTTPServer in python 2 and there may be other changes in syntax.
Samba
Another great tool for file sharing is samba which is installed on a linux server but its shared files can be accessed from both linux and windows OS. Server Message Block (SMB) was originally used to share files across Windows operating systems (Mac also supports SMB file sharing), but it was eventually cleaned up and optimized as the Common Internet File System (CIFS) protocol. Samba is the name given to the Linux tools used to deal with CIFS on Linux. You may share resources such as printers in addition to files.
To begin with, you need to install the samba on your system. You have to run the following command.
$ sudo apt update
$ sudo apt install samba
After that, you need to configure samba as per your needs. The default configuration file is located in /etc/samba/
the directory. To view the default configuration, run the command below.
$ sudo vi /etc/samba/smb.conf
There are lots of commands which are commented out. You may change these if the necessity arises. To verify the configuration, run the command below.
$ sudo testparm
You will see the output as follows:
Load smb config files from /etc/samba/smb.conf
Loaded services file OK.
Weak crypto is allowed
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions
# Global parameters
[global]
log file = /var/log/samba/log.%m
logging = file
map to guest = Bad User
max log size = 1000
obey pam restrictions = Yes
pam password change = Yes
panic action = /usr/share/samba/panic-action %d
passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* .
passwd program = /usr/bin/passwd %u
server role = standalone server
server string = %h server (Samba, Ubuntu)
unix password sync = Yes
usershare allow guests = Yes
idmap config * : backend = tdb
[printers]
browseable = No
comment = All Printers
create mask = 0700
path = /var/spool/samba
printable = Yes
[print$]
comment = Printer Drivers
path = /var/lib/samba/printers
Next, you need to select a directory that you want to share and give permissions and ownerships as follows:
$ sudo chmod -R 755 /home/ohid/Desktop/server
Now, set up a user for samba with a username and password which enhance the security. It can be done using the following command.
$ sudo useradd smbuser
$ sudo smbpasswd -a [username]
You need to provide a username in the command. After executing, it will ask for a password. Boom! You have added a samba user. As you have completed all the initial steps, you can now start and enable the samba daemons.
$ sudo systemctl start smbd
$ sudo systemctl enable smbd
$ sudo systemctl start nmbd
$ sudo systemctl enable nmbd
Before using it, ensure that the daemons are running.
$ sudo systemctl status smbd
The result shows that samba daemons are active and operating. There is also another software called sambaclient
that you may install. Run the following command to install it.
$ sudo apt install smbclient
After that, you can check the samba server IP.
$ sudo findsmb
The IP address may be found here. You may now connect to the server from a Windows PC. To do so, navigate to the network area and add the network as shown below.
//IP_address/share
The ‘share’ is the name that you want to add. After that, you may access the shared directory and carry out your desired tasks.