ÔõÑùͨ¹ýSSHʵÏÖ¿çЧÀÍÆ÷µÄLinux SysOpsÖÎÀí
ÔõÑùͨ¹ýSSHʵÏÖ¿çЧÀÍÆ÷µÄLinux SysOpsÖÎÀí
¸ÅÊö£º
ÔÚLinuxϵͳÖÎÀíÖУ¬¾³£ÐèҪͬʱÖÎÀí¶ą̀Զ³ÌЧÀÍÆ÷¡£Í¨¹ýSSH£¨Secure Shell£©ÐÒ飬ÎÒÃÇ¿ÉÒÔʵÏÖ¿çЧÀÍÆ÷µÄÖÎÀí²Ù×÷¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃSSHʵÏÖ¿çЧÀÍÆ÷µÄLinux SysOps£¨ÏµÍ³ÔËά£©ÖÎÀí£¬²¢ÌṩÏêϸµÄ´úÂëʾÀý¡£
SSH¼ò½é£º
SSHÊÇÒ»ÖÖ¼ÓÃܵÄÔ¶³ÌµÇ¼ÐÒ飬Ëü¿ÉÒÔͨ¹ýÇ徲ͨµÀÔÚ¿Í»§¶ËºÍЧÀÍÆ÷Ö®¼ä´«ÊäÊý¾Ý¡£SSHÐÒéÌṩÁËÉí·ÝÑéÖ¤¡¢Êý¾Ý¼ÓÃܺÍÊý¾ÝÍêÕûÐÔУÑéµÈÇå¾²»úÖÆ£¬°ü¹ÜÁËÔ¶³ÌµÇ¼µÄÇå¾²ÐÔ¡£
ÌìÉúSSH¹«Ë½Ô¿£º
ÔÚʹÓÃSSH¾ÙÐÐÔ¶³ÌµÇ¼֮ǰ£¬ÐèÒªÏÈÌìÉúSSH¹«Ë½Ô¿¶Ô¡£ÔÚ¿Í»§¶ËÉÏÖ´ÐÐÒÔÏÂÏÂÁîÌìÉú¹«Ë½Ô¿£º
ssh-keygen -t rsa
µÇ¼ºó¸´ÖÆ
Õ⽫ÌìÉúÒ»¶Ô¹«Ë½Ô¿Îļþ£¬Ä¬ÈÏ´æ´¢ÔÚÓû§µÄ.sshĿ¼Ï¡£¹«Ô¿ÎļþÃûΪid_rsa.pub£¬Ë½Ô¿ÎļþÃûΪid_rsa¡£
ÉèÖÃÃâÃÜÂëµÇ¼£º
ʹÓÃSSH¾ÙÐпçЧÀÍÆ÷ÖÎÀíʱ£¬Í¨³£»áÉèÖÃÃâÃÜÂëµÇ¼¡£Ïêϸ²Ù×÷ÈçÏ£º
£¨1£©½«¿Í»§¶ËµÄ¹«Ô¿Îļþid_rsa.pub¿½±´µ½ÐèÒªÖÎÀíµÄÔ¶³ÌЧÀÍÆ÷ÉÏ£º
ssh-copy-id -i ~/.ssh/id_rsa.pub user@remote_server
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬userÊÇÔ¶³ÌЧÀÍÆ÷ÉϵÄÓû§Ãû£¬remote_serverÊÇÔ¶³ÌЧÀÍÆ÷µÄIPµØµã»òÓòÃû¡£
£¨2£©ÔÚÔ¶³ÌЧÀÍÆ÷ÉÏ£¬½«¹«Ô¿ÎļþÄÚÈÝ×·¼Óµ½~/.ssh/authorized_keysÎļþÖУº
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
µÇ¼ºó¸´ÖÆ
£¨3£©ÉèÖÃÔ¶³ÌЧÀÍÆ÷ÉϵÄ~/.sshĿ¼ÒÔ¼°~/.ssh/authorized_keysÎļþµÄȨÏÞ£º
chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys
µÇ¼ºó¸´ÖÆ
Íê³ÉÒÔÉÏÉèÖú󣬼´¿ÉʵÏÖÃâÃÜÂëµÇ¼¡£
ʹÓÃSSH¾ÙÐпçЧÀÍÆ÷ÖÎÀí£º
ÔÚÉèÖÃÍêÃâÃÜÂëµÇ¼ºó£¬¿ÉÒÔͨ¹ýSSHÏÂÁî¾ÙÐпçЧÀÍÆ÷µÄÖÎÀí²Ù×÷¡£ÒÔÏÂʾÀý½ö×÷Ϊ²Î¿¼£º
£¨1£©Ô¶³ÌÖ´ÐÐÏÂÁ
ssh user@remote_server 'command'
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬userÊÇÔ¶³ÌЧÀÍÆ÷ÉϵÄÓû§Ãû£¬remote_serverÊÇÔ¶³ÌЧÀÍÆ÷µÄIPµØµã»òÓòÃû£¬commandÊÇÒªÔÚÔ¶³ÌЧÀÍÆ÷ÉÏÖ´ÐеÄÏÂÁî¡£
£¨2£©¿½±´Îļþ£º
½«ÍâµØÎļþ¿½±´µ½Ô¶³ÌЧÀÍÆ÷£º
scp local_file user@remote_server:remote_path
µÇ¼ºó¸´ÖÆ
½«Ô¶³ÌЧÀÍÆ÷ÉϵÄÎļþ¿½±´µ½ÍâµØ£º
scp user@remote_server:remote_file local_path
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬local_fileÊÇÍâµØÎļþµÄ·¾¶£¬remote_pathÊÇÔ¶³ÌЧÀÍÆ÷ÉÏÎļþµÄ·¾¶£¬remote_fileÊÇÔ¶³ÌЧÀÍÆ÷ÉϵÄÎļþ·¾¶£¬local_pathÊÇÍâµØĿ¼µÄ·¾¶¡£
ÅúÁ¿ÖÎÀí£º
ÈôÊÇÐèÒªÅúÁ¿ÖÎÀí¶ą̀ЧÀÍÆ÷£¬¿ÉÒÔʹÓÃÑ»·½á¹¹ºÍSSHÏÂÁ±éÀúЧÀÍÆ÷Áбí¾ÙÐвÙ×÷¡£ÒÔÏÂʾÀý½ö×÷Ϊ²Î¿¼£º
#!/bin/bash servers=("server1" "server2" "server3") for server in "${servers[@]}" do ssh user@$server 'command' done
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬serversÊÇЧÀÍÆ÷ÁÐ±í£¬userÊÇÔ¶³ÌЧÀÍÆ÷ÉϵÄÓû§Ãû£¬commandÊÇÒªÔÚÔ¶³ÌЧÀÍÆ÷ÉÏÖ´ÐеÄÏÂÁî¡£
ͨ¹ýÉÏÊö°ì·¨£¬ÎÒÃÇ¿ÉÒÔʹÓÃSSHÐÒéʵÏÖ¿çЧÀÍÆ÷µÄLinux SysOpsÖÎÀí¡£ÕâÖÖÒªÁì¿ÉÒÔÌá¸ßÖÎÀíЧÂÊ£¬ïÔÌÊÖ¶¯²Ù×÷µÄÊÂÇéÁ¿¡£Í¬Ê±£¬Í¨¹ý´úÂëʾÀýµÄѧϰ£¬ÎÒÃÇ¿ÉÒÔƾ֤ÏÖÕæÏàÐξÙÐÐÀ©Õ¹ºÍÓÅ»¯£¬Ìá¸ßÖÎÀí²Ù×÷µÄÎÞаÐԺͿɿ¿ÐÔ¡£
ÒÔÉϾÍÊÇÔõÑùͨ¹ýSSHʵÏÖ¿çЧÀÍÆ÷µÄLinux SysOpsÖÎÀíµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