I see there are multiple solutions to fix this by modifying the config file.
But I don't understand how to modify the config file of the private EC2 machine without logging in as I am getting this error while logging into the linux machine.
I could never login to the private instance. I have added EC2 full access role to my public EC2 and keypairs for both the instances are same. I saw many videos that after all these setups we can directly ssh to private EC2 from public EC2. I could login to public ec2 using .pem stored in my local and after that I tried ssh privateIPofprivateEC2
and ended up with this error.