OpenSSL/SSH: Difference between revisions

From wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
[[Category:Networking]]
[[Category:Networking]]
[[Category:Linux/Unix]]
=key-handling=
;ssh-keygen -t ed25519
:Create a keypair (accept all defaults, put a password if you want)
;ssh-keygen -y -f <privatkeyfile>
:Print the public key for the private key in <privatkeyfile>
;openssl rsa -text -noout -in id_rsa
;openssl rsa -text -noout -in id_rsa
:Print out key attributes (o.a. key length)
:Print out key attributes (o.a. key length)
Line 7: Line 15:
:Convert to DER format.
:Convert to DER format.


;ssh-keygen -t ed25519
=ssh commands=
:Create a keypair (accept all defaults, put a password if you want)
;ssh-keygen -y -f <privatkeyfile>
:Print the public key for the private key in <privatkeyfile>
 
;ssh user@target
;ssh user@target
:SSH into target system as user, if user is omitted the current username is used
:SSH into target system as user, if user is omitted the current username is used
Line 62: Line 66:
}
}
</syntaxhighlight>
</syntaxhighlight>
=ssh-agent=
ssh-agent can store private keys in memory unencrypted. The private key passphrase only needs to be provided when the key is uploaded to the agent.
;eval "$(ssh-agent)"
:Start a ssh-agent by evaluating the commands generated by ssh-agent
:;ssh-agent
::Generate the commands to start a ssh-agent. Output looks like:
  SSH_AUTH_SOCK=/tmp/ssh-wBZcxqiOp6co/agent.61466; export SSH_AUTH_SOCK;
  SSH_AGENT_PID=61467; export SSH_AGENT_PID;
  echo Agent pid 61467;
:The agent is not stopped when the user logs out. The agent can be accessed again by setting the environment variables as above.
;killall ssh-agent
:Stop all ssh-agent processes you can (of not root this mean your own.
;ssh-add <private key file>
:Add the key to the agent, it asks for the passphrase if that is required
;ssh-add -l
:List the keys currently in the agent

Revision as of 13:38, 7 August 2025


key-handling

ssh-keygen -t ed25519
Create a keypair (accept all defaults, put a password if you want)
ssh-keygen -y -f <privatkeyfile>
Print the public key for the private key in <privatkeyfile>
openssl rsa -text -noout -in id_rsa
Print out key attributes (o.a. key length)
openssl s_client -showcerts -connect <system>:<port>
Get the certificates available and test connectivity
openssl x509 -outform der -in <pemfile> -out <filename>.der -text
Convert to DER format.

ssh commands

ssh user@target
SSH into target system as user, if user is omitted the current username is used
ssh -vvv user@target
Be very verbose with debug information
ssh -i keyfile user@target
Use the key in keyfile to connect


Certificates

For web-servers you can buy a certificate of get one for free from letsencrypt.

If for some reason you want to have a self signed certificate you can use this

openssl genrsa  -out server.key 4096
openssl req -new -key server.key -out server.csr
openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.pem

Now copy the server.pem and the server.key to the location for your web server.

I used this to make a default server that catches all traffic to port 80 and to 443 to redirect it. If you don't do this nginx will serve the first server it finds in it's configuration. The certificate has no content except for the CN being 'fake'

Default server configuration
#
server {
	listen 80 default_server;
	listen [::]:80 default_server;

	# SSL configuration
	#
	listen 443 ssl default_server;
	listen [::]:443 ssl default_server;
    ssl_certificate      /var/www/tmp/fake.pem;
    ssl_certificate_key  /var/www/tmp/fake.key;

    root /var/www/html;

	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html;

	server_name _;

	location / {
		try_files $uri $uri/ =404;
	}

}

ssh-agent

ssh-agent can store private keys in memory unencrypted. The private key passphrase only needs to be provided when the key is uploaded to the agent.

eval "$(ssh-agent)"
Start a ssh-agent by evaluating the commands generated by ssh-agent
ssh-agent
Generate the commands to start a ssh-agent. Output looks like:
 SSH_AUTH_SOCK=/tmp/ssh-wBZcxqiOp6co/agent.61466; export SSH_AUTH_SOCK;
 SSH_AGENT_PID=61467; export SSH_AGENT_PID;
 echo Agent pid 61467;
The agent is not stopped when the user logs out. The agent can be accessed again by setting the environment variables as above.
killall ssh-agent
Stop all ssh-agent processes you can (of not root this mean your own.
ssh-add <private key file>
Add the key to the agent, it asks for the passphrase if that is required
ssh-add -l
List the keys currently in the agent