User Tools

Site Tools


networking:ssh

This is an old revision of the document!


SSH

Install SSH Server

In Ubuntu 16.04, you can install SSH server with

sudo apt-get install openssh-server

You can ensure it starts at boot with

systemctl enable ssh.socketv

SSH Key Generation

To generate an RSA key pair to work with version 2 of the protocol, type the following command at a shell prompt. Accept the default file location of ~/.ssh/id_rsa. Good security practice dictates that you should enter a password that is unique.

ssh-keygen -t rsa

The public key is written to ~/.ssh/id_rsa.pub. The private key is written to ~/.ssh/id_rsa. Never distribute your private key to anyone.

Change the permissions of the .ssh directory using the following command:

chmod 755 ~/.ssh

Copy the contents of ~/.ssh/id_rsa.pub into the file ~/.ssh/authorized_keys on the machine to which you want to connect. If the file ~/.ssh/authorized_keys exist, append the contents of the file ~/.ssh/id_rsa.pub to the file ~/.ssh/authorized_keys on the other machine. An easy way to do this is by using the

ssh-copy-id

command as follows

ssh-copy-id -i ~/.ssh/id_rsa.pub username@target.example.com

Change the permissions of the authorized_keys file using the following command:

ssh username@target.example.com 'chmod 644 ~/.ssh/authorized_keys'

Run Single Command Remotely

ssh username@target.example.com 'ls -l'

Run Multiple Commands Remotely

ssh username@target.example.com 'ls -l; ps -aux; whoami'

Run Single Command Remotely and Interactively

Note the -t flag. That tells SSH that you'll be interacting with remote shell. Without the -t flag,

top

will return results after which SSH will log you out of the remote host immediately. With the -t flag, SSH keeps you logged in until you exit the interactive command. The -t flag can be used with most interactive commands, including text editors like pico and vi.

ssh -t username@target.example.com 'top'

=Run Command With Single Quotes= When running remote SSH commands, you may need to escape quotes. Replace ' with '“'”'

's/%//g'

With

sed '"'"'s/%//g'"'"'

Connect to Host Without Checking Keys

While this is dangerous, it can be useful when running some data gathering scripts on a trusted network.

ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no username@target.example.com 'ls -l'

List SSH Fingerprint

If you want to see the fingerprint that you see first time you connect to a server, run this command

ssh-keygen -lf /etc/ssh/ssh_host_rsa_key.pub

You may also need the following command (but probably not)

ssh-keygen -lf /etc/ssh/ssh_host_key.pub

To list the key in the format shown by PuTTY, use

ssh-keygen -l -E md5 -f /etc/ssh/ssh_host_rsa_key.pub

Show all

for file in /etc/ssh/*sa_key.pub; do ssh-keygen -lf $file; done

Fix SSH Key Generation Problem in CentOS 6

By default, CentOS 6.0 and some later releases of 6.x, SELinux interferes with the root user's ability to generate SSH keys. To fix the problem, run the following commands as the root user.

chcon -t ssh_home_t ~/.ssh

Fix Remote Root Login on CentOS 6

By default, CentOS 6.0 and some later releases of 6.x, SELinux prevents the root user from logging into a system using SSH. This is because of SELinux attributes on the

/root/.ssh/authroized_keys

file. I believe this is due to the policy in

/etc/selinux/targeted/contexts/users/root

. To fix this, we just run the following command.

restorecon -R -v /root/.ssh

Restrict Users and Groups from Login

To only allow a specific list of users and groups to login, add the following to

/etc/ssh/sshd_config
AllowUsers user1 user2
AllowGroups group1 group2

Block Users and Groups from Login

To allow all users to login except for a specific list, add the following to

/etc/ssh/sshd_config
DenyUsers user1 user2
DenyGroups group1 group2

Fix Logon Delay

You may find that when connecting to a SSH server, the password prompt takes a long time to appear. Try setting the following line at the bottom of

/etc/ssh/sshd_config
GSSAPIAuthentication no

Then restart the sshd server with the following command

service sshd restart

If that doesn't work, try adding the following the bottom of

/etc/ssh/sshd_config
UseDNS no

Extract Public Key

chmod 600 private.pem
ssh-keygen -f private.pem -y > public.pub
networking/ssh.1676469931.txt.gz · Last modified: by bstafford