How to Talk to a CentOS server via Command Line

Here are a few commands that I found helpful when I learned to use the command line to connect to the server.

Connect

To connect to the server, type the following line into your command line tool. I’m on a Mac and use the terminal:

ssh username@ip

username is the name of a user that has ssh permission, example: root. ip is the ip of the domain you want to connect to, example: 321.130.21.12

you will then be asked for a password for the user, which you need to provide.

Getting around

To manoeuvre to the right directory, you can use some of the following commands:

  • ls (list files and folders in the current directory)
  • cd (change directory)
    cd .. (change to parent directory)
    cd folder/folder1/folder2/ (go to a directory directly by typing the exact location)
  • use the ~ symbol to get back to the home directory. Example: cd ~

Doing things with files

You can manipulate files and folders by using the following commands.

cp
The cp command makes a copy of a file for you. For example, cp file foo makes an exact copy of the file whose name you entered and names the copy foo, but the first file will still exist with its original name. After you use mv, the original file no longer exists, but after you use cp, that file stays and a new copy is made.

mv
The mv command moves a file to a different location or renames a file. Examples are as follows: mv file foo renames the original file to foo. mv foo ~/Desktop moves the file foo to your desktop directory but does not rename it. You must specify a new filename to rename a file.

rm
Use this command to remove or delete a file in your directory. It does not work on directories that contain files.

mkdir
The mkdir command allows you to create directories. For example, mkdir music creates a music directory.

chmod
The chmod command changes the permissions on the files listed.
Permissions are based on a fairly simple model. You can set permissions for user, group, and world, and you can set whether each can read, write, and/or execute the file. For example, if a file had permission to allow everybody to read but only the user could write, the permissions would read rwxr–r–. To add or remove a permission, you append a + or a – in front of the specific permission. For example, to add the capability for the group to edit in the previous example, you could type chmod g+x file.

Example:

chmod 764 pics.html

owner group other
777 = rwxrwxrwx
755 = rwxr-xr-x
744 = rwxr–r–
644 = rw-r–r–

chown
The chown command allows the user to change the user and group ownerships of a file. For example, chown john file changes the ownership of the file to John.

How To Use MySQL On MAMP Via Terminal

I’m still quite new to the use of command line, so if there is any better way of doing this, please don’t hesitate to let me know in the comments.

To connect to your MAMP MySQL database via command line, first navigate to the correct directory within your MAMP installation using the Terminal (or another SSH client). Example:

 cd /applications/mamp/Library/bin

Then run the command to connect to MySQL (the default user and password are both ‘root’).
Example:

 mysql -u root -p

You should now see this:

mysql>

Update:
Here is a very nice post with some more details:
aschroder.com
And a list of terminal commands and their meaning:
ss64.com/osx/

Update:
To avoid typing the location of MySQL each time you open the terminal, you can set the location by default. Open the ‘bash_profile’ file in the user root:

 pico .bash_profile

Add the location of MySQL in a new line:

 export PATH=$PATH:/Applications/MAMP/Library/bin/mysql

Instructions on how to use the Pico editor here: ss64.com/osx/pico.html

Now you should get to MySQL simply by typing mysql into the terminal.

x


Request a quote

I will get back to you within 24 hours.