I always forget how to do parts of this and have to look up the info to generate a ssh key usually, so maybe blogging it all will help me remember and you get started on building the command line into your workflow.
I’m going to walk you through a quick install of git on a server. I promise this is seriously easy and will save you so much time!
I do this on client servers so I can clone repos out of Github directly from SSH instead of uploading files. You can do a whole lot more of course, but this is probably the most common task I find myself doing.
I’ll cover other command line related topics in upcoming posts.
Most of my clients that I am using git on the server or simply running more complex sites I make sure to get on a VPS. That way I have full control and there are few, if any at all, limitations.
I like CentOS because it’s what I know and less obscure for anyone else who might need to manage the server, so you’ll see me reference something specific to CentOS and Fedora systems called ‘yum’.
yum is a package manager that you can install things from, similar to the WordPress repo!
Non CentOs/Fedora users will need to use apt-get or something else probably above my pay grade.
Issuing Install Access
The first thing you need to do is make sure you have su access on the server, this is so you don’t have to log into root directly. If you try to install anything from a lower user you’ll just get denied.
This is easy to do in cPanel/WHM setups by :
- Logging into WHM
- Search ‘users’ and click ‘Manage Wheel Group Users’
- Find your username, select it, click ‘Add to Group’
If you aren’t running a control panel, you can SSH in via the ‘root’ user and use the following command to add a user to the wheel. If you don’t know how to SSH in, I’ll tell you how in a moment.
usermod -G wheel username
Great, now you have su access and you’ll never need to do that again. Technically you could install git at this point, but you should logout of root and do it from your user to make sure you’re comfortable with using su or sudo instead.
If you need to issue access to anyone other than yourself that you don’t want having the root password you should set the wheel user group to have sudo ability. I like to use sudo regardless but this will work for the sake of moving along.
SSH Into The Server
The next few parts are seriously easy. Ready?
Open terminal and type:
ssh myserverhostname.com -l username
Input your password and you’re in.
Again, incredibly easy…
Input the root pass, not yours and run the next command:
yum install git-core
Once you’re done you need to type exit to leave root since we want to generate our SSH key from our user.
Generate the SSH Key
Since you’re back in our main user you should also be in its directory. Each user has its own .ssh directory where keys are stored.
I think by default cPanel creates a ssh key for users, at least that’s what I’ve seen since it usually asks me to overwrite when I generate my own. You’re welcome to use that one, or generate a fresh key like so:
ssh-keygen -t rsa -C “email@example.com”
Be sure to enter a pass to go along with your key.
Unfortunately at this point we have to open vi to copy the contents of our key.
Once you have that copied you’ll need to type this to exit vi
Now copy everything into your github profile section under SSH Keys > Add SSH Key.
Make sure you didn’t copy over any spaces!!
Navigate to a random folder (we will delete this in a moment)
mkdir testing to create the directory
cd testing to enter the directory
Now go to any github project you like and we’re going to clone it into the testing folder. I used jquery for my example screenshot. You’ll want to click SSH and then copy the url.
Now lets use that with the command ‘git clone’
git clone git clone firstname.lastname@example.org:jquery/jquery.git
It will ask for your passphrase on your ssh key, enter it and the repo will get cloned into your directory!
To remove your test directory:
cd .. to back out of the directory
rm -rf testing/ to delete the directory
That’s it! Hopefully I didn’t miss anything. I’ll have some more “command line 101” like stuff coming up so you can truly get a feel for how SSH/CLI can be your best friend.