How to fix scp: error: unexpected filename: in SSH

SSHINTERMEDIATEMEDIUMUpdated Jan 4, 2026· 162 views

This error occurs when SCP protocol receives a filename that doesn't match the request, due to OpenSSH security validations or special characters in filenames. It can also happen with paths containing colons or dots that require escaping.

Quick fix

Fixes scp: error: unexpected filename:

# Instead of:
scp file:name.txt user@host:/tmp/

# Use:
scp ./file:name.txt user@host:/tmp/

# For wildcard copies:
scp ./* user@host:/tmp/
  1. Use explicit relative paths with ./
  2. Use absolute paths instead of dot (.) destination
  3. Escape special characters properly
  4. Disable strict filename checking (if safe)
ssh — error
# Instead of:
scp file:name.txt user@host:/tmp/
# Use:
scp ./file:name.txt user@host:/tmp/
# For wildcard copies:
scp ./* user@host:/tmp/
scp: error: unexpected filename:

What this error means

The 'unexpected filename' error is a security validation check in newer OpenSSH versions. When you request a file via SCP, the server returns the file with its name and path. Modern OpenSSH validates that the returned filename matches what was requested. This error fires when there's a mismatch, which can happen due to: filename validation rules, special characters not being properly escaped, or security restrictions on certain path patterns (like dots or colons).

Typical symptoms

  • SCP command fails with 'error: unexpected filename:' message mid-transfer
  • File transfers work locally but fail when connecting to remote servers
  • Filenames with special characters (colons, spaces, dots) fail to transfer
  • Error occurs even though the file exists on the remote system
  • Works with older OpenSSH versions but fails after system updates

Common causes

  1. Filename contains a colon (:) which SCP interprets as a host specifier
  2. Using dot (.) as destination path in newer OpenSSH (CVE-2018-20685 fix)
  3. Special characters in filename not properly escaped or quoted
  4. Mismatch between what was requested and what the server returns
  5. Using absolute paths when relative paths are expected
  6. OpenSSH version mismatch between client and server

How to fix "scp: error: unexpected filename:"

1Use explicit relative paths with ./

Prefix filenames and paths with ./ to make them explicit relative paths. This prevents SCP from misinterpreting special characters like colons.

bash
# Instead of:
scp file:name.txt user@host:/tmp/

# Use:
scp ./file:name.txt user@host:/tmp/

# For wildcard copies:
scp ./* user@host:/tmp/

The ./ prefix explicitly marks the path as a file, not a remote host specification.

2Use absolute paths instead of dot (.) destination

If you're using . as the destination (current directory), replace it with the full path or $(pwd) (print working directory).

bash
# Instead of:
scp user@host:/path/to/file .

# Use absolute path:
scp user@host:/path/to/file /home/user/target/

# Or use $(pwd):
scp user@host:/path/to/file $(pwd)/

This is particularly important with OpenSSH versions that have the CVE-2018-20685 fix.

3Escape special characters properly

If filenames contain spaces, colons, or other special characters, ensure they are properly quoted or escaped.

bash
# For files with spaces:
scp "user@host:/path/to/file name.txt" ./

# For files with colons (using relative path):
scp user@host:/path/to/./file:name.txt ./

# Double-check the exact filename on remote:
ssh user@host ls -la /path/to/file*
4Disable strict filename checking (if safe)

If you trust the remote server and need a quick workaround, use the -T flag to disable strict filename validation.

bash
scp -T user@host:/path/to/file ./

Warning: Only use this when you fully trust the remote host, as it disables security validations. This should be a temporary measure while you fix the underlying issue.

5Use SFTP as alternative

If SCP continues to cause issues, use SFTP (SSH File Transfer Protocol) instead, which doesn't have the same filename validation restrictions.

bash
sftp user@host << EOF
get /path/to/file
bye
EOF

SFTP is more modern, more reliable, and handles special characters better than legacy SCP.

Advanced notes

The 'unexpected filename' error became more common after OpenSSH 7.4+ implemented CVE-2018-20685 security fixes. This vulnerability allowed malicious remote servers to send unexpected files during SCP transfers. The fix validates that filenames match requests, but this broke some legacy workflows and scripts using patterns like '.' as destination or certain special characters. Different platforms (Linux, macOS, Windows) may have different OpenSSH versions and behaviors. For CI/CD pipelines and automation, prefer SFTP over SCP. If you cannot upgrade tooling, the -T flag or explicit path escaping are temporary workarounds. Check your OpenSSH version with scp -V on both client and server—version mismatches can contribute to this issue.

Frequently asked questions

What does "scp: error: unexpected filename:" mean?
The 'unexpected filename' error is a security validation check in newer OpenSSH versions. When you request a file via SCP, the server returns the file with its name and path. Modern OpenSSH validates that the returned filename matches what was requested.
How do I fix "scp: error: unexpected filename:"?
This error occurs when SCP protocol receives a filename that doesn't match the request, due to OpenSSH security validations or special characters in filenames. It can also happen with paths containing colons or dots that require escaping.
What causes "scp: error: unexpected filename:"?
Filename contains a colon (:) which SCP interprets as a host specifier Using dot (.) as destination path in newer OpenSSH (CVE-2018-20685 fix) Special characters in filename not properly escaped or quoted
Is the "scp: error: unexpected filename:" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading