ssh(3erl)
π¦ ssh(3erl) β Erlang Module Definition π¦
π NAME π
ssh β Main API of the ssh application
πΈ DESCRIPTION πΈ
Okay bestie, this is the interface module for the SSH application. The Secure Shell (SSH) Protocol is literally a protocol for secure remote login and other secure network services over an insecure network. See ssh(7) for details of supported RFCs, versions, algorithms and unicode handling. π
With the SSH application it is possible to start clients and to start daemons (servers).
Clients are started with connect/2, connect/3 or connect/4. They open an encrypted connection on top of TCP/IP. In that encrypted connection one or more channels could be opened with ssh_connection:session_channel/2,4.
Each channel is an isolated "pipe" between a client-side process and a server-side process. Those process pairs could handle for example file transfers (sftp) or remote command execution (shell, exec and/or cli). If a custom shell is implemented, the user of the client could execute the special commands remotely. Note that the user is not necessarily a human but probably a system interfacing the SSH app. π«Ά
A server-side subssystem (channel) server is requested by the client with ssh_connection:subsystem/4.
A server (daemon) is started with daemon/1, daemon/2 or daemon/3. Possible channel handlers (subsystems) are declared with the subsystem option when the daemon is started.
To just run a shell on a remote machine, there are functions that bundle the needed three steps into one: shell/1,2,3. Similarly, to just open an sftp (file transfer) connection to a remote machine, the simplest way is to use ssh_sftp:start_channel/1,2,3.
To write your own client channel handler, use the behaviour ssh_client_channel. For server channel handlers use ssh_server_channel behaviour (replaces ssh_daemon_channel). β¨
Both clients and daemons accept options that control the exact behaviour. Some options are common to both. The three sets are called Client Options, Daemon Options and Common Options.
The descriptions of the options uses the Erlang Type Language with explaining text.
Note: The User's Guide has examples and a Getting Started section. π
π KEYS AND FILES π
A number of objects must be present for the SSH application to work. Those objects are per default stored in files. The default names, paths and file formats are the same as for OpenSSH. Keys could be generated with the ssh-keygen program from OpenSSH. See the User's Guide. π
The paths could easily be changed by options: user_dir and system_dir.
A completely different storage could be interfaced by writing call-back modules using the behaviours ssh_client_key_api and/or ssh_server_key_api. A callback module is installed with the option key_cb to the client and/or the daemon.
π₯οΈ Daemons π₯οΈ
The keys are by default stored in files:
- Mandatory: one or more Host key(s), both private and public. Default is to store them in the directory
/etc/sshin the filesssh_host_dsa_keyandssh_host_dsa_key.pubssh_host_rsa_keyandssh_host_rsa_key.pubssh_host_ecdsa_keyandssh_host_ecdsa_key.pub
The host keys directory could be changed with the option system_dir.
- Optional: one or more User's public key in case of
publickeyauthorization. Default is to store them concatenated in the file.ssh/authorized_keysin the user's home directory.
The user keys directory could be changed with the option user_dir.
π» Clients π»
The keys and some other data are by default stored in files in the directory .ssh in the user's home directory.
The directory could be changed with the option user_dir.
- Optional: a list of Host public key(s) for previously connected hosts. This list is handled by the SSH application without any need of user assistance. The default is to store them in the file
known_hosts.
The host_accepting_client_options() are associated with this list of keys.
- Optional: one or more User's private key(s) in case of
publickeyauthorization. The default files areid_dsaandid_dsa.pubid_rsaandid_rsa.pubid_ecdsaandid_ecdsa.pub
𧬠DATA TYPES π§¬
β¨ Client Options β¨
client_options() = [client_option()]
client_option() =
ssh_file:pubkey_passphrase_client_options() |
host_accepting_client_options() |
authentication_client_options() |
diffie_hellman_group_exchange_client_option() |
connect_timeout_client_option() |
recv_ext_info_client_option() |
opaque_client_options() |
gen_tcp:connect_option() |
common_option()
Options for clients. The individual options are further explained below or by following the hyperlinks. π
Heads up: not everygen_tcp:connect_option()is accepted. Seeset_sock_opts/2for a list of prohibited options.
Also note that setting a gen_tcp:connect_option() could change the socket in a way that impacts the ssh client's behaviour negatively. You use it on your own risk. π
host_accepting_client_options() =
{silently_accept_hosts, accept_hosts()} |
{user_interaction, boolean()} |
{save_accepted_host, boolean()} |
{quiet_mode, boolean()}
accept_hosts() =
boolean() |
accept_callback() |
{HashAlgoSpec :: fp_digest_alg(), accept_callback()}
fp_digest_alg() = md5 | crypto:sha1() | crypto:sha2()
accept_callback() =
fun((PeerName :: string(), fingerprint()) -> boolean()) |
fun((PeerName :: string(),
Port :: inet:port_number(),
fingerprint()) ->
boolean())
fingerprint() = string() | [string()]
silently_accept_hostsβ this option guides theconnectfunction on how to act when the connected server presents a Host Key that the client has not seen before. The default is to ask the user with a question on stdio of whether to accept or reject the new Host Key. See the optionuser_dirfor specifying the path to the fileknown_hostswhere previously accepted Host Keys are recorded. See also the optionkey_cbfor the general way to handle keys. π
The option can be given in three different forms as seen above:
- The value is a
boolean(). The valuetruewill make the client accept any unknown Host Key without any user interaction. The valuefalsepreserves the default behaviour of asking the user on stdio. - An
accept_callback()will be called and the boolean return valuetruewill make the client accept the Host Key. A return value offalsewill make the client to reject the Host Key and as a result the connection will be closed. The arguments to the fun are:PeerNameβ a string with the name or address of the remote host.FingerPrintβ the fingerprint of the Host Key ashostkey_fingerprint/1calculates it.
- A tuple
{HashAlgoSpec, accept_callback}. TheHashAlgoSpecspecifies which hash algorithm shall be used to calculate the fingerprint used in the call of theaccept_callback(). TheHashALgoSpecis either an atom or a list of atoms as the first argument inhostkey_fingerprint/2. If it is a list of hash algorithm names, theFingerPrintargument in theaccept_callback()will be a list of fingerprints in the same order as the corresponding name in theHashAlgoSpeclist.
user_interactionβ iffalse, disables the client to connect to the server if any user interaction is needed, such as accepting the server to be added to theknown_hostsfile, or supplying a password.
Even if user interaction is allowed it can be suppressed by other options, such as silently_accept_hosts and password. However, those options are not always desirable to use from a security point of view. π¨
Defaults to true.
save_accepted_hostβ iftrue, the client saves an accepted host key to avoid the accept question the next time the same host is connected. If the optionkey_cbis not present, the key is saved in the file"known_hosts". See optionuser_dirfor the location of that file.
If false, the key is not saved and the key will still be unknown at the next access of the same host.
Defaults to true
quiet_modeβ iftrue, the client does not print anything on authorization.
Defaults to false
authentication_client_options() =
{user, string()} | {password, string()}
userβ provides the username. If this option is not given,sshreads from the environment (LOGNAMEorUSERon UNIX,USERNAMEon Windows). π»passwordβ provides a password for password authentication. If this option is not given, the user is asked for a password, if the password authentication method is attempted. π
diffie_hellman_group_exchange_client_option() =
{dh_gex_limits,
{Min :: integer() >= 1,
I :: integer() >= 1,
Max :: integer() >= 1}}
Sets the three diffie-hellman-group-exchange parameters that guides the connected server in choosing a group. See RFC 4419 for the details. The default value is {1024, 6144, 8192}. πΈ
connect_timeout_client_option() = {connect_timeout, timeout()}
Sets a timeout on the transport layer connect time. For gen_tcp the time is in milli-seconds and the default value is infinity. β±οΈ
See the parameter Timeout in connect/4 for a timeout of the negotiation phase.
recv_ext_info_client_option() = {recv_ext_info, boolean()}
Make the client tell the server that the client accepts extension negotiation, that is, include ext-info-c in the kexinit message sent. See RFC 8308 for details and ssh(7) for a list of currently implemented extensions. β¨
Default value is true which is compatible with other implementations not supporting ext-info.
π οΈ Daemon Options (Server Options) π οΈ
daemon_options() = [daemon_option()]
daemon_option() =
subsystem_daemon_option() |
shell_daemon_option() |
exec_daemon_option() |
ssh_cli_daemon_option() |
tcpip_tunnel_out_daemon_option() |
tcpip_tunnel_in_daemon_option() |
authentication_daemon_options() |
diffie_hellman_group_exchange_daemon_option() |
max_initial_idle_time_daemon_option() |
negotiation_timeout_daemon_option() |
hello_timeout_daemon_option() |
hardening_daemon_options() |
callbacks_daemon_options() |
send_ext_info_daemon_option() |
opaque_daemon_options() |
gen_tcp:listen_option() |
common_option()
Okay so these are the options for daemons π each individual one gets explained further down, or you can just click through the hyperlinks, iykyk.
Heads up bestie: not everygen_tcp:listen_option()is accepted β checkset_sock_opts/2for the full list of prohibited ones, no cap.
Also lowkey important: setting a gen_tcp:listen_option() can literally change the socket in ways that mess with the ssh daemon's behaviour in a bad way π you use it at your own risk, fr fr.
subsystem_daemon_option() = {subsystems, subsystem_specs()}
subsystem_specs() = [subsystem_spec()]
subsystem_spec() = {Name :: string(), mod_args()}
This is how you define a subsystem in the daemon, periodt β¨
subsystem_nameβ the name a client asks to start, like withssh_connection:subsystem/4.channel_callbackβ the module implementing thessh_server_channelbehaviour (it replacesssh_daemon_channel, bestie, keep up π ) in the daemon. Peep the Creating a Subsystem section in the User's Guide for the full rundown and an example.
Not feeling the subsystems option? Then the value of ssh_sftpd:subsystem_spec([]) is used β so sftp is enabled by default, cute. And yes, you can set this option to the empty list [] if you don't want the daemon running any subsystems at all. Your daemon, your rules π
shell_daemon_option() = {shell, shell_spec()}
shell_spec() = mod_fun_args() | shell_fun() | disabled
shell_fun() = 'shell_fun/1'() | 'shell_fun/2'()
'shell_fun/1'() = fun((User :: string()) -> pid())
'shell_fun/2'() =
fun((User :: string(), PeerAddr :: inet:ip_address()) -> pid())
This defines the read-eval-print loop a daemon uses when a client requests a shell πΈ The default is the Erlang shell: {shell, start, []}.
Wanna know how the daemon handles shell-requests vs exec-requests depending on the shell- and exec-options? Go read the exec-option description below, it's all laid out there π
exec_daemon_option() = {exec, exec_spec()}
exec_spec() =
{direct, exec_fun()} | disabled | deprecated_exec_opt()
exec_fun() = 'exec_fun/1'() | 'exec_fun/2'() | 'exec_fun/3'()
'exec_fun/1'() = fun((Cmd :: string()) -> exec_result())
'exec_fun/2'() =
fun((Cmd :: string(), User :: string()) -> exec_result())
'exec_fun/3'() =
fun((Cmd :: string(),
User :: string(),
ClientAddr :: ip_port()) ->
exec_result())
exec_result() =
{ok, Result :: term()} | {error, Reason :: term()}
This one changes how the daemon executes exec-requests coming from clients π« The term in the return value gets formatted to a string if it's a non-string type. And no trailing newline is added in the ok-case β noted π
Examples? The One-Time Execution section in the User's Guide has you covered.
Here's the vibe on where stuff goes:
- Error texts come back on channel-type
1, which on e.g Linux systems usually gets piped to stderr. - Texts from a successful execution come back on channel-type
0and get piped to stdout in a similar manner. - The exit-status code is
0for success and255for errors π₯ - The exact results the client sees depends on the client and the client's operating system β every client is a little different, iconic behaviour honestly.
For the {direct, exec_fun()} variant (or no exec-option at all), all reads from standard_input come from the received data-events of type 0 β those are sent by the client. Similarly, all writes to standard_output get sent as data-events to the client. An OS shell client like the command ssh will usually use stdin and stdout for the user interface π₯οΈ
So the tea on how this option cooperates with the daemon-option shell β:
- If neither the exec-option nor the shell-option is present: the default Erlang evaluator is used for both exec and shell requests. The result is returned to the client.
- If the
exec_spec's value isdisabled(the shell-option may or may not be present): no exec-requests are executed, but shell-requests are not affected β they follow theshell_spec's value. - If the exec-option is present and the
exec_specvalue=/= disabled(the shell-option may or may not be present): theexec_specfun() is called with the same number of parameters as the arity of the fun, and the result is returned to the client. Shell-requests are not affected β they follow theshell_spec's value. - If the exec-option is absent, and the shell-option is present with the default Erlang shell as the
shell_spec's value: the default Erlang evaluator is used for both exec and shell requests. The result is returned to the client. - If the exec-option is absent, and the shell-option is present with a value that is neither the default Erlang shell nor the value
disabled: the exec-request is not evaluated and an error message is returned to the client. Shell-requests are executed according to the value of theshell_spec. - If the exec-option is absent, and the
shell_spec's value isdisabled: exec requests are executed by the default shell, but shell-requests are not executed.
And if you've installed a custom CLI (see the option ssh_cli β¨) then the rules above get replaced by those implied by the custom CLI. Mix and match at your peril bestie.
Note: the exec-option has literally existed for ages but was never documented before π The old definition and behaviour are retained but obey rules 1-6 above if there's a conflict. The old and undocumented style should not be used in new programs β periodt, don't do it.
deprecated_exec_opt() = function() | mod_fun_args()
Old-style exec specification kept around for compatibility, but please don't use it in new programs π it's giving legacy.
ssh_cli_daemon_option() = {ssh_cli, mod_args() | no_cli}
Provides your own CLI implementation in a daemon π§
It's a channel callback module that implements a shell and command execution. The shell's read-eval-print loop can be customized using the option shell. That means less work than implementing a whole own CLI channel β we love efficiency π
If ssh_cli is set to no_cli, the CLI channels like shell and exec are disabled and only subsystem channels are allowed.
authentication_daemon_options()
authentication_daemon_options() =
ssh_file:system_dir_daemon_option() |
{auth_method_kb_interactive_data, prompt_texts()} |
{user_passwords, [{UserName :: string(), Pwd :: string()}]} |
{pk_check_user, boolean()} |
{password, string()} |
{pwdfun, pwdfun_2() | pwdfun_4()} |
{no_auth_needed, boolean()}
prompt_texts() =
kb_int_tuple() | kb_int_fun_3() | kb_int_fun_4()
kb_int_tuple() =
{Name :: string(),
Instruction :: string(),
Prompt :: string(),
Echo :: boolean()}
kb_int_fun_3() =
fun((Peer :: ip_port(), User :: string(), Service :: string()) ->
kb_int_tuple())
kb_int_fun_4() =
fun((Peer :: ip_port(),
User :: string(),
Service :: string(),
State :: any()) ->
kb_int_tuple())
pwdfun_2() =
fun((User :: string(), Password :: string() | pubkey) ->
boolean())
pwdfun_4() =
fun((User :: string(),
Password :: string() | pubkey,
PeerAddress :: ip_port(),
State :: any()) ->
boolean() |
disconnect |
{boolean(), NewState :: any()})
Let's break these down one by one π«Ά
auth_method_kb_interactive_data β sets the text strings the daemon sends to the client for showing to the user when using keyboard-interactive authentication π If the fun/3 or fun/4 is used, it gets called when the actual authentication happens, so it can return dynamic data like time, remote ip, etc. The parameter Echo guides the client about whether it needs to hide the password. The default value is:
{auth_method_kb_interactive_data, {"SSH server", "Enter password for \""++User++"\"", "password: ", false}>
user_passwords β provides passwords for password authentication. These passwords get used when someone tries to connect to the server and public key user-authentication fails. The option provides a list of valid usernames and the corresponding passwords.
Warning: this is very insecure due to the plain-text passwords π it's intended for test purposes. Use the pwdfun option to handle the password checking instead, bestie, seriously.
pk_check_user β enables checking of the client's user name in the server when doing public key authentication. It is disabled by default.
The term "user" is used differently in OpenSSH and SSH in Erlang/OTP β see more in the User's Guide π
If the option is enabled, and no pwdfun is present, the user name must be present in the user_passwords for the check to succeed, but the value of the password is not checked. In the case of a pwdfun checking the user, the atom pubkey is put in the password argument.
password β provides a global password that authenticates any user π€―
Warning: intended to facilitate testing. From a security perspective this option makes the server very vulnerable. Like, do not ship this, I'm begging you π
pwdfun with pwdfun_4() β provides a function for password validation. You could use this for calling an external system or for handling passwords stored as hash values π This fun can also be used to make delays in authentication tries, for example by calling timer:sleep/1. Iconic anti-brute-force queen behaviour honestly π
To facilitate, like, counting failed tries and stuff, the State variable is right there for you. This state is per connection only, okay? First time pwdfun gets called for a connection, State is literally the value undefined. π«Ά
The fun should return:
trueif the user and password is validfalseif the user or password is invaliddisconnectif aSSH_MSG_DISCONNECTmessage should be sent immediately β it will be followed by a close of the underlying tcp connection π{true, NewState:any()}if the user and password is valid{false, NewState:any()}if the user or password is invalid
A third usage is to block login attempts from a misbehaving peer β you can totally use the State described above for that, and the return value disconnect is genuinely the move here. β¨
In case of the pk_check_user is set, the atom pubkey is put in the password argument when validating a public key login. The pwdfun is then responsible to check that the user name is valid. No cap, that's on you.
pwdfun with pwdfun_2()
Provides a function for password validation. This function is called with user and password as strings, and returns:
trueif the user and password is validfalseif the user or password is invalid
In case of the pk_check_user is set, the atom pubkey is put in the password argument when validating a public key login. The pwdfun is then responsible to check that the user name is valid.
This variant is kept for compatibility. It's giving legacy but make it work. π
no_auth_needed
If true, a client is authenticated without any need of providing any password or key.
This option is only intended for very special applications due to the high risk of accepting any connecting client. π Like, please be careful bestie.
The default value is false.
π diffie_hellman_group_exchange_daemon_option() π
{dh_gex_groups,
[explicit_group()] |
explicit_group_file() |
ssh_moduli_file()} |
{dh_gex_limits, {Min :: integer() >= 1, Max :: integer() >= 1}}
explicit_group() =
{Size :: integer() >= 1,
G :: integer() >= 1,
P :: integer() >= 1}
explicit_group_file() = {file, string()}
ssh_moduli_file() = {ssh_moduli_file, string()}
dh_gex_groups
Defines the groups the server may choose among when diffie-hellman-group-exchange is negotiated. See RFC 4419 for details, iykyk. The three variants of this option are:
{Size=integer(),G=integer(),P=integer()}β The groups are given explicitly in this list. There may be several elements with the sameSize. In such a case, the server will choose one randomly in the negotiatedSize. Slay, random selection.{file,filename()}β The file must have one or more three-tuples{Size=integer(),G=integer(),P=integer()}terminated by a dot. The file is read when the daemon starts.{ssh_moduli_file,filename()}β The file must be inssh-keygenmoduli file format. The file is read when the daemon starts.
The default list is fetched from the public_key application. π
dh_gex_limits
Limits what a client can ask for in diffie-hellman-group-exchange. The limits will be {MaxUsed = min(MaxClient,Max), MinUsed = max(MinClient,Min)} where MaxClient and MinClient are the values proposed by a connecting client.
The default value is {0,infinity}.
If MaxUsed < MinUsed in a key exchange, it will fail with a disconnect. π No negotiation, just vibes off.
See RFC 4419 for the function of the Max and Min values.
β° hello_timeout_daemon_option() = {hello_timeout, timeout()} β°
Maximum time in milliseconds for the first part of the ssh session setup, the hello message exchange. Defaults to 30000 ms (30 seconds). If the client fails to send the first message within this time, the connection is closed. Bye bestie π
For more information about timeouts, see the Timeouts section in the User's Guide Hardening chapter.
β³ negotiation_timeout_daemon_option() = {negotiation_timeout, timeout()} β³
Maximum time in milliseconds for the authentication negotiation. Defaults to 120000 ms (2 minutes). If the client fails to log in within this time, the connection is closed.
For more information about timeouts, see the Timeouts section in the User's Guide Hardening chapter.
πΈ max_initial_idle_time_daemon_option() = {max_initial_idle_time, timeout()} πΈ
Maximum time in milliseconds for the first channel start after completion of the authentication negotiation. Defaults to infinity.
For more information about timeouts, see the Timeouts section in the User's Guide Hardening chapter.
π₯ hardening_daemon_options() π₯
{max_sessions, integer() >= 1 | infinity} |
{max_channels, integer() >= 1 | infinity} |
{parallel_login, boolean()} |
{minimal_remote_max_packet_size, integer() >= 1}
For more information about hardening, see the Hardening section in the User's Guide chapter.
max_sessions
The maximum number of simultaneous sessions that are accepted at any time for this daemon. This includes sessions that are being authorized. Thus, if set to N, and N clients have connected but not started the login process, connection attempt N+1 is aborted. If N connections are authenticated and still logged in, no more logins are accepted until one of the existing ones log out. It's giving exclusive guest list π
The counter is per listening port. Thus, if two daemons are started, one with {max_sessions,N} and the other with {max_sessions,M}, in total N+M connections are accepted for the whole ssh application.
Notice that if parallel_login is false, only one client at a time can be in the authentication phase.
By default, this option is not set. This means that the number is not limited.
max_channels
The maximum number of channels with active remote subsystem that are accepted for each connection to this daemon
By default, this option is not set. This means that the number is not limited.
parallel_login
If set to false (the default value), only one login is handled at a time. If set to true, an unlimited number of login attempts are allowed simultaneously.
If the max_sessions option is set to N and parallel_login is set to true, the maximum number of simultaneous login attempts at any time is limited to N-K, where K is the number of authenticated connections present at this daemon.
Warning: Do not enableparallel_loginswithout protecting the server by other means, for example, by themax_sessionsoption or a firewall configuration. If set totrue, there is no protection against DOS attacks. π Fr fr, do not do this unprotected.
minimal_remote_max_packet_size
The least maximum packet size that the daemon will accept in channel open requests from the client. The default value is 0.
π¦ callbacks_daemon_options() π¦
{failfun,
fun((User :: string(),
PeerAddress :: inet:ip_address(),
Reason :: term()) ->
term())} |
{connectfun,
fun((User :: string(),
PeerAddress :: inet:ip_address(),
Method :: string()) ->
term())}
connectfun
Provides a fun to implement your own logging when a user authenticates to the server. π
failfun
Provides a fun to implement your own logging when a user fails to authenticate.
β¨ send_ext_info_daemon_option() = {send_ext_info, boolean()} β¨
Make the server (daemon) tell the client that the server accepts extension negotiation, that is, include ext-info-s in the kexinit message sent. See RFC 8308 for details and ssh(7) for a list of currently implemented extensions.
Default value is true which is compatible with other implementations not supporting ext-info. Iconic behaviour honestly.
π§ tcpip_tunnel_in_daemon_option() = {tcpip_tunnel_in, boolean()} π§
Enables (true) or disables (false) the possibility to tunnel a TCP/IP connection in to a server. Disabled per default.
π§ tcpip_tunnel_out_daemon_option() = {tcpip_tunnel_out, boolean()} π§
Enables (true) or disables (false) the possibility to tunnel a TCP/IP connection out of a server. Disabled per default.
π Options common to clients and daemons π
common_options() = [common_option()]
common_option() =
ssh_file:user_dir_common_option() |
profile_common_option() |
max_idle_time_common_option() |
max_log_item_len_common_option() |
key_cb_common_option() |
disconnectfun_common_option() |
unexpectedfun_common_option() |
ssh_msg_debug_fun_common_option() |
rekey_limit_common_option() |
id_string_common_option() |
pref_public_key_algs_common_option() |
preferred_algorithms_common_option() |
modify_algorithms_common_option() |
auth_methods_common_option() |
inet_common_option() |
fd_common_option()
The options above can be used both in clients and in daemons (servers). They are further explained below. Sharing is caring π«Ά
profile_common_option() = {profile, atom()}
Used together with ip-address and port to uniquely identify a ssh daemon. This can be useful in a virtualized environment, where there can be more that one server that has the same ip-address and port. If this property is not explicitly set, it is assumed that the the ip-address and port uniquely identifies the SSH daemon.
max_idle_time_common_option() = {idle_time, timeout()}
Sets a time-out on a connection when no channels are open. Defaults to infinity. The unit is milliseconds.
The timeout is not active until channels are started, so it does not limit the time from the connection creation to the first channel opening. Lowkey a chill timeout. β
For more details on timeouts, check out the Timeouts section in the User's Guide Hardening chapter β¨
πΈ max_log_item_len_common_option()
max_log_item_len_common_option() =
{max_log_item_len, limit_bytes()}
Caps how big a logged item can get, not counting the header π. Unit is bytes, and it defaults to 500.
π¦ rekey_limit_common_option()
rekey_limit_common_option() =
{rekey_limit,
Bytes ::
limit_bytes() |
{Minutes :: limit_time(), Bytes :: limit_bytes()}}
limit_bytes() = integer() >= 0 | infinity
limit_time() = integer() >= 1 | infinity
Sets the limit for when rekeying kicks off π. You can configure both the max time and the max amount of data:
{Minutes, Bytes}β rekeying starts when either of the limits is hit.Bytesβ rekeying starts onceBytesnumber of bytes have been transferred, or at the latest after one hour.
Whenever a rekeying goes through, both the timer and the byte counter get restarted π. Defaults to one hour and one GByte.
If Minutes is set to infinity, no rekeying will ever happen just because that max time has passed. Setting Bytes to infinity blocks rekeying after a certain amount of data has been transferred. And if the option value is {infinity, infinity}, no rekeying will be initiated at all. Quick heads-up though: rekeying initiated by the peer will still be performed, so don't think you're fully off the hook π.
π key_cb_common_option()
key_cb_common_option() =
{key_cb,
Module :: atom() | {Module :: atom(), Opts :: [term()]}}
A module that implements the ssh_client_key_api and/or ssh_server_key_api behaviour π«Ά. Use it to customize how public keys get handled. If you pass callback options alongside the module name, they're made available to the callback module through the options handed to it under the key 'key_cb_private'.
Opts defaults to [] when only the Module is specified.
The default value of this option is {ssh_file, []}. Also peep the manpage of ssh_file π.
A call to the call-back function F will look like this:
Module:F(..., [{key_cb_private,Opts}|UserOptions])
where ... are the arguments to F as in ssh_client_key_api and/or ssh_server_key_api. UserOptions are the options you gave to ssh:connect, ssh:shell or ssh:daemon.
π pref_public_key_algs_common_option()
pref_public_key_algs_common_option() =
{pref_public_key_algs, [pubkey_alg()]}
A list of user (client) public key algorithms to try to use π₯.
The default value is the public_key entry in the list returned by ssh:default_algorithms/0.
If there's no public key of a specified type available, that entry just gets ignored, no drama. Just know that the available set depends on the underlying cryptolib and the current user's public keys π§.
Also see the option user_dir for specifying the path to the user's keys.
π disconnectfun_common_option()
disconnectfun_common_option() =
{disconnectfun, fun((Reason :: term()) -> void | any())}
Gives you a fun to implement your own logging or other handling at disconnects π .
β unexpectedfun_common_option()
unexpectedfun_common_option() =
{unexpectedfun,
fun((Message :: term(), {Host :: term(), Port :: term()}) ->
report | skip)}
Gives you a fun to implement your own logging or other action when an unexpected message shows up π. If the fun returns report, the usual info report gets issued β but if it returns skip, no report is generated.
π¦ ssh_msg_debug_fun_common_option()
ssh_msg_debug_fun_common_option() =
{ssh_msg_debug_fun,
fun((ssh:connection_ref(),
AlwaysDisplay :: boolean(),
Msg :: binary(),
LanguageTag :: binary()) ->
any())}
Provide a fun to implement your own logging of the SSH message SSH_MSG_DEBUG π. The last three parameters come from the message, see RFC 4253, section 11.3. The connection_ref() is the reference to the connection the message arrived on. The return value from the fun isn't checked β do whatever, bestie.
The default behaviour is to ignore the message. If you want a printout for each message with AlwaysDisplay = true, use something like:
{ssh_msg_debug_fun, fun(_,true,M,_)-> io:format("DEBUG: ~p~n", [M]) end}
π id_string_common_option()
id_string_common_option() =
{id_string,
string() |
random |
{random, Nmin :: integer() >= 1, Nmax :: integer() >= 1}}
The string the daemon presents to a connecting peer initially π. The default value is "Erlang/VSN" where VSN is the ssh application version number.
The value random makes a random string get created at each connection attempt β this makes it a bit harder for a malicious peer to sniff out the ssh software brand and version π΅οΈ.
The value {random, Nmin, Nmax} gives you a random string with at least Nmin characters and at most Nmax characters.
πΈ preferred_algorithms_common_option()
preferred_algorithms_common_option() =
{preferred_algorithms, algs_list()}
algs_list() = [alg_entry()]
alg_entry() =
{kex, [kex_alg()]} |
{public_key, [pubkey_alg()]} |
{cipher, double_algs(cipher_alg())} |
{mac, double_algs(mac_alg())} |
{compression, double_algs(compression_alg())}
kex_alg() =
'diffie-hellman-group-exchange-sha1' |
'diffie-hellman-group-exchange-sha256' |
'diffie-hellman-group1-sha1' | 'diffie-hellman-group14-sha1' |
'diffie-hellman-group14-sha256' |
'diffie-hellman-group16-sha512' |
'diffie-hellman-group18-sha512' | 'curve25519-sha256' |
'curve25519-sha256@libssh.org' | 'curve448-sha512' |
'ecdh-sha2-nistp256' | 'ecdh-sha2-nistp384' |
'ecdh-sha2-nistp521'
pubkey_alg() =
'ecdsa-sha2-nistp256' | 'ecdsa-sha2-nistp384' |
'ecdsa-sha2-nistp521' | 'ssh-ed25519' | 'ssh-ed448' |
'rsa-sha2-256' | 'rsa-sha2-512' | 'ssh-dss' | 'ssh-rsa'
cipher_alg() =
'3des-cbc' | 'AEAD_AES_128_GCM' | 'AEAD_AES_256_GCM' |
'aes128-cbc' | 'aes128-ctr' | 'aes128-gcm@openssh.com' |
'aes192-ctr' | 'aes192-cbc' | 'aes256-cbc' | 'aes256-ctr' |
'aes256-gcm@openssh.com' | 'chacha20-poly1305@openssh.com'
mac_alg() =
'AEAD_AES_128_GCM' | 'AEAD_AES_256_GCM' | 'hmac-sha1' |
'hmac-sha1-etm@openssh.com' | 'hmac-sha1-96' |
'hmac-sha2-256' | 'hmac-sha2-512' |
'hmac-sha2-256-etm@openssh.com' |
'hmac-sha2-512-etm@openssh.com'
compression_alg() = none | zlib | 'zlib@openssh.com'
double_algs(AlgType) =
[{client2server, [AlgType]} | {server2client, [AlgType]}] |
[AlgType]
The list of algorithms to use in the algorithm negotiation π€. The default algs_list() can be grabbed from default_algorithms/0.
If an alg_entry() is missing from the algs_list(), the default value gets used for that entry.
Here's an example of this option, slay:
{preferred_algorithms,
[{public_key,['ssh-rsa','ssh-dss']},
{cipher,[{client2server,['aes128-ctr']},
{server2client,['aes128-cbc','3des-cbc']}]},
{mac,['hmac-sha2-256','hmac-sha1']},
{compression,[none,zlib]}
]
}
The example specifies different algorithms in the two directions (client2server and server2client) for cipher, but the same algorithms for mac and compression in both directions. The kex (key exchange) is implicit, but public_key is set explicitly π
.
For background and more examples, see the User's Guide.
If an algorithm name shows up more than once in a list, the behaviour is undefined π. The tags in the property lists are also assumed to occur at most one time.
Warning: Changing the values can make a connection less secure. Do not change unless you know exactly what you are doing. If you do not understand the values, then you are not supposed to change them. Periodt. π¨
π₯ modify_algorithms_common_option()
modify_algorithms_common_option() =
{modify_algorithms, modify_algs_list()}
modify_algs_list() =
[{append, algs_list()} |
{prepend, algs_list()} |
{rm, algs_list()}]
Modifies the list of algorithms used in the algorithm negotiation β¨. The modifications are applied after the option preferred_algorithms (if it exists) is applied.
The algorithm for modifications works like this:
- Input is the
modify_algs_list()and a set of algorithmsAobtained from thepreferred_algorithmsoption if it exists, or else fromssh:default_algorithms/0. - The head of the
modify_algs_list()modifiesA, giving the resultA'.
The possible modifications are:
- Append or prepend supported but not enabled algorithm(s) to the list of algorithms. If the wanted algorithms are already in
A, they will first be removed and then appended or prepended, - Remove (
rm) one or more algorithms fromA.
- Repeat the modification step with the tail of
modify_algs_list()and the resultingA'.
If an unsupported algorithm is in the modify_algs_list(), it will be silently ignored π€«.
If there's more than one modify_algorithms option, the result is undefined π.
Here's an example of this option:
{modify_algorithms,
[{prepend, [{kex, ['diffie-hellman-group1-sha1']}],
{rm, [{compression, [none]}]}
]
}
The example specifies that:
- the old key exchange algorithm
'diffie-hellman-group1-sha1'should be the main alternative. It'll be the main alternative since it's prepended to the list π - The compression algorithm
none(= no compression) is removed, so compression is enforced ποΈ
For background and more examples, see the User's Guide.
π inet_common_option()
inet_common_option() = {inet, inet | inet6}
Which IP version to use when the host address is specified as any π.
π auth_methods_common_option()
auth_methods_common_option() = {auth_methods, string()}
A comma-separated string that decides which authentication methods the client supports and in what order they get tried π. Defaults to "publickey,keyboard-interactive,password".
Note that the client is free to use any order and to exclude methods β it's giving main character energy, honestly π .
π fd_common_option()
fd_common_option() = {fd, gen_tcp:socket()}
Lets an existing file-descriptor be used (passed on to the transport protocol) π.
π¦ Other data types
host() = string() | inet:ip_address() | loopback
Ok so the type definitions keep rolling, and every single one stays exactly as written:
ip_port() = {inet:ip_address(), inet:port_number()}
mod_args() = {Module :: atom(), Args :: list()}
mod_fun_args() =
{Module :: atom(), Function :: atom(), Args :: list()}
open_socket() is literally just a gen_tcp:socket() β and the tea is: that socket is supposed to be the result of a gen_tcp:connect or a gen_tcp:accept. It must be in passive mode, like opened with the option {active,false}. No active-mode chaos here, bestie. π«Ά
open_socket() = gen_tcp:socket()
daemon_ref() is an opaque data type representing a daemon. You get one back from the functions daemon/1,2,3.
daemon_ref()
connection_ref() is an opaque data type representing a connection between a client and a server (daemon). Returned by the functions connect/2,3,4 and ssh_sftp:start_channel/2,3.
connection_ref()
channel_id() is an opaque data type representing a channel inside a connection. Returned by the functions ssh_connection:session_channel/2,4.
channel_id()
connection_info_tuple() =
{client_version, version()} |
{server_version, version()} |
{user, string()} |
{peer, {inet:hostname(), ip_port()}} |
{sockname, ip_port()} |
{options, client_options()} |
{algorithms, conn_info_algs()} |
{channels, conn_info_channels()}
version() = {protocol_version(), software_version()}
protocol_version() =
{Major :: integer() >= 1, Minor :: integer() >= 0}
software_version() = string()
conn_info_algs() =
[{kex, kex_alg()} |
{hkey, pubkey_alg()} |
{encrypt, cipher_alg()} |
{decrypt, cipher_alg()} |
{send_mac, mac_alg()} |
{recv_mac, mac_alg()} |
{compress, compression_alg()} |
{decompress, compression_alg()} |
{send_ext_info, boolean()} |
{recv_ext_info, boolean()}]
conn_info_channels() = [proplists:proplist()]
These are return values from the connection_info/1 and connection_info/2 functions. In the option info tuple, only the options that differ from the default values are included.
daemon_info_tuple() =
{port, inet:port_number()} |
{ip, inet:ip_address()} |
{profile, atom()} |
{options, daemon_options()}
These are return values from the daemon_info/1 and daemon_info/2 functions. In the option info tuple, only the options that differ from the default values are included.
opaque_client_options()
opaque_daemon_options()
opaque_common_options()
These are opaque types that define experimental options that are not to be used in products. Periodt. π«
π₯ EXPORTS π₯
π close/1 β say bye to the connection π
close(ConnectionRef) -> ok | {error, term()}
Types:
ConnectionRef = connection_ref()
Closes an SSH connection. That's it. Bye π
π«Ά connect/2,3,4 β slide into an SSH server π«Ά
connect(Host, Port, Options) -> Result
connect(Host, Port, Options, NegotiationTimeout) -> Result
connect(TcpSocket, Options) -> Result
connect(TcpSocket, Options, NegotiationTimeout) -> Result
Types:
Host = host()
Port = inet:port_number()
Options = client_options()
TcpSocket = open_socket()
NegotiationTimeout = timeout()
Result = {ok, connection_ref()} | {error, term()}
Connects to an SSH server at the Host on Port.
As an alternative, an already open TCP socket could be passed to the function in TcpSocket. The SSH initiation and negotiation will be initiated on that one with the SSH that should be at the other end.
No channel is started. This is done by calling ssh_connection:session_channel/[2, 4].
The NegotiationTimeout is in milli-seconds. The default value is infinity or the value of the connect_timeout option, if present. For connection timeout, use the option connect_timeout.
π connection_info/1,2 β spill the connection tea π
connection_info(ConnectionRef) -> InfoTupleList
connection_info(ConnectionRef, Key :: ItemList | Item) ->
InfoTupleList | InfoTuple
Types:
ConnectionRef = connection_ref()
ItemList = [Item]
Item =
client_version | server_version | user | peer | sockname |
options | algorithms | sockname
InfoTupleList = [InfoTuple]
InfoTuple = connection_info_tuple()
Returns information about a connection intended for e.g debugging or logging.
When the Key is a single Item, the result is a single InfoTuple.
β οΈ set_sock_opts/2 β touch the socket under the SSH, carefully β οΈ
set_sock_opts(ConnectionRef, SocketOptions) ->
ok | {error, inet:posix()}
Types:
ConnectionRef = connection_ref()
SocketOptions = [gen_tcp:option()]
Sets tcp socket options on the tcp-socket below an ssh connection.
This function calls the inet:setopts/2, read that documentation and for gen_tcp:option().
All gen_tcp socket options except
activedelivermodeandpacket
are allowed. The excluded options are reserved by the SSH application.
Warning: This is an extremely dangerous function. You use it on your own risk. Some options are OS and OS version dependent. Do not use it unless you know what effect your option values will have on a TCP stream. Some values may destroy the functionality of the SSH protocol. π
π get_sock_opts/2 β peek at the socket options π
get_sock_opts(ConnectionRef, SocketGetOptions) ->
ok | {error, inet:posix()}
Types:
ConnectionRef = connection_ref()
SocketGetOptions = [gen_tcp:option_name()]
Get tcp socket option values of the tcp-socket below an ssh connection.
This function calls the inet:getopts/2, read that documentation.
π daemon/1,2,3 β start the SSH server era π
daemon(Port | TcpSocket) -> Result
daemon(Port | TcpSocket, Options) -> Result
daemon(HostAddress, Port, Options) -> Result
Types:
Port = integer()
TcpSocket = open_socket()
Options = daemon_options()
HostAddress = host() | any
Result = {ok, daemon_ref()} | {error, atom()}
Starts a server listening for SSH connections on the given port. If the Port is 0, a random free port is selected. See daemon_info/1 about how to find the selected port number.
As an alternative, an already open TCP socket could be passed to the function in TcpSocket. The SSH initiation and negotiation will be initiated on that one when an SSH starts at the other end of the TCP socket.
For a description of the options, see Daemon Options.
Please note that by historical reasons both the HostAddress argument and the gen_tcp connect_option() {ip,Address} set the listening address. This is a source of possible inconsistent settings.
The rules for handling the two address passing options are:
- if
HostAddressis an IP-address, that IP-address is the listening address. An'ip'-option will be discarded if present. - if
HostAddressis the atomloopback, the listening address is loopback and a loopback address will be chosen by the underlying layers. An'ip'-option will be discarded if present. - if
HostAddressis the atomanyand no'ip'-option is present, the listening address is any and the socket will listen to all addresses - if
HostAddressisanyand an'ip'-option is present, the listening address is set to the value of the'ip'-option
π daemon_replace_options/2 β glow-up the daemon options π
daemon_replace_options(DaemonRef, NewUserOptions) ->
{ok, daemon_ref()} | {error, term()}
Types:
DaemonRef = daemon_ref()
NewUserOptions = daemon_options()
Replaces the options in a running daemon with the options in NewUserOptions. Only connections established after this call are affected, already established connections are not.
Note: In the final phase of this function, the listening process is restarted. Therefore a connection attempt to the daemon in this final phase could fail. The handling of Erlang configurations is described in the User's Guide; see chapters Configuration in SSH and Configuring algorithms in SSH.
π daemon_info/1,2 β daemon lore drop π
daemon_info(DaemonRef) ->
{ok, InfoTupleList} | {error, bad_daemon_ref}
daemon_info(DaemonRef, Key :: ItemList | Item) ->
InfoTupleList | InfoTuple | {error, bad_daemon_ref}
Types:
DaemonRef = daemon_ref()
ItemList = [Item]
Item = ip | port | profile | options
InfoTupleList = [InfoTuple]
InfoTuple = daemon_info_tuple()
Returns information about a daemon intended for e.g debugging or logging.
When the Key is a single Item, the result is a single InfoTuple.
Note that daemon_info/1 and daemon_info/2 return different types due to compatibility reasons.
π§ default_algorithms/0 β the algorithm menu π§
default_algorithms() -> algs_list()
Returns a key-value list, where the keys are the different types of algorithms and the values are the algorithms themselves.
See the User's Guide for an example.
π shell/1,2,3 β remote shell, but make it interactive π
shell(Host | TcpSocket) -> Result
shell(Host | TcpSocket, Options) -> Result
shell(Host, Port, Options) -> Result
Types:
Host = host()
TcpSocket = open_socket()
Port = inet:port_number()
Options = client_options()
Result = ok | {error, Reason::term()}
Connects to an SSH server at Host and Port (defaults to 22) and starts an interactive shell on that remote host.
As an alternative, an already open TCP socket could be passed to the function in TcpSocket. The SSH initiation and negotiation will be initiated on that one and finally a shell will be started on the host at the other end of the TCP socket.
For a description of the options, see Client Options.
The function waits for user input, and does not return until the remote shell is ended (that is, exit from the shell).
β¨ start/0,1 β boot the SSH app stack β¨
start() -> ok | {error, term()}
start(Type) -> ok | {error, term()}
Types:
Type = permanent | transient | temporary
Utility function that starts the applications crypto, public_key, and ssh. Default type is temporary. For more information, see the application(3erl) manual page in Kernel.
π stop/0 β shut it down π
stop() -> ok | {error, term()}
Stops the ssh application. For more information, see the application(3erl) manual page in Kernel.
π stop_daemon/1 β daemon, you may exit π
stop_daemon(DaemonRef :: daemon_ref()) -> ok
π
stop_daemon/2 and stop_daemon/3
stop_daemon(Address :: inet:ip_address(),
Port :: inet:port_number()) ->
ok
stop_daemon(Address :: any | inet:ip_address(),
Port :: inet:port_number(),
Profile :: atom()) ->
ok
This one is the full send π: it stops the listener and all connections started by the listener. Like, everyone out of the pool, no cap.
π stop_listener/1, stop_listener/2, and stop_listener/3
stop_listener(SysSup :: daemon_ref()) -> ok
stop_listener(Address :: inet:ip_address(),
Port :: inet:port_number()) ->
ok
stop_listener(Address :: any | inet:ip_address(),
Port :: inet:port_number(),
Profile :: term()) ->
ok
This is the gentler vibe: it stops the listener, but leaves existing connections started by the listener operational. So no new besties get in, but the ones already in the group chat stay. π«Ά
π§ tcpip_tunnel_from_server/5 and tcpip_tunnel_from_server/6
tcpip_tunnel_from_server(ConnectionRef, ListenHost, ListenPort,
ConnectToHost, ConnectToPort) ->
{ok, TrueListenPort} | {error, term()}
tcpip_tunnel_from_server(ConnectionRef, ListenHost, ListenPort,
ConnectToHost, ConnectToPort, Timeout) ->
{ok, TrueListenPort} | {error, term()}
Types:
ConnectionRef = connection_ref()ListenHost = host()ListenPort = inet:port_number()ConnectToHost = host()ConnectToPort = inet:port_number()Timeout = timeout()TrueListenPort = inet:port_number()
Okay so this one asks the remote server of ConnectionRef to listen to ListenHost:ListenPort. When someone connects that address, the connection is forwarded in an encrypted channel from the server to the client. The client β that is, at the node that calls this function β then connects to ConnectToHost:ConnectToPort. π
The returned TrueListenPort is the port that is listened to. It is the same as ListenPort, except when ListenPort = 0. In that case a free port is selected by the underlying OS. β¨
β οΈ Note that in case of an Erlang/OTP SSH server (daemon) as peer, that server must have been started with the option tcpip_tunnel_out to allow the connection.
π¦ tcpip_tunnel_to_server/5 and tcpip_tunnel_to_server/6
tcpip_tunnel_to_server(ConnectionRef, ListenHost, ListenPort,
ConnectToHost, ConnectToPort) ->
{ok, TrueListenPort} | {error, term()}
tcpip_tunnel_to_server(ConnectionRef, ListenHost, ListenPort,
ConnectToHost, ConnectToPort, Timeout) ->
{ok, TrueListenPort} | {error, term()}
Types:
ConnectionRef = connection_ref()ListenHost = host()ListenPort = inet:port_number()ConnectToHost = host()ConnectToPort = inet:port_number()Timeout = timeout()TrueListenPort = inet:port_number()
This one tells the local client to listen to ListenHost:ListenPort. When someone connects to that address, the connection is forwarded in an encrypted channel to the peer server of ConnectionRef. That server then connects to ConnectToHost:ConnectToPort. Itβs giving tunnel main character energy π§
The returned TrueListenPort is the port that is listened to. It is the same as ListenPort, except when ListenPort = 0. In that case a free port is selected by the underlying OS. β
β οΈ Note that in case of an Erlang/OTP SSH server (daemon) as peer, that server must have been started with the option tcpip_tunnel_in to allow the connection.
π hostkey_fingerprint/1, hostkey_fingerprint/2, and hostkey_fingerprint/2 with a list
hostkey_fingerprint(HostKey) -> string()
hostkey_fingerprint(DigestType, HostKey) -> string()
hostkey_fingerprint([DigestType], HostKey) -> [string()]
Types:
HostKey = public_key:public_key()DigestType = public_key:digest_type()
This calculates a ssh fingerprint from a public host key as openssh does. π Iconic behavior.
The algorithm in hostkey_fingerprint/1 is md5 to be compatible with older ssh-keygen commands. The string from the second variant is prepended by the algorithm name in uppercase as in newer ssh-keygen commands. π
Examples:
2> ssh:hostkey_fingerprint(Key).
"f5:64:a6:c1:5a:cb:9f:0a:10:46:a2:5c:3e:2f:57:84"
3> ssh:hostkey_fingerprint(md5,Key).
"MD5:f5:64:a6:c1:5a:cb:9f:0a:10:46:a2:5c:3e:2f:57:84"
4> ssh:hostkey_fingerprint(sha,Key).
"SHA1:bSLY/C4QXLDL/Iwmhyg0PGW9UbY"
5> ssh:hostkey_fingerprint(sha256,Key).
"SHA256:aZGXhabfbf4oxglxltItWeHU7ub3Dc31NcNw2cMJePQ"
6> ssh:hostkey_fingerprint([sha,sha256],Key).
["SHA1:bSLY/C4QXLDL/Iwmhyg0PGW9UbY",
"SHA256:aZGXhabfbf4oxglxltItWeHU7ub3Dc31NcNw2cMJePQ"]
Thatβs the whole page, bestie! π Footer shoutout goes to Ericsson AB, version ssh 4.15.3.1, and the page tag ssh(3erl). Slay. β¨π
π¦ where this page comes from
- ubuntu noble: erlang-manpages 1:25.3.2.8+dfsg-1ubuntu4.8 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms