diff --git a/sysdeps/unix/sysv/linux/tst-futimens.c b/sysdeps/unix/sysv/linux/tst-futimens.c
index 8f48cfee63..739635f9f3 100644
--- a/sysdeps/unix/sysv/linux/tst-futimens.c
+++ b/sysdeps/unix/sysv/linux/tst-futimens.c
@@ -22,9 +22,6 @@
 #include <sys/stat.h>
 #include <support/check.h>
 #include <support/xunistd.h>
-#include <support/temp_file.h>
-
-static int temp_fd = -1;
 
 /* struct timespec array with Y2038 threshold minus 2 and 1 seconds.  */
 const struct timespec t1[2] = { { 0x7FFFFFFE, 0 },  { 0x7FFFFFFF, 0 } };
@@ -35,13 +32,9 @@ const struct timespec t2[2] = { { 0x80000001ULL, 0 },  { 0x80000002ULL, 0 } };
 /* struct timespec array around Y2038 threshold.  */
 const struct timespec t3[2] = { { 0x7FFFFFFE, 0 },  { 0x80000002ULL, 0 } };
 
-#define PREPARE do_prepare
-static void
-do_prepare (int argc, char *argv[])
-{
-  temp_fd = create_temp_file ("futimensat", NULL);
-  TEST_VERIFY_EXIT (temp_fd > 0);
-}
+/* The do_prepare function creates a temporary file to work with,
+   defining `int temp_fd' and `char *testfile'.  */
+#include "tst-utime-prep.c"
 
 static int
 test_futimens_helper (const struct timespec *ts)
diff --git a/sysdeps/unix/sysv/linux/tst-utime-prep.c b/sysdeps/unix/sysv/linux/tst-utime-prep.c
new file mode 100644
index 0000000000..3729d8d92e
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/tst-utime-prep.c
@@ -0,0 +1,44 @@
+/* do_prepare function for tests of the utime family of functions.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <sys/types.h>
+#include <support/check.h>
+#include <support/temp_file.h>
+#include <dirent.h>
+
+static int temp_fd = -1;
+char *testfile;
+
+#define PREPARE do_prepare
+static void
+do_prepare (int argc, char *argv[])
+{
+  /* Prefer using /dev/shm when it exists.  It uses tmpfs, which is
+     Y2038-safe.  /tmp could use, e.g. XFS, which presently has a Y2038 bug
+     triggering a test failure.  */
+  DIR *d = opendir ("/dev/shm");
+  if (d != NULL)
+    {
+      temp_fd = create_temp_file_in_dir ("utime", "/dev/shm", &testfile);
+      closedir (d);
+    }
+  else
+    temp_fd = create_temp_file ("utime", &testfile);
+
+  TEST_VERIFY_EXIT (temp_fd > 0);
+}
diff --git a/sysdeps/unix/sysv/linux/tst-utime.c b/sysdeps/unix/sysv/linux/tst-utime.c
index 6735421657..130a0c6a6e 100644
--- a/sysdeps/unix/sysv/linux/tst-utime.c
+++ b/sysdeps/unix/sysv/linux/tst-utime.c
@@ -24,10 +24,6 @@
 #include <utime.h>
 #include <support/check.h>
 #include <support/xunistd.h>
-#include <support/temp_file.h>
-
-static int temp_fd = -1;
-char *testfile;
 
 /* struct utimbuf with Y2038 threshold minus 2 and 1 seconds.  */
 const static struct utimbuf t1 = { 0x7FFFFFFE, 0x7FFFFFFF };
@@ -38,13 +34,9 @@ const static struct utimbuf t2 = { 0x80000001ULL, 0x80000002ULL };
 /* struct utimbuf around Y2038 threshold.  */
 const static struct utimbuf t3 = { 0x7FFFFFFE, 0x80000002ULL };
 
-#define PREPARE do_prepare
-static void
-do_prepare (int argc, char *argv[])
-{
-  temp_fd = create_temp_file ("utime", &testfile);
-  TEST_VERIFY_EXIT (temp_fd > 0);
-}
+/* The do_prepare function creates a temporary file to work with,
+   defining `int temp_fd' and `char *testfile'.  */
+#include "tst-utime-prep.c"
 
 static int
 test_utime_helper (const struct utimbuf *ut)
diff --git a/sysdeps/unix/sysv/linux/tst-utimes.c b/sysdeps/unix/sysv/linux/tst-utimes.c
index 8c7b006a22..fb59ef263e 100644
--- a/sysdeps/unix/sysv/linux/tst-utimes.c
+++ b/sysdeps/unix/sysv/linux/tst-utimes.c
@@ -23,10 +23,6 @@
 #include <sys/time.h>
 #include <support/check.h>
 #include <support/xunistd.h>
-#include <support/temp_file.h>
-
-static int temp_fd = -1;
-char *testfile;
 
 /* struct timeval array with Y2038 threshold minus 2 and 1 seconds.  */
 const static struct timeval t1[2] = { { 0x7FFFFFFE, 0 },  { 0x7FFFFFFF, 0 } };
@@ -39,13 +35,9 @@ struct timeval t2[2] = { { 0x80000001ULL, 0 },  { 0x80000002ULL, 0 } };
 const static
 struct timeval t3[2] = { { 0x7FFFFFFE, 0 },  { 0x80000002ULL, 0 } };
 
-#define PREPARE do_prepare
-static void
-do_prepare (int argc, char *argv[])
-{
-  temp_fd = create_temp_file ("utimes", &testfile);
-  TEST_VERIFY_EXIT (temp_fd > 0);
-}
+/* The do_prepare function creates a temporary file to work with,
+   defining `int temp_fd' and `char *testfile'.  */
+#include "tst-utime-prep.c"
 
 static int
 test_utime_helper (const struct timeval *tv)
