Archive for the ‘ssh’ Category

Unix and SSH Files From Scratch

Friday, May 6th, 2011

Again I am assuming some Technical/Programming experience. No Unix experience except for my previous post. I am going to cover some default files. As well as how to make you own bash file. I am going to try and explain this through a tutorial on how to add a welcome message to your login.

The first thing you need to do is decide on a message. You could just have a one line message like “Ola welcome to the server” which would be fine.

On the other hand if you really wana go at it. You could get some ascii art or text. Personally I am far from an ascii artist but I have ascii every where. There are a few websites that have ascii fonts. All you have to do is type in what you want and it will write it in ascii for you. http://www.network-science.de/ascii/ was the last site I tried. There are loads out there.

If you have administrative rights on your box creating your welcome msg is easy. If you dont have admin rights thats fine, you can use Option 2  which is also easy.

If you are unsure if you have admin access try and open an editor with admin access if it lets you in, BINGO your admin. Type in sudo pico enter your password when prompted if it lets into the pico editor then you have admin.

[shell]sudo pico[/shell]



Option 1(if you are admin)

There is a file named motd. It is just a text file without the txt file extension. Anything saved in the file will be displayed when you log in.

Step 1 Navigate to your root of your system

[shell] cd / [/shell]

Step 2 Navigate to the etc folder

[shell] cd etc [/shell]

Step 3 Edit the motd file with admin access

[shell] sudo pico motd [/shell]

Step 4 Type or copy/past your welcome msg in to your editor

Step 5 Save and exit

Now if you log off the server and log in again you should see your msg.


Option 2(if you are on a shared host or if you dont have admin)

So you dont have admin access and you want to trigger something at login. Well what you do is save the msg in a text file then prompt it to be displayed. There is  file that runs every time you log in named .bash_profile. What we will do is create a bash file that when run will display the text from the file. Now this is a bit of a round about way of doing it but it helps cover both bash files and the .bash_profile.

Anything you want to run at login can be added to the .bash_profile file.

A bash file is a Unix file that runs a group of commands. It has a language behind it that contains most of what you would expect if statements, loops, etc. Bash stands for Bourne Again Shell. you can use it to make your server practically do anything.

Step 1 Navigate to your home directory. This is the directory u start in when u log in.

Step 2 Create a new text file called login.txt

[shell]touch login.txt[/shell]

Step 3 Assuming you have your msg ready or your ascii if not go get it se the links above.

Step 4 Open the the login.txt file in an editor

[shell]pico login.txt[/shell]

Step 5 Past your msg save and exit pico

Step 6 Find the location of you bash

[shell]echo $SHELL[/shell]

Step 7 Copy the return value it is more than likely this:   /bin/bash   but not necessarily

Step 8 Create a new file in you home directory again which is the bash script

[shell]touch login.sh[/shell]

Step 9 Edit the bash script in pico

[shell]pico login.sh[/shell]

Step 10 Type in #! then the bash location that you copied in Step 7

[shell]#!/bin/bash[/shell]

Step 11 Type the command that will display the text

[shell]cat login.txt[/shell]

Step 12 Save and Exit

Step 13 While still in the root set the permisions for bash file

[shell]chmod 700 ./login.sh[/shell]

Step 14 In your home directory edit your .bash_profile so it runs the login bash at login time

[shell]pico .bash_profile[/shell]

Step 15 Add to the bottom of the file the command that runs your bash

[shell]./login.sh[/shell]

Step 16 Save and Exit

Thats all there is. Next time you login you should see your msg. This covers of the basics if you want to know how to write complex scripts there are loads of guides and tutorials to help you on your way out on the interwebs,

Unix and SSH Commands From Scratch

Tuesday, April 19th, 2011

In my last post I discussed node.js. This can be installed on a server. Originally I wanted to talk through how I installed Node.js and what plugins I have come to use. I took a look around and found a few ppl struggling with ssh. I figured best to cover some of the basics of server based commands first. I expect any one reading this have some Technical/Programming experience.

I am going to assume you have a sever and are working from a Unix based machine(Linux or OSX).

Accessing your server:

[shell]ssh usr@host[/shell]

usr: would be your user name to the server for the sake if this example lets say your user name is peter

host: is your server url/ip lets say for this example its Watson.com

so in your command window type in your server details press enter:

[shell]#these are fake details use your user name and server in their place
ssh peter@watson.com[/shell]

Enter you password and you should be in.

Basic Commands:

Here is a list of basic commands to help you move around the folders and manipulate the files.


[shell]
#this is how you comment if you where to write a script file
ls
[/shell]

Type that and it will list all the files and folders


[shell]ls -a[/shell]

That will show all the hidden files too.


[shell]cd somefolder/[/shell]

Primary navigation command.


[shell]cd ..[/shell]

goes up a folder.


[shell]cd folderName[/shell]

navigates to the folder with the name folderName


[shell]cd ~[/shell]

Takes you to your home directory.


[shell]mkdir newDirName[/shell]

creates a directory


[shell]cp txtFile.txt somefolder/copyOfFile.txt[/shell]

copies file.txt to someFolder and renames the file.


[shell]mv txtFile.txt somefolder/copyOfFile.txt[/shell]

moves file.txt to someFolder and renames the file.


[shell]rm txtFile.txt[/shell]

deletes txtFile.txt


[shell]touch newTextFile.txt[/shell]

touch creates the file with no content


[shell]top[/shell]

Displays resources and process


Downloading

[shell]wget http://www.google.co.uk/index.html[/shell]
wget downloads a file at a location the above line downloads googles index.html page


[shell]git clone https://github.com/joyent/node.git[/shell]
git clone will clone a git repository to your current location


[shell]svn checkout http://as3-youtube-data-api.googlecode.com/svn/trunk/ as3-youtube-data-api-read-only[/shell]

svn checkout checks out a svn repo


Editors
You will edit a lot of text files on server a far bit. So I will list 3 Degrees of editors.

Easy

[shell]pico textFile.txt[/shell]

this one is easy all shortcuts (^ is ctrl) are listed at the bottom but not as powerful as the other two.

Medium

[shell]vi  textFile.txt[/shell]

this is obviously more powerful than pico but to achieve that it uses states when u start you can’t type you need to enable typing. This is just a taster of how to use it. There are entire pages on the web that list all the shortcuts.

i
press i when u are not in edit mode and it will insert the cursor to allow you to type

press ESC then :wq
esc enters command mode w stands for write and q stands for quite so save and exit

press ESC then :q!
quit with out saving

Hard

[shell]vim  textFile.txt[/shell]

best text editor i could write a whole post on it. If you wana use google it.

Compiling Code
In Unix you can tailer a build for your system. so its compiled for you and you alone 🙂 Lets say you downloaded a project from the internet using git or svn.

Complete in order below

cd
to the location of the files

[shell]./configure[/shell]

set settings in the build for your machine its automated it just does it


[shell]make[/shell]

compiles your code

[shell]make check[/shell]

tests the compile

[shell]make install[/shell]

installs your compiled code

[shell]make clean[/shell]

Removes all the compiled files and cleans up for you

Comming soon 🙂 sudo pico /etc/motd