Home Fileserver How To Instantly Setup Static File Server In Linux

How To Instantly Setup Static File Server In Linux

By sk
Published: Last Updated on 7.3K views

Ever wanted to share your files or project over network, but don't know how to do? No worries! Here is a simple utility named "Serve" to share your files instantly over network. The Serve utility will instantly setup static file server in Linux and allow you to serve your files over local area network. You can access the files from any devices regardless of their operating system. All you need is a web browser. This utility can also be used to serve static websites. It is formerly known as "list" and "micro-list", but now the name has been changed to "serve", which is much more suitable for the purpose of this utility.

Setup Static File Server using Serve in Linux

To install "serve", you need to install NodeJS and NPM first. Refer the following link to install NodeJS and NPM in your Linux box.

Once NodeJS and NPM installed, run the following command to install "serve".

$ npm install -g serve

Done! Now is the time to serve the files and folders over http network.

The typical syntax to use "serve" is:

$ serve [options] <path-to-files-or-folders>

Serve Specific Files or Folders

For example, let us share the contents of the Documents directory. To do so, run:

$ serve Documents/

Sample output would be:

Share files and folders using Serve in Linux
Share files and folders using Serve in Linux

As you can see in the above screenshot, the contents of the given directory have been served over network via two URLs.

To access the contents from the local system itself, all you have to do is open your web browser and navigate to http://localhost:5000/ URL.

Access files and folders via web browser
Access files and folders via web browser

The Serve utility displays the contents of the given directory in a simple layout. You can download (right click on the files and choose "Save link as..") or just view them in the browser.

If you want to open local address automatically in the browser, use -o flag.

$ serve -o Documents/

Once you run the above command, The Serve utility will open your web browser automatically and display the contents of the shared item.

Similarly, to access the shared directory from a remote system over network, type http://192.168.43.192:5000 in the browser's address bar. Replace 192.168.43.192 with your system's IP.

Serve Contents via Different Port

As you may noticed, the serve utility uses port 5000 by default. So, make sure the port 5000 is allowed in your firewall or router. If it is blocked for some reason, you can serve the contents using different port using -p flag.

$ serve -p 1234 Documents/

The above command will serve the contents of Documents directory via port 1234.

Serve file contents via different port
Serve file contents via different port

To serve a file, instead of a folder, just give its full path like below.

$ serve Documents/Papers/notes.txt

The contents of the shared directory can be accessed by any user on the network as long as they know the path.

Serve the Entire $HOME Directory

Open your Terminal and type:

$ serve

This will share the contents of your entire $HOME directory over network.

To stop the sharing, press CTRL+C.

Exclude Files or Folders

You may not want to share all files or directories, but only a few in a directory. You can do this by excluding the files or directories using -i flag.

$ serve -i Downloads/

The above command will serve entire file system except Downloads directory.

Serve Contents Only on Localhost

Sometimes, you want to serve the contents only on the local system itself, not on the entire network. To do so, use -l flag as shown below:

$ serve -l Documents/

This command will serve the Documents directory only on localhost.

Serve contents only on localhost
Serve contents only on localhost

This can be useful when you're working on a shared server. All users in the in the system can access the share, but not the remote users.

Serve Content via HTTPS

Since we serve the contents over the insecure local network (i.e. http), we need not to use SSL. However, Serve utility has the ability to shares contents via HTTPS using --ssl flag.

$ serve --ssl Documents/
Serve content via SSL
Serve content via SSL

To access the shares via web browser use "https://localhost:5000" or "https://ip:5000".

Securely access files and folders via web browser
Securely access files and folders via web browser

Serve Contents with Authentication

In all above examples, we served the contents without any authentication. So anyone on the network can access them without any authentication. You might feel some contents should be accessed with username and password.

To do so, use:

$ SERVE_USER=ostechnix SERVE_PASSWORD=123456 serve --auth

Now the users need to enter the username (i.e ostechnix in our case) and password (123456) to access the shares.

Serve contents with authentication
Serve contents with authentication

The Serve utility has some other features, such as disable Gzip compression, setup * CORS headers to allow requests from any origin, prevent copy address automatically to clipboard etc. You can read the complete help section by running the following command:

$ serve help

Resource:

You May Also Like

2 comments

Rick Stanley April 14, 2018 - 8:23 pm

Rather than install NodeJS and NPM, then serve, I would strongly recommend installing SSH server and client. “serve” may, and probably do have security issues.

SSH can be used safely remotely, and locally.

Please see the following sites:

https://www.nixtutor.com/linux/installing-and-configuring-an-ssh-server/ (Many other sites give similar instructions)
https://www.digitalocean.com/community/tutorials/how-to-configure-ssh-key-based-authentication-on-a-linux-server

To secure ssh even more:

https://www.cyberciti.biz/tips/linux-unix-bsd-openssh-server-best-practices.html
https://linux-audit.com/audit-and-harden-your-ssh-configuration/

and others.

Reply
Jay Woods April 14, 2018 - 9:36 pm

[woodsj@verle ~]$ serve Documents/
/usr/lib/node_modules/serve/bin/serve.js:83
detect(port).then(async open => {
^^^^^

SyntaxError: missing ) after argument list
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:383:7)
at startup (bootstrap_node.js:149:9)
[woodsj@verle ~]$

What?

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More