User Tools

Site Tools


tutorials:remote_access

Remote access

SSH

Login

ssh (SSH client) is a program for logging into a remote machine. The typical usage is ssh user@hostname, for example:

ssh jake@168.192.1.1

If the username is the same in the two machines (i.e., the local workstation and the remote server), the username can be omitted:

ssh 168.192.1.1

Note: when connecting to a remote host for the first time, expect to see a warning about the authenticity of the host can't be established. Simply type yes will add the host key to the ~/.ssh/known_hosts file on the local machine. Unless the configuration changes on either machine, this warning will not appear again.

After you logged into the remote server via ssh, the shell runs just like you are sitting in front of the remote server. You can execute all your shell command as you normally would.

Logout

To logout once you are done, type:

exit

You will return to the shell running on your local machine, just like before you logging into the remote server.

Key

To generate key-pairs, use:

ssh-keygen -t rsa -b 4096

This command will create a pair of private key (~/.ssh/id_rsa) and public key (~/.ssh/id_rsa.pub). To enable authentication on a remote host, append the public key to the ~/.ssh/authorized_keys file in the remote host (this file should be owned by the user and have the 644 permissions).

If the home directory is encrypted, authorized_keys would not work while within the home directory before the user is logged in. Move the authorized_keys file and modify /etc/ssh/sshd_config accordingly. For example (see https://help.ubuntu.com/community/SSH/OpenSSH/Keys#Troubleshooting:

  • set up /etc/ssh/<username> (owned by user, 755 permissions).
  • in /etc/ssh/sshd_config, add AuthorizedKeysFile /etc/ssh/%u/authorized_keys
  • restart sshd: sudo service ssh restart

Keep alive

To prevent the ssh sessions from being disconnected due to inactivity, add the following lines to /etc/ssh_config on the client:

ServerAliveInterval 60
ServerAliveCountMax 60

This sends a signal every 60 seconds for a maximum of 60 times (one hour) from the client to the server.

If you do not have the permission to make this modification, put those lines to ~/.ssh/config (create the file is necessary).

SCP

scp copies files between hosts on a network. The typical usage is:

scp user@host1:file1 user@host2:file2

Similar to the example in ssh, the username can be omitted if it is the same in the two hosts; in addition, the local host can be omitted as well.

For example, to copy a file (foo.txt) from the home directory (~) of the local workstation to the home directory in the remote server, use:

scp ~/foo.txt 168.192.1.1:~

Reversely, to copy the file from the remote server back to the local workstation, use:

scp 168.192.1.1:~/foo.txt ~

Some commonly used options include:

  • -p: Preserves modification times, access times, and modes from the original file
  • -r: Recursively copy entire directories.

For example, to preserve the property of the file being copied, use:

scp -p ~/foo.txt 168.192.1.1:~

To copy an entire directory, use:

scp -r ~/foo_dir 168.192.1.1:~
tutorials/remote_access.txt · Last modified: 2019/01/11 17:35 by chkuo