October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk9 min

How to Use Different SSH Keys for Different Linux Servers

Set up per-server SSH keys once in ~/.ssh/config, control ssh-agent behavior with IdentitiesOnly, group hosts with patterns, and verify every alias with ssh -G.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a separate Host block in ~/.ssh/config for every server or server group. Set the real destination with HostName, the login account with User, and the matching private key with IdentityFile. Add IdentitiesOnly yes when you want OpenSSH to ignore unrelated keys loaded in ssh-agent.

After that one-time setup, connect with short commands such as ssh production and ssh staging instead of repeatedly typing -i or risking the wrong key.

As an Amazon Associate I earn from qualifying purchases.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The basic configuration

OpenSSH reads per-user settings from ~/.ssh/config. A configuration alias is the word written after Host; it does not have to be the server’s DNS name. HostName supplies the actual DNS name or IP address used for the connection.

Host production
    HostName prod.example.com
    User deploy
    IdentityFile ~/.ssh/id_ed25519_production
    IdentitiesOnly yes

Host staging
    HostName staging.example.com
    User deploy
    IdentityFile ~/.ssh/id_ed25519_staging
    IdentitiesOnly yes

With this file saved, run ssh production to reach prod.example.com as deploy using the production key, or ssh staging to use the staging key. The alias keeps the command stable even if the underlying hostname changes later.

#1 Best Overall
Sale
TOZO A1 Wireless Earbuds Bluetooth 5.3 Light Weight in Ear Headphones | IPX5 Waterproof,2 Mics for AI Calls,Immersive Premium Sound Bass Headset with Charging Case,32 EQ via App Black
  • [Ultra-Lightweight Ear Buds Designed for Small Ears] Each earbud weighs only 3.7g and features a compact, ergonomic in-ear design made especially for small ears. Secure, low-profile, and comfortable for workouts, all-day wear or overnight sleeping.The ultra-slim profile sits flush in your ear with no hard edges poking or rubbing, so you can even sleep on your side without discomfort.
  • [Immersive Stereo Sound with TOZO OrigX Technology] TOZO OrigX tuning delivers clear vocals, balanced mids, and natural stereo sound for music, podcasts, and videos.
  • [Long Battery Life for Daily Use] Get up to 7 hours of playtime on a single charge, with up to 32 hours total using the charging case—ideal for workdays, commuting, and extended listening sessions.
  • [Bluetooth 5.3 & Stable Connection] Bluetooth 5.3 provides fast pairing, stable wireless performance, and reduced dropouts as you move around home or office.
  • [Deep Bass with Clear Vocals] High-performance drivers produce punchy bass while keeping vocals clean and detailed for everyday listening.

Prepare the keys and configuration file

Inventory the private keys

Before editing the configuration, identify the private key file that belongs to each server. Use clear names that reflect the trust boundary, such as id_ed25519_production and id_ed25519_staging. The corresponding public-key files may sit beside them, but IdentityFile points to the private key.

Do not paste private-key contents into tickets, chat, scripts, or shell history. Keep each private key protected with restrictive filesystem permissions. If a key is copied to another workstation, protect the copy there as well.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Create or edit ~/.ssh/config

  1. Create the per-user SSH directory and configuration file if they do not already exist.
  2. Open ~/.ssh/config in your editor.
  3. Add one Host block for each server or group, using spaces to indent the settings.
  4. Save the file, then inspect the effective result with ssh -G before making a production connection.

Each block should contain, at minimum, a host selector, the real destination, the remote account, and the intended private key. Adding IdentitiesOnly yes makes the selection strict when an authentication agent is active.

What each directive controls

Host: the selector or alias

Host determines which configuration block applies. An explicit name such as production creates a readable alias. You can also use patterns to apply one block to several names.

HostName: the real destination

HostName is the DNS name or address that OpenSSH contacts. It is independent of the alias. For example, Host production can map to HostName 203.0.113.20 without changing the command you type.

User: the remote login account

User sets the account used on the Linux server. If it is omitted, OpenSSH normally derives the account from your local username, which may not be the account intended for administration or deployment. Set it explicitly when servers use a dedicated account.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

IdentityFile: the private key to offer

IdentityFile names a private key used for public-key authentication. Use an explicit path when each server has a different trust boundary. A path beginning with ~ is supported, so ~/.ssh/id_ed25519_production refers to a file below the current user’s home directory.

