Tuesday, July 17, 2012

Basic linux useful commands

ou may or may not be versed in Linux. No worries. We all have to start somewhere! This article contains a number of useful Linux commands you will likely need to use when utilizing your VPS.

Server Administration

These are some basic commands to see the health of your VPS, check processes, etc.

w
The w command shows you a few things. First, it shows your server's current load. Secondly, it shows uptime. Finally, it shows you who is logged into shell and as what user. Handy!
ps
The ps commands shows your current running processes. A good set of flags to run with the command are aux, so the command would be ps aux. This will show all processes from all users on the server, plus some other vital information on them.
top
Top is a useful tool to see real time process information. It's different from ps as it updates the information and displays the biggest resource users on the top of the list. This is useful for seeing heavy memory and CPU users and processes. To quit top, simply press "q".
kill
Kill does exactly what it sounds like it does; kills things. Specifically, processes. If you have a process that isn't responsive, or, hung, you can use kill to run it. So, if I have a process I see from running ps and want to kill it, I'll grab the process ID (in the second column of the ps aux output) and run kill ID (replacing ID with the process ID). If it doesn't kill it, you may need to use the -9 switch to force it to close. So, kill -9 ID.
free
Free shows current memory usage. It displays the output in bytes, however, so you may want to use the  -m switch to show it in megabytes and make it more readable. You will see the total amount of memory, the used amount and the free amount, as well as memory swap usage. Note that Linux loves to cache things in memory, so having high memory usage doesn't necessarily indicate a problem.
df
The df command shows current disk usage. Check this article for more information.
du
The du command shows disk usage for a certain directory. The above article explains du as well as df.
netstat
The netstat command shows network status, specifically, all current connections. Try netstat -anp to see a great output for all connections!

Navigating and managing files and folders.

Navigating around on the command line can be intimidating. We'll try to make it less so here!

ls
The ls command lists the contents of the directory you're in. You can try the ls -alh command to show a list of files and folders, plus,
cd
The cd command stands for change directory. That allows you to change to a directory by typing in it's full path (so, cd /home/user/public_html) or, into a folder in the current directory you're in (so, if you're in /root and want to enter a directory called "files" that you've created in there, simply type cd files).
chown
The chown command stands for change owner and group. its basic usage is: chown user.group /path/to/file. You will want to replace user and group with the username you're changing the file to, plus, the group separated by a period as shown, or a colon.
chmod
chmod allows you to make permission changes to files. Files and folders have a set of permissions that allow users, groups and everyone to use or not use the file. You will need to chmod the binary settings for the file or folder. Basic usage is: chmod ### /path/to/file where ### is the binary permission. If you'd like a chmod calculator, try this page.
pwd
pwd shows the directory you're currently in. It stands for present working directory.
cp
The cp command is the command you can use to copy files and folders. It's general usage is: cp file1 file2 where file1 is the file name you're wanting to replicate. To copy directories, you need to use cp -r to recursively copy the directory.
mv
mv is similar to the copy command (cp), but instead of copying the file, it moves it from one place to another. Its general usage is: mv file1 newfile1 where file1 is the file you're moving the newfile1 is the file's new name, or, path to the directory you're moving it to.
rm
The rm command stands for remove. It deletes files and folders. Be careful with this command! There's no turning back! (well, there is a little! We do perform backups).
locate
The locate command uses a database that indexes all files and folders on your server. It's a quick and easy way to find files by name on your server. You may have to run the updatedb command, though, if the database that has your indexed files and folders hasn't been updated in a while.
find
find does exactly what you'd expect it to do. Finds stuff. Essentially, it's used similar to locate, but, it doesn't use a database. Instead, it searches the directory you're in (or, the one you specify in the search string). Say, for example, you wanted to find all files containing the word "index" in your current directory. You could use: find . -type f -exec fgrep -li index {} \;
The find command is pretty powerful! Try typing man find to see the manual for find.
stat
The stat command shows you detailed access, modify and change timestamps on a file. Its basic usage is: stat filename. This is useful to see when a file may have changed last.

No comments:

Post a Comment