Startig SSH on boot can be enabled with a simple command:
sudo systemctl enable ssh
Or you can use it once using sudo systemctl start sshd.
In some cases this strategy might not work. For those cases, try the following commands:
sudo update-rc.d ssh defaults
sudo systemctl enable ssh.socket
sudo systemctl enable ssh.service
You can check your sshd.service file using sudo cat /etc/systemd/system/sshd.service. My file looks like the following:
[Unit]
Description=OpenBSD Secure Shell server
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target auditd.service
ConditionPathExists=!/etc/ssh/sshd_not_to_be_run
[Service]
EnvironmentFile=-/etc/default/ssh
ExecStartPre=/usr/sbin/sshd -t
ExecStart=/usr/sbin/sshd -D $SSHD_OPTS
ExecReload=/usr/sbin/sshd -t
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartPreventExitStatus=255
Type=notify
RuntimeDirectory=sshd
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
Alias=sshd.service
If all these don't work, try creating the sshd.service file yourself and enable it. Follow these steps to achieve that:
- Create a new Systemd unit file for SSH:
sudo nano /etc/systemd/system/sshd.service. - Add the following contents to the file:
[Unit] Description=OpenSSH Server After=network.target [Service] ExecStart=/usr/sbin/sshd Restart=on-failure [Install] WantedBy=multi-user.target - Reload the Systemd daemon:
sudo systemctl daemon-reload. - Enable the SSH service:
sudo systemctl enable sshd. - Start the SSH service:
sudo systemctl start sshd.
For more information: https://askubuntu.com/questions/892447/start-ssh-automatically-on-boot
To set up ngrok in a way that it always starts when the machine starts, follow the steps below.
- You can install ngrok using the following command:
snap install ngrok. - Now that you have installed ngrok on your Linux device, link it to your ngrok account by using your authtoken:
ngrok authtoken NGROK_AUTHTOKEN. Replace NGROK_AUTHTOKEN with your unique ngrok authtoken found in the ngrok dashboard (https://dashboard.ngrok.com/get-started/your-authtoken) - You can fire up an easy SSH access with ngrok:
ngrok tcp 22. - To make ngrok start and run the
ngrok tcp 22command automatically whenever the system starts, we have to make a systemd service file. Create a systemd service file named ngrok.service with the following contents:Here,[Unit] Description=ngrok After=network.target [Service] Type=simple ExecStart=/snap/bin/ngrok tcp 22 RuntimeMaxSec=7100s Restart=always [Install] WantedBy=multi-user.target/snap/is where ngrok has been installed (since we installed it using snap), and/snap/bin/is where the ngrok executable lie. Change it if you installed ngrok differently. TheRuntimeMaxSecis set at 7100 seconds, which is roughly 118 minutes. Since ngrok's service automatically ends every 2 hours, we are restarting our service every 2 hours (almost). This way, we will always have an ngrok service running. Although every 2 hours you will be assigned a new address, but that you can check from ngrok's website time to time. - To make the file, use the command:
sudo nano /etc/systemd/system/ngrok.service. Write the contents in the file. Save it by pressingctrl + xfollowed by anenter. And close the editor by pressingctrl + o. - Enable the systemd service by running the following command:
sudo systemctl enable ngrok.service - Start the systemd service by running the following command:
sudo systemctl start ngrok.service
For more information, check these two resources:
- Ngrok download page: https://ngrok.com/download
- Ngrok easy guide for linux: https://ngrok.com/docs/guides/device-gateway/linux/
- Ngrok configuration and systemd service instructions: https://stackoverflow.com/questions/50681671/how-to-keep-ngrok-running-even-when-signing-off-of-a-server
Use the following command to SSH into a server. Notice that the -p here is small 'p', unlike how -P is used to denote a port in the scp command, which is a capital 'P'.
ssh -p port_number username@server_address
For example:
ssh -p 10609 rootuser@0.tcp.in.ngrok.io
Get this port and the IP from https://dashboard.ngrok.com/tunnels/agents. For me, the username of my linux user is rootuser. So I log into that first. Then I access the root using sudo su. I can not log into root directly.
After entering this command, you will be prompted with:
The authenticity of host '[0.tcp.in.ngrok.io]:10609 ([3.6.122.107]:10609)' can't be established.
ECDSA key fingerprint is SHA256:B5****************w4Y.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Type yes and hit enter. It should next ask you for your password. In most cases this should be the same password as your root password.
Use the SCP command to send a file from PC to the server.
scp -P port [options] source_file destination_file
Here are some additional options that you can use with the SCP command:
-r: Recursively copy the entire directory and its contents.-p: Preserve the file permissions and timestamps. [This is small p, port is capital P.]-v: Verbose mode, which will display more information about the file transfer.
For example, to copy the file myfile.txt from your Windows PC to the directory /home/rootuser/ on the Linux machine, preserving the file permissions and timestamps, and using port 3940 for the SSH connection, you would use the following command:
scp -P 3940 -p myfile.txt rootuser@linux_machine_ip:/home/user/
To download the file from the Linux machine to your Windows PC, you can use the SCP command again. The syntax is the same as before, but you need to specify the source file and destination file in reverse. For example, to download the file myfile.txt from the directory /home/user/Documents on the Linux machine to the directory C:\Users\username\Downloads on your Windows PC, you would use the following command:
scp rootuser@linux_machine_ip:/home/user/Documents/myfile.txt C:\Users\username\Downloads
You can use the -P option to specify the port of the linux machine here as well.
To have your own network attached storage, you need to install Samba on your linux system. But remember, this NAS can not be accessed outside local network without port forwarding. To install samba and setup a network drive, follow the steps below.
- To install Samba, run:
sudo apt updateand then,sudo apt install samba. - Check if the installation was successful by running:
whereis samba. The output should look like:samba: /usr/sbin/samba /usr/lib/samba /etc/samba /usr/share/samba /usr/share/man/man7/samba.7.gz /usr/share/man/man8/samba.8.gz. - Now that Samba is installed, we need to create a directory for it to share:
mkdir /home/<username>/SharedStorage/. For example, for me, the command was:mkdir /home/rootuser/SharedStorage/ - The configuration file for Samba is located at
/etc/samba/smb.conf. To add the new directory as a share, we edit the file by running:sudo nano /etc/samba/smb.confAt the bottom of the file, add the following lines:[shared] comment = Samba on Ubuntu path = /home/username/SharedStorage read only = no browsable = yes - Then press
ctrl + oto save andctrl+xto exit from the nano text editor. - Now that we have our new share configured, restart Samba for it to take effect:
sudo service smbd restart. - Update the firewall rules to allow Samba traffic:
sudo ufw allow samba. - We need to set up a Samba password for our user account:
sudo smbpasswd -a username. Note that, username used must belong to a system account, else it won’t save. For me, the command was:sudo smbpasswd -a rootuser. Make sure this password contains no special characters or capital letters (not sure about the capital letters). - To make samba run every time your machine boots up, run:
sudo systemctl enable smbd(I have not tested this). - Run this command to see your IP address:
ifcofig. Copy the IP address that looks like192.168.xxx.xxx. - To connect the drive to a windows PC, ope file manager, to go This PC. Go to Map netowrk drive -> Map network drive. Put the IP address you copied on the followig format:
\\ip-address\shared. Here,sharedis the string you wrote inside the square brackets in the config file. For me, it was\\192.168.25.96\share. Check the box 'Connect with different credentials' and hit 'Finish'. You will be prompted for a password. Type in the samba password you created.
For more information: https://ubuntu.com/tutorials/install-and-configure-samba#3-setting-up-samba
Common troubleshooting:
- Windows connection issue: https://stackoverflow.com/questions/58528469/windows-10-cant-connect-to-ubuntu-16-04-samba
- Windows password issue: https://serverfault.com/questions/549289/the-network-folder-specified-is-currently-mapped-using-a-different-user-name-and
Download and install WinSCP on Windows. Then follow the following steps:
- Open WinSCP. Click on new tab.
- Put the following values in the following fields.
- File protocol: SCP
- Hostname: The ngrok address without the
tcp://part - Port: ngrok port
- Username and password: your user's username and password (not the Samba one).
- Save the configuration and connect.
- On the left you will see your Windows files, on the right you will see the files and directories in your linux system.
When you transfer to or from a directory in linux, you might see and error such as, you don't have sufficient permissions for this. This occurs when your linux user does not have permission to a specific directory or file. Using the following command can give permission to an user: chmod -R <permissions> <directory>. To give all users complete permissions to a directory, use chmod -R 777 <directory>. Here, -R is for recursive. It gives permission for all files, directories inside the directory mentioned.
Some more information: https://superuser.com/questions/311658/make-a-network-drive-available-over-the-internet
This step is only required if you want to use Localtunnel. Doing apt-get install nodejs always installs version 10.x. But we need the latest version. So here's how you achieve that.
- Go to NodeJs official download site (https://nodejs.org/en/download/current).
- Find 'Linux Binaries (x64)', right click on the download link (currently it's just a button with '64-bit' written on it) and copy the link.
- Go to your ubuntu terminal, run the following:
wget <link you just copied>. For example:wget https://nodejs.org/dist/v21.2.0/node-v21.2.0-linux-x64.tar.xz. Make sure to get the ARM link for ARM systems. You can know what system you are on by running,uname -m. If it shows something likex86_64then you are not on an ARM system. - Make a directory where you want to install NodeJs and your other dowloaded softwares in. I dowloaded the archive in my /home/user/ (the user directory you end up in after you run
sudo su) and I created a directory in it to install NodeJs and other downloaded softwares in. You can do it usingmkdir <directory name>. For example, I ranmkdir installed-softwares. - Extract the files using this command:
sudo tar -xJvf <nodejs package filename> -C <installation-directory>. For example, my command was:sudo tar -xJvf node-v21.2.0-linux-x64.tar.xz -C installed-softwares/. Run this command with-xJfinstead of-xJvfto not show logs during the extraction process. - Go to installed-softwared:
cd installed-softwares. - Check the files and folders there:
ls. - You should see a directory named
node-v21.2.0-linux-x64. Go in it:cd node-v21.2.0-linux-x64. Check the files ad directories inside it usign thelscommand. There should abindirectory. The whole path to bin should be something like/home/rootuser/installed-softwares/node-v21.2.0-linux-x64. We will need this directory location later. - Open your shell configuration file for editing. For example:
nano ~/.bashrcornano ~/.bash_profile. Choose the appropriate file based on your shell. For menano ~/.bashrcworked, so I recommed that. - Add the following line in the very bottom on the file:
export PATH=<nodejs installation directory>/bin:$PATH. For me, this was:export PATH=/home/rootuser/installed-softwares/node-v21.2.0-linux-x64/bin:$PATH - Save the file by pressig
ctrl + oand exit from nano editor by pressingctrl + x. - To apply the changes to your current session, you can either restart your terminal or run:
source ~/.bashrc. - Lastly, check the installation by running:
node -v.
For more info: https://github.com/nodejs/help/wiki/Installation
Localtunnel does not have a web UI like ngrok. So if you are running an SSH server on a machine without a monitor, you won't be able to know what address your localtunnel service is available at. You can't use localtunnel for SSH. But you can use localtunnel on a machine where ngrok is already running, and use localtunnel to expose an apache webserver or something to the web. But, opening a localtunnel address on a browser prompts you to put the IP of the host machine first. This is a headache sometimes. So, just use bore instead (instructions below).
After you have installed NodeJs and npm (npm comes with NodeJs), run this command to install localtunnel.
npm install -g localtunnel
To expose a port to the web, run this command:
lt --port <port>
For example, to expose port 8000, run:
lt --port 8000
Bore does not have a web UI like ngrok. So if you are running an SSH server on a machine without a monitor, you won't be able to know what address your bore service is available at. For SSH server purposes, ngrok is still the best. But you can use bore on a machine where ngrok is already running, and use bore to expose an apache webserver or something to the web.
Bore installation is very similar to NodeJs installation.
- Go to bore official GitHub page (https://github.com/ekzhang/bore).
- Go to the Releases page (https://github.com/ekzhang/bore/releases), and find the release that suits you. For me, it is the 64-bit Linux release. Make sure to get the ARM link for ARM systems. You can know what system you are on by running
uname -m. If it shows something likex86_64then you are not on an ARM system. Currently, the filename for 64-bit linux systems looks likebore-v0.5.0-x86_64-unknown-linux-musl.tar.gz. Right click on it and copy the link. - Go to your ubuntu terminal, run the following:
wget <link you just copied>. For example:wget bore-v0.5.0-x86_64-unknown-linux-musl.tar.gz. - Make a directory where you want to install bore and your other dowloaded softwares in. I dowloaded the archive in my /home/user/ (the user directory you end up in after you run
sudo su) and I created a directory in it to install bore and other downloaded softwares in. You can do it usingmkdir <directory name>. For example, I ranmkdir installed-softwares. Skip this step if you have already done this earlier. - Make a directory specifically for bore inside that directory you created in the previous step. This is because extracting bore package just extracts a runnable file, that's not in any directory. Run
mkdir <your softwares directory>/bore/. For me, this command was:mkdir installed-softwares/bore. - Extract the files using this command:
sudo tar -xzvf <nodejs package filename> -C <installation-directory>. For example, my command was:sudo tar -xJvf bore-v0.5.0-x86_64-unknown-linux-musl.tar.gz -C installed-softwares/bore/. Run this command with-xzfinstead of-xzvfto not show logs during the extraction process. - Go to installed-softwared:
cd installed-softwares. - Check the files and folders there:
ls. - You should see a directory named
bore, that you created earlier. Go in it:cd bore. Check the files and directories inside it usign thelscommand. There should a runnable filebore. The whole path to bore directory should be something like/home/rootuser/installed-softwares/bore. We will need this directory location later. - Open your shell configuration file for editing. For example:
nano ~/.bashrcornano ~/.bash_profile. Choose the appropriate file based on your shell. For menano ~/.bashrcworked, so I recommed that. - Add the following line in the very bottom on the file:
export PATH=<bore installation directory>:$PATH. For me, this was:export PATH=/home/rootuser/installed-softwares/bore:$PATH - Save the file by pressig
ctrl + oand exit from nano editor by pressingctrl + x. - To apply the changes to your current session, you can either restart your terminal or run:
source ~/.bashrc.
To use bore, run the following command:
bore local <port> --to bore.pub
This would expose the localhost on the port specified to the web. Bore will print a randomly generated port number (example: bore.pub:18989). Use this address to access your localhost:<port> on the web.
To expose a webserver running on port 8000, run:
bore local 8000 --to bore.pub
If this content helped you, please give a star to this repository. Feel free to clone this repository. PRs are also welcome.
A very small portion of the content here is copied from other sources. I hope it doesn't fall under piracy as I only intend to share the kowledge through this writeup.