Linux SysOpsÖÐʹÓÃSSH¾ÙÐÐÔ¶³Ì¾ç±¾Ö´ÐеÄÒªÁì
Linux SysOpsÖÐʹÓÃSSH¾ÙÐÐÔ¶³Ì¾ç±¾Ö´ÐеÄÒªÁ죬ÐèÒªÏêϸ´úÂëʾÀý
¸ÅÊö£º
ÔÚLinuxϵͳ²Ù×÷ÖУ¬Ê¹ÓÃSSH£¨Secure Shell£©¿ÉÒÔʵÏÖÔ¶³ÌµÇ¼ºÍÖ´ÐÐÏÂÁî¡£Ô¶³Ì¾ç±¾Ö´ÐÐÊÇLinux SysOpsÊÂÇéÖо³£Óõ½µÄÊÖÒÕ£¬Ëü¿ÉÒÔ×ÊÖúÎÒÃÇÔ¶³ÌÖÎÀí¡¢ÉèÖúͼà¿Ø¶ą̀LinuxЧÀÍÆ÷¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃSSH¾ÙÐÐÔ¶³Ì¾ç±¾Ö´ÐУ¬²¢ÌṩÏêϸµÄ´úÂëʾÀý£¬ÒÔ×ÊÖú¶ÁÕ߸üºÃµØÃ÷È·ºÍÓ¦Óá£
°ì·¨1£º×°ÖÃSSH¿Í»§¶ËºÍЧÀÍÆ÷
ÔÚ×îÏÈ֮ǰ£¬ÎÒÃÇÐèҪȷ±£¿Í»§¶ËºÍЧÀÍÆ÷É϶¼×°ÖÃÁËSSH¡£Í¨³££¬´ó´ó¶¼Linux¿¯Ðа涼»áĬÈÏ×°ÖÃSSHЧÀÍÆ÷£¬µ«ÈôÊÇδװÖã¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî¾ÙÐÐ×°Öãº
sudo apt-get install openssh-server # Debian/Ubuntuϵͳ sudo yum install openssh-server # CentOS/RHELϵͳ
µÇ¼ºó¸´ÖÆ
SSH¿Í»§¶Ëͨ³£Ò²»áËæ×ÅLinuxϵͳһͬװÖã¬ÈôÊÇûÓÐ×°Öã¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî¾ÙÐÐ×°Öãº
sudo apt-get install openssh-client # Debian/Ubuntuϵͳ sudo yum install openssh-clients # CentOS/RHELϵͳ
µÇ¼ºó¸´ÖÆ
°ì·¨2£ºÌìÉúSSHÃÜÔ¿¶Ô
ΪÁËÈ·±£SSHÅþÁ¬µÄÇå¾²ÐÔ£¬ÎÒÃÇͨ³£»áʹÓÃÃÜÔ¿ÈÏÖ¤Ìæ»»ÃÜÂëÈÏÖ¤¡£ÎÒÃÇ¿ÉÒÔʹÓÃSSHÃÜÔ¿¶ÔÀ´¾ÙÐÐÔ¶³Ì¾ç±¾Ö´ÐС£Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚÍâµØÌìÉú¹«Ô¿ºÍ˽Կ¡£¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÀ´ÌìÉúSSHÃÜÔ¿¶Ô£º
ssh-keygen -t rsa
µÇ¼ºó¸´ÖÆ
¸ÃÏÂÁ»áÔÚÓû§µÄ.sshĿ¼ÏÂÌìÉú¹«Ô¿id_rsa.pubºÍ˽Կid_rsa¡£
°ì·¨3£º½¨ÉèSSHÐÅÍйØϵ
ÔÚÔ¶³ÌÖ´Ðо籾֮ǰ£¬ÎÒÃÇÐèÒªÔÚÍâµØºÍÔ¶³ÌÖ÷»úÖ®¼ä½¨ÉèSSHÐÅÍйØϵ£¬ÒÔʹԶ³ÌÖ÷»úÐÅÍÐÎÒÃǵĹ«Ô¿¡£ÎÒÃÇ¿ÉÒÔʹÓÃssh-copy-idÏÂÁ¹«Ô¿¸´ÖƵ½Ô¶³ÌÖ÷»úÉÏ£º
ssh-copy-id username@remote_host
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬usernameÊÇÔ¶³ÌÖ÷»úÉϵÄÓû§Ãû£¬remote_hostÊÇÔ¶³ÌÖ÷»úµÄIPµØµã»òÖ÷»úÃû¡£Ö´ÐÐÏÂÁîºó£¬ÏµÍ³»áÒªÇóÊäÈëÔ¶³ÌÖ÷»úµÄÃÜÂ룬Ȼºó½«¹«Ô¿¸´ÖƵ½Ô¶³ÌÖ÷»úµÄ~/.ssh/authorized_keysÎļþÖС£
°ì·¨4£ºÔ¶³Ì¾ç±¾Ö´ÐÐ
Ò»µ©½¨ÉèÁËSSHÐÅÍйØϵ£¬ÎÒÃǾͿÉÒÔʹÓÃSSH¾ÙÐÐÔ¶³Ì¾ç±¾Ö´ÐÐÁË¡£ÒÔÏÂÊÇÒ»¸öʾÀý¾ç±¾£¬Ëü½«Ô¶³ÌÖ´ÐÐÔ¶³ÌÖ÷»úÉϵÄÏÂÁ²¢½«Êä³ö´òÓ¡µ½ÍâµØÖնˣº
#!/bin/bash remote_host="remote_host" remote_user="remote_user" command="command_to_execute" output=$(ssh $remote_user@$remote_host $command) echo "$output"
µÇ¼ºó¸´ÖÆ
Ôھ籾ÖУ¬½«remote_hostÌ滻ΪԶ³ÌÖ÷»úµÄIPµØµã»òÖ÷»úÃû£¬½«remote_userÌ滻ΪԶ³ÌÖ÷»úÉϵÄÓû§Ãû£¬½«command_to_executeÌ滻ΪҪÔÚÔ¶³ÌÖ÷»úÉÏÖ´ÐеÄÏÂÁͨ¹ýsshÏÂÁÎÒÃÇ¿ÉÒÔÔÚÍâµØÖ´ÐÐÔ¶³ÌÖ÷»úÉϵÄÏÂÁ²¢½«Êä³öÉúÑĵ½output±äÁ¿ÖС£×îºó£¬ÎÒÃǽ«Êä³ö´òÓ¡µ½ÍâµØÖնˡ£
ÎÒÃÇ¿ÉÒÔƾ֤ÏÖʵÐèÇó¾ÙÐÐÐ޸ĺÍÀ©Õ¹£¬ºÃ±Èͨ¹ýÑ»·½á¹¹Ö´Ðжà¸öÏÂÁî¡¢½«¾ç±¾°²Åŵ½¶à¸öÔ¶³ÌÖ÷»úµÈ¡£
×ܽ᣺
ͨ¹ýʹÓÃSSH¾ÙÐÐÔ¶³Ì¾ç±¾Ö´ÐУ¬Linux SysOps¿ÉÒÔÔ½·¢¸ßЧµØÖÎÀíºÍά»¤¶ą̀LinuxЧÀÍÆ÷¡£±¾ÎÄÏÈÈÝÁËʹÓÃSSH¾ÙÐÐÔ¶³Ì¾ç±¾Ö´Ðеİ취£¬²¢ÌṩÁËÏêϸµÄ´úÂëʾÀý¡£¶ÁÕß¿ÉÒÔƾ֤ÏÖʵÐèÇó¾ÙÐÐÐ޸ĺÍÀ©Õ¹£¬ÒÔ˳Ӧ²î±ðµÄ³¡¾°ºÍʹÃü¡£Ï£Íû±¾ÎĶԶÁÕß¾ÙÐÐÓÐÒæµÄÖ¸µ¼£¬ÌáÉýÆäLinux SysOpsÊÖÒÕÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇLinux SysOpsÖÐʹÓÃSSH¾ÙÐÐÔ¶³Ì¾ç±¾Ö´ÐеÄÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