Git is offered in Ubuntu’s default bundle repositories on each 22.04 LTS and 24.04, so set up is quick. The extra concerned half is configuring SSH key authentication for GitHub or GitLab, which is what this information covers in full. You’ll additionally discover a part on managing a number of Git identities on a single server, which comes…
Conditions
- A Linux VPS or cloud server operating Ubuntu 22.04 LTS or Ubuntu 24.04
- SSH entry with sudo privileges
- A GitHub or GitLab account (for the SSH key configuration steps)
Each Ubuntu 22.04 LTS and 24.04 ship with OpenSSH shopper pre-installed. No further instruments are required earlier than starting.
Not arrange on a VPS but? InMotion Internet hosting’s Cloud VPS contains Ubuntu 22.04 LTS as a supported OS. The Managed VPS helps Ubuntu throughout all plan tiers.
Step 1: Replace Bundle Lists
Earlier than putting in any bundle, sync your apt bundle index to make sure you’re pulling the most recent accessible model.
sudo apt replace
Step 2: Set up Git
sudo apt set up git -y
The -y flag confirms the set up robotically. On Ubuntu 22.04, this installs Git 2.34.x. On 24.04, you’ll get Git 2.43.x. To confirm the put in model:
git --model
Anticipated output will look much like: git model 2.43.0
Step 3: Configure Your Git Identification
Git requires a reputation and electronic mail handle hooked up to each commit. Set these globally for the server person you’ll be working with.
git config --world person.title "Your Identify"
git config --world person.electronic mail "[email protected]"
Confirm the configuration:
git config --checklist
Step 4: Set Up SSH Key Authentication
Cloning and pushing to distant repositories over HTTPS requires a password or token on each operation. SSH keys authenticate as soon as and by no means immediate once more. That is the usual strategy for server-to-repository authentication.
4a. Generate the SSH key pair
ssh-keygen -t ed25519 -C "[email protected]"
The -t ed25519 flag specifies the Ed25519 algorithm, which is the trendy beneficial key kind. When prompted for a file location, press Enter to simply accept the default (~/.ssh/id_ed25519). Optionally set a passphrase.
In case your distant service requires RSA (older GitLab cases, for instance), use:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
4b. Show your public key
cat ~/.ssh/id_ed25519.pub
Copy the complete output, together with the ssh-ed25519 prefix and the remark on the finish.
4c. Add the important thing to GitHub or GitLab
On GitHub: Settings > SSH and GPG keys > New SSH key. Paste your public key and provides it a descriptive title (for instance, ‘InMotion VPS manufacturing’).
On GitLab: Person Settings > SSH Keys. Paste the important thing, give it a title, and set an expiration date in case your safety coverage requires one.
4d. Check the connection
Anticipated output: Hello username! You’ve efficiently authenticated, however GitHub doesn’t present shell entry.
Anticipated output: Welcome to GitLab, @username!
Step 5: Clone a Repository
With SSH authentication configured, cloning makes use of the SSH URL slightly than HTTPS.
git clone [email protected]:yourusername/your-repo.git
The repository will clone right into a listing named after the repo. Navigate into it and ensure the distant configuration:
cd your-repo && git distant -v
Managing A number of Git Identities on One Server
Improvement VPS environments incessantly must authenticate as totally different customers for various repositories. A developer account for private initiatives, a CI deploy key for a shopper’s repository, a separate identification for work. The SSH config file handles this cleanly.
Create a per-host SSH config
nano ~/.ssh/config
Add a block for every host identification:
Host github-private HostName github.com Person git IdentityFile ~/.ssh/id_ed25519Host github-shopper HostName github.com Person git IdentityFile ~/.ssh/id_ed25519_client
Generate the second key pair with a distinct filename:
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_client -C "[email protected]"
Add id_ed25519_client.pub to the shopper’s GitHub or GitLab account underneath their deploy keys.
Use the alias when cloning
git clone git@github-shopper:clientorg/their-repo.git
SSH resolves github-client to github.com and makes use of the required key file. This strategy works with none battle between identities, even on the identical bodily server.
Setting a Default Department Identify
Git’s default department title modified from ‘grasp’ to ‘principal’ in newer configurations, however repositories created on older setups should still use ‘grasp’. To align your server’s Git habits together with your group’s conference, set the default department title globally:
git config --world init.defaultBranch principal
Non-obligatory: Set up a Newer Model by way of PPA
Ubuntu’s default repositories might not embrace absolutely the newest Git launch. In case you want a more moderen model for particular options, the Git Maintainers PPA gives present builds for Ubuntu LTS releases:
sudo add-apt-repository ppa:git-core/ppasudo apt updatesudo apt set up git
That is elective for many use instances. The model in Ubuntu’s default repos is steady and ample for the overwhelming majority of workflows.
Associated information: The right way to Setup a VPS Server covers the complete VPS setup workflow from provisioning by deployment.
| Run Git on production-ready infrastructure. InMotion’s Cloud VPS contains Ubuntu 22.04 LTS with root SSH entry, high-availability structure, and no-nonsense pricing. See plans at inmotionhosting.com/cloud-vps. |
