Browse a remote folder visually in Linux (ssh)

In Windows, you need to resort to other programs like WinSCP or Filezilla to visually browse ssh shares. In Linux, file managers natively support this.

First add your ssh key to the agent:

ssh-add ~/.ssh/mykey_openssh.pvt

Next, for convenience, create a config file so you can use aliases to connect.
Eg: To connect to 35.180.101.49 as root, we will use the alias ovh. So config file looks like this:

Host ovh
     Hostname 35.180.101.49
     User root
     Port 22

Now open a file manager, and in the address bar type this:

sftp://ovh4

It will momentarily show the root directory of the remote server. You can now edit remote scripts on your favorite locally installed IDEs. Enjoy!
Thunar seems slow for this. Nemo is perfect.


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Change remote origin url for a git repo

From within the repo issue:

git remote set-url origin [email protected]:droidzone/reponame.git

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Error pushing to remote git

git push
Access denied.
fatal: The remote end hung up unexpectedly

More detailed info:

ssh -vt [email protected]

Log:

[email protected]:~/gitlab$ less ../gitlab-shell/gitlab-shell.log
W, [2014-12-28T14:42:17.051111 #3354]  WARN -- : gitlab-shell: Access denied for git command <git-receive-pack 'root/bash-advanced-scripts.git'> by user with key key-9.
W, [2014-12-28T14:48:25.869656 #4670]  WARN -- : gitlab-shell: Access denied for git command <git-receive-pack 'root/bash-advanced-scripts.git'> by user with key key-9.
E, [2014-12-28T19:10:24.320928 #7340] ERROR -- : API call <POST http://gitfactory.droidzone.in//api/v3/internal/allowed> failed: 401 => <{"message":"401 Unauthorized"}>.
W, [2014-12-28T19:10:24.322391 #7340]  WARN -- : gitlab-shell: Access denied for git command <git-receive-pack 'root/bash-advanced-scripts.git'> by user with key key-9.
E, [2014-12-28T19:11:46.385783 #7368] ERROR -- : API call <GET http://gitfactory.droidzone.in//api/v3/internal/discover?key_id=9> failed: 401 => <{"message":"401 Unauthorized"}>.
E, [2014-12-28T19:12:37.361048 #7978] ERROR -- : API call <GET http://gitfactory.droidzone.in//api/v3/internal/discover?key_id=9> failed: 401 => <{"message":"401 Unauthorized"}>.

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

How to set up key authentication for a server?

If you have SSH installed, you should be able to run..

ssh-keygen

Then go through the steps, you’ll have two files, id_rsa and id_rsa.pub (the first is your private key, the second is your public key – the one you copy to remote machines)

Then, connect to the remote machine you want to login to, to the file ~/.ssh/authorized_keys add the contents of your that id_rsa.pub file.

chmod 600 all the id_rsa* files (both locally and remote), so no other users can read them:

chmod 600 ~/.ssh/id_rsa*

Similarly, ensure the remote ~/.ssh/authorized_keys file is chmod 600 also:

chmod 600 ~/.ssh/authorized_keys

Then, when you do ssh remote.machine, it should ask you for the key’s password, not the remote machine.

To make it nicer to use, you can use ssh-agent to hold the decrypted keys in memory – this means you don’t have to type your keypair’s password every single time. To launch the agent, you run (including the back-tick quotes, which eval the output of the ssh-agent command)

ssh-agent

On some distros, ssh-agent is started automatically. If you run echo $SSH_AUTH_SOCK and it shows a path (probably in /tmp/) it’s already setup, so you can skip the previous command.

Then to add your key, you do

ssh-add ~/.ssh/id_rsa

and enter your passphrase. It’s stored until you remove it (using the ssh-add -D command, which removes all keys from the agent)


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Adding a second or multiple remotes to an existing git repository

Github does not provide free private git repos. So I use gitlab to host my own git server, and when I decide to go public, I switch to Github, or use Github as a second remote.

Here’s how you can add a second remote to your existing git repo.

Typically the existing remote is labelled as ‘origin’. In the example below, you’ll be adding a new remote labelled ‘github’.

As a first step, got to Github and create a new git repo. Get the github url (the git@ url, not the https one, since the latter asks for a password while pushing)

At the bash prompt, cd to the directory of your existing remote

cd /path/to/gitrepo
git remote add github [email protected]:droidzone/wordpress_plugin_updater.git
git push github master

To change a remote:

git remote set-url github [email protected]:droidzone/wordpress_plugin_updater.git

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Create a new git repository from existing code on Bitbucket

I have code on my server at /var/www/virtual/joel.co.in/droidzone.in/htdocs/keys. I wish to import it to a new Git repo on bitbucket.

First create the empty repo on Bitbucket: Visit

Now, on the server, navigate to the directory containing the code you need to import.

cd /var/www/virtual/joel.co.in/droidzone.in/htdocs/keys

Now initialize an empty repo

#git init .
Initialized empty Git repository in /var/www/virtual/joel.co.in/droidzone.in/htdocs/keys/.git/

Now, add your files to it, and make the first commit.

git add .
git commit -a

Add a remote to it:

#git remote add origin ssh:[email protected]/droidzone/securessh.git

Push everything for the first time:

#git push -u origin --all
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 1.19 KiB, done.
Total 4 (delta 0), reused 0 (delta 0)
To ssh:[email protected]/droidzone/securessh.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Running graphical programs on a remote server

This tutorial walks through setting up a remote VNC session to control the desktop of your VPS. Using it, you can control programs like Firefox, Google Chrome. My favourite use is running Firefox to download a huge file remotely from a file sharing site like Hotfile. Downloading it normally on my home PC takes ages, during which Hotfile usually disconnects me.

Steps:

Install an OS on your VPS.

Update and upgrade your packages:

# apt-get update
# apt-get upgrade

Installing for Gnome:

apt-get install ubuntu-desktop
apt-get install gdm
service gdm start
apt-get install tightvncserver
vncserver :1 -geometry 1366x768
vncserver -kill :1
vi  ~/.vnc/xstartup

Add the following to the bottom of that file:

gnome-session &amp;

Now start the server:

vncserver :1 -geometry 1024x768

Install TightVNC Windows client on your PC.

Run TightVNC Viewer, and add the following details, click Connect:

  • IP Address: [your server’s main IP]
  • Port: 5901 (or 5901, 5902 for desktop :2, etc)
  • Password: [password you set earlier]

Instructions for KDE:

apt-get install kubuntu-desktop
apt-get install kdm
service kdm start
apt-get install tightvncserver
vncserver :1 -geometry 1366x768
vncserver -kill :1
vi  ~/.vnc/xstartup

Add the following to the bottom of that file:

startkde &

Now start the server:

vncserver :1 -geometry 1024x768

Install TightVNC Windows client on your PC.

Run TightVNC Viewer, and add the following details, click Connect:

  • IP Address: [your server’s main IP]
  • Port: 5901 (or 5901, 5902 for desktop :2, etc)
  • Password: [password you set earlier]

 


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Browse sites on remote firefox session and save to remote server

Install firefox on both servers,

Add the following to /etc/ssh/ssh_config on the client computer:

ForwardX11 yes

Now, login to the remote server, and install the following:

apt-get install xauth

Now, on the remote server, run the following:

./firefox --no-remote

A few seconds later, a new firefox window will open locally, ehich actually runs remotely.

Troubleshooting:

[[email protected]] ~ #firefox -no-xshm
(process:2658): GLib-CRITICAL **: g_slice_set_config: assertion 'sys_page_size == 0' failed
PuTTY X11 proxy: unable to connect to forwarded X server: Network error: Connection refused
Error: cannot open display: localhost:10.0

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Sync directories across servers

nohup rsync --partial --progress -avvvz -e "ssh -i /root/.ssh/id_rsa" ./backups [email protected]:~/ &amp;

Here, the directory ./backups from current server is being transferred over to the remote server at 192.157.59.133. The ssh authentication normally requires a password. We have already setup a key for the server. The public key corresponding to id_rsa has been appended to .ssh/authorized_keys on remote server, so it works without asking for a password.

The command immediately returns to the shell.

If you wish to see the progress, exec:

tail -f nohup.out

In this way you can see the progress but still avoid the connection terminating and stopping the transfer. Even if disconnected this kind of command allows resume at the failed position.


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Recursive upload a directory to remote ftp location

lftp is required.

The command is:

nohup lftp -e 'mirror -R /var/www/virtual/htdocs/kernels /kernel/htdocs' -u username,password ftp2.mydomain.in

The nohup is added to prevent disconnections until the process is completed.

-R indicates reverse transfer (from local to remote)

 

Normal lftp syntax (Manual method):

lftp -u username,password ftp.mydomain.in

Now you can use various commands like:

ls – list directory

rm – delete files

rm -r recursive delete a directory

You may wish to set certain options in the lftp config file at ~/.lftp/rc:

set ftp:ssl-allow 0
set ssl:verify-certificate no
set ftp:list-options -a
debug 3

 


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.