Rank #2
Sale
Smart Watch for Men Women Compatible with iPhone Samsung Android Phone 1.69 inch Full Touch Screen IP68 Waterproof Bluetooh Fitness Tracker Heart Rate/Sleep Monitor (Black)
  • 【IP68 Waterproof Multifunctinal Smart Watch】Torjalph fitness activity tracker smart watch is IP68 waterproof technology ,you can wear it when swimming,handwashing,it's rain-proof which means that you perform multiple exercise modes outside without worrying about sweat or rain corroding your fitness smart watch,and this watch equipped with a 230 mAh large capacity battery magnetic charging ,and this sports activity tracker watch can be used 5-7 days If fully charged.
  • 【Larger 1.83'' Full Screen Watch & Multi-dial Selection】1.83 inch full HD color touch screen smart watch for men / women provides excellent 240*284 resolution image quality ,This Bluetooth smart watch offers a better touch experience and remarkable immersion and ,more than 200 watch faces for you to choose from App"Da Fit ", you can change the dial style according to your mood to wear keeping the world on your wrist
  • 【Accurate Activity Tracker Smart Watch 】sports watch activity tracker uses sweat and dust resistant breathable silicone strap, our Fitness Tracker Smart Watch for Android iOS Phones supports 12 sports modes,walking,running,bicycle,swimming,badminton,basketball,football,rope skipping. This bluetooth calls smart watch for men women is lightweight and stylish, bringing more convenient and relaxing sports experience while also providing more protection.
  • 【Your 24 Hour Personal Daily Health Monitor】Fitness watch for women men full touch screen IP67 waterproof bluetooth smartwatch for Android iOS Phones with heart rate blood pressure monitor multifunction Andriod smart watch with steps counter sleep monitor sports fitness watch, monitoring blood oxygen saturation allows you to understand your physical condition in real time to track fitness progress ,to know your health status in time.
  • 【More Functions Bluetooth Call Smart Watch】This fitness activity smart watch full screen for iPhone and Android phones includes a bunch of new features including:The bluetooth smart watch can hang up the phone, receive and display WhatsAPP /Text /Facebook /Twitter /Messenger reminder.Support sedentary reminder, alarm clock, stopwatch, timer,weather information,music control, remote camera control compatible with Android 5.0 and above or iOS 9.0 and above smartphones.

IdentitiesOnly yes: prevent unrelated agent keys

OpenSSH can receive identities from an authentication agent. When several keys are loaded, the client may offer keys that are unrelated to the destination. IdentitiesOnly yes tells the client to use only the configured identity and certificate files, even when the agent offers additional identities.

This matters because a server can reject a connection after too many incorrect key offers. Pairing one IdentityFile with IdentitiesOnly yes gives each alias a predictable key-selection boundary.

Set up separate aliases step by step

Production and staging example

  1. Choose distinct private-key filenames, for example ~/.ssh/id_ed25519_production and ~/.ssh/id_ed25519_staging.
  2. Open ~/.ssh/config.
  3. Add this configuration:
Host production
    HostName prod.example.com
    User deploy
    IdentityFile ~/.ssh/id_ed25519_production
    IdentitiesOnly yes

Host staging
    HostName staging.example.com
    User deploy
    IdentityFile ~/.ssh/id_ed25519_staging
    IdentitiesOnly yes
  1. Save the file.
  2. Run ssh -G production and check that the resolved hostname, user, identityfile, and identitiesonly values match production.
  3. Repeat with ssh -G staging for the staging alias.
  4. Connect with ssh production or ssh staging.

The command uses the alias, while the connection uses the destination, account, and key resolved from its block.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a different account as well as a different key

A key and a username are separate settings. If production requires admin while staging uses deploy, give each alias its own User value even when the key filenames follow the same naming pattern.

Host production
    HostName prod.example.com
    User admin
    IdentityFile ~/.ssh/id_ed25519_production
    IdentitiesOnly yes

Host staging
    HostName staging.example.com
    User deploy
    IdentityFile ~/.ssh/id_ed25519_staging
    IdentitiesOnly yes

Use wildcard patterns for server groups

Host accepts patterns. A wildcard group is useful when many servers share the same account and key policy.

Apply one key to a domain group

Host *.prod.example.com
    User deploy
    IdentityFile ~/.ssh/id_ed25519_production
    IdentitiesOnly yes

This block matches names ending in .prod.example.com. For example, api.prod.example.com and worker.prod.example.com receive the same settings when those names are used as the connection targets.

Understand the pattern characters

  • * matches any sequence of characters.
  • ? matches one character.
  • Comma-separated pattern lists combine alternatives.
  • A pattern can be negated by placing ! before it.

