Why does the earth have a magnetic field?
[1] https://en.wikipedia.org/wiki/Earth%27s_magnetic_field [2] https://en.wikipedia.org/wiki/Dynamo_theory
The Earth rotates, liquid iron (inside Earth) flows => a current flows in the iron => there is a magnetic field.