The Sun and sun-like stars commonly host the multi-million-Kelvin coronae and the 10,000-Kelvin chromospheres. These extremely hot gases generate X-ray and Extreme Ultraviolet emissions that may impact the erosion and chemistry of (exo)planetary atmospheres, influencing the climate and conditions of habitability. However, the mechanism of coronal and chromospheric heating is still poorly understood. While the magnetic field most probably plays a key role in driving and transporting energy from the stellar surface upwards, it is not clear if the atmospheric heating mechanisms of the Sun and active sun-like stars can be described in a unified manner. To this end, we report on a systematic survey of the responses of solar and stellar atmospheres to surface magnetic flux over a wide range of temperatures. By analyzing 10 years of multi-wavelength synoptic observations of the Sun, we reveal that the irradiance and magnetic flux show power-law relations with an exponent decreasing from above-to sub-unity as the temperature decreases from the corona to the chromosphere. Moreover, this trend indicating the efficiency of atmospheric heating can be extended to sun-like stars. We also discover that the power-law exponent has a solar cycle dependence, where it becomes smallest at activity maximum, probably due to the saturation of atmospheric heating. Our study provides observational evidence that the mechanism of atmospheric heating is universal among the Sun and sun-like stars, regardless of age or activity. Keywords: G dwarf stars (556) -Solar analogs (1941) -Stellar coronae (305) -Stellar chromospheres (230) -Stellar magnetic fields (1610) -Solar coronal heating (1989) -Solar chromospheric heating (1987) -Solar magnetic fields (1503)