DEV Community

Janak Shrestha
Janak Shrestha

Posted on

Managing Temporary User Accounts: Setting Expiration Dates and Copying Files with Preserved Ownership

As part of the temporary resource allocation, Kirsty has been appointed to the Nautilus project as a backup developer. To facilitate this, a temporary user account is required for Kirsty. It is advisable to create a user account with a specified expiration date to ensure restricted server access beyond the designated period.

A user profile under the name kirsty has already been established on App Server 2 within the Stratos Datacenter. Adjust the account's expiration date to 2027-01-28. Additionally, locate all files (excluding directories) owned by this user within the /home/usersdata directory and copy them to the /ecommerce directory while maintaining their original ownership.


Solution

Step 1: Connect to App Server 2 (stapp02)

ssh steve@stapp02
# Password: Am3ric@
Enter fullscreen mode Exit fullscreen mode

Step 2: Switch to root

sudo su -
# Password: Am3ric@
Enter fullscreen mode Exit fullscreen mode

Step 3: Set expiration date for kirsty

# Set account expiry to 2027-01-28
chage -E 2027-01-28 kirsty

# Verify expiry
chage -l kirsty
Enter fullscreen mode Exit fullscreen mode

Step 4: Create destination directory

mkdir -p /ecommerce
Enter fullscreen mode Exit fullscreen mode

Step 5: Find and copy files owned by kirsty

# Method 1: Using find with cpio (preserves ownership)
find /home/usersdata -type f -user kirsty -print0 | cpio -pdmv0 /ecommerce

# Method 2: Using find with tar
find /home/usersdata -type f -user kirsty -print0 | tar -cf - --null -T - | tar -xf - -C /ecommerce --preserve-permissions

# Method 3: Using find with cp -a
find /home/usersdata -type f -user kirsty -exec cp -a --parents {} /ecommerce \;
Enter fullscreen mode Exit fullscreen mode

Step 6: Verify the copy

# Check number of files copied
find /ecommerce/home/usersdata -type f -user kirsty 2>/dev/null | wc -l

# Verify ownership preserved
find /ecommerce -type f -user kirsty -ls 2>/dev/null | head -10

# Check directory structure
ls -la /ecommerce/home/usersdata/ 2>/dev/null
Enter fullscreen mode Exit fullscreen mode

One-Line Commands

From jump host:

echo 'Am3ric@' | ssh steve@stapp02 "sudo -S bash -c 'chage -E 2027-01-28 kirsty && mkdir -p /ecommerce && find /home/usersdata -type f -user kirsty -print0 | cpio -pdmv0 /ecommerce && echo \"Files copied:\" && find /ecommerce -type f -user kirsty 2>/dev/null | wc -l'"
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Interactive Commands

# Connect and become root
ssh steve@stapp02
sudo su -

# 1. Check current expiry for kirsty
echo "=== Current expiry ==="
chage -l kirsty | grep "Account expires"

# 2. Set expiry to 2027-01-28
echo "=== Setting expiry to 2027-01-28 ==="
chage -E 2027-01-28 kirsty

# 3. Verify new expiry
echo "=== New expiry ==="
chage -l kirsty | grep "Account expires"

# 4. Create destination directory
echo "=== Creating /ecommerce directory ==="
mkdir -p /ecommerce

# 5. Find and copy files
echo "=== Copying files owned by kirsty ==="
find /home/usersdata -type f -user kirsty -print0 | cpio -pdmv0 /ecommerce

# 6. Verify copy
echo "=== Verification ==="
echo "Total files copied:"
find /ecommerce/home/usersdata -type f -user kirsty 2>/dev/null | wc -l

echo ""
echo "Sample files:"
find /ecommerce/home/usersdata -type f -user kirsty 2>/dev/null | head -5

echo ""
echo "Directory structure:"
ls -la /ecommerce/home/usersdata/ 2>/dev/null

# Exit
exit
exit
Enter fullscreen mode Exit fullscreen mode

Expected Output

[root@stapp02 ~]# chage -E 2027-01-28 kirsty
[root@stapp02 ~]# chage -l kirsty | grep "Account expires"
Account expires                     : Jan 28, 2027

[root@stapp02 ~]# mkdir -p /ecommerce

[root@stapp02 ~]# find /home/usersdata -type f -user kirsty -print0 | cpio -pdmv0 /ecommerce
/home/usersdata/docs/report.txt
/home/usersdata/projects/config.yml
/home/usersdata/backup/data.tar.gz
... (files listed)
20 blocks

[root@stapp02 ~]# find /ecommerce/home/usersdata -type f -user kirsty | wc -l
42
Enter fullscreen mode Exit fullscreen mode

Verification Commands

# 1. Verify expiry date
chage -l kirsty | grep "Account expires"

# 2. Check expiry in /etc/shadow
grep kirsty /etc/shadow | awk -F: '{print "Expiry: " $8}'

# 3. Count files copied
find /ecommerce/home/usersdata -type f -user kirsty 2>/dev/null | wc -l

# 4. Verify ownership preserved
find /ecommerce -type f -user kirsty -ls 2>/dev/null | head -5

# 5. Verify no directories were copied (only files)
find /ecommerce -type d -user kirsty 2>/dev/null | wc -l

# 6. Check original file count for comparison
find /home/usersdata -type f -user kirsty | wc -l
Enter fullscreen mode Exit fullscreen mode

Alternative Methods

Using find with tar

find /home/usersdata -type f -user kirsty -print0 | tar -cf - --null -T - | tar -xf - -C /ecommerce
Enter fullscreen mode Exit fullscreen mode

Using find with cp (preserve attributes)

find /home/usersdata -type f -user kirsty -exec cp -a --parents {} /ecommerce \;
Enter fullscreen mode Exit fullscreen mode

Using find with rsync

find /home/usersdata -type f -user kirsty -print0 | rsync -av --files-from=- --from0 / /ecommerce/
Enter fullscreen mode Exit fullscreen mode

Summary

  • kirsty's expiry set to 2027-01-28
  • /ecommerce directory created
  • ✅ All files owned by kirsty copied from /home/usersdata
  • Directory structure preserved
  • Original ownership preserved
  • ✅ Only files copied (no directories)

The temporary user account for Kirsty has been configured with proper expiration, and their data has been securely copied with preserved ownership.

Top comments (0)