10 פקודות SCP להעברת קבצים/תיקיות ב-Linux

מפעילי Linux צריכים להכיר את סביבת שורת הפקודה. מאחר שGUI (Graphical User Interface) בשרתי Linux אינו נפוץ להתקין.

SSH עשוי להיות הפרוטוקול הפופולרי ביותר לאפשר למפעילי Linux לנהל את השרתים בצורה מרוחקת ובטוחה. בנוי עם SSH פקודה יש את SCP פקודה, שמשמשת להעתקת קובץ (ים) בין שרתים בצורה בטוחה.

בסיסי תחביר של פקודת SCP

הפקודה הבאה תיקרא כמו להעתיק "מקור_שמות_קבצים" לתוך "תיקייה_יעד" ב"מארח_יעד" באמצעות חשבון "שם_משתמש".

scp source_file_name username@destination_host:destination_folder

יש הרבה פרמטרים בפקודת SCP שאפשר להשתמש בהם. הנה הפרמטרים שאולי תשתמש בהם בשימוש יומי.

העברת קבצים באופן מאובטח ב-Linux

הפקודה SCP הבסיסית ללא פרמטרים תעתיק את הקבצים ברקע. המשתמש לא יראה כלום מלבד אם התהליך יסתיים או שמשהו מופיע.

אפשר להשתמש בפרמטר " -v" להדפסת מידע באגים למסך. זה יכול לעזור לך לבדוק בעיות קישור, אימות והתקנה.

העתקת קובץ מהשרת המקומי לשרת רחוק

הפקודה הבאה מעתיקה קובץ "scp-cheatsheet.pdf" ממקומי למערכת קרדינל רחוקה תחת המדריך /home/tecmint.

$ scp -v scp-cheatsheet.pdf [email protected]:/home/tecmint/.

תוצאות דוגמה:

SCP – Copy File to Remote Linux Server
Executing: program /usr/bin/ssh host 192.168.0.183, user tecmint, command scp -v -t /home/tecmint/.
OpenSSH_8.2p1 Ubuntu-4ubuntu0.5, OpenSSL 1.1.1f  31 Mar 2020
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to 192.168.0.183 [192.168.0.183] port 22.
debug1: Connection established.
debug1: identity file /home/tecmint/.ssh/id_rsa type -1
debug1: identity file /home/tecmint/.ssh/id_rsa-cert type -1
debug1: identity file /home/tecmint/.ssh/id_dsa type -1
debug1: identity file /home/tecmint/.ssh/id_dsa-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa_sk type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ed25519 type -1
debug1: identity file /home/tecmint/.ssh/id_ed25519-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ed25519_sk type -1
debug1: identity file /home/tecmint/.ssh/id_ed25519_sk-cert type -1
debug1: identity file /home/tecmint/.ssh/id_xmss type -1
...

העתקת קובץ ממארח רחוק למארח מקומי

הפקודה הבאה מעתיקה קובץ "ssh-cheatsheet.pdf" ממארח רחוק למערכת מקומית תחת המדריך /home/tecmint.

$ scp -v [email protected]:/home/ravi/ssh-cheatsheet.pdf /home/tecmint/.

תוצאות דוגמה:

SCP – Copy File to Local System
Executing: program /usr/bin/ssh host 192.168.0.183, user tecmint, command scp -v -f /home/ravi/ssh-cheatsheet.pdf
OpenSSH_8.2p1 Ubuntu-4ubuntu0.5, OpenSSL 1.1.1f  31 Mar 2020
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to 192.168.0.183 [192.168.0.183] port 22.
debug1: Connection established.
debug1: identity file /home/tecmint/.ssh/id_rsa type -1
debug1: identity file /home/tecmint/.ssh/id_rsa-cert type -1
debug1: identity file /home/tecmint/.ssh/id_dsa type -1
debug1: identity file /home/tecmint/.ssh/id_dsa-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa_sk type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ed25519 type -1
debug1: identity file /home/tecmint/.ssh/id_ed25519-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ed25519_sk type -1
...

