Linux
Archiving
Command Line
Error Handling
System Administration

Command /usr/sbin/chown failed with exit code 1 when Archiving

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The error Command /usr/sbin/chown failed with exit code 1 appears when a build or archive process tries to change file ownership but lacks the required permissions. In Xcode, this happens during the "Copy Bundle Resources" or "Archive" phase when the build system runs chown on files owned by another user. On Linux, it occurs when a non-root user tries to change file ownership during tar extraction or package building. The fix depends on context. In Xcode it is usually a project configuration issue, and on Linux it is a permissions issue.

Xcode Context: The Most Common Case

This error frequently appears when archiving iOS/macOS apps in Xcode:

 
Command /usr/sbin/chown failed with exit code 1

Fix 1: Install Manifest Ownership

Open your target's Build Settings and change:

 
INSTALL_OWNER = ""    # Clear this (was set to a specific user)
INSTALL_GROUP = ""    # Clear this too

Or in Xcode UI: Build Settings > search "Install Owner" > set to empty.

When INSTALL_OWNER is set, Xcode runs chown to change file ownership during archiving. If the build user does not have permission to change ownership to the specified user, the command fails.

Fix 2: Check File Permissions in Project

bash
1# Find files owned by root or other users in your project
2ls -la /path/to/YourProject/
3
4# Fix ownership to your user
5sudo chown -R $(whoami):staff /path/to/YourProject/

Files copied into the project from another machine or extracted from archives may have different ownership.

Fix 3: Clean Build Folder

bash
# In Xcode: Product > Clean Build Folder (Shift+Cmd+K)
# Or delete DerivedData
rm -rf ~/Library/Developer/Xcode/DerivedData/YourProject-*

Stale build artifacts with incorrect ownership can cause this error on subsequent archives.

Fix 4: CocoaPods / SPM Dependencies

Third-party dependencies sometimes include files with incorrect permissions:

bash
1# Fix CocoaPods
2cd /path/to/YourProject
3sudo chown -R $(whoami):staff Pods/
4pod deintegrate
5pod install
6
7# Fix SPM
8rm -rf ~/Library/Developer/Xcode/DerivedData/YourProject-*
9# Xcode will re-resolve packages on next build

Fix 5: Check Build Phase Scripts

Review "Run Script" build phases for chown commands that may fail:

bash
1# This will fail if not running as root
2chown root:wheel "${BUILT_PRODUCTS_DIR}/MyApp.app"
3
4# Fix: remove the chown or use the build user
5# Just delete the line. Xcode handles ownership correctly

Linux Context

During tar Extraction

bash
1# This fails for non-root users because tar tries to preserve ownership
2tar xzf archive.tar.gz
3# chown: Operation not permitted
4
5# Fix: use --no-same-owner
6tar xzf archive.tar.gz --no-same-owner
7
8# Or as root
9sudo tar xzf archive.tar.gz

During Package Building (dpkg, rpm)

bash
1# Building a .deb package with fakeroot to avoid chown errors
2fakeroot dpkg-deb --build mypackage
3
4# Without fakeroot, dpkg-deb runs chown and fails for non-root users
5dpkg-deb --build mypackage
6# chown: Operation not permitted

fakeroot intercepts system calls like chown and pretends they succeed without actually changing ownership.

Docker Build Context

dockerfile
1# This fails if the file is owned by a different user
2COPY --chown=node:node . /app
3
4# Fix: set ownership in a separate RUN command
5COPY . /app
6RUN chown -R node:node /app

In multi-stage builds, files may carry unexpected ownership from the build stage.

Understanding chown

bash
1# Basic syntax
2chown user:group file
3
4# Recursive
5chown -R user:group directory/
6
7# Only root can change file ownership to a different user
8# Regular users can only change group (to a group they belong to)
9sudo chown newuser:newgroup file    # Requires sudo
10chown :staff file                   # OK if you're in the 'staff' group

Why Non-Root Cannot chown

Unix security model: only root can give files to other users. This prevents users from creating files that appear to be owned by root, which would be a security risk (setuid attacks, quota evasion).

Diagnosing the Error

bash
1# Check who owns the problematic file
2ls -la /path/to/file
3# -rw-r--r--  1 root  wheel  1234 Jan 15 10:00 file.txt
4#              ^ owned by root, non-root chown will fail
5
6# Check who you're running as
7whoami
8# myuser
9
10# Check the exact chown command that failed (from Xcode build log)
11# Command: /usr/sbin/chown -RH myuser:staff /path/to/build/MyApp.app

In Xcode, expand the error in the build log to see the exact chown command and its arguments.

CI/CD Pipelines

yaml
1# GitHub Actions: fix ownership before build
2- name: Fix permissions
3  run: sudo chown -R runner:docker .
4
5# Jenkins: common after SCM checkout
6- sh 'sudo chown -R jenkins:jenkins ${WORKSPACE}'
7
8# GitLab CI: Docker executor may have root-owned files
9before_script:
10  - chown -R $(id -u):$(id -g) .

Common Pitfalls

  • Running Xcode as root: Never run Xcode with sudo. It creates files owned by root that later builds cannot modify. If you have root-owned files in DerivedData, delete them: sudo rm -rf ~/Library/Developer/Xcode/DerivedData.
  • INSTALL_OWNER in Debug builds: INSTALL_OWNER is only relevant for Archive/Release builds. If it is set, clear it unless you have a specific reason (enterprise distribution with custom ownership).
  • Ignoring the error and retrying: The error will persist until the ownership is fixed. Running the archive again does not help. Fix the file permissions first.
  • fakeroot not available: On macOS, fakeroot is not installed by default. Use Homebrew: brew install fakeroot. On Linux, install via your package manager.
  • Mounted volumes in Docker: Files on mounted volumes keep host ownership. Inside the container, chown on a mounted volume may fail depending on the mount configuration. Use --user flag or named volumes instead.

Summary

  • In Xcode: clear INSTALL_OWNER and INSTALL_GROUP build settings, fix file ownership with chown -R $(whoami):staff .
  • On Linux: use tar --no-same-owner for extraction, fakeroot for package building
  • Only root can change file ownership to a different user. This is a Unix security constraint
  • Check the Xcode build log for the exact chown command to identify the problematic file
  • In CI/CD: fix ownership after checkout with sudo chown -R
  • Clean DerivedData when stale artifacts have incorrect ownership

Course illustration
Course illustration

All Rights Reserved.