[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"\u002Fblog\u002Fforce-ssh-client-to-use-private-key":3,"$f3oPZbOtdiVp3of_RnRXDAeZeTuQ3C3bbob77isdpsME":290,"i-local-icon:calendar-days":293,"i-local-icon:tag":298,"i-local-icon:eye":300},{"id":4,"title":5,"body":6,"categories_slug":279,"comment_count":281,"date_published":282,"description":283,"extension":284,"meta":285,"navigation":95,"path":286,"seo":287,"stem":288,"__hash__":289},"content\u002Fblog\u002F10.force-ssh-client-to-use-private-key.md","Force SSH Client to use specific private key",{"type":7,"value":8,"toc":273},"minimark",[9,13,18,23,27,30,60,64,67,71,79,216,219,248,252,255,266,269],[10,11,5],"h1",{"id":12},"force-ssh-client-to-use-specific-private-key",[14,15],"meta-card",{":categories":16,":date":17},"categories_slug","date_published",[19,20,22],"h2",{"id":21},"overview","Overview",[24,25,26],"p",{},"While SSH is an essential tool for system administrators and developers, repeatedly specifying key files and connection parameters can become cumbersome, especially when managing multiple servers. Fortunately, SSH config files provide a solution to streamline these connections, making the process more efficient and user-friendly.",[24,28,29],{},"For example, a standard CLI (command line interface) SSH command may look as follows:",[31,32,37],"pre",{"className":33,"code":34,"language":35,"meta":36,"style":36},"language-bash shiki shiki-themes github-dark","ssh -i ~\u002F.ssh\u002Fmy_specific_private_key user_name@10.2.25.5\n","bash","",[38,39,40],"code",{"__ignoreMap":36},[41,42,45,49,53,57],"span",{"class":43,"line":44},"line",1,[41,46,48],{"class":47},"svObZ","ssh",[41,50,52],{"class":51},"sDLfK"," -i",[41,54,56],{"class":55},"sU2Wk"," ~\u002F.ssh\u002Fmy_specific_private_key",[41,58,59],{"class":55}," user_name@10.2.25.5\n",[19,61,63],{"id":62},"understanding-ssh-config-files","Understanding SSH Config Files",[24,65,66],{},"SSH config files allows you to define various configuration parameters for SSH connections. By creating a centralized configuration file, you can avoid the need to specify key files, usernames, and other connection details each time they connect to a server. Instead, the configuration file automates these processes, simplifying the overall SSH experience.",[19,68,70],{"id":69},"creating-an-ssh-config-file","Creating an SSH Config File",[24,72,73,74,78],{},"To get started, create an SSH config file in your home directory or within the .ssh directory (on MacOS this will be found at ",[75,76,77],"em",{},"\u002FUsers\u002Fyour_name\u002F.ssh","). The default filename is config. You can use a text editor of your choice to create or modify this file. Here's a basic template:",[31,80,82],{"className":33,"code":81,"language":35,"meta":36,"style":36},"# ~\u002F.ssh\u002Fconfig\n\n# Server 1 example\n# This is the CLI equivalent of typing:  ssh -i ~\u002F.ssh\u002Fmy_private_key root@10.2.25.5\n# Now you only need to type:  ssh server1\n\nHost server1\n  HostName 10.2.25.5\n  User root\n  IdentityFile ~\u002F.ssh\u002Fmy_private_key\n\n# Server 2 example\n# This is the CLI equivalent of typing:  ssh -i ~\u002F.ssh\u002Fmy_private_key2 username@example.com\n# Now you only need to type: ssh server2\n\nHost server2\n  HostName example.com\n  User username\n  IdentityFile ~\u002F.ssh\u002Fmy_private_key2\n",[38,83,84,90,97,103,109,115,120,129,138,147,156,161,167,173,179,184,192,200,208],{"__ignoreMap":36},[41,85,86],{"class":43,"line":44},[41,87,89],{"class":88},"sAwPA","# ~\u002F.ssh\u002Fconfig\n",[41,91,93],{"class":43,"line":92},2,[41,94,96],{"emptyLinePlaceholder":95},true,"\n",[41,98,100],{"class":43,"line":99},3,[41,101,102],{"class":88},"# Server 1 example\n",[41,104,106],{"class":43,"line":105},4,[41,107,108],{"class":88},"# This is the CLI equivalent of typing:  ssh -i ~\u002F.ssh\u002Fmy_private_key root@10.2.25.5\n",[41,110,112],{"class":43,"line":111},5,[41,113,114],{"class":88},"# Now you only need to type:  ssh server1\n",[41,116,118],{"class":43,"line":117},6,[41,119,96],{"emptyLinePlaceholder":95},[41,121,123,126],{"class":43,"line":122},7,[41,124,125],{"class":47},"Host",[41,127,128],{"class":55}," server1\n",[41,130,132,135],{"class":43,"line":131},8,[41,133,134],{"class":47},"  HostName",[41,136,137],{"class":51}," 10.2.25.5\n",[41,139,141,144],{"class":43,"line":140},9,[41,142,143],{"class":47},"  User",[41,145,146],{"class":55}," root\n",[41,148,150,153],{"class":43,"line":149},10,[41,151,152],{"class":47},"  IdentityFile",[41,154,155],{"class":55}," ~\u002F.ssh\u002Fmy_private_key\n",[41,157,159],{"class":43,"line":158},11,[41,160,96],{"emptyLinePlaceholder":95},[41,162,164],{"class":43,"line":163},12,[41,165,166],{"class":88},"# Server 2 example\n",[41,168,170],{"class":43,"line":169},13,[41,171,172],{"class":88},"# This is the CLI equivalent of typing:  ssh -i ~\u002F.ssh\u002Fmy_private_key2 username@example.com\n",[41,174,176],{"class":43,"line":175},14,[41,177,178],{"class":88},"# Now you only need to type: ssh server2\n",[41,180,182],{"class":43,"line":181},15,[41,183,96],{"emptyLinePlaceholder":95},[41,185,187,189],{"class":43,"line":186},16,[41,188,125],{"class":47},[41,190,191],{"class":55}," server2\n",[41,193,195,197],{"class":43,"line":194},17,[41,196,134],{"class":47},[41,198,199],{"class":55}," example.com\n",[41,201,203,205],{"class":43,"line":202},18,[41,204,143],{"class":47},[41,206,207],{"class":55}," username\n",[41,209,211,213],{"class":43,"line":210},19,[41,212,152],{"class":47},[41,214,215],{"class":55}," ~\u002F.ssh\u002Fmy_private_key2\n",[24,217,218],{},"Explanation of the config key components:",[220,221,222,230,236,242],"ul",{},[223,224,225,229],"li",{},[226,227,228],"strong",{},"Host:"," This is a label for the specific configuration block. It represents the name or alias you want to use when connecting to the server. For example, you can now type from the command line:  ssh server1",[223,231,232,235],{},[226,233,234],{},"HostName:"," Specifies the actual domain name or IP address of the server.",[223,237,238,241],{},[226,239,240],{},"User:"," Defines the username used for connecting to the server. If omitted, your local username will be used.",[223,243,244,247],{},[226,245,246],{},"IdentityFile:"," Specifies the location of the private key file. This eliminates the need to use the -i option with every SSH command.",[19,249,251],{"id":250},"connecting-without-specifying-options","Connecting Without Specifying Options",[24,253,254],{},"Once your SSH config file is set up, connecting to a server is as simple as:",[31,256,258],{"className":33,"code":257,"language":35,"meta":36,"style":36},"ssh server1\n",[38,259,260],{"__ignoreMap":36},[41,261,262,264],{"class":43,"line":44},[41,263,48],{"class":47},[41,265,128],{"class":55},[24,267,268],{},"This command will automatically use the specified username, hostname, and private key from the configuration file.",[270,271,272],"style",{},"html pre.shiki code .svObZ, html code.shiki .svObZ{--shiki-default:#B392F0}html pre.shiki code .sDLfK, html code.shiki .sDLfK{--shiki-default:#79B8FF}html pre.shiki code .sU2Wk, html code.shiki .sU2Wk{--shiki-default:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html pre.shiki code .sAwPA, html code.shiki .sAwPA{--shiki-default:#6A737D}",{"title":36,"searchDepth":92,"depth":92,"links":274},[275,276,277,278],{"id":21,"depth":92,"text":22},{"id":62,"depth":92,"text":63},{"id":69,"depth":92,"text":70},{"id":250,"depth":92,"text":251},[280],"macos",null,"2024-01-03","SSH config files allow you to define various configuration parameters for SSH connections. By creating a centralized configuration file, you can avoid the need to specify key files, usernames, and other connection details each time they connect to a server.","md",{},"\u002Fblog\u002Fforce-ssh-client-to-use-private-key",{"title":5,"description":283},"blog\u002F10.force-ssh-client-to-use-private-key","OJ6urWOEgiRlWFxthiZ3G6kLbckpPENqdztIiRTH8kg",[291],{"id":149,"name":286,"count":292},3219,{"left":294,"top":294,"width":295,"height":295,"rotate":294,"vFlip":296,"hFlip":296,"body":297},0,24,false,"\u003Cpath\n    fill=\"none\"\n    stroke=\"currentColor\"\n    stroke-linecap=\"round\"\n    stroke-linejoin=\"round\"\n    stroke-width=\"1.5\"\n    d=\"M6.75 3v2.25M17.25 3v2.25M3 18.75V7.5a2.25 2.25 0 0 1 2.25-2.25h13.5A2.25 2.25 0 0 1 21 7.5v11.25m-18 0A2.25 2.25 0 0 0 5.25 21h13.5A2.25 2.25 0 0 0 21 18.75m-18 0v-7.5A2.25 2.25 0 0 1 5.25 9h13.5A2.25 2.25 0 0 1 21 11.25v7.5m-9-6h.008v.008H12zM12 15h.008v.008H12zm0 2.25h.008v.008H12zM9.75 15h.008v.008H9.75zm0 2.25h.008v.008H9.75zM7.5 15h.008v.008H7.5zm0 2.25h.008v.008H7.5zm6.75-4.5h.008v.008h-.008zm0 2.25h.008v.008h-.008zm0 2.25h.008v.008h-.008zm2.25-4.5h.008v.008H16.5zm0 2.25h.008v.008H16.5z\" \u002F>",{"left":294,"top":294,"width":295,"height":295,"rotate":294,"vFlip":296,"hFlip":296,"body":299},"\u003Cg\n    fill=\"none\"\n    stroke=\"currentColor\"\n    stroke-linecap=\"round\"\n    stroke-linejoin=\"round\"\n    stroke-width=\"1.5\">\n    \u003Cpath\n      d=\"M9.568 3H5.25A2.25 2.25 0 0 0 3 5.25v4.318c0 .597.237 1.17.659 1.591l9.581 9.581c.699.699 1.78.872 2.607.33a18.1 18.1 0 0 0 5.224-5.223c.54-.827.368-1.908-.33-2.607l-9.583-9.58A2.25 2.25 0 0 0 9.568 3\" \u002F>\n    \u003Cpath d=\"M6 6h.008v.008H6z\" \u002F>\n  \u003C\u002Fg>",{"left":294,"top":294,"width":295,"height":295,"rotate":294,"vFlip":296,"hFlip":296,"body":301},"\u003Cg\n    fill=\"none\"\n    stroke=\"currentColor\"\n    stroke-linecap=\"round\"\n    stroke-linejoin=\"round\"\n    stroke-width=\"1.5\">\n    \u003Cpath\n      d=\"M2.036 12.322a1 1 0 0 1 0-.639C3.423 7.51 7.36 4.5 12 4.5c4.638 0 8.573 3.007 9.963 7.178c.07.207.07.431 0 .639C20.577 16.49 16.64 19.5 12 19.5c-4.638 0-8.573-3.007-9.963-7.178\" \u002F>\n    \u003Cpath d=\"M15 12a3 3 0 1 1-6 0a3 3 0 0 1 6 0\" \u002F>\n  \u003C\u002Fg>"]