Wednesday, August 14, 2019

Using socks proxy to tunnel apt command in ubuntu 18.04

This is useful when we have a machine (m1) that does not have an internet connection to do apt update/upgrade, but we have another machine (m2) that is able to access internet, and accessible via ssh from the m1.

Create a dynamic tunnel (socks5 proxy) from m1 to m2, using port 8888
$ ssh myuser@m2 -fN -D 8888

Set apt to use the socks proxy created above
$ echo "Acquire::http::proxy "socks5h://localhost:8888";"  | sudo tee -a /etc/apt/apt.conf.d/12proxy

Run apt command as usual in m1, and your apt command will be tunneled via the socks5h proxy
$ sudo apt update
...
0% [Connecting to SOCKS5h proxy (socks5h://localhost:8888)] [Connecting to SOCKS5h proxy (socks5h://localhost:8888)]

...

Once you are done, do not forget to kill the tunnel
$ kill `pidof ssh`

and remove the proxy option from apt
$ sudo sed -i 's/Acquire/#Acquire/' /etc/apt/apt.conf.d/12proxy 

Tuesday, August 13, 2019

Installing NFS server and client on ubuntu 18.04

Assuming the nfs server's ip address is 10.20.30.40 and the client is 10.20.30.50.

NFS Server


Install nfs-kernel-server
$ sudo apt update; sudo apt install nfs-kernel-server -y

Start nfs service
$ sudo systemctl start nfs-server

Create the export directory
$ sudo mkdir /sharing

Change permission and ownership of export directory
$ sudo chown nobody.nogroup /sharing
$ sudo chmod 777 /sharing

Allow the export directory to be accessed by client. The options are rw for read write, sync for server to write any change to disk from applying and no_subtree_check to prevent subtree checking
$ echo "/sharing 10.20.30.50(rw,sync,no_subtree_check)" | sudo tee -a /etc/exports

Export the above setting to NFS table of exports
$ sudo exportfs -a

Check if the NFS table of exports has been updated
$ sudo exportfs 
/sharing           10.20.30.50

NFS Client

Install nfs-common
$ sudo apt update; sudo apt install nfs-common

Mount the nfs export directory
$ sudo mount 10.20.30.40:/sharing /mnt

Check if client can write to the export directory and the file written, appeared in the server
$ mount | grep nfs
$ touch /mnt/newfile
$ rm /mnt/newfile

To make the mount point permanent, append it to /etc/fstab
$ echo "10.20.30.40:/sharing /mnt nfs4 defaults 0 0" | sudo tee -a /etc/fstab

Test mount from /etc/fstab
$ sudo umount /mnt; sudo mount -a



Friday, June 14, 2019

Access Your Android Phone's Storage, using Commands

There are a lot of apps for android, that enable file sharing over wifi. Some of them are free, and some of them are paid. But in this article, I will be sharing a kind of geek way on how to share your android phone's internal storage with your linux machine.

Install an app called termux from the play store. Download the app here: https://play.google.com/store/apps/details?id=com.termux . This app will add terminal emulation to your android

Once installed, allow termux to access to storage. You can do that by going to Settings --> Apps & Notifications --> Termux --> Permissions, and enable Storage

Optional: to make your typing experience better, I recommend this keyboard app: https://play.google.com/store/apps/details?id=org.pocketworkstation.pckeyboard

The http server of our choice is, the python's simple-http server. To use the simple-http, first you need to install python. Run below in your termux console
$ pkg install python -y

Get your phone's ip address. This can be easily done in termux
$ ip a | grep wlan0

Run http.server, to share your phone's internal storage over the wifi. /storage/emulated/0 is the default root directory for android phone's internal storage
$ python -m http.server /storage/emulated/0 

In your PC/laptop/other android, just use a browser to access the phone, via port 8000

Thursday, June 13, 2019

Accessing Android Phone via ssh

Sometimes, for whatever reason, you need to access your phone from your laptop. This can be done by installing openssh inside your phone.

First, you need an app called termux, to enable terminal emulator inside android. The app can be downloaded here, in google play store.


Once installed, fire up termux, and install openssh 
$ pkg install openssh

You need password to login via ssh, so set a password for your current user
$ passwd

You also need to know, what is the current username
$ whoami

Run sshd. By default sshd in termux runs on port 8022
$ sshd

Get the ip address of your android phone
$ ip addr show wlan0

Connect to your phone (replace 192.168.43.200 with the output of the ip addr command), using standard ssh client on linux/mac terminal, or putty on windows
$ ssh u0_a211@192.168.43.200 -p 8022 

In order to browse the whole internal storage of your phone, you need to enable storage permission for termux. That can be done by running:
$ termux-setup-storage

and clicking allow



Friday, May 31, 2019

Update time and date using chrony

Chrony is an application for a machine to sync with network time protocol servers.

To install chrony
# yum install chrony -y

Then, make sure to include some legit ntp servers in /etc/chrony.conf. In this case, we are using centos ntp pool servers.
# cat /etc/chrony.conf
...
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst

...

Start chronyd (chrony daemon)
# systemctl start chronyd

Chrony will gradually update the system clock to follow the ntp servers, once chronyd is started. But if you want to chrony to update the time quickly, just use below command
# chronyc makestep

To start chronyd on boot:
# systemctl enable chronyd