There are a lot many ways to transfer a file from one machine to a remote or a local machine in a Linux terminal. Here are a few of them which may help you when you are stuck. Before starting i would like to thank ippsec for all these tricks. Thank you man, Kudos to you!!!



For this post i will use a simple text file which is called as text.file

  • Python Http Server

    You can use the Python’s SimpleHTTPServer module for this particular technique. First you need to fire up the python’s HTTP server using the command python -m SimpleHTTPServer. The -m is a switch to specify the module python and there you can run this command directly on your shell. And this server serves the files in the current directory where it is running.


    This command is executed on the machine from which you want to transfer files to your machine. By default this server will run port 8000 you can even specify the custom port by just appending the port at the end. In the below example, the server serves on port 80.
    python -m SimpleHTTPServer 80
    And on the remote machine use the wget command to download the file from the server.

    If Python3 is installed then, the twin of SimpleHTTPServer module of Python2 is the http.server. The syntax is just similar to the other one, python3 -m http.server <port> as default port is 8000.



  • Python FTP Server

    For this technique, you need to install the python package pyftpdlib if not installed.

    To know more about installing packages click here.

    Then use the command python -m pyftpdlib to start the ftp server. By default the server runs on port 2121 and this can even be customized using the -p switch.


    This server provides anonymous login facility, and can also include custom username and password.



  • Twistd FTP Server

    You need to install twistd program to use this technique. You can do this by running sudo apt-get install twistd command on your terminal or with any equivalent package manager. You can start the twistd FTP server by running twistd ftp command on the terminal. The service starts and runs in the background. By default the twistd service runs on port 2121, we can customize the port. This also allows anonymous login and hence the credentials can also be changed if needed. The twistd FTP service uses /usr/local/ftp as the default root directory for the file server. You can modify it by using the switch -r or --root= and provide the path.



  • Nginx server

    You need to have an nginx server on the machine where you want to accept the files. Then you just need to configure a bit to work appropriately. For the Ubuntu system, the configuration files will be under the /etc/nginx/ directory. We actually need to create a new file here its called transfer_files under /etc/nginx/sites-available/ and add the below code in it.
 
server {
		listen 8421 default_server;
        location / {
        		root /dev/shm;
                	dav_methods PUT;
       	}
}


This server actually listens on port 8421 and accepts the requests with the PUT method and places the file in the /dev/shm/ directory.You customize the port and the root directory by replacing the port and the directory path in the above code.
    Then we need to create a symbolic link of the created file to the /etc/nginx/site-enabled directory.

ln -s /etc/nginx/sites-available/transfer_files /etc/nginx/sites-enabled
Then restart the service by using the command sudo service nginx restart. You can use curl command to upload the file from the remote machine to the nginx server. You can use curl --upload-file /path/to/file <ip>:<port> to upload the file.


    Then the file test will be transfered to the /dev/shm of the remote machine which is running nginx server.

  • Apache Server

    This technique is similar to the above technique, but here we use the Apache Server. The apache server will run to server files here. The default directory which the apache uses it /var/www/html/. We can put any files into this directory if we are root user. Then start/restart the Apache service using the command sudo service apache2 start. Now, the remote machine can use any tool like, curl or wget to download the file.

  • SCP Command

    When we are on a ssh connection to the client, we can actually use the scp command to transfer the file to the remote machine securly over ssh tunnel. The scp command to send a file to the remote machine from your local machine is given by,
    scp test.file <RemoteUsername>@<hostname/ip>:/remote/destination/directory


The reverse is also possible, i.e it is possible to receive the files from the remote machine using scp.The command to retrieve files from the remote machine is given by, scp <RemoteUsername>@<hostname/ip>:/remote/source/file /destination/local/path

  • Base64 command

    This command is very helpful to decode and encode the characters in base64. This command can also be used when we are running a VM locally or, when we are connected to the VPN to transfer files. This command can be used to convert the contents of the file to the base64 and then on the local machine decode it. This is done because, it prevents loss or damage of characters while copying directly.


    Here the file is encoded into base64 and then the encoded text is copied into the local machine.


    In the local machine, the encoded text is put into a file, and then decoded using the command base64 -d. This technique supports any kind of files.



  • Netcat or Ncat

        Netcat or Ncat is the swiss army tool for performing the sysadmin work. We can use it transfer files too. On the sender side, we need to listen on a particular port to serve files, cat filename | ncat -lvnp <port>.


On the receiver side, this command bash -c "cat < /dev/tcp/ip/port > /tmp/filename" is used to recieve the contents of the file and then write it to the file in the /tmp directory. Here the ip and port has to be replaced with the ip and port of the server.



Still much more techniques are yet to come..!!