Qais bin Abi Hazim narrated that I heard Ibn Mas'ud (may Allah be pleased with him) say: "We used to fight in jihad with the Messenger of Allah (ﷺ) and we did not have our wives with us, so we intended to become castrated. But the Messenger of Allah (ﷺ) forbade us from that, then he permitted us to marry women for a specified period in exchange for something (a dowry)."
Hadith Referenceمسند الإمام الشافعي / كتاب النكاح / 1155
Hadith Takhrijاخرجه البخارى النكاح، باب ما يكره من التبتل والخصاء (5075) ، (4615) ومسلم النكاح ، باب نكاح المتعة وبيان انه ابيح ثم نسخ ...... الخ (1404)۔
It is narrated from Qais bin Abi Hazim: I heard Abdullah bin Mas'ud (may Allah be pleased with him) say: "We used to go for jihad with the Prophet (ﷺ) and our wives were not with us. We wanted to become eunuchs, but we were forbidden from doing so. Then we were granted permission to contract marriage with a woman for a specified period in exchange for something."
Hadith Referenceمسند الإمام الشافعي / كتاب النكاح / 1156