01Why Choose SCP?
① What is SCP
SCP (Secure Copy Protocol) is a command that uses SSH (Secure Shell Protocol) for secure file and directory transfers. It allows copying files and directories between remote hosts with strong security. It has long been used to securely transfer files from one Linux system to another or to transfer compiled programs from a virtual machine development environment to a development board.
② Core Advantages of SCP
-
Instant Deployment: Deploy immediately to the development board after compilation
-
Version Control: Avoid version confusion caused by manual copying
-
Automated Integration: Can be embedded in Makefile or CI/CD processes
-
Remote Management: No physical contact with the development board required
③ Comparison of File Transfer Solutions for Development Boards
| Transfer Method | Advantages | Disadvantages | Applicable Scenarios |
|---|---|---|---|
| SCP over SSH | Secure, fast, no physical contact required | Requires network connection | Daily development and debugging |
| USB Drive/SD Card | No network required | Manual operation, slow speed, prone to damage | Initial system installation |
| NFS Mount | Real-time synchronization | Complex configuration, depends on network stability | Frequent modifications during debugging |
| TFTP | Simple | Insecure, slow speed | Transfer during U-boot phase |
02SCP Basics
① Command Format
scp [options] source_file [target_path] [target_path] = username@target_host:/target_directory For example: scp -r /home/user/source_dir [email protected]:/home/user/target_dir Here, the -r parameter indicates recursive copying of the directory and all its subdirectories. /home/user/source_dir indicates the local directory/source file. [email protected] indicates the IP address and username of the target host. /home/user/target_dir is the target directory on the target host.
Options Description Usage Scenario
-P Specify SSH port Connect to a server on a non-default port (22)
-r Recursive copy directory Transfer entire folder
-C Enable compression Speed up network transfer
-i Specify identity file Use key authentication
-l Limit bandwidth Avoid occupying too much network resources
-v Verbose output Debug connection issues
-q Quiet mode Do not display transfer progress
② Configuring SSH Service on Development Board
Before using the command, ensure that the SSH service is enabled on the development board:
# Install OpenSSH server on the development board
sudo apt-get install openssh-server # Ubuntu/Debian
opkg install openssh-sftp-server # OpenWrt
# Start SSH service
sudo service ssh start
# or
sudo /etc/init.d/sshd start
Check network configuration:
# View the IP address of the development board
ifconfig eth0 # Wired network
ifconfig wlan0 # Wireless network
# Test network connectivity
ping 192.168.1.100 # Replace with the actual IP of the development board
03SCP Practical Demonstration
① Local to Remote Transfer
# Command line input demonstration
# Basic transfer
scp local_file.txt user@remote_host:/path/to/destination/
# Transfer specifying port
scp -P 2222 local_file.txt user@remote_host:/remote/path/
# Transfer and rename
scp local_file.txt user@remote_host:/remote/path/new_name.txt
# Transfer entire directory
scp -r local_dir/ user@remote_host:/remote/path/
# No need to specify username if the remote host's username is the same as the local username, it can be simplified to scp -r local_dir/ remote_host:/remote/path/
② Remote to Local Transfer
# Command line input demonstration
# Download a single file
scp user@remote_host:/remote/path/file.txt ./local_dir/
# Download entire directory
scp -r user@remote_host:/remote/dir/ ./local_dir/
# Download from non-standard port
scp -P 2222 user@remote_host:/remote/file.txt ./
③ Direct Transfer Between Servers
# Command line input demonstration
# Transfer via local relay
scp user1@host1:/path/file.txt user2@host2:/path/
# Use SSH proxy forwarding (avoid local storage)
scp -o ProxyJump=user@gateway user@remote_host:/remote/file.txt ./
④ Automation Deployment Script
# shell script
#!/bin/bash
# deploy_to_board.sh
BOARD_IP="192.168.1.100"
BOARD_USER="root"
TARGET_DIR="/home/root/app"
LOCAL_BUILD_DIR="./build"
echo "Starting deployment to the development board..."
# Check network connectivity
ping -c 1 -W 2 $BOARD_IP > /dev/null 2>&1
if [ $? -ne 0 ]; then echo "Error: Unable to connect to the development board $BOARD_IP" exit 1
fi
# Transfer executable file
echo "Transferring application..."
scp $LOCAL_BUILD_DIR/main_app $BOARD_USER@$BOARD_IP:$TARGET_DIR/
# Transfer configuration files
echo "Transferring configuration files..."
scp $LOCAL_BUILD_DIR/config/*.conf $BOARD_USER@$BOARD_IP:$TARGET_DIR/config/
# Transfer library dependencies
echo "Transferring library files..."
scp $LOCAL_BUILD_DIR/libs/*.so $BOARD_USER@$BOARD_IP:$TARGET_DIR/libs/
# Set execution permissions
echo "Setting execution permissions..."
ssh $BOARD_USER@$BOARD_IP "chmod +x $TARGET_DIR/main_app"
echo "Deployment completed!"
04 Makefile Integration Example
Add Deployment Rules in Makefile
# Makefile deployment configuration
BOARD_IP = 192.168.1.100
BOARD_USER = root
DEPLOY_PATH = /home/root/$(PROJECT_NAME)
# Compile target
all: main_app
main_app: main.c $(CC) $(CFLAGS) -o $@ $^
# Deploy to development board
deploy: main_app @echo "Deploying to development board..." scp main_app $(BOARD_USER)@$(BOARD_IP):$(DEPLOY_PATH)/ scp -r config/ $(BOARD_USER)@$(BOARD_IP):$(DEPLOY_PATH)/ ssh $(BOARD_USER)@$(BOARD_IP) "chmod +x $(DEPLOY_PATH)/main_app" @echo "Deployment completed"
# Clean files on the development board
clean-remote: ssh $(BOARD_USER)@$(BOARD_IP) "rm -rf $(DEPLOY_PATH)"
.PHONY: deploy clean-remote
Note: If you want to integrate it into the engineering CMake, try referring to the image below:
06Summary
Whether you are transferring files from local to remote hosts or downloading files from remote hosts, you can use the SCP command for file transfer. Once proficient, you can:
✅ Efficient Deployment: Quickly transfer programs to the development board✅ Automated Processes: Integrate into Makefile or scripts✅ Secure Transfer: Use key authentication to protect data✅ Troubleshoot Issues: Handle various exceptions during transfer
Thank you for your attention and support. I will update various useful content from time to time. If you have any questions or different opinions, feel free to comment or leave a message for discussion and learning together~
Feel free to follow the public account and click “Share”, “Like”, “View”~Wishing you successful compilation and debugging!