העתקת קובץ ממארח רחוק למארח רחוק אחר

הפקודה הבאה מעתיקה קובץ "ssh-cheatsheet.pdf" ממארח רחוק למערכת קרדינל רחוקה תחת המדריך /home/tecmint.

$ scp -v [email protected]:/home/ravi/ssh-cheatsheet.pdf [email protected]:/home/anusha/.

העתקת קבצים עם תאריך ושעה יצירה מקוריים

הפרמטר "-p" ישמור על התאריכים המקוריים של שינוי קבצים ושעת גישה בעת העתקת קבצים, יופיע במקביל הזמן הנדרש ומהירות החיבור על המסך.

$ scp -p scp-cheatsheet.pdf [email protected]:/home/tecmint/.

תוצאות דוגמה:

SCP – Preserve File Timestamps
[email protected]'s password: 
scp-cheatsheet.pdf                                                                                                                                                                 100%  531   721.4KB/s   00:00    

Compress Scp בעת העתקת קבצים

אחד הפרמטרים שיכול להאיץ את העברת הקבצים שלך הוא הפרמטר "-C", שמשמש לדחיסת הקבצים שלך במהלך התהליך. הדבר הייחודי הוא שהדחיסה מתרחשת רק ברשת. כאשר הקובץ הגיע לשרת היעד, הוא יחזור לגודל המקורי כמו לפני הדחיסה.

הסתכל על הפקודות האלה. היא משתמשת בקובץ בודד של 93 מגה.

$ scp -pv messages.log [email protected]:.

תוצאות דוגמה:

SCP Transfers File Without Compression
Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp -v -p -t.
OpenSSH_6.0p1 Debian-3, OpenSSL 1.0.1c 10 May 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 202.x.x.x [202.x.x.x] port 22.
debug1: Connection established.
debug1: identity file /home/pungki/.ssh/id_rsa type -1
debug1: Found key in /home/pungki/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: Trying private key: /home/pungki/.ssh/id_rsa
debug1: Next authentication method: password
[email protected]'s password:
debug1: Authentication succeeded (password).
Authenticated to 202.x.x.x ([202.x.x.x]:22).
debug1: Sending command: scp -v -p -t.
File mtime 1323853868 atime 1380425711
Sending file timestamps: T1323853868 0 1380425711 0
messages.log 100% 93MB 58.6KB/s 27:05
Transferred: sent 97614832, received 25976 bytes, in 1661.3 seconds
Bytes per second: sent 58758.4, received 15.6
debug1: Exit status 0

העתקת קבצים ללא הפרמטר "-C" תגרום ל1661.3 שניות. אפשר להשוות את התוצאה לפקודה שלמטה שמשתמשת בפרמטר "-C".

$ scp -Cpv messages.log [email protected]:.

תוצאות דוגמה:

SCP – Transfers File with Compression
Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp -v -p -t.
OpenSSH_6.0p1 Debian-3, OpenSSL 1.0.1c 10 May 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 202.x.x.x [202.x.x.x] port 22.
debug1: Connection established.
debug1: identity file /home/pungki/.ssh/id_rsa type -1
debug1: Host '202.x.x.x' is known and matches the RSA host key.
debug1: Found key in /home/pungki/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: Next authentication method: publickey
debug1: Trying private key: /home/pungki/.ssh/id_rsa
debug1: Next authentication method: password
[email protected]'s password:
debug1: Enabling compression at level 6.
debug1: Authentication succeeded (password).
Authenticated to 202.x.x.x ([202.x.x.x]:22).
debug1: channel 0: new [client-session]
debug1: Sending command: scp -v -p -t .
File mtime 1323853868 atime 1380428748
Sending file timestamps: T1323853868 0 1380428748 0
Sink: T1323853868 0 1380428748 0
Sending file modes: C0600 97517300 messages.log
messages.log 100% 93MB 602.7KB/s 02:38
Transferred: sent 8905840, received 15768 bytes, in 162.5 seconds
Bytes per second: sent 54813.9, received 97.0
debug1: Exit status 0
debug1: compress outgoing: raw data 97571111, compressed 8806191, factor 0.09
debug1: compress incoming: raw data 7885, compressed 3821, factor 0.48

