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