Usage of ssh/scp/sshfs

This article will describe basic usage of ssh, scp and sshfs.

 

1 Install openssh-server

In case of Ubuntu 16.04 / Debian 8 is as below.

$ sudo apt install -y openssh-server
$ sudo systemctl enable ssh # enabled by default
$ sudo systemctl start ssh  # started by default

In case of Fedora 24 is as below.

$ sudo dnf install -y openssh-server
$ sudo systemctl enable sshd
$ sudo systemctl start sshd

In case of CentOS 7 is as below.

$ sudo yum install -y openssh-server
$ sudo systemctl enable sshd
$ sudo systemctl start sshd

2 Public key authentication for no passphrase

Public key authentication can skip passphrase. In case of Windows, this article uses teraterm as ssh client.

2.1 Client (Linux/Unix)

Create public key and private key. Make passhrase be empty for no passhurase.

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hiroom2/.ssh/id_rsa):
Created directory '/home/hiroom2/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/hiroom2/.ssh/id_rsa.
Your public key has been saved in /home/hiroom2/.ssh/id_rsa.pub.
The key fingerprint is:

The private key is created at ${HOME}/.ssh/id_rsa and public key at ${HOME}/.ssh/id_rsa.pub.

$ ls -l ~/.ssh
total 8
-rw------- 1 hiroom2 hiroom2 1675  6月 29 17:26 id_rsa
-rw-r--r-- 1 hiroom2 hiroom2  399  6月 29 17:26 id_rsa.pub

Copy id_rsa.pub to server. This needs passphrase yet.

$ scp .ssh/id_rsa.pub <server>:${HOME}/

2.2 Client (Windows)

Create public key and private key with teraterm.

Select SSH KeyGenerator.

0001_SSH-KeyGenerator.png

Generate with RSA and 2048 bit by default.

0002_RSA-2048.png

Make passhrase be empty for no passhurase, and select Save public key / Save private key.

0003_Key-passphrase.png

Copy id_rsa.pub to server.

The teraterm needs setting for public key authentication.

Select SSH Authentication.

0004_SSH-Authentication.png

Input username and set created id_rsa as Private key file. Select OK.

0005_Use-RSA.png

Select Save setup and overwrite to below file.

C:\Program Files (x86)\teraterm\TERATERM.ini

0006_Save-setup.png

Input server IP address or hostname at New connections. Select OK.

0007_New-connection.png

id_rsa is used for SSH authentication by default. Select OK and connect to server.

0008_default.png

2.3 Server

Add id_rsa.pub created by client to ${HOME}/.ssh/authorized_keys.

$ cat id_rsa.pub >> ${HOME}/.ssh/authorized_keys

ssh/scp/sshfs commands does not need pass phrase.

3 Disable checking known_hosts for some host

ssh detects server spoofing with ${HOME}/.ssh/known_hosts. This is good for security but inconvenience for virtual machine.

This article will disable checking known_hosts for 192.168.11.0 network and *.hiroom2.com domain.

$ cat .ssh/config
Host 192.168.11.*
  StrictHostKeyChecking no
  UserKnownHostsFile=/dev/null

Host *.hiroom2.com
  StrictHostKeyChecking no
  UserKnownHostsFile=/dev/null

4 File transfer with scp command

The scp command transfer file between client and server. It is included in openss-client/openssh-clients package.

$ sudo apt install -y openssh-client  # Ubuntu 16.04 / Debian 8
$ sudo dnf install -y openssh-clients # Fedora 24
$ sudo yum install -y openssh-clients # CentOS 7

In case of file is as below.

$ scp <path-to-file> <server>:<path-to>
$ scp <server>:<path-to-file> <path-to>

In case of directory is as below.

$ scp -r <path-to-dir> <server>:<path-to>
$ scp -r <server>:<path-to-dir> <path-to>

4.1 WinSCP

 WinSCP provides scp command on Windows.

Run WinSCP and input server setting. For public authentication, WinSCP allow only keys created by putty and need exchange keys created by teraterm.

0009_Login.png

Connect to server.

0010_WinSCP.png

5 Mount server directory with sshfs

Install sshfs with apt、yum、dnf and homebrew.

$ sudo apt install -y sshfs  # Ubuntu 16.04 / Debian 8
$ sudo yum install -y sshfs  # CentOS 7 with EPEL repository
$ sudo dnf install -y sshfs  # Fedora 24
$ brew install sshfs         # OSX

sshfs only do mount.

$ sshfs ubuntu-16.04-sshfs.hiroom2.com:/home/hiroom2 mnt
$ ls mnt/
Desktop    Downloads  Pictures  Templates  bin               mnt
Documents  Music      Public    Videos     examples.desktop  src

fusermount or umount is needed for unmount.

$ fusermount -u mnt # Run "umount mnt" on OSX

There is no sshfs client for Windows 10. win-sshfs will work on Windows 10.