כפי שאפשר לראות, כשמשתמשים בדחיסה, התהליך של העברת הקבצים מתבצע ב162.5 שניות. זה 10 פעמים מהר יותר מאשר ללא השימוש בפרמטר "-C". אם אתה מעתיק הרבה קבצים ברשת, הפרמטר "-C" יעזור לך להקטין את הזמן הכולל שאתה צריך.

הדבר שעלינו לשים לב הוא ששיטת הדחיסה לא תעבוד על כל קבצים. כשהקובץ המקורי כבר דחוס, לא תמצא שיפור כלשהו שם. קבצים כמו .zip, .rar, תמונות, וקבצי .iso לא יושפעו מהפרמטר "-C".

שנה את צופן SCP להצפנת קבצים

כברירת מחדל, SCP משתמש ב" AES-128 "להצפנת קבצים. אם אתה רוצה לשנות לצופן אחר להצפנה, אתה יכול להשתמש בפרמטר " -c ".

תסתכל על הפקודה הזו.

$ scp -c 3des Label.pdf [email protected]:.

[email protected]'s password:
Label.pdf 100% 3672KB 282.5KB/s 00:13

הפקודה הנ"ל אומרת ל SCP להשתמש ב אלגוריתם3des להצפנת הקובץ. אנא היזהרו שהפרמטר משתמש ב" -c "ולא " -C ".

הגבלת שימוש ברוחב פס עם פקודת SCP

פרמטר נוסף שעשוי להיות שימושי הוא הפרמטר " -l ". הפרמטר " -l " יגביל את הרוחב הפס לשימוש. זה יהיה שימושי אם אתה עושה תסריט אוטומציה להעתקת הרבה קבצים, אך אינך רוצה שהרוחב הפס יושאר על ידי תהליך SCP.

$ scp -l 400 Label.pdf [email protected]:.

[email protected]'s password:
Label.pdf 100% 3672KB 50.3KB/s 01:13

הערך 400 מאחורי הפרמטר " -l " פירושו שאנו מגבילים את הרוחב הפס עבור תהליך SCP רק ל 50 קיב/שנייה.

דבר אחד לזכור הוא שרוחב הפס מסומן ב קילוביטים/שנייה (kbps). זה אומר ש 8 סיביות שוות ל 1 בייט.

בעוד SCP סופר בקילובייט/שנייה (KB/s). אז אם אתה רוצה להגביל את שימוש בבנייה למקסימום של SCP רק 50 KB/s, אתה צריך להגדיר אותו על 50 x8 = 400.

SCP עם פורט אחר

בדרך כלל, SCP משתמש בפורט 22 כפורט ברירת מחדל, אך מסיבות בטיחותיות, אולי תרצה לשנות את הפורט לפורט אחר. למשל, אנו משתמשים בפורט 2249.

ואז הפקודה צריכה להיראות ככה.

$ scp -P 2249 Label.pdf [email protected]:.

[email protected]'s password:
Label.pdf 100% 3672KB 262.3KB/s 00:14

ודא שהיא משתמשת באות גדולה "P" ולא "p" מאחר ו"p" כבר משמש לזמנים ומצביעים שמורים.

SCP – העתקת קבצים ותיקיות באופן רקורסיבי

לפעמים אנו זקוקים להעתיק את התיקיה וכל קבצים/תיקיות בתוכה. יהיה טוב יותר אם נוכל לעשות זאת בפקודה יחידה באמצעות הפרמטר "r", שמעתיק את כל התיקיה באופן רקורסיבי.

$ scp -r documents [email protected]:.

