Does key authentication in SSH protect from MitM? How do cryptography in SSH(2) work (secure channel establishment, authentication)?

Does key authentication in SSH(2) protect from MitM by providing authentication and integrity and why?
How do cryptography in SSH(2) work?


This is a very broad question. If you're interested in the details, then read the relevant RFCs that describe the SSH protocol. In particular:


RFC 4253 - The Secure Shell (SSH) Transport Protocol
RFC 4252 - The Secure Shell (SSH) Authentication Protocol



  Does key authentication in SSH(2) protect from MitM by providing authentication and integrity and why?


Assuming you're referring to Public Key Authentication (where a client authenticates by proving they hold the private key of a pre-shared public key), then not directly/entirely. Public key authentication is a feature of the SSH Authentication Protocol, which runs on top of the SSH Transport Protocol, and generally assumes that the transport protocol provides confidentiality and integrity.

The Public Key Authentication does provide some protection against mitm attacks (by associating the proof of the private key with the SSH session ID), which will prevent the mitm passing on the private key proof as an authentication credential, but won't prevent an attacker using other credentials to access the target system.

The primary mechanism for man-in-the-middle prevention in the SSH Transport Protocol is the verification of pre-shared (or otherwise verifiable via some trusted channel) host keys.

RFC 4251 - The Secure Shell (SSH) Protocol Architecture discusses potential Man-in-the-middle attacks on the SSH protocol and the approaches taken to prevent them.