How can I `git push` to GitHub on Ubuntu using a single command, without having to type in my email each time? - linux

I want to git push on Ubuntu via a single command, such as:
echo -e "email\ntoken" | git push origin branchName
git push origin branchName && email && token
But after the command I have to put in my email:

How to use ssh keys to easily push to / pull from GitHub
You need to:
Configure your remote to use the ssh version of the GitHub repo address instead of the http version.
Generate a public/private ssh key pair, and add the public key to your GitHub account manually via your web browser.
Details
Configure your remote to use the ssh version of the GitHub repo address instead of the http version. Ex:
For this repo of mine: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world, use this ssh URL: git#github.com:ElectricRCAircraftGuy/eRCaGuy_hello_world.git instead of this HTTPS one: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world.git:
# View your current remote servers and their URLs
git remote -v
# Set your `origin` remote server to use the ssh URL instead
# of the HTTPS one
git remote set-url origin https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world.git
Generate a public/private ssh key pair, and add the public key to your GitHub account manually via your web browser.
See my full notes on ssh stuff here: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/tree/master/home/.ssh
# generate a public/private ssh key pair
ssh-keygen -t ed25519 -C "your_email#example.com"
# Ensure the ssh-agent is running (this starts the `ssh-agent`)
eval "$(ssh-agent -s)"
# Add your private key to it; update the path to your private key below, as
# required, based on what path you interactively selected above when
# generating the key
ssh-add ~/.ssh/id_ed25519
# Verify what keys have been added to the ssh-agent by listing
# (`-l`) currently-added keys.
# A. If you see "Could not open a connection to your authentication agent.",
# it means the `ssh-agent` has not been started yet, so you must start it
# with `eval "$(ssh-agent -s)"`.
# B. If you see "The agent has no identities.", it means the ssh-agent is
# running but you haven't added any ssh keys to it, so run `ssh-add
# path/to/private_key` to add a key to the agent.
ssh-add -l
Now log into github in a web browser and click on your profile image --> Settings --> SSH and GPG keys (on left) --> New SSH key --> copy and paste the contents of your .pub key file (ex: run cat ~/.ssh/id_ed25519.pub on your Ubuntu machine to read the public key--adjust that path as necessary if you used a different file name) into GitHub here --> click "Add SSH key".
Now, whenever you type git push it automatically works, using your ssh key.
References
My full ssh notes: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/tree/master/home/.ssh

You can provide the username once as part of the https git remote address.
First run git remote -vv to get the full, current remote URL.
Then to change your existing remote, you can do a command like:
git remote set-url origin https://yourname#github.com/yourname/yourrepo.git
where the new part is yourname# (substitute your github user name) and the rest of the URL should be the same as shown in git remote -vv

Related

Gitalb SSH into ubuntu server

in my local dev windows machine I generated shh key using PuttyGen. I also pasted public key into gitlab ssh keys section so now are linked.
I can correcty use ssh now from my windows manchine but I want to use it also in my production server which uses ubuntu.
For example I wan to ssh clone a repository into my ubuntu machine, where and how should I add the ssh keys to my ubuntu server so I can link it with gitlab.
I used this tutorial to generate ssh keys in windows with Putty.
https://ourcodeworld.com/articles/read/1421/how-to-create-a-ssh-key-to-work-with-github-and-gitlab-using-puttygen-in-windows-10
How should I add the ssh keys to my Ubuntu server so I can link it with GitLab?
Ideally, you would create a dedicated key pair on your Ubuntu server, in order to be able to clone GitLab repositories.
On that Ubuntu server, go to your $HOME folder of your account 'user' (replace user by the actual user name you are login with on that server).
cd
# assuming you do not have a default key yet:
ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa
# copy ~/.ssh/id_rsa.pub to your GitLab account
# Check the key is working
ssh -Tv git#gitlab.com
# Use your key to clone repositories
git clone git#gitlab.com:me/myRepository

git#gitlab.com: Permission denied (publickey). fatal: Could not read from remote repository