[email protected]'s password:
Label.pdf 100% 3672KB 282.5KB/s 00:13
scp.txt 100% 10KB 9.8KB/s 00:00

כאשר ההעתקה מתבצעת, תמצא בשרת היעד תיקיה בשם "documents" עם כל הקבצים שלה. התיקיה "documents" נוצרת אוטומטית.

SCP – כיבוי הודעות התקדמות

אם אינך רוצה לראות את מסך ההתקדמות והודעות האזהרה / האבחון מה-SCP, תוכל לבטל אותו באמצעות הפרמטר "-q". הנה דוגמה.

$ scp -q Label.pdf [email protected]:.

[email protected]'s password:
pungki@mint ~/Documents $

כפי שאפשר לראות, לאחר שנכנסת לסיסמה, אין מידע על תהליך ה-SCP. לאחר שהתהליך הושלם, תראה שוב הפקודה.

SCP – העתקת קבצים באמצעות פרוקסי

שרת הפרוקסי בדרך כלל משמש בסביבת המשרד. מעצם הטבע, ה-SCP אינו מוגדר כפרוקסי. כאשר הסביבה שלך משתמשת בפרוקסי, אתה צריך "לספר" ל-SCP לתקשר עם הפרוקסי.

הנה המצב. כתובת הפרוקסי היא 10.0.96.6 ופורט הפרוקסי הוא 8080. הפרוקסי מיושם גם אימות משתמש. קודם כל, אתה צריך ליצור את הקובץ "~/.ssh/config". שנית, שים את הפקודה הזאת בתוכו.

ProxyCommand /usr/bin/corkscrew 10.0.96.6 8080 %h %p ~/.ssh/proxyauth

לאחר מכן, אתה צריך ליצור את הקובץ "~/.ssh/proxyauth" אשר מכיל.

myusername:mypassword

לאחר מכן, אפשר לבצע את SCP באופן שקוף כרגיל.

אנא שים לב שה-corkscrew עשוי עדיין לא להיות מותקן במערכת שלך. ב-Linux Mint שלי, אני צריך להתקין אותו קודם, באמצעות הניתוח הרגיל של Linux Mint.

$ apt-get install corkscrew

עבור מערכות yum-based אחרות, המשתמשים יכולים להתקין corkscrew באמצעות הפקודת yum הבאה.

# yum install corkscrew

דבר נוסף הוא שמאחר וקובץ "~/.ssh/proxyauth" מכיל את ה"משתמש"username וה"סיסמה"password בפורמט ברור-טקסט, יש לוודא שהקובץ ניתן לגישה רק על ידיך.

בחר קובץ ssh_config אחר

עבור משתמשים ניידים שלעתים קרובות עוברים בין רשתות החברה לרשתות ציבוריות, זו תהיה סבלה לשנות תמיד את ההגדרות ב-SCP. יהיה טוב יותר אם נוכל לשים קובץ ssh_config אחר שיתאים לצרכינו.

ה-Proxy משמש ברשת החברה אך לא ברשת הציבורית ואתה מעביר רגילות בין רשתות.

$ scp -F /home/pungki/proxy_ssh_config Label.pdf

[email protected]:.
[email protected]'s password:
Label.pdf 100% 3672KB 282.5KB/s 00:13

כברירת מחדל, קובץ "ssh_config" למשתמש יחיד ימצא ב"~/.ssh/config". יוצרים קובץ "ssh_config" ספציפי עם תכונות תואמות ל-proxy יקל על העברה בין רשתות.

כשאתה ברשת החברה, אתה יכול להשתמש בפרמטר "-F". כשאתה ברשת ציבורית, אתה יכול לדלג על הפרמטר "-F".

זה כל מה שקשור לSCP. אפשר לראות את דפי העברת האדם של SCP לפרטים נוספים. אנא הרשו לעצמכם להשאיר הערות והצעות.

Source:
https://www.tecmint.com/scp-commands-examples/