I heard the Messenger of Allah ﷺ say, "If a plague breaks out in a land while you are not there, do not enter it, and if it breaks out in a land where you are, do not leave it."
حدثنا روح حدثنا محمد بن أبي حفصة حدثنا الزهري عن عبيد الله بن عبد الله عن ابن عباس قال سمعت
سمعت رسول الله ﷺ يقول إذا كان الوباء بأرض ولست بها فلا تدخلها وإذا كان بأرض وأنت بها فلا تخرج منها
Its isnad is sahih. Muhammad bin Abu Hafsa Basri is trustworthy, deemed reliable by Ibn Main, Abu Dawud, and others. Bukhari mentioned him in Tarikh al-Kabir 1/1/226 under the name Muhammad bin Maysara, which is Abu Hafsa's name, and the two Shaykhs (Bukhari and Muslim) both narrated from him. Ubaydullah bin Abdullah bin Utba bin Masud was a trustworthy successor, a jurist, and a poet, with abundant knowledge and many narrations.This hadith was narrated by Bukhari (10:15–16, 12:303), Muslim (1:188), and Abu Dawud (3:153–154) through the route of Zuhri, from Abdul-Hameed bin Abdul-Rahman bin Zayd bin Khattab, from Abdullah bin Abdullah bin Harith bin Nawfal, from Ibn Abbas, and it contains a narrative account in Bukhari and Muslim. It will also come again through this same route at 1679. What is meant here by "plague" (waba) is the plague (taun) specifically. See also 1678.