I am using macOS Catalina. I already have a repository on GitLab and an SSH-key assigned. Now I want to create another repository from the terminal. I do the following:
git config user.name my_name
git config user.email my_email
git init
Then I get this:
Initialized empty Git repository in directory
So far so good.
git remote add origin git#gitlab.com:my_name/repo.git
git add .
git commit -m 'commit'
git push -u origin master
Then I get the following error:
git#gitlab.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Then I go to the repository I already had and try to push there, everything works so I guess I don't have a problem with SSH-key. I know this is a very common question on the internet but none of the answers solved my problem.
First, you should get "Initialized empty Git repository in directory" only after a git init ., not after a git remote add origin ...
Second, with GitLab, you can push to create a new project, as illustrated in this MR, starting with GitLab 10.5 (Q1 2018)
Third, if the error persists, then the key is somehow at fault.
Test it with:
ssh -Tv git#gitlab.com
Also
git -c core.sshCommand="ssh -v" push -u origin master
To generate a valid key:
ssh-keygen -t rsa -P "" -m PEM
And register your new id_rsa.pub to your GitLab profile.
I tried all the above mentioned solutions but none of it worked. I then read the logs and found that it is looking for the key in a specific folder and I created the key and added it to my Gitlab profile too. Then it started working.
Git authentication issue can be solved by reading the logs of the git and creating appropriate SSH keys under appropriate folders.
Steps
Run the following command and it will try to push the code and if it not successful then it will display where the error is
git -c core.sshCommand="ssh -v" push -u origin master
Now, we can generate a new SSH key and the following command will generate a key in the working folder.
ssh-keygen -t rsa -P "" -m PEM
It will ask for key name, you can give id_rsa as the key name or any name which the Bash displays as "Trying private key: c:/Users/Dell/.ssh/".
Once the key is generated in bash, your working directory will have the key.
While running the command in step1, you will see that the folder in which it is looking for a private key. In my case it is "c:/Users/Dell/.ssh/id_rsa"
We should put the generated keys from the working folder into this folder
 
We should also make sure that we add our SSH Key to the Gitlab account.
Click on your Gitlab account MyProfile and select preferences.
Click to see how to add SSH to your Gitlab account
 
