SSH Like A Boss

Hello reader,

Do you need to be a ninja hacker to use secure shells to interact with servers? Absolutely not. In fact many newcomers to webdevelopment are switching from ftp ssh plugins to terminal emulation like putty on Windows, or just firing up the bash shell on Mac or Linux.

Generally a Linux server has a file structure that acts more or less like a library filesystem, expanding from the root, to the branches associated with each function in that server tree.

On an ordinary Linux desktop the tree will include user files, folders, and code to use the functions of those files closer to the root, and outward towards the actual desktop.

From root on the desktop we might have bin, boot, dev, home, lib, tmp, usr, and so on.

Screenshot from 2016-01-14 14:42:37
The folders included in the root directory

The terminal displays only the files and folders in the relevant directory tree…

Terminal displaying relevant directories
Terminal displaying relevant directories
Terminal displaying the files within a directory
Terminal displaying the files within a directory.

Navigating the command line

To ssh into a remote machine you need the clientside IP, a login credential or “key” and the protocol you will be using may have other specific requirements; (some tunneling programs require ad hoc permissions)

To see the list of at level files and folders once you have terminal access on the remote machine simply type: ls

To change directories you can either use the cd command followed by the location or by the “slashes” i.e. cd\ vs cd/ can move through directories on some systems. cd Downloads is quicker than searching for the downloads folder but you can of course navigate to it by using the slashes and other directories in the tree. (cd/usr/Brian/Downloads)

Generally to make changes to a directory you need to be able to move files, mv, remove files, rm, copy files, cp, make directories, mkdir, find files, find, search for text within a file, grep, change permissions on files and programs, chmod, and oftener than not executing programs via command line is simply a matter of typing the name of the program; i.e. skype fires up skype etc.

 

Permissions by the numbers:

7 = Read + Write + Execute
6 = Read + Write
5 = Read + Execute
4 = Read
3 = Write + Execute
2 = Write
1 = Execute

Ergo 777 = read write and execute where as 744 = read only.

(This is why some upload folders deny access to their admins and why some admins “freak out” until you explain how to change that in the backend.)

So far we’ve demonstrated the simplest permissions and commands, to really get more out of the terminal or really even the server you are accessing via ssh you need a specific goal. In the following scenario you are accessing a Linux server and installing WordPress via ssh. A more in depth tutorial is included at the end of this article but the basic overview is essentially:

  1. Opening terminal or emulator and adding the ssh command as well as the credentials and login key.
  2. Changing directory to either the user bin, or the clients chosen location using the cd command.
  3. Adding the wordpress program, on Ubuntu based systems it’s done by typing sudo apt-get install wordpress after which you’ll be prompted for your password.
  4. Checking to see if the program is configured as well as installed correctly. <— #This isn’t something we’ve covered. To do it you really only need to go into the program itself by typing wordpress, after which you may need to assign nameservers and other non-local options depending on how the site is hosted.

Many other commands are listed in resources for ssh on the web, some of those resources include:

The Windows SSH program putty is available here: Putty

Another “Way more thorough” tutorial for installing WordPress via command line is available here.

Open ssh vs Putty – last night putty connected, so today we’ll talk about putty. I figured a few tips for new admins might be useful and a bit about installing tools this way on a server – because some of you are doing this even as I write this article.

Generally on new servers you are logged in as root and therefore you are probably scratching your heads over things like “sudo” triggering error messages. As Root you simply omit the sudo from any command – a command like sudo apt-get install git – becomes apt-install git.  If you are doing something that requires a ton of copy pasting you can add a user account and change a visudo file to just copy paste as the codes are written. (same logic applies to rpm etc)

Adding a user and granting sudo privileges

type useradd then the name eg:

useradd crazyexamplenonsensename
passwd crazyexamplenonsensename (This prompts a password request which asks you to add the password for this user)

Now as root type: visudo

You should get an output like below: (I added the user to line 5)

## User Aliases
## These aren't often necessary, as you can use regular groups
## (ie, from files, LDAP, NIS, etc) in this file - just use %groupname
## rather than USERALIAS
# User_Alias ADMINS = crazyexamplenonsensename,othername, 
ALL=(ALL) ALL
ics ALL=(ALL) ALL 
csm ALL=(ALL) ALL 
coa ALL=(ALL) ALL
To save in Vim press esc then type :x and press enter

Alternatively you can add nano (apt-get nano or rpm install nano -i) and add the user this way:

nano /etc/sudoers

then add:

user    ALL=(ALL:ALL) ALL

On a Ubuntu system or any with preset sudoers file, after doing:

type useradd then the name eg:

useradd crazyexamplenonsensename
passwd crazyexamplenonsensename (This prompts a password request which asks you to add the password for this user)

You can simply:

usermod -aG sudo username <—Of course this should be the name you are adding.>

Other fun things to know

Once you have done change directory or cd to get to a directory (like cd /opt) any followup cd command must exclude the directory you are in.

Example: We just typed: cd /example *But to get to example/bin from example – we only type: cd /bin because we are already in example.

./install – only works in the target directory – so if you exit and come back you do really need to remember which directory you were in when you left.

Apply this logic when installing from a git repo – “I just cloned in and told my terminal to download the files – which means I now must go to the directory it went to – to configure and install.”

In Conclusion

Once you’ve done it correctly it’s easy to remember, but it can actually be tricky at first. Many times you’ll be looking at the installation instructions and seeing sudo apt-get install git (even after you already have done so) so omit that part and move on – likewise if you are root – be root but then omit sudo from the commands.

Vim requires esc then :x then enter to save and exit.
Spread the love

706 thoughts on “SSH Like A Boss”

  1. Hello, i read your blog from time to time and i own a similar one and i was just wondering if you get a lot of spam feedback? If so how do you stop it, any plugin or anything you can suggest? I get so much lately it’s driving me crazy so any support is very much appreciated.

  2. Do you have a spam problem on this site; I also am a blogger, and I was curious about your situation; we have developed some nice procedures and we are looking to exchange techniques with others, please shoot me an e-mail if interested.

  3. Hey just wanted to give you a quick heads up. The text in your post seem to be running off the screen in Firefox. I’m not sure if this is a formatting issue or something to do with browser compatibility but I figured I’d post to let you know. The design look great though! Hope you get the issue resolved soon. Many thanks

  4. Hi there would you mind stating which blog platform you’re working with? I’m looking to start my own blog soon but I’m having a difficult time selecting between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design seems different then most blogs and I’m looking for something completely unique. P.S My apologies for being off-topic but I had to ask!

Leave a Reply

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