A pattern list containing only negated patterns does not produce a positive match. Include at least one positive pattern when using exclusions.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
New Bee Guitar Cable 10ft Electric Instrument Bass AMP Cord
  • [Low Noise High Fidelity] New bee guitar cord with 22AWG oxygen-free-copper center conductors provides enhanced signal transfer, the PVC insulation material and the high-density stranded copper double shield provide noise rejection for quiet operation
  • [Ideal for Choice] New bee 1/4 straight and right angle metal connectors, ensures a secure connection in any instrument, pedal, or amp jack. L shape great fro on stage guitar stands
  • [Wide Compatible] Suitable for mixer, power amplifier, microphone, effector, decoder, equalizer, electoronic organ, electric guitar, electric bass and other electronic equipments. Widely used in the installation of smart home, karaoke speaker, professional stage audio, etc
  • [Easy Disassemble] Making incidental repairs quick and easy! Very nice, flexible cable
  • [Dont Hesite to Order] 24 hours customer services and professional technology team are standing by

Combine a group with an exception

Use a positive group pattern and then exclude a host that must use another policy. The exact arrangement should be tested with ssh -G, because the effective result depends on which selectors match the name you supply.

Host *.example.com !legacy.example.com
    User deploy
    IdentityFile ~/.ssh/id_ed25519_standard
    IdentitiesOnly yes

The positive pattern identifies the group; the negated pattern prevents the named exception from matching that group. Give the exception its own explicit block or another matching rule.

Choose aliases when names are unstable

Use an explicit alias when you want a short, stable command or when the same server is reached through different names. A wildcard is best for a genuinely uniform group. Do not force a wildcard onto hosts that have different keys or accounts merely because their DNS names look similar.

Why multiple IdentityFile lines can surprise you

Multiple IdentityFile directives are additive: “Multiple IdentityFile directives will add to the list of identities tried.” They do not replace one another the way a single-valued option would.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Host recovery
    HostName recovery.example.com
    User root
    IdentityFile ~/.ssh/id_ed25519_recovery_primary
    IdentityFile ~/.ssh/id_ed25519_recovery_backup

In this example, both identities are candidates and are tried in sequence. That is appropriate only when deliberately trying a sequence is part of the connection design. For strict per-server selection, use one IdentityFile together with IdentitiesOnly yes.

Parameterized key paths

IdentityFile supports ~ and documented tokens that are expanded for the connection. Tokenized paths can reduce repetition when a predictable naming scheme covers many hosts.

Token Meaning Example use
%d Local home directory %d/.ssh/keys/%h
%h Remote host ~/.ssh/keys/%h
%n Original host name supplied to SSH ~/.ssh/keys/%n
%p Port ~/.ssh/keys/%h-%p
%r Remote user ~/.ssh/keys/%r@%h
%u Local user ~/.ssh/keys/%u/%h

%h represents the resolved remote host, whereas %n preserves the original name you supplied, such as an alias. That distinction is useful when aliases map several names to one destination. Explicit filenames remain easier to audit when servers have different trust boundaries, so use tokens only when the naming rule is clear and dependable.

Rank #4
BSIMB 15.6" Digital Family Calendar & Chore Chart, No Subscription Fees
  • Digital Family Calendar: BSIMB digital calendar wall touch screen puts an end to your chaotic life. Designed for busy families, the digital calendar keeps you organised and in step with your family. Assign different colours and tasks to each family member to make family scheduling easier and more intuitive.
  • Easy Setup and Auto-Sync: Plug in, connect to WLAN and auto-syn your electric calendar in minutes. Digital wall calendar compatible with Google calendar, iCloud calendar, Outlook calendar, Cozi calendar and Yahoo calendar. Add events directly on the device or through the ecalendar app. Electronic calendar stays current whether you're at work or traveling—no manual updates needed.
  • All-In-One Home Assistant: Smart calendar builds habits and encourage independence with personalised chores. Display daily meal plan to reduce the daily ‘What's for dinner?’ stress. To-do lists keep track of everything you want. Turning chores into valuable achievements with stars and rewards, making life easier and more enjoyable.
  • Manage Anytime Anywhere: BSIMB electronic calendar for family stay connected with the powerful ecalendar app. Access and manage your digital wall calendar anytime, anywhere with free features like stars & rewards, photo screensaver, chore charts, meals plans, to-do lists etc. Electronic planner paid features like Magic Import tailor advanced tools for users with special needs.
  • Stay in Touch with Loved Ones: Share photos or videos from your phone to electronic wall calendar with app, instantly update your life moment, and keep in touch with your family, especially those who are far away; you can also surprise your smart calendar digital wall by sending him a wish card; it's also great for keeping tech-illiterate elders in touch with the youngsters.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose between aliases, wildcard groups, and -i