Click the SSH keys menu, open the generated key file using notepad and copy the content of the key from notepad and paste it in the SSH key text editor and save it .
Click to see how to add SSH Key to your Gitlab account
Again, run the following command and check now. The code will be pushed.
git -c core.sshCommand="ssh -v" push -u origin master
the code will be pushed.
The same issue happened.
I used HTTPS instead of SSH
(I followed the instruction steps after creating repo in GitLab but that cause a Permission issue. It's is because of ssh pub key to upload)
These steps work without using SSH
Create a repository/project in GitLab
I removed .git (that caused permission issue in previous. For to start with fresh)
git config --global user.name "user_name"
git config --global user.email "user.email#gmail.com"
git init .
git remote add origin https://gitlab.com/user.account/user_project.git
git add . and git commit -m "initial commit"
git push -u origin master
It will ask username and password. Then fixed.

How do I use SSH in a Jenkins pipeline?

I have some Jenkins jobs defined using a Jenkins Pipeline Model Definition, which builds NPM projects. I use Docker containers to build these projects (using a common image with
just Node.js + npm + yarn).
The results of the builds are contained in the dist/ folder that I zipped using a zip pipeline command.
I want to copy this ZIP file to another server using SSH/SCP (with private key authentication). My private key is added to the Jenkins environment (credentials manager), but when I use Docker containers, an SSH connection cannot be established.
I tried to add agent { label 'master' } to use the master Jenkins node for file transfer, but it seems to create a clean workspace with new Git fetch, and without my built files.
After I tried the SSH Agent Plugin, I have this output:
Identity added: /srv/jenkins3/workspace/myjob-TFD#tmp/private_key_370451445598243031.key (rsa w/o comment)
[ssh-agent] Started.
[myjob-TFD] Running shell script
+ scp -r dist test#myremotehost:/var/www/xxx
$ docker exec bfda17664965b14281eef8670b34f83e0ff60218b04cfa56ba3c0ab23d94d035 env SSH_AGENT_PID=1424 SSH_AUTH_SOCK=/tmp/ssh-k658r0O76Yqb/agent.1419 ssh-agent -k
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
echo Agent pid 1424 killed;
[ssh-agent] Stopped.
Host key verification failed.
lost connection
How do I add a remote host as authorized?
I had a similar issue. I did not use the label 'master', and I identified that the file transfer works across slaves when I do it like this:
Step 1 - create SSH keys in a remote host server, include the key to authorized_keys
Step 2 - Create credential using SSH keys in Jenkins, use the private key from the remote host
Use the SSH agent plugin:
stage ('Deploy') {
steps{
sshagent(credentials : ['use-the-id-from-credential-generated-by-jenkins']) {
sh 'ssh -o StrictHostKeyChecking=no user#hostname.com uptime'
sh 'ssh -v user#hostname.com'
sh 'scp ./source/filename user#hostname.com:/remotehost/target'
}
}
}
Use the SSH agent plugin:
SSH Agent Plugin
SSH Agent Plugin
When using this plugin you can use the global credentials.
To add a remote host to known hosts and hopefully cope with your error try to manually ssh from the Jenkins host to the target host as the Jenkins user.
Get on the host where Jenkins is installed. Type
sudo su jenkins
Now use ssh or scp like
ssh username#server
You should be prompted like this:
The authenticity of host 'server (ip)' can't be established.
ECDSA key fingerprint is SHA256:some-weird-string.
Are you sure you want to continue connecting (yes/no)?
Type yes. The server will be permanently added as a known host. Don't even bother passing a password, just Ctrl + C and try running a Jenkins job.
Like #haschibaschi recommends, I also use the ssh-agent plugin. I have a need to use my personal UID credentials on the remote machine, because it doesn't have any UID Jenkins account. The code looks like this (using, for example, my personal UID="myuid" and remote server hostname="non_jenkins_svr":
sshagent(['e4fbd939-914a-41ed-92d9-8eededfb9243']) {
// 'myuid#' required for scp (this is from UID jenkins to UID myuid)
sh "scp $WORKSPACE/example.txt myuid#non_jenkins_svr:${dest_dir}"
}
The ID e4fbd939-914a-41ed-92d9-8eededfb9243 was generated by the Jenkins credentials manager after I created a global domain credentials entry.
After creating the credentials entry, the ID is found under the "ID" column on the credentials page. When creating the entry, I selected type 'SSH Username with private key' ('Kind' field), and copied the RSA private key I had created for this purpose under the myuid account on host non_jenkins_svr without a passphrase.

Password for GitLab

I've installed GitLab per https://github.com/gitlabhq/gitlab-recipes/tree/master/install/centos.
The instructions state to add user "git"
adduser --system --shell /sbin/nologin --comment 'GitLab' --create-home --home-dir /home/git/ git
All seemed to work. I then added a project on the GitLab server, and it gave instructions to push to it:
cd existing_git_repo
git remote add origin git#mysite.com:root/bidjunction.git
git push -u origin master
I then went to my client to push to the git server.
[Michael#devserver bidjunction]$ git push -u origin master
The authenticity of host 'mysite.com (123.456.789.01)' can't be established.
RSA key fingerprint is cd:32:3c:5a:4e:33:44:11:df:ee:3s:4b:3a:c2:a4:c2.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'mysite.com,123.456.789.01' (RSA) to the list of known hosts.
Address 123.456.789.01 maps to ve6.phpwebhosting.com, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
git#mysite.com's password:
Is there a password I should have set? Probably not, and instructions don't address.
Reading System ask password when push project to github, it appears it might be related to SSH.
GitLab provides the following instructions to add a SSH key. I followed them, and first added the key to my general user's home on the server. No change. Then tried logging on a root, and added a key to git's home. Still none, but I am thinking that the client already has my general user's key, thus is not pulling the new git key.
EDIT. Please confirm that I should add these keys to the GitLab server, and not my Linux client.
Any and all help would be very much appreciated.
SSH Keys
SSH key allows you to establish a secure connection between your computer and GitLab
Before generating an SSH key, check if your system already has one by running cat ~/.ssh/id_rsa.pub If your see a long string starting with ssh-rsa or ssh-dsa, you can skip the ssh-keygen step.
To generate a new SSH key just open your terminal and use code below. The ssh-keygen command prompts you for a location and filename to store the key pair and for a password. When prompted for the location and filename you can press enter to use the default. It is a best practice to use a password for an SSH key but it is not required and you can skip creating a password by pressing enter. Note that the password you choose here can't be altered or retrieved.
ssh-keygen -t rsa -C "$your_email"
Use the code below to show your public key.
cat ~/.ssh/id_rsa.pub
Copy-paste the key to the 'My SSH Keys' section under the 'SSH' tab in your user profile. Please copy the complete key starting with ssh- and ending with your username and host.
EDIT 2
Looks like I was confused, and used GitLab's server id_rsa.pub. Guess that doesn't make any sense! I've since corrected it, but now I get this error:
[Michael#devserver ~]$ ssh git#mysite.com
Address 123.456.789.01 maps to ve6.phpwebhosting.com, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
PTY allocation request failed on channel 0
This account is currently not available.
Connection to mysite.com closed.
[Michael#devserver ~]$
Seems like a SELinux permission issue. Steps to follow:
restorecon -R -v /home/git/.ssh
usermod -s /bin/bash git
Edit your home machine user's .ssh/config to something like:
Host mysite.com
User gitlab_username
Hostname mysite.com
PreferredAuthentications publickey
IdentityFile /home/user/.ssh/id_rsa
Try connecting to the gitlab server ssh -T git#mysite.com. You should see a message welcoming you.
I added it to the installation guide. I have a merge request ready with several enhancements. I will merge it when gitlab 6.8 is released.
I believe you need to set up your global config username and email
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe#example.com
Check if the git user has a no login shell as you specified. Changing that to bash as suggested fixed my problem (usermod -s /bin/bash git).

how to append a string after a command?

Each time I run a command like this
ssh user#myhost.com
I've to type in the ssh password.
Each time I run a command like this
git push origin master
(pushing code to github)
I've to type in the github password.
Is there any bash thing to write these password inline with the command?
I mean something like
git push origin master < 'mypass'
or
git push origin master | 'mypass'
Use public key authentication for git hub.
For ssh-ing to the host add your key to the host's authorized_keys file(default location is the /home/<user>/.ssh directory of the user you are logging in as).
If the daemon is configured correctly it will simple use your key to authenticate before it prompts for a password.

Resources