There are a lot of things that you might not know how to do with SSH that can be helpful. Here are a few tricks that I've learned over time....
Accidently deleted your public key? This command will regenerate the public key for you. ssh-keygen -y -f ~/.ssh/id_rsa > ~/.ssh/id_rsa.pub Need to convert that RSA key to a PEM key for a windows box? Here you go openssl req -x509 -new -key ~/.ssh/id_rsa -out id_rsa.pem Forgot how many bits that private key was? openssl rsa -text -noout -in ~/.ssh/id_rsa | grep Private-Key: Private-Key: (8192 bit) Have a few SSH keys and can't keep track of which goes to what server? Want an easier way to log into your servers?
vi ~/.ssh/config
#Insert these lines:
Host server1 192.168.1.100
HostName 192.168.1.100
IdentityFile ~/.ssh/id_rsa
User ubuntu
Host server2 192.168.1.150
HostName 192.168.1.150
IdentityFile ~/.ssh/id_rsa-2
User centos
Host server3 192.168.1.200
HostName 192.168.1.200
IdentityFile ~/.ssh/id_rsa-3
User dev
Once this is done, you can do things like: # To SSH into server1: ssh server1 # To copy a local file to server1: scp myfile server1:~ #sftp with options: sftp -oPort=24 server1 Need to forward local ports to a remote machine: ssh -L 3306:localhost:3306 username@hostname Some Helpful Commands: To determine how many bits your private key is: openssl rsa -text -noout -in id_rsa | grep Private-Key: Output: Private-Key: (8192 bit) To reproduce your public key in case you lost it: ssh-keygen -y -f ~/.ssh/id_rsa > ~/.ssh/id_rsa.pub To copy your public key to allow for passwordless SSH: ssh-copy-id -i ~/.ssh/id_rsa.pub username@remotehost To create a PEM file from id_rsa (Note: an existing private key is needed): openssl req -x509 -new -key id_rsa -out PEM_ID_RSA_FILE.pem Output: PEM_ID_RSA_FILE.pem To send a command over SSH: ssh user@ipaddress 'ls -lrt' To send a command over SSH that requires user interaction: ssh -t user@ipaddress 'top' References: https://linux.die.net/man/5/ssh_config https://stackoverflow.com/questions/2419566/best-way-to-use-multiple-ssh-private-keys-on-one-client https://serversforhackers.com/ssh-tricks |
AuthorJames Benson is an IT professional. Archives
August 2022
Categories
All
|