The command-line option -i can select a key for a single connection, but it is easy to mistype and does not encode the account or destination. The configuration-file approaches make the policy repeatable.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Setup Repeatability Wrong-key risk Auditing ssh-agent behavior Portability
Explicit aliases in ~/.ssh/config High after one-time setup Low when each block has one key and IdentitiesOnly yes Easy to review in one file Unrelated agent identities are excluded when IdentitiesOnly yes is set Requires copying the key files and config to another machine
Wildcard Host groups High for uniform server groups Depends on accurate patterns and exceptions Group policy is visible, but exceptions must be checked Follows the group’s IdentitiesOnly setting Portable when the pattern and key naming scheme are preserved
Command-line -i Low; repeated for each command Higher because the key is selected manually each time Selection is scattered across shell history and scripts Does not by itself prevent other agent identities from being offered Works without a config file on the current machine

For regular administration, explicit aliases are the clearest default. Use wildcard groups only where the same identity policy truly applies. Reserve -i for one-off connections, diagnostics, or environments where you cannot install a per-user configuration.

Verify the effective configuration before connecting

ssh -G production asks the client to print the effective configuration for the alias without opening an interactive session. Check these resolved fields:

  • hostname must be the intended DNS name or address.
  • user must be the intended remote account.
  • identityfile must contain the expected private-key path.
  • identitiesonly should be enabled when strict per-server selection is required.

Run the same check for every alias that matters, including names covered by wildcard rules. This catches a typo in an alias, an unexpected pattern match, or a key path that points to the wrong file before authentication begins.

Host-key identity is separate from user-key identity

HostKeyAlias controls the name used for host-key lookup and storage. It is useful in situations such as tunneling or multiple servers running on one host, where the name used for host-key database operations needs to differ from the connection name.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

HostKeyAlias does not choose the private key used to authenticate your user. Keep host-key naming and user-key selection conceptually separate: use IdentityFile for the latter, and change HostKeyAlias only when host-key database naming and validation require it.

Troubleshoot the common failure modes

The wrong key is offered

Inspect the alias with ssh -G alias. If the identityfile value is wrong, correct the matching Host block or the alias you typed. If the path is correct but an agent still supplies other identities, add IdentitiesOnly yes to that block.

The server rejects the connection after several offers

This usually indicates that too many identities were presented before the intended key. Use one IdentityFile and IdentitiesOnly yes for that server, then verify the effective values with ssh -G.

The alias reaches the wrong machine

Check the resolved hostname. Remember that the word after Host is only a selector; HostName is the actual destination. Correct the latter if a DNS name or address was mistyped.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The login account is rejected

Check the resolved user value. A valid private key can still fail when it is offered to the wrong account. Set User explicitly for aliases that do not use your local username.

A wildcard affects an exception

Review every pattern that can match the supplied name, including negated patterns. Give exceptional hosts explicit aliases and inspect each one with ssh -G. A stable alias is often safer than relying on a broad pattern for a host with a different trust boundary.

Two keys are being tried unexpectedly

Search the matching configuration for repeated IdentityFile directives. They add to the identity list rather than replacing earlier entries. Remove the extra directive unless sequential attempts are intentional.

Operational and security practices

  • Use a distinct private key for each server or trust group when separation is required.
  • Name keys so an operator can identify their purpose without opening them.
  • Keep private keys under the SSH directory with restrictive permissions.
  • Use IdentitiesOnly yes for aliases where an unrelated agent key could cause rejection or policy confusion.
  • Review ssh -G output after changing a hostname, username, key path, or pattern.
  • Keep the configuration and private keys synchronized when moving to another workstation; the alias alone is not the key.
  • Do not treat HostKeyAlias as an authentication setting. It changes host-key database naming and validation behavior, not the user private key.

A complete, auditable example

The following file separates production, staging, and a production subdomain group while keeping the key choice explicit.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Host production
    HostName prod.example.com
    User deploy
    IdentityFile ~/.ssh/id_ed25519_production
    IdentitiesOnly yes

Host staging
    HostName staging.example.com
    User deploy
    IdentityFile ~/.ssh/id_ed25519_staging
    IdentitiesOnly yes

Host *.prod.example.com
    User deploy
    IdentityFile ~/.ssh/id_ed25519_production
    IdentitiesOnly yes

Use ssh -G production, ssh -G staging, and ssh -G api.prod.example.com to verify each route. Then connect with the alias or matching hostname. If one production subdomain needs a different key, give it an explicit alias and configuration block rather than adding a second production identity to the group.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.