Difference between revisions of "Linux"

From Server Knowledge Base
Jump to navigationJump to search
 
(80 intermediate revisions by the same user not shown)
Line 1: Line 1:
To check your server info, do lscpu <br>
To run a bash script without executing it, do bash -n scriptname.sh<br>
To run a bash script without executing it, do bash -n scriptname.sh<br>
To see help commands do command --help or man command. To paste into PuTTY, use SHIFT + INSERT.<br>
To see help commands do command --help or man command. To paste into PuTTY, use SHIFT + INSERT.<br>
Line 24: Line 25:


<pre>date +%d\ %B\ %Y\ %H:%M:%S</pre>
<pre>date +%d\ %B\ %Y\ %H:%M:%S</pre>
== authorized_keys (RSA) ==
This will show you how to SCP, SSH and rSync without prompting for password between two servers.
Whenever you need to use SCP to copy files, it asks for passwords. Same with rSync as it (by default) uses SSH as well. Usually SCP and rSync commands are used to transfer or backup files between known hosts or by the same user on both the hosts. It can get really annoying the password is asked every time. I even had the idea of writing an expect script to provide the password. Of course, I didn't. Instead I browsed for a solution and found it after quite some time. There are already a couple of links out there which talk about it. I am adding to it...
Lets say you want to copy between two hosts host_src and host_dest. host_src is the host where you would run the SCP, SSH or rSync command, irrespective of the direction of the file copy!
On host_src, run this command as the user that runs SCP/SSH/rSync
<pre>ssh-keygen -t rsa</pre>
This will prompt for a passphrase. Just press the enter key. It'll then generate an identification (private key) and a public key. Do not ever share the private key with anyone! ssh-keygen shows where it saved the public key. This is by default ~/.ssh/id_rsa.pub:
Your public key has been saved in <your_home_dir>/.ssh/id_rsa.pub
Transfer the id_rsa.pub file to host_dest by either FTP, SCP, rSync or any other method.
On host_dest, '''login as the remote user which you plan to use when you run SCP, SSH or rSync on host_src'''.
Make sure the folder ~/.ssh exists first, if not do:
<pre>mkdir ~/.ssh</pre>
Copy the contents of id_rsa.pub to ~/.ssh/authorized_keys
<pre>cat id_rsa.pub >>~/.ssh/authorized_keys
chmod 700 ~/.ssh/authorized_keys</pre>
If this file does not exists, then the above command will create it. Make sure you remove permission for others to read this file. If its a public key, why prevent others from reading this file? Probably, the owner of the key has distributed it to a few trusted users and has not placed any additional security measures to check if its really a trusted user.
Note that SSH by default does not allow root to log in. This has to be explicitly enabled on host_dest. This can be done by editing /etc/ssh/sshd_config and changing the option of PermitRootLogin from no to yes. Don't forget to restart SSHD so that it reads the modified config file. Do this only if you want to use the root login.
Well, thats it. Now you can run SCP, SSH and rSync on host_src connecting to host_dest and it won't prompt for the password. Note that this will still prompt for the password if you are running the commands on host_dest connecting to host_src. You can reverse the steps above (generate the public key on host_dest and copy it to host_src) and you have a two way setup ready!
== .bash_history ==
Change Epoch time in .bash_history:
https://askubuntu.com/questions/391082/how-to-see-time-stamps-in-bash-history/391087


== .bashrc ==
== .bashrc ==
Line 30: Line 72:


=== Colours ===
=== Colours ===
https://wiki.archlinux.org/index.php/Color_Bash_Prompt


Add these:
Add these:
Line 63: Line 107:


== Check Linux version/kernel ==
== Check Linux version/kernel ==
cat /etc/issue


For Ubuntu do cat /etc/issue or for some CentOS distributions use cat /etc/redhat-release
For Ubuntu do cat /etc/issue or for some CentOS distributions use cat /etc/redhat-release
Line 90: Line 136:
<pre>apt-get install cpu-checker
<pre>apt-get install cpu-checker
kvm-ok</pre>
kvm-ok</pre>
== Check your PuTTY (TTY) session ==
Type this into command line:
tty
== Clear last login info ==
[https://www.cyberciti.biz/faq/howto-display-clear-last-login-information/ Clear last login info]


== CLI/bash Commands and scripting ==
== CLI/bash Commands and scripting ==
Line 105: Line 161:
- The $() syntax allows you to load anything echoed to STDOUT to be loaded into a variable, and the single quotes ensure that if there are any spaces in the directory name, it still gets loaded into a single variable. To use command substitution, enclose any command that generates output to standard output inside parentheses and precede the opening parenthesis with a dollar sign, $(command). Command substitution is useful when assigning a value to a variable. It is handy for using the output of one command as an argument to another command. [http://mywiki.wooledge.org/BashFAQ/082 Why is $(...) preferred over `...` (backticks)?] . A good use of this is in http://serverkb.co.uk/tools/slow.sh
- The $() syntax allows you to load anything echoed to STDOUT to be loaded into a variable, and the single quotes ensure that if there are any spaces in the directory name, it still gets loaded into a single variable. To use command substitution, enclose any command that generates output to standard output inside parentheses and precede the opening parenthesis with a dollar sign, $(command). Command substitution is useful when assigning a value to a variable. It is handy for using the output of one command as an argument to another command. [http://mywiki.wooledge.org/BashFAQ/082 Why is $(...) preferred over `...` (backticks)?] . A good use of this is in http://serverkb.co.uk/tools/slow.sh


* To quote double quotes (") do the following: echo -e "Testing \"quotes\" here" - this will show as Testing "quotes"
* Quotes prevent wildcard (*) expansion.
* Quotes prevent wildcard (*) expansion.


Line 146: Line 203:


awk '{ printf "%-20s %-40s\n", $1, $2}' allows you to '''[http://stackoverflow.com/questions/6462894/how-can-i-format-the-output-of-a-bash-command-in-neat-columns print information in columns]'''
awk '{ printf "%-20s %-40s\n", $1, $2}' allows you to '''[http://stackoverflow.com/questions/6462894/how-can-i-format-the-output-of-a-bash-command-in-neat-columns print information in columns]'''
=== Cat ===
Parse JSON file:
<pre>cat file | python .mjson.tool</pre>


=== Cut ===
=== Cut ===
Line 170: Line 233:
=== eval ===
=== eval ===


Use this of you want run a variable after a pipe and to shorten down your scripts. e.g. in http://serverkb.co.uk/tools/getdns.sh
Use this if you want to run a variable after a pipe and to shorten down your scripts. e.g. in http://serverkb.co.uk/tools/getdns.sh


<pre>ns="ns.nameserver.co.uk"
<pre>ns="ns.nameserver.co.uk"
Line 237: Line 300:


=== Grep ===
=== Grep ===
If you are grepping a a .gz or .zip file you generally need to use zgrep.


==== Exclude multiple directories ====
==== Exclude multiple directories ====
Line 250: Line 315:


courtesy of [http://stackoverflow.com/questions/427979/how-do-you-extract-ip-addresses-from-files-using-a-regex-in-a-linux-shell/428086#428086 SO]
courtesy of [http://stackoverflow.com/questions/427979/how-do-you-extract-ip-addresses-from-files-using-a-regex-in-a-linux-shell/428086#428086 SO]
Note to self:
d 0000 | grep IP | awk '{print $4}' | grep -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'


==== Multiple terms ====
==== Multiple terms ====
Line 262: Line 323:


'''It is important to remember to not put a \| after the last text term.'''
'''It is important to remember to not put a \| after the last text term.'''
This can be used with tail as well.


==== Remove pipe symbols from MySQL ====
==== Remove pipe symbols from MySQL ====
Line 271: Line 334:
<pre>-A after
<pre>-A after
-B before</pre>
-B before</pre>
==== Wildcards for filepaths ====
From http://unix.stackexchange.com/questions/203195/wildcards-for-filepaths-arent-working-in-grep :
<pre>* in a regex is not like a filename glob. It means 0 or more of the previous character/pattern. So your examples would be looking for a A then 0 or more B then -DEF
. in regex means "any character" so you could fix your pattern by using
grep 'AB.*DEF'</pre>


=== Head ===
=== Head ===
Line 333: Line 404:
read createdfilename
read createdfilename
touch $createdfilename</pre>
touch $createdfilename</pre>
You will need the -n for echo to allow input next to echo instead of a line below.


=== sed ===
=== sed ===
Line 344: Line 417:
Notes: Blank lines count as a row. The command awk 'NR==22' would do the same.
Notes: Blank lines count as a row. The command awk 'NR==22' would do the same.


'''To print multiple lines do:'''
'''To print individual/separate lines, e.g. 1, 4 and 5:'''
 
sed -ne '1p;4p;5p'
 
'''To print between lines 22 to 39:'''


sed '22,39 ! d'
sed '22,39 ! d'
Line 368: Line 445:


sed -i -e 's/'''File\"quotes\"WithA\ Space'''/''HereIs''\/''ABackslash''/g' filename
sed -i -e 's/'''File\"quotes\"WithA\ Space'''/''HereIs''\/''ABackslash''/g' filename
=== sleep and usleep ===
Use sleep if you want to "wait" X seconds. usleep is measured in microseconds and cannot do more than 1 second. 100000 (100,000 / 100k) = 0.1 seconds. 1,000,000 = 1 second.


=== Shells and subshells ===
=== Shells and subshells ===
Line 377: Line 458:
This information was provided by these sources: [http://stackoverflow.com/questions/874452/change-current-directory-from-a-script 1], [http://stackoverflow.com/questions/255414/why-doesnt-cd-work-in-a-bash-shell-script 2]
This information was provided by these sources: [http://stackoverflow.com/questions/874452/change-current-directory-from-a-script 1], [http://stackoverflow.com/questions/255414/why-doesnt-cd-work-in-a-bash-shell-script 2]


=== [http://mywiki.wooledge.org/BashFAQ/031 Use [[ instead of [] ===
=== tee ===


=== xargs ===
Tee command is used to store and view (both at the same time) the output of any other command.


This can be incredibly useful if you get "Argument list too long"
Tee command writes to the STDOUT, and to a file at a time.


cd /to/directory , find . -type f | xargs rm -Rf
By default the tee command overwrites the file. You can instruct tee command to append (like >> does) to the file using the option –a as shown below.


If you need to remove a list of files with spaces in them, do this:
<pre>ls | tee –a outputfile</pre>


<pre>ls -lah | grep "SpecificString" | awk '{print $9,$10,$11,$etc}' | xargs -I {} rm -v {}</pre>
=== [http://mywiki.wooledge.org/BashFAQ/031 Use [[ instead of [] ===


Use ls -lh to not include hidden files/file starting with a full stop.
=== Variables ===


For simple removal of normal files do this in a screen session:
If you store a variable with a command within it as follows:


<pre>ls -lh | awk '{print $9}' | xargs -t rm</pre>
<pre>variablename=`command`</pre>


== Compare a remote file with a local file ==
you should instead store it like this:


ssh user@host cat /path/to/remotefile | diff /path/to/localfile -
<pre>variablename=$(command)</pre>


Credit to User [http://www.commandlinefu.com/commands/by/root Root]
And then call it as follows:
 
<pre>${variablename}</pre>


=== Compare files in a directory ===
=== Watch ===


diff -bur folder1/ folder2/
Example/s:


Warning: when doing diff on folders, if the timestamps are different it will think the files are different. If you actually compare the files, they will be the same.
To monitor a file's size:


== Console Clock in corner ==
<pre>watch -n 1 'ls -lh | grep filename'</pre>


while true; do echo -ne "\e[s\e[0;$((COLUMNS-27))H$(date)\e[u"; sleep 1; done &
=== xargs ===


'''Warning, this auto scrolls your SSH session'''. To kill it, do:
This can be incredibly useful if you get "Argument list too long"


ps aux | grep bash | grep -v grep
cd /to/directory , find . -type f | xargs rm -Rf


Then kill the bash session at the time you ran it:
If you need to remove a list of files with spaces in them, do this:


kill <processid>
<pre>ls -lah | grep "SpecificString" | awk '{print $9,$10,$11,$etc}' | xargs -I {} rm -v {}</pre>


Credit to User [http://www.commandlinefu.com/commands/by/SQUIIDUX Squiidux]
Use ls -lh to not include hidden files/file starting with a full stop.


<!-- == Create chroot user with SCP access ==
For simple removal of normal files do this in a screen session:


http://www.linuxscrew.com/2012/07/05/linux-restricted-shells-rssh-and-scponly<br>
<pre>ls -lh | awk '{print $9}' | xargs -t rm</pre>
http://ubuntuforums.org/showthread.php?t=128206<br>
http://internetpartner.info/en/ubuntu/87-openssh-sftp-chroot-on-ubuntu.html<br>
http://unix.stackexchange.com/questions/9837/do-you-need-a-shell-for-scp<br>
http://unix.stackexchange.com/questions/48509/configure-scp-access-for-login-without-a-local-account


<pre>apt-get install rssh
Otherwise try using find:
cd /usr/src
wget http://serverkb.co.uk/tools/scponly-20110526.tgz
tar scponly-20110526.tgz
cd scponly-20110526
./configure
make
make install</pre>


vim /etc/rssh.conf
<pre>find . -exec grep PATTERN {} + | wc -l</pre>


Uncomment:
Copying multiple files:


<pre>allowscp
<pre>ls -lh | grep TEXT | awk '{print $9}' | xargs cp -t /target/path/</pre>
allowsftp
chrootpath = /path/to/new/home/directory
user=username:011:000110:/path/to/new/home/directory</pre>


<pre>adduser username --home /path/to/new/home/directory</pre>
== Compare a remote file with a local file ==
http://aristomagnus.wordpress.com/2007/09/28/easy-sftp-and-chroot-sftp-with-scponly/
<!-- https://wiki.archlinux.org/index.php/SFTP-chroot
http://answers.tectia.com/questions/605/how-do-i-chroot-users-on-unix-platforms
http://administratosphere.wordpress.com/2011/11/04/restricting-users-to-sftp-only-and-to-home-directories-using-a-chroot/
<pre>adduser username --home /path/to/home/directory</pre>
<pre>usermod conscp -s /sbin/nologin</pre>
<pre>vim /etc/ssh/sshd_config</pre>
<pre>Match User username
        ChrootDirectory /path/to/home/directory
        ForceCommand internal-sftp</pre> --> -->


== Cronjob/Crontab ==
ssh user@host cat /path/to/remotefile | diff /path/to/localfile -


To check a cronjob has at least attempted to run/execute, check this at the time of execution:
Credit to User [http://www.commandlinefu.com/commands/by/root Root]


<pre>tail -f /var/log/syslog | grep CRON</pre>
=== Compare files in a directory ===


[http://www.openjs.com/scripts/jslibrary/demos/crontab.php Generator]
diff -bur folder1/ folder2/


<pre>crontab -e
Warning: when doing diff on folders, if the timestamps are different it will think the files are different. If you actually compare the files, they will be the same.
crontab -l


*    *    *  *    *        command to be executed
== Console Clock in corner ==
-    -    -  -    -
|    |    |  |    |
|    |    |  |    +----- day of week (0 - 6) (Sunday = 0 or 7)
|    |    |  +------- month (1 - 12)
|    |    +--------- day of        month (1 - 31)
|    +----------- hour (0 - 23)
+------------- min (0 - 59)
</pre>


Guides:
while true; do echo -ne "\e[s\e[0;$((COLUMNS-27))H$(date)\e[u"; sleep 1; done &


http://www.adminschoice.com/crontab-quick-reference
'''Warning, this auto scrolls your SSH session'''. To kill it, do:


To do a job every X minutes, do */X * * * *
ps aux | grep bash | grep -v grep


To do a job every minute in a specific hour, do * X * * *
Then kill the bash session at the time you ran it:


To do a job every X hours, do * */X * * *
kill <processid>


To do a job every day at X hour on the hour, do 0 9 * * * for 9am each day.
Credit to User [http://www.commandlinefu.com/commands/by/SQUIIDUX Squiidux]


To do a job every week at 2am on Sunday, do 0 2 * * 0
<!-- == Create chroot user with SCP access ==


To monitor active cron jobs, do tail -f /var/log/cron
http://www.linuxscrew.com/2012/07/05/linux-restricted-shells-rssh-and-scponly<br>
http://ubuntuforums.org/showthread.php?t=128206<br>
http://internetpartner.info/en/ubuntu/87-openssh-sftp-chroot-on-ubuntu.html<br>
http://unix.stackexchange.com/questions/9837/do-you-need-a-shell-for-scp<br>
http://unix.stackexchange.com/questions/48509/configure-scp-access-for-login-without-a-local-account


=== /usr/local/bin/php: No such file or directory ===
<pre>apt-get install rssh
cd /usr/src
wget http://serverkb.co.uk/tools/scponly-20110526.tgz
tar scponly-20110526.tgz
cd scponly-20110526
./configure
make
make install</pre>


Do whereis php
vim /etc/rssh.conf


Generally it is actually in /usr/bin/php
Uncomment:


== Cron Daemon email ==
<pre>allowscp
allowsftp
chrootpath = /path/to/new/home/directory
user=username:011:000110:/path/to/new/home/directory</pre>


If you get the below email:
<pre>adduser username --home /path/to/new/home/directory</pre>
http://aristomagnus.wordpress.com/2007/09/28/easy-sftp-and-chroot-sftp-with-scponly/
<!-- https://wiki.archlinux.org/index.php/SFTP-chroot
http://answers.tectia.com/questions/605/how-do-i-chroot-users-on-unix-platforms
http://administratosphere.wordpress.com/2011/11/04/restricting-users-to-sftp-only-and-to-home-directories-using-a-chroot/
<pre>adduser username --home /path/to/home/directory</pre>
<pre>usermod conscp -s /sbin/nologin</pre>
<pre>vim /etc/ssh/sshd_config</pre>
<pre>Match User username
        ChrootDirectory /path/to/home/directory
        ForceCommand internal-sftp</pre> --> -->


<pre>cd / && run-parts --report /etc/cron.daily
== Create symlink ==
/etc/cron.daily/sysklogd:
chown: cannot access `/var/log/mail.warn': No such file or directory
chown: cannot access `/var/log/syslog': No such file or directory
chown: cannot access `/var/log/mail.info': No such file or directory</pre>


Do the following:
https://www.cyberciti.biz/faq/creating-soft-link-or-symbolic-link/


<pre>cd /var/log
How to chown a symlink: https://superuser.com/questions/68685/chown-is-not-changing-symbolic-link
touch /var/log/mail.warn /var/log/syslog /var/log/mail.info
/etc/init.d/sysklogd restart</pre>


== Echo colours ==
Old method:


[http://misc.flogisoft.com/bash/tip_colors_and_formatting Tips for colours and formatting]
<pre>ln -s TARGET LINK_NAME</pre>


Place 0; for the normal version (e.g. Black is 0;30)<br>
== Cronjob/Crontab ==
Place 1; before these to get the light colour version.
 
To check a cronjob has at least attempted to run/execute, check this at the time of execution:
 
<pre>tail -f /var/log/syslog | grep CRON</pre>
 
[http://www.openjs.com/scripts/jslibrary/demos/crontab.php Generator]


Here are the colour codes:
<pre>crontab -e
crontab -l


{| border="1" align="center" style="text-align:center;" class="collapsible collapsed wikitable"
*    *    *  *    *        command to be executed
|-
-    -    -  -   -
! colspan="3"|
|     |     |   |   |
|-
|     |     |   |   +----- day of week (0 - 6) (Sunday = 0 or 7)
|'''Colour'''
|     |     |   +------- month (1 - 12)
|'''Foreground'''
|     |     +--------- day of        month (1 - 31)
|'''Background'''
|     +----------- hour (0 - 23)
|-
+------------- min (0 - 59)
|Black
</pre>
|30
 
|40
Guides:
|-
 
|Dark Grey
http://www.adminschoice.com/crontab-quick-reference
|1;30
 
|1;40
To do a job every X minutes, do */X * * * *
|-
 
|Red
To do a job every minute in a specific hour, do * X * * *
|31
 
|41
To do a job every X hours, do * */X * * *
|-
 
|Light Red
To do a job every day at X hour on the hour, do 0 9 * * * for 9am each day.
|1;31
 
|1;41
To do a job every week at 2am on Sunday, do 0 2 * * 0
|-
 
|Green
To monitor active cron jobs, do tail -f /var/log/cron
|32
 
|42
=== /usr/local/bin/php: No such file or directory ===
|-
 
|Light Green
Do whereis php
|1;32
 
|1;42
Generally it is actually in /usr/bin/php
|-
 
|Yellow
== Cron Daemon email ==
|1;33
 
|1;43
If you get the below email:
|-
 
|Brown
<pre>cd / && run-parts --report /etc/cron.daily
|0;33
/etc/cron.daily/sysklogd:
|0;43
chown: cannot access `/var/log/mail.warn': No such file or directory
|-
chown: cannot access `/var/log/syslog': No such file or directory
|Blue
chown: cannot access `/var/log/mail.info': No such file or directory</pre>
|34
 
|44
Do the following:
|-
 
|Light Blue
<pre>cd /var/log
|1;34
touch /var/log/mail.warn /var/log/syslog /var/log/mail.info
|1;44
/etc/init.d/sysklogd restart</pre>
|-
 
|Magenta (Purple)
== Date ==
|35
|45
|-
|Light Purple
|1;35
|1;45
|-
|Cyan
|36
|46
|-
|Light Cyan
|1;36
|1;46
|-
|White
|37
|47
|-
|Light Gray
|0;37
|0;47
|-
|}


An example:
=== Spaces ===


<pre>#!/bin/bash
To put spaces between variables (e.g +%H%M) use single quotes:


wipe="\033[1m\033[0m"
<pre>The time is `date '+%R:%S %Y %Z'` on the following Day/Month/Year `date '+%a %b %d'`</pre>


black="40m"
== Echo colours ==
darkggrey='\E[1;30m'
red='\E[31m'
lightred='\E[1;31m'
green='\E[32m'
lightgreen='\E[1;32m'
yellow='\E[1;33m'
brown='\E[0;33m'
blue='\E[34m'
lightblue='\E[1;34m'
purple='\E[35m'
lightpurple='\E[1;35m'
cyan='\E[36m'
lightcyan='\E[1;36m'
white='\E[37m'
lightgray='\E[0;37m'
green='\E[32m;'</pre>


<pre>echo -e "$green$black"
[http://misc.flogisoft.com/bash/tip_colors_and_formatting Tips for colours and formatting]
echo Hello World
echo -e "$wipe"</pre>


or
Place 0; for the normal version (e.g. Black is 0;30)<br>
Place 1; before these to get the light colour version.


<pre>echo -e "Output a ${green}coloured${wipe} word."</pre>
Here are the colour codes:


<pre>./colourtest.sh
{| border="1" align="center" style="text-align:center;" class="collapsible collapsed wikitable"
 
|-
Hello World</pre>
! colspan="3"|
 
|-
The Hello World text appears green.
|'''Colour'''
 
|'''Foreground'''
== Execute one off command whenever ==
|'''Background'''
 
|-
echo "ls -l" | at midnight
|Black
 
|30
This is an alternative to cron which allows a one-off task to be scheduled for a certain time.
|40
 
|-
Credit to User [http://www.commandlinefu.com/commands/by/root Root]
|Dark Grey
 
|1;30
== File Locations (index priority) ==
|1;40
 
|-
/etc/apache2/mods-enabled/dir.conf (on Ubuntu) or /etc/httpd/conf/httpd.conf (on CentOS, ~line 402) and it should show something like:
|Red
 
|31
<pre>DirectoryIndex index.html index.html.var index.shtml index.cfm index.php index.htm</pre>
|41
 
|-
This shows the default priority in which the index page is picked up and this can be set in the [[.htaccess]] file as well with:
|Light Red
 
|1;31
<pre>DirectoryIndex index.html index.php</pre>
|1;41
 
|-
These are good places to check if you are using all of your [[Resource_Usage|disk space]].
|Green
 
|32
cd /var/log<br>
|42
cd /var/www/vhosts (website and statistics)<br>
|-
cd /var/lib/mysql (database) or mysql/mysql<br>
|Light Green
cd /usr/bin/mysqldump (mysql dump)<br>
|1;32
cd var/qmail (mail logs, queue and configuration)<br>
|1;42
cd /var/lib/psa/dumps (physical Plesk backups)<br>
|-
cd opt/psa/tmp/ (Plesk stores temporary files here for backups)
|Yellow
 
|1;33
== File Permissions ==
|1;43
 
|-
[http://linuxcommand.org/lts0070.php One of the best permissions guide]
|Brown
 
|0;33
The values and their equivalent permissions. R is Read, W is Write and X is Execute. There are three sets on a file or folder (e.g. -rwx-w--w- , -rw-r--r-- , -rwxr-xrwx) so you need to put in a value of three (or four in rare occasions) for a file/folder e.g. 644.
|0;43
 
|-
<pre>0  ---<br>
|Blue
1 --x
|34
2  -w-
|44
3  -wx
|-
4  r--
|Light Blue
5  r-x
|1;34
6  rw-
|1;44
7  rwx</pre>
|-
 
|Magenta (Purple)
'''The syntax for chmod is (for example) 755 fileorfolder. The -R option can also be used to set the permissions on anything below a folder.'''<br>
|35
'''The syntax for chown is chown user:group fileorfolder . To apply the user:group to anything below a folder you need to use the -R option.'''
|45
 
|-
[http://permissions-calculator.org/ Permissions Calculator]<br>
|Light Purple
[http://www.tuxfiles.org/linuxhelp/filepermissions.html Help Guide 1]<br>
|1;35
[http://www.freeos.com/articles/3127?page=3 Help Guide 2]<br>
|1;45
[http://en.wikipedia.org/wiki/Filesystem_permissions#Notation_of_traditional_Unix_permissions Unix Notation]<br>
|-
[http://www.faqs.org/docs/linux_intro/sect_03_04.html File protection with chmod]
|Cyan
|36
|46
|-
|Light Cyan
|1;36
|1;46
|-
|White
|37
|47
|-
|Light Gray
|0;37
|0;47
|-
|}


If you use PHP Support as '''Apache module, it will use the third value of''' -rw- r-- '''r-x''' to permissions. If it is using '''Fast CGI it will use the user:group''' for the first two values '''-rwx rw-''' --x
An example:


If you are using Plesk a good place to check if you are getting Forbidden errors on your website is /var/www/vhosts/yourdomain.com/httpdocs/statistics/logs/error_log . Access your site and tail -f that file. If you get .htaccess pcfg_openfile: unable to check htaccess file, ensure it is readable name your .htaccess file correctly, set your httpdocs to 757 and see [http://serverfault.com/questions/61726/permission-denied-home-htaccess-pcfg-openfile-unable-to-check-htaccess-file this link].
<pre>#!/bin/bash


'''Special permissions'''
wipe="\033[1m\033[0m"
 
black="40m"
darkggrey='\E[1;30m'
red='\E[31m'
lightred='\E[1;31m'
green='\E[32m'
lightgreen='\E[1;32m'
yellow='\E[1;33m'
brown='\E[0;33m'
blue='\E[34m'
lightblue='\E[1;34m'
purple='\E[35m'
lightpurple='\E[1;35m'
cyan='\E[36m'
lightcyan='\E[1;36m'
white='\E[37m'
lightgray='\E[0;37m'
green='\E[32m;'</pre>


There is also a, g & s which are not widely used as it just needs additional representation at the special/setid permission to the group.
<pre>echo -e "$green$black"
echo Hello World
echo -e "$wipe"</pre>


To set a file to -rws--x--x for example use
or


<pre>4 = setuid - s
<pre>echo -e "Output a ${green}coloured${wipe} word."</pre>
2 = setgid - s
4 + 2 = S
1 = Sticky Bit
4 + 2 + 1 = T
2511  -r-x--s--x (e.g. /var/qmail/bin/qmail-remote or /var/qmail/bin/qmail-queue)
4655  -rwSr-xr-x
4711  -rws--x--x
4744  -rwsr--r--
4755  -rwsr-xr-x
6411  -r-S--s--x.
6511  -r-s--s--x
6644  -rwSr-Sr--
6666  -rwSrwSrw-
7000  ---S--S--T
7644  -rwSr-Sr-T
7711  -rws--s--t
7744  -rwsr-Sr-T
7755  -rwsr-sr-t</pre>


[http://serverfault.com/questions/111350/what-chmod-and-ownergroup-settings-are-best-for-a-web-application Owner and Group advice for websites]
<pre>./colourtest.sh


== [http://adminlogs.info/2011/06/02/linux-find-command-tips Find command guide] ==
Hello World</pre>


== FreeBSD ==
The Hello World text appears green.


* mysqldump location: /mysql/bin/mysqldump
== Execute one off command whenever ==
* Remove syntax:


- rm -r folderName
echo "ls -l" | at midnight
* Generic tunneling interface starts with gif


=== [http://forums.freebsd.org/showthread.php?t=5852 Check software versions] ===
This is an alternative to cron which allows a one-off task to be scheduled for a certain time.


=== fetch instead of wget ===
Credit to User [http://www.commandlinefu.com/commands/by/root Root]


To download a file.
== File Locations (index priority) ==


=== whereis instead of locate command ===
/etc/apache2/mods-enabled/dir.conf (on Ubuntu) or /etc/httpd/conf/httpd.conf (on CentOS, ~line 402) and it should show something like:


To find files/folders.
<pre>DirectoryIndex index.html index.html.var index.shtml index.cfm index.php index.htm</pre>


== Full Directory listing ==
This shows the default priority in which the index page is picked up and this can be set in the [[.htaccess]] file as well with:


<pre>apt-get install tree
<pre>DirectoryIndex index.html index.php</pre>
yum install tree
tree > tree.txt</pre>


If you feel adventurous do cat tree.txt , it will take a while ;)
These are good places to check if you are using all of your [[Resource_Usage|disk space]].


=== Find the deepest directory in your server/file structure ===
cd /var/log<br>
 
cd /var/www/vhosts (website and statistics)<br>
<pre>find . -type d -printf '%d:%p\n' | sort -n | tail -1</pre>
cd /var/lib/mysql (database) or mysql/mysql<br>
cd /usr/bin/mysqldump (mysql dump)<br>
cd var/qmail (mail logs, queue and configuration)<br>
cd /var/lib/psa/dumps (physical Plesk backups)<br>
cd opt/psa/tmp/ (Plesk stores temporary files here for backups)


=== Find and remove specific file types from current directory ===
== File Permissions ==


<pre>cd into the directory
[http://linuxcommand.org/lts0070.php One of the best permissions guide]
find . -type f -name '*.filetype' -exec rm -v {} \;</pre>


=== Line count a file ===
The values and their equivalent permissions. R is Read, W is Write and X is Execute. There are three sets on a file or folder (e.g. -rwx-w--w- , -rw-r--r-- , -rwxr-xrwx) so you need to put in a value of three (or four in rare occasions) for a file/folder e.g. 644.


wc -l /file/path
<pre>0  ---<br>
1  --x
2  -w-
3  -wx
4  r--
5  r-x
6  rw-
7  rwx</pre>


== Generate random number ==
'''The syntax for chmod is (for example) 755 fileorfolder. The -R option can also be used to set the permissions on anything below a folder.'''<br>
'''The syntax for chown is chown user:group fileorfolder . To apply the user:group to anything below a folder you need to use the -R option.'''


Between 1 and 10:
[http://permissions-calculator.org/ Permissions Calculator]<br>
 
[http://www.tuxfiles.org/linuxhelp/filepermissions.html Help Guide 1]<br>
seq 10| shuf | head -1
[http://www.freeos.com/articles/3127?page=3 Help Guide 2]<br>
[http://en.wikipedia.org/wiki/Filesystem_permissions#Notation_of_traditional_Unix_permissions Unix Notation]<br>
[http://www.faqs.org/docs/linux_intro/sect_03_04.html File protection with chmod]


== GeoIP - Block countries accessing website ==
If you use PHP Support as '''Apache module, it will use the third value of''' -rw- r-- '''r-x''' to permissions. If it is using '''Fast CGI it will use the user:group''' for the first two values '''-rwx rw-''' --x


Ubuntu: apt-cache search geoip ; apt-get install geoip-database libgeoip-dev libgeoip1 python-geoip geoip-bin libapache2-mod-geoip tclgeoip<br>
If you are using Plesk a good place to check if you are getting Forbidden errors on your website is /var/www/vhosts/yourdomain.com/httpdocs/statistics/logs/error_log . Access your site and tail -f that file. If you get .htaccess pcfg_openfile: unable to check htaccess file, ensure it is readable name your .htaccess file correctly, set your httpdocs to 757 and see [http://serverfault.com/questions/61726/permission-denied-home-htaccess-pcfg-openfile-unable-to-check-htaccess-file this link].
CentOS: yum list |grep -i geo , yum install GeoIP.x86_64<br>
http://www.webhostingtalk.com/showthread.php?t=959646<br>
http://askmatt.co.uk/blog/2010/05/block-countries-using-apache-mod-geo-ip-list-of-countries-by-fraud-risk


== Gunzip and Zip ==
'''Special permissions'''


To gzip a file (.gz) up, do:
There is also a, g & s which are not widely used as it just needs additional representation at the special/setid permission to the group.


<pre>gzip file</pre>
To set a file to -rws--x--x for example use


Or if zip is installed:
<pre>4 = setuid - s
 
2 = setgid - s
<pre>zip -r filename.zip filename</pre>
4 + 2 = S
 
1 = Sticky Bit
To unzip a .gz file, do:
4 + 2 + 1 = T
2511  -r-x--s--x (e.g. /var/qmail/bin/qmail-remote or /var/qmail/bin/qmail-queue)
4655  -rwSr-xr-x
4711  -rws--x--x
4744  -rwsr--r--
4755  -rwsr-xr-x
6411  -r-S--s--x.
6511  -r-s--s--x
6644  -rwSr-Sr--
6666  -rwSrwSrw-
7000  ---S--S--T
7644  -rwSr-Sr-T
7711  -rws--s--t
7744  -rwsr-Sr-T
7755  -rwsr-sr-t</pre>


<pre>gunzip file</pre>
[http://serverfault.com/questions/111350/what-chmod-and-ownergroup-settings-are-best-for-a-web-application Owner and Group advice for websites]


== [[.htaccess]] ==
== [http://adminlogs.info/2011/06/02/linux-find-command-tips Find command guide] ==


[http://en.wikipedia.org/wiki/Htaccess HyperText access]
== [http://serverfault.com/questions/35076/need-to-fix-file-permissions-in-a-users-home-directory Fix file and folder permissions easily] ==


== [http://stackoverflow.com/questions/653096/howto-free-inode-usage inode usage] ==
== FreeBSD ==


<pre>df --si
* mysqldump location: /mysql/bin/mysqldump
df -ih</pre>
* Remove syntax:


A lot of the time the cause can be /tmp (/var/lib/php/session/ on Plesk) due to sessions. You may want to delete the files in there.
- rm -r folderName
* Generic tunneling interface starts with gif


== Investigating high load ==
=== [http://forums.freebsd.org/showthread.php?t=5852 Check software versions] ===


If you are getting a high load average in top, these are some of the steps you can take to investigate the issue.
=== fetch instead of wget ===


Check which process has the most open of itself:
To download a file.


<pre>ps aux | awk '{print $11}' | sort | uniq -c | sort -nk1 | tail -n5</pre>
=== whereis instead of locate command ===


Stop that process, then run the above command a second time. Then start it again and run the command a third time.
To find files/folders.


=== Useful software ===
== Full Directory listing ==


* top
<pre>apt-get install tree
* htop
yum install tree
* iotop
tree > tree.txt</pre>


All these will do the job. Firstly check the CPU wait time, this is shown within top in Cpu(s):  8.0%us,  2.8%sy,  0.0%ni, 40.7%id, 48.3%wa
If you feel adventurous do cat tree.txt , it will take a while ;)


<pre>%wa in</pre>
=== Find the deepest directory in your server/file structure ===


If this is high, check the Status column (S column in top) to see if any are labelled D. The [http://stackoverflow.com/questions/666783/how-to-find-out-which-process-is-consuming-wait-cpu-i-e-i-o-blocked processes blocked on IO] are the ones marked as D.
<pre>find . -type d -printf '%d:%p\n' | sort -n | tail -1</pre>


=== On a Plesk server ===
=== Find and remove specific file types from current directory ===


Ensure sites are running PHP as Fast CGI Application instead of Apache module so you can see which USER the process is running as. Pressing the letter "c" on your keyboard will show the path and normally the website name.
<pre>cd into the directory
find . -type f -name '*.filetype' -exec rm -v {} \;</pre>


<pre>wget http://serverkb.co.uk/tools/memcpu.sh ; chmod +x memcpu.sh ; ./memcpu.sh > usage.log &
=== Line count a file ===
tail -f usage.log</pre>


You can alter the PHP Handler on Plesk boxes in the psa database easily by doing:
wc -l /file/path


<pre>mysql -uadmin -p`cat /etc/psa/.psa.shadow`;
== Generate random number ==
use psa
select * from hosting\G
select dom_id,www_root,php_handler_type from hosting;
update hosting set php_handler_type="module" where dom_id=x;</pre>


If sites run PHP as an Apache module scripts will execute as the Apache user www-data, this can make it difficult to see which site they belong to. This also means scripts run with privileges of the Apache user so if an account is compromised an attacker can get access to all other accounts. Also running as Apache module can make the Apache process CPU report look artificially high. Running PHP as Fast-CGI executes scripts as an FTP user associated with each subscription allowing easier identification of problem scripts and limit the damage of rogue scripts.
Between 1 and 10:


CPU reports are not an easy way to determine server health. We'd recommend you look at changes and trends rather than the absolute numbers. Most importantly consider your real world performance.
seq 10| shuf | head -1


== Linux Container ==
== GeoIP - Block countries accessing website ==


This install below is for an Ubuntu physical server, I may update this in the future for CentOS, Fedora and others.
Ubuntu: apt-cache search geoip ; apt-get install geoip-database libgeoip-dev libgeoip1 python-geoip geoip-bin libapache2-mod-geoip tclgeoip<br>
CentOS: yum list |grep -i geo , yum install GeoIP.x86_64<br>
http://www.webhostingtalk.com/showthread.php?t=959646<br>
http://askmatt.co.uk/blog/2010/05/block-countries-using-apache-mod-geo-ip-list-of-countries-by-fraud-risk


https://help.ubuntu.com/12.04/serverguide/lxc.html
== Gunzip and Zip ==


If you need to get file off the container, you can just scp it off. If the recipient server is slow, try moving the file to the host machine by doing:
To gzip a file (.gz) up, do:


scp -Psshport file root@hostIPaddress:~
<pre>gzip file</pre>


=== Installation of LXC ===
The above won't work for folders.


<pre>apt-get install lxc
Or if zip is installed:
cat /etc/init/lxc-net.conf | grep USE_LXC_BRIDGE</pre>


If true set to false unless you want the containers to NAT to your servers real IP addresses, and to be accessible externally.
<pre>zip -r filename.zip filename</pre>


=== Pre-container creation steps ===
To unzip a .gz file, do:


To reduce errors pre-container creation do the following:
<pre>gunzip file</pre>


<pre>dpkg-reconfigure locales
To extract a .tgz file, do:
locale-gen en_GB
update-locale LANG=en_GB.UTF-8</pre>
<!-- <pre>vim ~/.profile
export LC_CTYPE=en_GB.UTF-8 export LC_ALL=en_GB.UTF-8</pre>


Or:
<pre>tar zxvf fileNameHere.tgz</pre>


<pre>vim /etc/environment
See http://serverkb.co.uk/wiki/Linux#tar_command for further details.
LC_ALL="en_GB.utf8"</pre>-->


=== Creating/deleting containers ===
=== 7zip ===


[http://wiki.openvz.org/Download/template/precreated OpenVZ Template list]
https://www.howtoforge.com/tutorial/how-to-install-and-use-7zip-file-archiver-on-ubuntu-linux/


Check the templates below and pick one:
DO NOT USE the 7-zip format for backup purpose on Linux/Unix because:


<pre>cd /usr/lib/lxc/templates/ ; ls -lah</pre>
- 7-zip does not store the owner/group of the file.


Create a container from one of the templates:
On Linux/Unix, in order to backup directories you must use tar:


<pre>lxc-create -t ubuntu -n NameOfTheContainer</pre>
- to backup a directory  : tar cf - directory  |  7za  a  -si  directory.tar.7z
 
- to restore your backup : 7za x -so directory.tar.7z | tar xf -


If you want to install the fedora package, do apt-get install yum
If you want to send files and directories (not the owner of file) to others Unix/MacOS/Windows users, you can use the 7-zip format.


To delete it just do:
Example:


<pre>lxc-destroy -n NameOfTheContainer</pre>
7za a directory.7z  directory


=== Start/stop a container ===
Do not use "-r" because this flag does not do what you think.


<pre>lxc-start -n NameOfTheContainer -d</pre>
Do not use directory/* because of ".*" files (example  :  "directory/*" does not match "directory/.profile")


/etc/init.d/lxc stop
https://www.unixtutorial.org/2014/08/7zip-ubuntu/


=== Access the container ===
== [http://eng.eelcowesemann.nl/plesk/changing-the-servers-hostname-in-plesk Hostname guide] (rough) ==


The default user is 'ubuntu' with the password 'ubuntu', to enter a container, do:
If the server runs Plesk and Virtuozzo, the permanent one needs setting on the hardware or in Virtuozzo.


<pre>lxc-console -n NameOfTheContainer
Run dig -x IP.IP.IP.IP from any Linux server and it will show you the PTR/hostname.
sudo -i</pre>


Exit using Ctrl + a, then press q
'''Note:''' Most servers by default come with a non resolving hostname of localhost, localhost.localdomain or something generic.


To re-enter the container, do lxc-console -n NameOfTheContainer and then press enter (you may have to a few times)
The hostname should never be left as the default value on a server sending mail, as it is one of three things mail recipient's mailservers see to determine if mail is spam or not. The other two are reverse DNS and the SMTP banner.


'''Access externally:'''
If Plesk throws an error when clicking Websites & Domains tab regarding hostname -f, see [[Plesk_Errors#Domains_area_is_blank | this resolution]].


iptables -t nat -A PREROUTING -p tcp --dport 2222 -j DNAT --to 10.0.3.61:22
== How to fix broken packages ==


'''Make sure you remove this rule afterwards and DO NOT reboot your server.'''
Run the following commands below:


=== Configuration settings ===
<pre>dpkg --configure -a
apt-get install -f
#or
apt-get -f install</pre>


* By default you can ping a container from the host, and vice versa, and you can ping the outside world from the container.
If the problem still exists, then edit dpkg status file:
* You can set the hostname just like a normal server, if you want to rename the container.


==== Default configuration of system files ====
<pre>gksudo gedit /var/lib/dpkg/status</pre>


<pre>cd /var/lib/lxc/nameofcontainer/rootfs/etc/sysconfig/network-scripts
Find the package in question, and remove everything that has anything to do with it and save the file.
vim ifcfg-eth0</pre>
<pre>DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
HOSTNAME=phptester
NM_CONTROLLED=no
TYPE=Ethernet
MTU=</pre>


<pre>vim /var/lib/lxc/nameofcontainer/config</pre>
== How to install .deb files ==
<pre>lxc.network.type=veth
lxc.network.link=lxcbr0
lxc.network.flags=up
lxc.network.hwaddr = MAC Address
lxc.utsname = MT</pre>


<pre>vim /etc/lxc/lxc.conf</pre>
<pre>dpkg -i filename.deb</pre>
<pre>lxc.network.type=veth
lxc.network.link=lxcbr0
lxc.network.flags=up</pre>


You can add the below to /etc/network/interfaces
== [[.htaccess]] ==


<pre>auto br1
[http://en.wikipedia.org/wiki/Htaccess HyperText access]
iface br1 inet dhcp
    bridge_ports eth0</pre>


==== Fedora ====
== [http://stackoverflow.com/questions/653096/howto-free-inode-usage inode usage] ==


The mirrors/repositories the container uses may be broken by default, don't try to install anything. You'll likely get:
<pre>df --si
df -ih</pre>


''Error: Cannot retrieve repository metadata (repomd.xml) for repository: fedora. Please verify its path and try again''
A lot of the time the cause can be /tmp (/var/lib/php/session/ on Plesk) due to sessions. You may want to delete the files in there.


And you likely won't be able to ping anything except the host machine and localhost/127.0.0.1
== Investigating high load ==


iptables -t nat -A POSTROUTING -s ContainerIP/24 -j SNAT --to-source PhysicalHostIP<br>
If you are getting a high load average in top, these are some of the steps you can take to investigate the issue.
iptables -t nat -A PREROUTING -m tcp -p tcp --dport 10022 -j DNAT -i eth0 --to-destination ContainerIP:80<br>
iptables -t nat -A PREROUTING -m tcp -p tcp --dport 10443 -j DNAT -i eth0 --to-destination ContainerIP:443


[https://www.berrange.com/posts/2011/09/27/getting-started-with-lxc-using-libvirt libvirt]
Check which process has the most open of itself:


<pre>vi /etc/yum.repos.d/fedora.repo
<pre>ps aux | awk '{print $11}' | sort | uniq -c | sort -nk1 | tail -n5</pre>
vi /etc/yum.repos.d/fedora-updates.repo</pre>


Uncomment (#) the lines starting with "baseurl"
Stop that process, then run the above command a second time. Then start it again and run the command a third time.


yum update
=== Useful software ===


==== OpenSUSE ====
* top
* htop
* iotop


[http://en.wikipedia.org/wiki/OpenSUSE#Version_history Version history]
All these will do the job. Firstly check the CPU wait time, this is shown within top in Cpu(s): 8.0%us,  2.8%sy,  0.0%ni, 40.7%id, 48.3%wa


http://www.lacerta.be/d7/content/opensuse-lxc-container-inside-ubuntu
<pre>%wa in</pre>


=== List containers ===
If this is high, check the Status column (S column in top) to see if any are labelled D. The [http://stackoverflow.com/questions/666783/how-to-find-out-which-process-is-consuming-wait-cpu-i-e-i-o-blocked processes blocked on IO] are the ones marked as D.


lxc-list
=== On a Plesk server ===


=== Set passwords ===
Ensure sites are running PHP as Fast CGI Application instead of Apache module so you can see which USER the process is running as. Pressing the letter "c" on your keyboard will show the path and normally the website name.


Log in as the root user of the container:
<pre>wget http://serverkb.co.uk/tools/memcpu.sh ; chmod +x memcpu.sh ; ./memcpu.sh > usage.log &
tail -f usage.log</pre>


'''Fedora''' container:
You can alter the PHP Handler on Plesk boxes in the psa database easily by doing:


<pre>Username: root
<pre>mysql -uadmin -p`cat /etc/psa/.psa.shadow`;
Password: root</pre>
use psa
select * from hosting\G
select dom_id,www_root,php_handler_type from hosting;
update hosting set php_handler_type="module" where dom_id=x;</pre>


Set the root password to something different:
If sites run PHP as an Apache module scripts will execute as the Apache user www-data, this can make it difficult to see which site they belong to. This also means scripts run with privileges of the Apache user so if an account is compromised an attacker can get access to all other accounts. Also running as Apache module can make the Apache process CPU report look artificially high. Running PHP as Fast-CGI executes scripts as an FTP user associated with each subscription allowing easier identification of problem scripts and limit the damage of rogue scripts.


<pre>passwd</pre>
CPU reports are not an easy way to determine server health. We'd recommend you look at changes and trends rather than the absolute numbers. Most importantly consider your real world performance.


You will need to do yum install vim when inside the server.
== Linux Container ==


'''Ubuntu''' container:
This install below is for an Ubuntu physical server, I may update this in the future for CentOS, Fedora and others.


<pre>sudo -i
https://help.ubuntu.com/12.04/serverguide/lxc.html
Username: ubuntu
Password: ubuntu</pre>


Set the user's password:
If you need to get file off the container, you can just scp it off. If the recipient server is slow, try moving the file to the host machine by doing:


<pre>passwd ubuntu</pre>
scp -Psshport file root@hostIPaddress:~


Set the root user's password:
=== Installation of LXC ===


passwd
<pre>apt-get install lxc
cat /etc/init/lxc-net.conf | grep USE_LXC_BRIDGE</pre>


=== SSH in externally ===
If true set to false unless you want the containers to NAT to your servers real IP addresses, and to be accessible externally.


'''To route from externally through the host to the container, just do the below iptables rule''':
=== Pre-container creation steps ===


<pre>iptables -t nat -A PREROUTING -p tcp --dport 1337 -j DNAT --to 10.0.4.60:22
To reduce errors pre-container creation do the following:
iptables-save</pre>


In the above case we are saying:
<pre>dpkg-reconfigure locales
locale-gen en_GB
update-locale LANG=en_GB.UTF-8</pre>
<!-- <pre>vim ~/.profile
export LC_CTYPE=en_GB.UTF-8 export LC_ALL=en_GB.UTF-8</pre>


- You want to SSH in on port 1337<br>
Or:
- The container's eth0 IP address is 10.0.4.60<br>
- Then below we are saying the physical machine has an IP address of 110.111.112.113


Then externally from the server do:
<pre>vim /etc/environment
LC_ALL="en_GB.utf8"</pre>-->


<pre>ssh [email protected] -p2222</pre>
=== Creating/deleting containers ===


And bingo! You should be in the container.
[http://wiki.openvz.org/Download/template/precreated OpenVZ Template list]


<pre>passwd</pre>
Check the templates below and pick one:
<!-- OpenVZ


https://code.google.com/p/ovz-web-panel/
<pre>cd /usr/lib/lxc/templates/ ; ls -lah</pre>


apt-get install vzctl vzdump vzquota
Create a container from one of the templates:
wget -O - http://ovz-web-panel.googlecode.com/svn/installer/ai.sh | sh
gem install net-ssh
gem install net-sftp -->


== [http://wiki.debian.org/LSBInitScripts LSB Init Scripts] ==
<pre>lxc-create -t ubuntu -n NameOfTheContainer</pre>


== ls list only directories or files ==
If you want to install the fedora package, do apt-get install yum


Directories:
To delete it just do:


<pre>ls -lad */</pre>
<pre>lxc-destroy -n NameOfTheContainer</pre>


Files:
=== Start/stop a container ===


<pre>ls -la | grep -v ^d</pre>
<pre>lxc-start -n NameOfTheContainer -d</pre>


== Kill tty session ==
/etc/init.d/lxc stop


<pre>w
=== Access the container ===
ps aux | grep bash | grep -v grep
ps aux | grep tty | grep -v grep
kill -HUP <processid></pre>


[http://www.cyberciti.biz/faq/howto-kill-unix-linux-user-session Further info]
The default user is 'ubuntu' with the password 'ubuntu', to enter a container, do:


== Maldet ==
<pre>lxc-console -n NameOfTheContainer
sudo -i</pre>


[http://www.rfxn.com/projects/linux-malware-detect Documentation]<br>
Exit using Ctrl + a, then press q
[http://www.rfxn.com/downloads/maldetect-current.tar.gz Maldet .tar.gz]<br>
[http://blog.hostonnet.com/how-to-install-and-configure-maldet-linux-malware-detect-lmd Install and Configure]


<pre>cd /root ; wget http://serverkb.co.uk/tools/maldet.sh ; chmod +x maldet.sh ; ./maldet.sh</pre>
To re-enter the container, do lxc-console -n NameOfTheContainer and then press enter (you may have to a few times)


Always run your scans from chroot environment (if it has one/is possible) and in Screen (screen -S NameIt), and detach by doing CTRL + A, then press D
'''Access externally:'''


Scan reports are normally stored in /usr/local/maldetect/sess/ as session. files.
iptables -t nat -A PREROUTING -p tcp --dport 2222 -j DNAT --to 10.0.3.61:22


maldet -a -e -l filepath
'''Make sure you remove this rule afterwards and DO NOT reboot your server.'''


== Manually alter time/date ==
=== Configuration settings ===


'''Ubuntu'''
* By default you can ping a container from the host, and vice versa, and you can ping the outside world from the container.
* You can set the hostname just like a normal server, if you want to rename the container.


<pre>date
==== Default configuration of system files ====
date mmddtimeyear #as seen below</pre>


'''Cent OS'''
<pre>cd /var/lib/lxc/nameofcontainer/rootfs/etc/sysconfig/network-scripts
vim ifcfg-eth0</pre>
<pre>DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
HOSTNAME=phptester
NM_CONTROLLED=no
TYPE=Ethernet
MTU=</pre>


<pre>cd /etc/
<pre>vim /var/lib/lxc/nameofcontainer/config</pre>
ls -lah
<pre>lxc.network.type=veth
rm localtime
lxc.network.link=lxcbr0
ln -s /usr/share/zoneinfo/Europe/London /etc/localtime
lxc.network.flags=up
date mmddtimeyear
lxc.network.hwaddr = MAC Address
    | |  |  | ---> 2012
lxc.utsname = MT</pre>
    | |  | 24hr --> XX:XX without :
    | | dd -------> day
    | mm ---------> month


/etc/init.d/ntpd restart or start
<pre>vim /etc/lxc/lxc.conf</pre>
date</pre>
<pre>lxc.network.type=veth
lxc.network.link=lxcbr0
lxc.network.flags=up</pre>


== PID ==
You can add the below to /etc/network/interfaces


Under construction.
<pre>auto br1
iface br1 inet dhcp
    bridge_ports eth0</pre>


Process ID.
==== Fedora ====


To determine the usage of a specific process, do top -p PID
The mirrors/repositories the container uses may be broken by default, don't try to install anything. You'll likely get:


The maximum number of pids can be obtained from /proc/sys/kernel/pid_max
''Error: Cannot retrieve repository metadata (repomd.xml) for repository: fedora. Please verify its path and try again''


== Proxmox ==
And you likely won't be able to ping anything except the host machine and localhost/127.0.0.1


Rough notes:
iptables -t nat -A POSTROUTING -s ContainerIP/24 -j SNAT --to-source PhysicalHostIP<br>
iptables -t nat -A PREROUTING -m tcp -p tcp --dport 10022 -j DNAT -i eth0 --to-destination ContainerIP:80<br>
iptables -t nat -A PREROUTING -m tcp -p tcp --dport 10443 -j DNAT -i eth0 --to-destination ContainerIP:443


apt-get install sudo
[https://www.berrange.com/posts/2011/09/27/getting-started-with-lxc-using-libvirt libvirt]


Proxmox
<pre>vi /etc/yum.repos.d/fedora.repo
vi /etc/yum.repos.d/fedora-updates.repo</pre>


/var/lib/vz
Uncomment (#) the lines starting with "baseurl"


dump is for backups
yum update
images is for OS images
private is for OpenVZ container file systems
template/cache is for OpenVZ templates


http://openvz.org/Download/template/precreated
==== OpenSUSE ====


Create VM creates KVM<br>
[http://en.wikipedia.org/wiki/OpenSUSE#Version_history Version history]
Create CT creates OpenVZ container<br>


vzctl enter id<br>
http://www.lacerta.be/d7/content/opensuse-lxc-container-inside-ubuntu
vzctl start/stop id<br>


iptables -t nat -A POSTROUTING -o vmbr1 -j MASQUERADE<br>
=== List containers ===
restart networking on host and CT


=== cman_tool: Cannot open connection to cman ===
lxc-list


<pre>pvecm status
=== Set passwords ===
pvecm nodes
cman_tool: Cannot open connection to cman, is it running ?</pre>


<pre>service pve-cluster restart
Log in as the root user of the container:
pvecm delnode NodeName</pre>


=== Force remove an OpenVZ container ===
'''Fedora''' container:


<pre>vzctl stop 100 ; vzctl destroy ContainerID
<pre>Username: root
cd /var/lib/vz/private
Password: root</pre>
rm ContainerIDfolder -R
cd /var/lib/vz/root
rm ContainerIDfolder -R
cd /etc/pve/nodes/ContainerName/openvz
mv ContainerID.conf ContainerID.bak</pre>


Make sure it does not exist in cat /etc/pve/.members
Set the root password to something different:


=== Remount a logical partition/volume ===
<pre>passwd</pre>


lvdisplay
You will need to do yum install vim when inside the server.


/dev/mapper/pve-data /var/lib/vz (this will be different in your file system)
'''Ubuntu''' container:


=== Unable to get local IP address ===
<pre>sudo -i
Username: ubuntu
Password: ubuntu</pre>
 
Set the user's password:


<pre>/etc/init.d/pve-cluster restart
<pre>passwd ubuntu</pre>
service pve-cluster start
Starting pve cluster filesystem : pve-cluster[main] crit: Unable to get local IP address
(warning).</pre>


Make sure in /etc/hosts your domain name resolves to the server and you also have it without the .co.uk/.com etc in the file as so:
Set the root user's password:


<pre>ServerIPaddress domain.co.uk domain pvelocalhost
passwd
127.0.0.1 localhost localhost.localdomain</pre>


Then do:
=== SSH in externally ===


<pre>/etc/init.d/hostname.sh stop
'''To route from externally through the host to the container, just do the below iptables rule''':
/etc/init.d/hostname.sh start
service pve-cluster start</pre>


=== Transport endpoint is not connected ===
<pre>iptables -t nat -A PREROUTING -p tcp --dport 1337 -j DNAT --to 10.0.4.60:22
iptables-save</pre>


<pre>df -h
In the above case we are saying:
df: `/etc/pve': Transport endpoint is not connected</pre>


<pre>ls -lah /etc/pve
- You want to SSH in on port 1337<br>
ls: cannot access pve: Transport endpoint is not connected
- The container's eth0 IP address is 10.0.4.60<br>
d?????????  ? ?    ?                      ?            ? pve</pre>
- Then below we are saying the physical machine has an IP address of 110.111.112.113


Do this:
Then externally from the server do:


<pre>umount /etc/pve
<pre>ssh [email protected] -p2222</pre>
pvecm status</pre>


You will get:
And bingo! You should be in the container.


unable to get IP for node 'hostname' - node offline?
<pre>passwd</pre>
<!-- OpenVZ


The fix is the same as [[Linux#Unable_to_get_local_IP_address | this]] one, ensure you have the domain without the web extension in /etc/hosts resolving to the servers IP address. Then do:
https://code.google.com/p/ovz-web-panel/


service pve-cluster start
apt-get install vzctl vzdump vzquota
wget -O - http://ovz-web-panel.googlecode.com/svn/installer/ai.sh | sh
gem install net-ssh
gem install net-sftp -->


== Recover deleted files ==
== [http://wiki.debian.org/LSBInitScripts LSB Init Scripts] ==


You need to install this software before you delete any files:
== ls list only directories or files ==


<pre>apt-get install foremost</pre>
Directories:


Then see this documentation:
<pre>ls -lad */</pre>


https://help.ubuntu.com/community/DataRecovery<br>
Files:
http://ddailygirl.wordpress.com/2010/08/17/recovering-files-after-rm-in-linux<br>
http://www.howtoforge.com/recover-deleted-files-with-foremost<br>
http://www.webupd8.org/2009/03/recover-deleted-files-in-ubuntu-debian.html


== Remove file starting with dash ==
<pre>ls -la | grep -v ^d</pre>


<pre>rm -- -filename</pre>
== Kill tty session ==


Remove folder starting with dash
<pre>w
ps aux | grep bash | grep -v grep
ps aux | grep tty | grep -v grep
kill -HUP <processid></pre>


<pre>rm -rf -- -folder/</pre>
[http://www.cyberciti.biz/faq/howto-kill-unix-linux-user-session Further info]


== Remove/rename file called tilde ==
== Maldet ==


<pre>mv '~' newfilename
[http://www.rfxn.com/projects/linux-malware-detect Documentation]<br>
rm '~'</pre>
[http://www.rfxn.com/downloads/maldetect-current.tar.gz Maldet .tar.gz]<br>
[http://blog.hostonnet.com/how-to-install-and-configure-maldet-linux-malware-detect-lmd Install and Configure]


== Restart service or service ==
<pre>cd /root ; wget http://serverkb.co.uk/tools/maldet.sh ; chmod +x maldet.sh ; ./maldet.sh</pre>


Useful for differently named ones, e.g.
Always run your scans from chroot environment (if it has one/is possible) and in Screen (screen -S NameIt), and detach by doing CTRL + A, then press D


[ -f /etc/init.d/mysqld ] && service mysqld restart ; [ -f /etc/init.d/mysql ] && service mysql restart
Scan reports are normally stored in /usr/local/maldetect/sess/ as session. files.


[ -f /etc/init.d/httpd ] && service httpd restart ; [ -f /etc/init.d/apache2 ] && service apache2 restart
maldet -a -e -l filepath


== [http://eng.eelcowesemann.nl/plesk/changing-the-servers-hostname-in-plesk Rough Hostname guide] ==
== Manually alter time/date ==


If the server runs Plesk and Virtuozzo, the permanent one needs setting on the hardware or in Virtuozzo.
'''Ubuntu'''


Run dig -x IP.IP.IP.IP from any Linux server and it will show you the PTR/hostname.
<pre>date
date mmddtimeyear #as seen below</pre>


'''Note:''' Most servers by default come with a non resolving hostname of localhost, localhost.localdomain or something generic.
'''Cent OS'''


The hostname should never be left as the default value on a server sending mail, as it is one of three things mail recipient's mailservers see to determine if mail is spam or not. The other two are reverse DNS and the SMTP banner.
<pre>cd /etc/
ls -lah
rm localtime
ln -s /usr/share/zoneinfo/Europe/London /etc/localtime
date mmddtimeyear
    | |  |  | ---> 2012
    | |  | 24hr --> XX:XX without :
    | | dd -------> day
    | mm ---------> month


If Plesk throws an error when clicking Websites & Domains tab regarding hostname -f, see [[Plesk_Errors#Domains_area_is_blank | this resolution]].
/etc/init.d/ntpd restart or start
date</pre>


== Roughly list file count ==
== Mount NFS drive ==


This includes nested directories:
<pre>apt-get install nfs-common
cd /media
mount -t nfs HostName:/export/ftpbackup/ServiceName /FolderMount</pre>


<pre>find /full/file/path -type f | wc -l</pre>
The example above contains variables, which you will need to substitute with your own values.


== SCP Command - Secure Copy ==
HostName: The host name of your backup storage<br>
ServiceName: The name of your server (e.g. ns0000000.ip-123-123-123.net)<br>
FolderMount: The folder where you want to mount the NFS share


To secure copy a file from one Linux server to another, use the following syntax form:
e.g.


<pre>scp -P PORT file user@IPAddress:/filepath</pre>
mount -t nfs ftpback-xxx1-123.ovh.net:/export/ftpbackup/ns123456.ip-XX-XXX-XXX.eu /media/YourNewFolder


For example:
== Move files into your home directory not owned by your own user ==


<pre>scp -P 22 index.html [email protected]:/</pre>
Copy to /var/tmp or /tmp (for small files) and do:


To move a folder, put -r in between the port and the folder. If you receive the following error while trying to SCP a file from one server to the other:
<pre>chmod ugo+rw <filename></pre>


<pre>stdin: is not a tty</pre>
== PID ==


You can solve the issue quickly by doing the following on the destination server:
Under construction.


<pre>vi ~/.bashrc
Process ID.
 
if [ $(expr index "$-" i) -eq 0 ]; then
return
fi</pre>


== Screen ==
To determine the usage of a specific process, do top -p PID


A better alternate is [[Linux#tmux | tmux]].
You can use the "c" key to show the file path a PID is being called from.


[http://www.ubuntugeek.com/screen-manages-multiple-sessions-on-one-terminal.html#more-1415 Manage Multiple sessions in one Terminal]<br>
The maximum number of pids can be obtained from /proc/sys/kernel/pid_max
[http://www.samsarin.com/blog/2007/03/11/gnu-screen-working-with-the-scrollback-buffer Scrollback] (vim /home/.screenrc + defscrollback 1000)<br>
[http://polishlinux.org/howtos/screen-tips-tricks Tips and Tricks]


== Setting the time ==
== .profile ==


http://geoffhankerson.com/node/112<br>
Put this in .profile file for on user startup:
http://codeghar.wordpress.com/2007/12/06/manage-time-in-ubuntu-through-command-line/


== sudo ==
<pre>echo "" ; df -h | sed -ne '1p;5p' ; echo "rootfs"
echo "" ; free -m
echo "" ; w | head -1 | sed 's/^ *//g'
echo "" ; w | tail -10 | grep -v average ; echo ""</pre>


If you exit out of root access and want to run the last command you entered without authentication, do:
== Proxmox ==


<pre>sudo !!</pre>
To access via the web go to https://IPaddress:8006


== System Logs and Shutdown troubleshooting ==
Rough notes:


If you are having an issue, a reboot should not be performed if the server can be accessed in any way (e.g. locally in the data centre, only if it is a Dedicated server). This is because after a reboot there is little you can find out from the logs as the important logs get cleared on restart.
apt-get install sudo


<pre>last reboot
Proxmox
last</pre>


Do this  to check which files exist:
/var/lib/vz


ls -lh /var/log/syslog ; ls -lh /var/log/kern.log ; ls -lh /var/log/dmesg ; ls -lh /var/log/messages
dump is for backups<br>
images is for OS images<br>
private is for OpenVZ container file systems<br>
template/cache is for OpenVZ templates


Then do one or more of these depending on which exist:
http://openvz.org/Download/template/precreated


grep -i error /var/log/syslog ; grep -i panic /var/log/syslog ; grep -i warning /var/log/syslog<br>
Create VM creates KVM<br>
grep -i error /var/log/dmesg ; grep -i panic /var/log/dmesg ; grep -i warning /var/log/dmesg<br>
Create CT creates OpenVZ container<br>
grep -i error /var/log/kern.log ; grep -i panic /var/log/kern.log ; grep -i warning /var/log/kern.log<br>
grep -i error /var/log/messages ; grep -i panic /var/log/messages ; grep -i warning /var/log/messages


errpt may show an error report on some Unix OS'.
vzctl enter id<br>
vzctl start/stop id<br>


To find .log files, run [[Linux#updatedb_.28locate_command.29 | updatedb]] and then locate *.log
iptables -t nat -A POSTROUTING -o vmbr1 -j MASQUERADE<br>
restart networking on host and CT


To restart and [http://go2linux.garron.me/reboot-check-disks-for-errors-avoid-force-fsck on boot do a disk check] do (or -rF):
=== cman_tool: Cannot open connection to cman ===


<pre>shutdown -Fr now</pre>
<pre>pvecm status
pvecm nodes
cman_tool: Cannot open connection to cman, is it running ?</pre>


== [http://www.pendrivelinux.com/how-to-open-a-tar-file-in-unix-or-linux tar command] ==
<pre>service pve-cluster restart
pvecm delnode NodeName</pre>


To archive and compress a folder/files do:
=== Force remove an OpenVZ container ===


<pre>tar -czvf files.tgz files/
<pre>vzctl stop 100 ; vzctl destroy ContainerID
tar -czvf folder.tar.gz folder/
cd /var/lib/vz/private
tar cvf mubackup.tar mu/ ; gzip -9 mubackup.tar
rm ContainerIDfolder -R
cd /var/lib/vz/root
rm ContainerIDfolder -R
cd /etc/pve/nodes/ContainerName/openvz
mv ContainerID.conf ContainerID.bak</pre>


#.tgz is same as .tar.gz</pre>
Make sure it does not exist in cat /etc/pve/.members


c creates the archive (tar), z compresses it into the gzip, v is verbose, f is the file/folder
=== Remount a logical partition/volume ===


To extract do:
lvdisplay


<pre>#for tar
/dev/mapper/pve-data /var/lib/vz (this will be different in your file system)
tar xvf file.tar


#for .tgz or tar.gz
=== Unable to get local IP address ===
tar zxvf file.tar.gz</pre>


[http://www.fluidthoughts.com/howto/tar-gzip/ Guide 1]<br>
<pre>/etc/init.d/pve-cluster restart
[http://www.thegeekstuff.com/2010/04/unix-tar-command-examples/ Guide 2]<br>
service pve-cluster start
[http://superuser.com/questions/305128/how-to-specify-level-of-compression-when-using-tar-zcvf 3] and [http://superuser.com/questions/156207/untar-ungz-gz-tar-how-do-you-remember-all-the-useful-options 4]
Starting pve cluster filesystem : pve-cluster[main] crit: Unable to get local IP address
(warning).</pre>


For bz2 files, use:
Make sure in /etc/hosts your domain name resolves to the server and you also have it without the .co.uk/.com etc in the file as so:


<pre>tar -xvjpf file</pre>
<pre>ServerIPaddress domain.co.uk domain pvelocalhost
127.0.0.1 localhost localhost.localdomain</pre>


== tmux ==
Then do:


New session:
<pre>/etc/init.d/hostname.sh stop
/etc/init.d/hostname.sh start
service pve-cluster start</pre>


<pre>tmux new-session -n NameTheSession</pre>
=== Transport endpoint is not connected ===


Detach from a session:
<pre>df -h
df: `/etc/pve': Transport endpoint is not connected</pre>


CTRL +B then D
<pre>ls -lah /etc/pve
ls: cannot access pve: Transport endpoint is not connected
d?????????  ? ?    ?                      ?            ? pve</pre>


List the sessions available:
Do this:


<pre>tmux ls</pre>
<pre>umount /etc/pve
pvecm status</pre>


Re-attach to a session:
You will get:


<pre>tmux attach
unable to get IP for node 'hostname' - node offline?
tmux attach -t ID or NameTheSession</pre>


Remotely execute a command:
The fix is the same as [[Linux#Unable_to_get_local_IP_address | this]] one, ensure you have the domain without the web extension in /etc/hosts resolving to the servers IP address. Then do:


<pre>tmux send-keys -t ID "command" ENTER</pre>
service pve-cluster start


== User ID 99 ==
== Recover deleted files ==


This is most commonly the user nobody. This can be caused by PHP or done on purpose. cat /etc/passwd | grep 99 ; vim /etc/passwd
You need to install this software before you delete any files:


== User is not in the sudoers file ==
<pre>apt-get install foremost</pre>


If you try to sudo into a server or run sudo and get the following message:
Then see this documentation:


"''is not in the sudoers file. This incident will be reported.''"
https://help.ubuntu.com/community/DataRecovery<br>
http://ddailygirl.wordpress.com/2010/08/17/recovering-files-after-rm-in-linux<br>
http://www.howtoforge.com/recover-deleted-files-with-foremost<br>
http://www.webupd8.org/2009/03/recover-deleted-files-in-ubuntu-debian.html


Do the following command as root and add the username into the file in the same format as the root user:
== Remove file starting with dash ==


visudo
<pre>rm -- -filename</pre>


== Use unusual characters in filenames ==
Remove folder starting with dash


If you want to specify a space, lets say the file = /usr/local/etc/'''testingdatabase.sql'''
<pre>rm -rf -- -folder/</pre>


You need to name the file/folder as follows:
== Remove/rename file called tilde ==


<pre>mv /usr/local/etc/testingdatabase.sql /usr/local/etc/testing\ database.sql</pre>
<pre>mv '~' newfilename
rm '~'</pre>


This will make the file be '''testing database.sql''' , on command line this will appear as '''testing\ database.sql''' .
== Restart service or service ==


A backslash symbol \ needs to be used before an apostrophe ' , bracket (), exclamation/bang ! symbol or question marks ?:
Useful for differently named ones, e.g.


<pre>01\ -\ It\'s\ You.txt
[ -f /etc/init.d/mysqld ] && service mysqld restart ; [ -f /etc/init.d/mysql ] && service mysql restart
#How it appears: 01 - It's You.txt


02\ -\ Boom\!.m3u
[ -f /etc/init.d/httpd ] && service httpd restart ; [ -f /etc/init.d/apache2 ] && service apache2 restart
#How it appears: 02 - Boom!.m3u


03\ -\ Why\ Wont\ You\ Work\?.sh
== Roughly list file count ==
#How it appears: 03 - Why Wont You Work?.sh
 
This includes nested directories:
 
<pre>find /full/file/path -type f | wc -l</pre>
 
== SCP Command - Secure Copy ==
 
To secure copy a file from one Linux server to another, use the following syntax form:
 
<pre>scp -P PORT file user@IPAddress:/filepath</pre>
 
For example:
 
<pre>scp -P 22 index.html [email protected].1:/</pre>
 
To move a folder, put -r in between the port and the file(s)/folder(s) like so:


04\ -\ Musical\ Playlist\ For\ \(VPS\)\ Server.m3u
<pre>scp -P 22 -r testdirectory/ root@127.0.0.1:/</pre>
#How it appears: 04 - Musical Playlist For (VPS) Server.m3u</pre>


== updatedb (locate command) ==
If you receive the following error while trying to SCP a file from one server to the other:


This command is used when you cannot '''locate''' a file and you get "''locate: warning: database /var/lib/slocate/slocate.db' is more than 8 days old''". It is advised to run updatedb '''at least once a month'''. However if you get the following error when using it:
<pre>stdin: is not a tty</pre>


<pre>updatedb: fatal error: load_file: Could not open file: /etc/updatedb.conf: No such file or directory</pre>
You can solve the issue quickly by doing the following on the destination server:
 
<pre>vi ~/.bashrc
 
if [ $(expr index "$-" i) -eq 0 ]; then
return
fi</pre>
 
== Screen ==
 
Re-join screen session
 
<pre>screen -r</pre>
 
or
 
<pre>screen -D -r '1234.somescreensession'</pre>
 
A better alternate is [[Linux#tmux | tmux]].
 
[http://www.ubuntugeek.com/screen-manages-multiple-sessions-on-one-terminal.html#more-1415 Manage Multiple sessions in one Terminal]<br>
[http://www.samsarin.com/blog/2007/03/11/gnu-screen-working-with-the-scrollback-buffer Scrollback] (vim /home/.screenrc + defscrollback 1000)<br>
[http://polishlinux.org/howtos/screen-tips-tricks Tips and Tricks]
 
== Setting the time ==
 
http://geoffhankerson.com/node/112<br>
http://codeghar.wordpress.com/2007/12/06/manage-time-in-ubuntu-through-command-line/
 
== Standard redirection ==
 
http://www.xaprb.com/blog/2006/06/06/what-does-devnull-21-mean/
 
STDIN, STDOUT and STDERR.
 
0, 1 and 2
 
1>/dev/null<br>
2>/dev/null
 
== sudo ==
 
If you exit out of root access and want to run the last command you entered without authentication, do:
 
<pre>sudo !!</pre>
 
== System Logs and Shutdown troubleshooting ==
 
If you are having an issue, a reboot should not be performed if the server can be accessed in any way (e.g. locally in the data centre, only if it is a Dedicated server). This is because after a reboot there is little you can find out from the logs as the important logs get cleared on restart.
 
<pre>last reboot
last</pre>
 
Do this  to check which files exist:
 
ls -lh /var/log/syslog ; ls -lh /var/log/kern.log ; ls -lh /var/log/dmesg ; ls -lh /var/log/messages
 
Then do one or more of these depending on which exist:
 
grep -i error /var/log/syslog ; grep -i panic /var/log/syslog ; grep -i warning /var/log/syslog<br>
grep -i error /var/log/dmesg ; grep -i panic /var/log/dmesg ; grep -i warning /var/log/dmesg<br>
grep -i error /var/log/kern.log ; grep -i panic /var/log/kern.log ; grep -i warning /var/log/kern.log<br>
grep -i error /var/log/messages ; grep -i panic /var/log/messages ; grep -i warning /var/log/messages
 
errpt may show an error report on some Unix OS'.
 
To find .log files, run [[Linux#updatedb_.28locate_command.29 | updatedb]] and then locate *.log
 
To restart and [http://go2linux.garron.me/reboot-check-disks-for-errors-avoid-force-fsck on boot do a disk check] do (or -rF):
 
<pre>shutdown -Fr now</pre>
 
== [http://www.pendrivelinux.com/how-to-open-a-tar-file-in-unix-or-linux tar command] ==
 
To archive and compress a folder/files do:
 
<pre>tar -czvf files.tgz files/
tar -czvf folder.tar.gz folder/
tar cvf mubackup.tar mu/ ; gzip -9 mubackup.tar
 
#.tgz is same as .tar.gz</pre>
 
c creates the archive (tar), z compresses it into the gzip, v is verbose, f is the file/folder
 
To extract do:
 
<pre>#for tar
tar xvf file.tar
 
#for .tgz or tar.gz
tar zxvf file.tar.gz</pre>
 
[http://www.fluidthoughts.com/howto/tar-gzip/ Guide 1]<br>
[http://www.thegeekstuff.com/2010/04/unix-tar-command-examples/ Guide 2]<br>
[http://superuser.com/questions/305128/how-to-specify-level-of-compression-when-using-tar-zcvf 3] and [http://superuser.com/questions/156207/untar-ungz-gz-tar-how-do-you-remember-all-the-useful-options 4]
 
For bz2 files, use:
 
<pre>tar -xvjpf file</pre>
 
== top ==
 
The TIME column in top is displayed in minutes:seconds.hundredths
 
== tmux ==
 
C-c is CTRL+C<br>
C-m is ENTER
 
=== New session ===
 
Latest:
 
<pre>tmux new -s session_name</pre>
 
Also possible:
 
<pre>tmux new-session -s session_name</pre>
 
Old:
 
<pre>tmux new-session -n NameTheSession</pre>
 
=== Detach from a session ===
 
<pre>CTRL +B then D</pre>
 
=== Kill session ===
 
<pre>tmux kill-session -t myname</pre>
 
If this fails and it says:
 
"''session not found''"
 
Do the following:
 
<pre>tmux ls
tmux kill-session -t X</pre>
 
- where X is the number of the session on the left.
 
=== List sessions available ===
 
<pre>tmux ls</pre>
 
=== Re-attach to session ===
 
<pre>tmux attach
tmux attach -t ID or NameTheSession</pre>
 
=== Remotely execute a command ===
 
<pre>tmux send-keys -t ID "command" ENTER</pre>
 
== User ID 99 ==
 
This is most commonly the user nobody. This can be caused by PHP or done on purpose. cat /etc/passwd | grep 99 ; vim /etc/passwd
 
== User is not in the sudoers file ==
 
If you try to sudo into a server or run sudo and get the following message:
 
"''is not in the sudoers file.  This incident will be reported.''"
 
Do the following command as root and add the username into the file in the same format as the root user:
 
visudo
 
== Use unusual characters in filenames ==
 
If you want to specify a space, lets say the file = /usr/local/etc/'''testingdatabase.sql'''
 
You need to name the file/folder as follows:
 
<pre>mv /usr/local/etc/testingdatabase.sql /usr/local/etc/testing\ database.sql</pre>
 
This will make the file be '''testing database.sql''' , on command line this will appear as '''testing\ database.sql''' .
 
A backslash symbol \ needs to be used before an apostrophe ' , bracket (), exclamation/bang ! symbol or question marks ?:
 
<pre>01\ -\ It\'s\ You.txt
#How it appears: 01 - It's You.txt
 
02\ -\ Boom\!.m3u
#How it appears: 02 - Boom!.m3u
 
03\ -\ Why\ Wont\ You\ Work\?.sh
#How it appears: 03 - Why Wont You Work?.sh
 
04\ -\ Musical\ Playlist\ For\ \(VPS\)\ Server.m3u
#How it appears: 04 - Musical Playlist For (VPS) Server.m3u</pre>
 
== updatedb (locate command) ==
 
This command is used when you cannot '''locate''' a file and you get "''locate: warning: database /var/lib/slocate/slocate.db' is more than 8 days old''". It is advised to run updatedb '''at least once a month'''. However if you get the following error when using it:
 
<pre>updatedb: fatal error: load_file: Could not open file: /etc/updatedb.conf: No such file or directory</pre>


You need to create or edit this file
You need to create or edit this file


<pre>vim /etc/updatedb.conf</pre>
<pre>vim /etc/updatedb.conf</pre>
 
and put the following inside of it:
 
<pre>PRUNE_BIND_MOUNTS="yes"
PRUNEPATHS="/tmp /var/spool /media"
PRUNEFS="NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs shfs sysfs cifs lustre_lite tmpfs usbfs udf"</pre>
 
Alternatively run the below script after reading this file http://serverkb.co.uk/tools/README.txt:
 
http://serverkb.co.uk/tools/updatedbscript.sh
 
To vim a file you locate, see [http://stackoverflow.com/questions/8228831/why-does-locate-filename-xargs-vim-cause-strange-terminal-behaviour this]. vim $(command)
 
=== Cannot find an existing file ===
 
Ensure that the directory path of the file that '''does''' exist is not in the following section in /etc/updatedb.conf:
 
<pre>PRUNEPATHS="/tmp /var/spool /media"</pre>
<!-- Alternate prune file:
PRUNE_BIND_MOUNTS="yes"
# PRUNENAMES=".git .bzr .hg .svn"
PRUNEPATHS="/tmp /var/spool /media"
PRUNEFS="NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs shfs sysfs cifs lustre_lite tmpfs usbfs udf (FROM HERE IS DIFFERENT) fuse.glusterfs fuse.sshfs ecryptfs fusesmb devtmpfs" -->
 
== vim ==
 
To make a copy the line below where you cursor is, hold CTRL + e. Then put a hash (comment) in front of this line. This is useful for backing up an old value before changing it.
 
=== Create backup of file whilst in vim ===
 
<pre>vim file
:!cp % %-</pre>
 
Press enter and continue to edit
 
Credit to User [http://www.commandlinefu.com/commands/by/mpb MPB]
 
=== Enter Insert mode ===
 
Press the letter i (I)
 
=== Enter Replace mode ===
 
Press the letter r (R)
 
=== E21: Cannot make changes, 'Modifiable' is off ===
 
<pre>:set modifiable</pre>
 
=== Search in file for multiple terms ===
 
Go to search mode i.e. type '/' and then type \v followed by the words you want to search separated by '|' (pipe).
 
Example:
 
<pre>/\vword1|word2|word3</pre>
 
Go to search mode and type the words you want to search separated by '\|'.
 
Example:
 
<pre>/word1\|word2\|word3</pre>
 
The first way puts you in the regular expression mode so that you do not need to put any extra back slashes before every pipe or other delimiters used for searching.
 
=== See changes made before exiting ===
 
<pre>:w !diff % -</pre>
 
=== Editing a jar/zip file ===


and put the following inside of it:
(zip#Write) sorry, your system doesn't appear to have the zip pgm


PRUNE_BIND_MOUNTS="yes"PRUNEPATHS="/tmp /var/spool /media"PRUNEFS="NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs shfs sysfs cifs lustre_lite tmpfs usbfs udf"
1) Check zip is installed


Alternatively run the below script after reading this file http://serverkb.co.uk/tools/README.txt:
2)


http://serverkb.co.uk/tools/updatedbscript.sh
<pre>:set modifiable
:set write</pre>


To vim a file you locate, see [http://stackoverflow.com/questions/8228831/why-does-locate-filename-xargs-vim-cause-strange-terminal-behaviour this]. vim $(command)
3)
<!-- Alternate prune file:
PRUNE_BIND_MOUNTS="yes"
# PRUNENAMES=".git .bzr .hg .svn"
PRUNEPATHS="/tmp /var/spool /media"
PRUNEFS="NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs shfs sysfs cifs lustre_lite tmpfs usbfs udf (FROM HERE IS DIFFERENT) fuse.glusterfs fuse.sshfs ecryptfs fusesmb devtmpfs" -->


== vim ==
E382: Cannot write, 'buftype' option is set


To make a copy the line below where you cursor is, hold CTRL + e. Then put a hash (comment) in front of this line. This is useful for backing up an old value before changing it.
Check if the file has -e on it using:


=== Create backup of file whilst in vim ===
<pre>lsattr filename</pre>


<pre>vim file
If so, you likely cannot edit it.
:!cp % %-</pre>
 
Press enter and continue to edit


Credit to User [http://www.commandlinefu.com/commands/by/mpb MPB]
4)


=== Insert keyboard shortcut ===
Alternatively, download the file (e.g. a jar) and use an Archive tool like 7-zip to edit it.
 
Press the letter i (I)
 
=== Replace keyboard short ===
 
Press the letter r (R)
 
=== See changes made before exiting ===
 
<pre>:w !diff % -</pre>


== [http://www.cymru1.net/linux-vps/vps-hints-and-tips.php VPS Hints and Tips] ==
== [http://www.cymru1.net/linux-vps/vps-hints-and-tips.php VPS Hints and Tips] ==

Latest revision as of 10:13, 8 July 2019

To check your server info, do lscpu
To run a bash script without executing it, do bash -n scriptname.sh
To see help commands do command --help or man command. To paste into PuTTY, use SHIFT + INSERT.
For detailed software and hardware info do apt-get install hardinfo then hardinfo. For CentOS 6 use this.
To write to a user in the same SSH server, do w, get their tty session and then do write user ttySession. If they are root, do write root Session
Awk introduction, If manual.

Append date to same line

Either of these will work:

| awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; }'
| ts '%F %T'

To customise date, put a + symbol in front of the flag:

date +%R
14:32

To get this format of the date/time:

24 May 2013 10:25:33

Use:

date +%d\ %B\ %Y\ %H:%M:%S

authorized_keys (RSA)

This will show you how to SCP, SSH and rSync without prompting for password between two servers.

Whenever you need to use SCP to copy files, it asks for passwords. Same with rSync as it (by default) uses SSH as well. Usually SCP and rSync commands are used to transfer or backup files between known hosts or by the same user on both the hosts. It can get really annoying the password is asked every time. I even had the idea of writing an expect script to provide the password. Of course, I didn't. Instead I browsed for a solution and found it after quite some time. There are already a couple of links out there which talk about it. I am adding to it...

Lets say you want to copy between two hosts host_src and host_dest. host_src is the host where you would run the SCP, SSH or rSync command, irrespective of the direction of the file copy!

On host_src, run this command as the user that runs SCP/SSH/rSync

ssh-keygen -t rsa

This will prompt for a passphrase. Just press the enter key. It'll then generate an identification (private key) and a public key. Do not ever share the private key with anyone! ssh-keygen shows where it saved the public key. This is by default ~/.ssh/id_rsa.pub:

Your public key has been saved in <your_home_dir>/.ssh/id_rsa.pub

Transfer the id_rsa.pub file to host_dest by either FTP, SCP, rSync or any other method.

On host_dest, login as the remote user which you plan to use when you run SCP, SSH or rSync on host_src.

Make sure the folder ~/.ssh exists first, if not do:

mkdir ~/.ssh

Copy the contents of id_rsa.pub to ~/.ssh/authorized_keys

cat id_rsa.pub >>~/.ssh/authorized_keys
chmod 700 ~/.ssh/authorized_keys

If this file does not exists, then the above command will create it. Make sure you remove permission for others to read this file. If its a public key, why prevent others from reading this file? Probably, the owner of the key has distributed it to a few trusted users and has not placed any additional security measures to check if its really a trusted user.

Note that SSH by default does not allow root to log in. This has to be explicitly enabled on host_dest. This can be done by editing /etc/ssh/sshd_config and changing the option of PermitRootLogin from no to yes. Don't forget to restart SSHD so that it reads the modified config file. Do this only if you want to use the root login.

Well, thats it. Now you can run SCP, SSH and rSync on host_src connecting to host_dest and it won't prompt for the password. Note that this will still prompt for the password if you are running the commands on host_dest connecting to host_src. You can reverse the steps above (generate the public key on host_dest and copy it to host_src) and you have a two way setup ready!

.bash_history

Change Epoch time in .bash_history:

https://askubuntu.com/questions/391082/how-to-see-time-stamps-in-bash-history/391087

.bashrc

vim /root/.bashrc

Colours

https://wiki.archlinux.org/index.php/Color_Bash_Prompt

Add these:

alias ls='ls --color=auto'
alias grep='grep --color=auto'
source /root/.bashrc
vim /root/.vimrc
syntax on

Exit your session and re-enter it.

Crontab editor

Add this line in:

EDITOR=vim; export EDITOR
crontab -e

Binary/binaries

These are normally in the bin or sbin folder for a program.

Calendar

apt-get install gcal
gcal -K -q GB_EN December/2012-January/2013 # Holidays for Dec/2012 and Jan/2013 with week numbers

Credit to User MPB

Check Linux version/kernel

cat /etc/issue

For Ubuntu do cat /etc/issue or for some CentOS distributions use cat /etc/redhat-release

uname -a
uname -r #for just the kernel
uname -rs #for OS and kernel

Check if SELinux is enabled

apt-get install chkconfig
yum install chkconfig

chkconfig --list
cat /etc/sysconfig/selinux
sestatus
selinuxenabled

Check if Virtualization is available for server

CentOS:

egrep '(vmx|svm)' --color=always /proc/cpuinfo

Ubuntu:

apt-get install cpu-checker
kvm-ok

Check your PuTTY (TTY) session

Type this into command line:

tty

Clear last login info

Clear last login info

CLI/bash Commands and scripting

  • For variables with multiple pipes "|", use tacs `` instead of quotes ""
  • If you are attempting to use the Unix mail function, you have to specify a body otherwise it will hang.
  • If you have an if statement and its requirements are not met (e.g. greater than) and it then moves onto the next if statement, if it uses a mail function it may send it to the root user's email or the Admin/Administrator's email address. To avoid this use > /dev/null . 2>&1 likely won't work.
  • It seems if you are using the read function, you can only call a variable that is inside of it (subshell), from here.
  • To stop a ping after x amount of responses, do ping -c x or use ping -oc y (where y equals a maximum amount of tries)
  • Use >> to append an output to the end of the file.
  • The use of && means that you only echo the name of the directory if the directory creation is successful.

- The $() syntax allows you to load anything echoed to STDOUT to be loaded into a variable, and the single quotes ensure that if there are any spaces in the directory name, it still gets loaded into a single variable. To use command substitution, enclose any command that generates output to standard output inside parentheses and precede the opening parenthesis with a dollar sign, $(command). Command substitution is useful when assigning a value to a variable. It is handy for using the output of one command as an argument to another command. Why is $(...) preferred over `...` (backticks)? . A good use of this is in http://serverkb.co.uk/tools/slow.sh

  • To quote double quotes (") do the following: echo -e "Testing \"quotes\" here" - this will show as Testing "quotes"
  • Quotes prevent wildcard (*) expansion.

$?

This is the exit status/code of the last executed function/program/command.

ack

To look into.

Awk

To print out a list of just folders/files you want, do;

ls -l filepath | awk '{print $9;}'

This won't work correctly if there is spaces in the filename. To resolve this, use this command whilst in the directory itself.

To do multiple sections of a result, do:

awk '{print $1,$2,$4,$X;}'

If you want to get rid of/cut certain/specific lines/rows from STDOUT aka print a specific line, use awk in this way:

awk 'NR==22'

This is for use without a file. This example will only display the 22nd row from your output.

Notes: Blank lines count as a row. To do multiple lines do awk 'NR==22,NR==25' . This will output line 22-25.

The command sed '22 ! d' would do the same as the awk 'NR==22' example.

To get rid of pipe symbols in a file, do:

awk -F'|' '{print $1,$10}' FileWithPipes > FileWithoutPipes

Alternatively if you get a list, e.g of domains from MySQL with only one column selected, put them in a file and you can remove the pipes by doing:

cat domainlist | awk '{print $2}' > list

awk '{ printf "%-20s %-40s\n", $1, $2}' allows you to print information in columns

Cat

Parse JSON file:

cat file | python .mjson.tool

Cut

This tool can be used in an example where you get an output but want to strip it to exactly what you need e.g.

lookupipscript.sh <IPaddress>

Output below:

Plan : Bronze, Silver, Gold
Type : IPv4 or IPv6
URL  : http
IP   : 0.0.0.0(primary)

lookupipscript.sh <IPaddress> | sed 'row ! d' | awk '{print $column;}'

This would output 0.0.0.0(primary). To get just the IP address and not the "(primary)" section, do:

| cut -c 1-7

Cut counts the first number/letter as 1, not 0.

eval

Use this if you want to run a variable after a pipe and to shorten down your scripts. e.g. in http://serverkb.co.uk/tools/getdns.sh

ns="ns.nameserver.co.uk"
d="domain.co.uk"
g="grep $d | grep -v 'DiG\|;'"

echo "dig @$ns $d A"
dig @$ns $d A | eval $g

It is a good replacement instead of $() or `` or running just a variable.

Find

Exclude directories

Guide Part 1

Find Help (this includes mtime commands)

find . -maxdepth 1 -type f -printf "%f\n"  | sed s/,$//

Finds all files that contain "some string". This command is useful in a directory (e.g. mail), so you would do ls -lah | grep year-mm then:

find . -type f -exec grep -l "some string" {} \;

for loop examples

Functions

A prime use of functions is in our script to replace the date.timezone settings in php.ini at http://serverkb.co.uk/tools/phptimezone.sh

#!/bin/bash
APACHEPHP="/etc/php5/apache2/php.ini"
CURRENT="\n\nThis is the current timezone configuration:"
DONE="If the settings were wrong, they are as below now:"
ETC="/etc/php.ini"

function UBDEB(){
 echo -e "\nThe operating system is Debian/Ubuntu, so editing $APACHEPHP" $CURRENT
 grep "date." $1 | head -7 | grep -v '; http\|Define' ; echo ""
  sed -i -e 's/;date.timezone\ =/date.timezone\ =\ \"Europe\/London\"/g' $1
  sed -i -e 's/;date.default_latitude\ =\ 31.7667/date.default_latitude\ =\ 51.500181/g' $1
  sed -i -e 's/;date.default_longitude\ =\ 35.2333/date.default_longitude\ =\ 0.12619/g' $1
 echo $DONE
 grep "date." $1 | head -7 | grep -v '; http\|Define' ; echo ""
}

function RHEL(){
 echo -e "\nThe operating system is likely CentOS, editing $ETC" "$CURRENT
 grep "date." $1 | head -7 | grep -v '; http\|Define' ; echo ""
  sed -i -e 's/;date.timezone\ =/date.timezone\ =\ \"Europe\/London\"/g' $1
  sed -i -e 's/;date.default_latitude\ =\ 31.7667/date.default_latitude\ =\ 51.500181/g' $1
  sed -i -e 's/;date.default_longitude\ =\ 35.2333/date.default_longitude\ =\ 0.12619/g' $1
 echo $DONE
 grep "date." $1 | head -7 | grep -v '; http\|Define' ; echo ""
}

if [[ `cat /etc/issue | sed '1 ! d' | awk '{print $1;}'` == "Debian" || `cat /etc/issue | sed '1 ! d' | awk '{print $1;}'` == "Ubuntu" ]];
then
 UBDEB "$APACHEPHP"
  else
 RHEL "$ETC"
fi

Credit to Sam Teale for helping me with this.

Grep

If you are grepping a a .gz or .zip file you generally need to use zgrep.

Exclude multiple directories

grep 'string' -R . -il --exclude="/proc" --color
grep -iR "string" * | grep -v "/proc"

Look for IP address

do this:

grep -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'

courtesy of SO

Multiple terms

Exclude using -v

grep 'chroot\|default\|fs\|fs-passwd\|httpsdocs'

It is important to remember to not put a \| after the last text term.

This can be used with tail as well.

Remove pipe symbols from MySQL

mysql -pPASSWORD admin -e"select domain from domains;" grep -v "|" > FileWithoutPipes

Switches/flags

-A after
-B before

Wildcards for filepaths

From http://unix.stackexchange.com/questions/203195/wildcards-for-filepaths-arent-working-in-grep :

* in a regex is not like a filename glob. It means 0 or more of the previous character/pattern. So your examples would be looking for a A then 0 or more B then -DEF
. in regex means "any character" so you could fix your pattern by using
grep 'AB.*DEF'

Head

If for example you do /var/qmail/bin/qmail-qstat and it prints two separate lines, do this to get just the first line:

head -1

If and Else

Advanced if usage

Use -f for files, -d for directories

if [ -f $VARIABLE ]
then
parameters (e.g. echo, mkdir, touch, rm)
fi

Combine if and $? to get to do something with the exit status of the last executed command.

if [ $? -eq 0 ]; then
commands
fi

If you want to prompt for both y and Y for yes input, use:

if [[ $variable1 = y || $variable2 = Y ]];

Another example of the above if x or y is here. Use || to do if = x or y.

if [[ value = x || y ]

If you get the following error when executing a bash script outside of it's directory:

[[: not found

You need to either bash /file/path/to/script or alter the syntax to use:

-eq instead of =

or

(( command )) instead of [[ command ]]

If value equals multiple values

Using the syntax above:

echo -ne "Enter a server number (1, 2 or 3): "
read ServerNo

if [[ ( $ServerNo -eq 1 ) || ( $ServerNo -eq 2 ) || ( $ServerNo -eq 3 ) ]]; then
  echo "$ServerNo is a valid server, continuing."
 else
  echo "$ServerNo is not a valid server, exiting."
fi

read

One of the simplest uses of this command is to do it like so:

echo -ne "Enter the filename you want to create: "
read createdfilename
touch $createdfilename

You will need the -n for echo to allow input next to echo instead of a line below.

sed

If you want to get rid of/cut certain/specific lines/rows from STDOUT aka print a specific line, use sed in this way:

sed '22 ! d'

This is for use without a file. This example will only display the 22nd row from your output.

Notes: Blank lines count as a row. The command awk 'NR==22' would do the same.

To print individual/separate lines, e.g. 1, 4 and 5:

sed -ne '1p;4p;5p'

To print between lines 22 to 39:

sed '22,39 ! d'

To put a variable inside sed, use quotes instead of apostrophes:

sed "22,$variable ! d"

To delete parenthesis/brackets, use this:

sed 's/[()]//g'
sed 's/[)]//g'
sed 's/[(]//g'
sed 's/[[]]//g'
sed 's/[]]//g'
sed 's/[[]//g'

Replace text in a file

sed -i -e 's/TextToFindToReplace/TextToReplaceItWith/g' filename

To handle spaces, forward slashes and quotes " ", use back slashes like you do in filenames:

sed -i -e 's/File\"quotes\"WithA\ Space/HereIs\/ABackslash/g' filename

sleep and usleep

Use sleep if you want to "wait" X seconds. usleep is measured in microseconds and cannot do more than 1 second. 100000 (100,000 / 100k) = 0.1 seconds. 1,000,000 = 1 second.

Shells and subshells

If you want a script to make changes to your current shell, not the subshell a bash script works in, execute the script by doing the following (sometimes you may need to do ./ still):

. script

This information was provided by these sources: 1, 2

tee

Tee command is used to store and view (both at the same time) the output of any other command.

Tee command writes to the STDOUT, and to a file at a time.

By default the tee command overwrites the file. You can instruct tee command to append (like >> does) to the file using the option –a as shown below.

ls | tee –a outputfile

Use [[ instead of [

Variables

If you store a variable with a command within it as follows:

variablename=`command`

you should instead store it like this:

variablename=$(command)

And then call it as follows:

${variablename}

Watch

Example/s:

To monitor a file's size:

watch -n 1 'ls -lh | grep filename'

xargs

This can be incredibly useful if you get "Argument list too long"

cd /to/directory , find . -type f | xargs rm -Rf

If you need to remove a list of files with spaces in them, do this:

ls -lah | grep "SpecificString" | awk '{print $9,$10,$11,$etc}' | xargs -I {} rm -v {}

Use ls -lh to not include hidden files/file starting with a full stop.

For simple removal of normal files do this in a screen session:

ls -lh | awk '{print $9}' | xargs -t rm

Otherwise try using find:

find . -exec grep PATTERN {} + | wc -l

Copying multiple files:

ls -lh | grep TEXT | awk '{print $9}' | xargs cp -t /target/path/

Compare a remote file with a local file

ssh user@host cat /path/to/remotefile | diff /path/to/localfile -

Credit to User Root

Compare files in a directory

diff -bur folder1/ folder2/

Warning: when doing diff on folders, if the timestamps are different it will think the files are different. If you actually compare the files, they will be the same.

Console Clock in corner

while true; do echo -ne "\e[s\e[0;$((COLUMNS-27))H$(date)\e[u"; sleep 1; done &

Warning, this auto scrolls your SSH session. To kill it, do:

ps aux | grep bash | grep -v grep

Then kill the bash session at the time you ran it:

kill <processid>

Credit to User Squiidux

-->

Create symlink

https://www.cyberciti.biz/faq/creating-soft-link-or-symbolic-link/

How to chown a symlink: https://superuser.com/questions/68685/chown-is-not-changing-symbolic-link

Old method:

ln -s TARGET LINK_NAME

Cronjob/Crontab

To check a cronjob has at least attempted to run/execute, check this at the time of execution:

tail -f /var/log/syslog | grep CRON

Generator

crontab -e
crontab -l

*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6) (Sunday = 0 or 7)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of        month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

Guides:

http://www.adminschoice.com/crontab-quick-reference

To do a job every X minutes, do */X * * * *

To do a job every minute in a specific hour, do * X * * *

To do a job every X hours, do * */X * * *

To do a job every day at X hour on the hour, do 0 9 * * * for 9am each day.

To do a job every week at 2am on Sunday, do 0 2 * * 0

To monitor active cron jobs, do tail -f /var/log/cron

/usr/local/bin/php: No such file or directory

Do whereis php

Generally it is actually in /usr/bin/php

Cron Daemon email

If you get the below email:

cd / && run-parts --report /etc/cron.daily
/etc/cron.daily/sysklogd:
chown: cannot access `/var/log/mail.warn': No such file or directory
chown: cannot access `/var/log/syslog': No such file or directory
chown: cannot access `/var/log/mail.info': No such file or directory

Do the following:

cd /var/log
touch /var/log/mail.warn /var/log/syslog /var/log/mail.info
/etc/init.d/sysklogd restart

Date

Spaces

To put spaces between variables (e.g +%H%M) use single quotes:

The time is `date '+%R:%S %Y %Z'` on the following Day/Month/Year `date '+%a %b %d'`

Echo colours

Tips for colours and formatting

Place 0; for the normal version (e.g. Black is 0;30)
Place 1; before these to get the light colour version.

Here are the colour codes:

An example:

#!/bin/bash

wipe="\033[1m\033[0m"

black="40m"
darkggrey='\E[1;30m'
red='\E[31m'
lightred='\E[1;31m'
green='\E[32m'
lightgreen='\E[1;32m'
yellow='\E[1;33m'
brown='\E[0;33m'
blue='\E[34m'
lightblue='\E[1;34m'
purple='\E[35m'
lightpurple='\E[1;35m'
cyan='\E[36m'
lightcyan='\E[1;36m'
white='\E[37m'
lightgray='\E[0;37m'
green='\E[32m;'
echo -e "$green$black"
echo Hello World
echo -e "$wipe"

or

echo -e "Output a ${green}coloured${wipe} word."
./colourtest.sh

Hello World

The Hello World text appears green.

Execute one off command whenever

echo "ls -l" | at midnight

This is an alternative to cron which allows a one-off task to be scheduled for a certain time.

Credit to User Root

File Locations (index priority)

/etc/apache2/mods-enabled/dir.conf (on Ubuntu) or /etc/httpd/conf/httpd.conf (on CentOS, ~line 402) and it should show something like:

DirectoryIndex index.html index.html.var index.shtml index.cfm index.php index.htm

This shows the default priority in which the index page is picked up and this can be set in the .htaccess file as well with:

DirectoryIndex index.html index.php

These are good places to check if you are using all of your disk space.

cd /var/log
cd /var/www/vhosts (website and statistics)
cd /var/lib/mysql (database) or mysql/mysql
cd /usr/bin/mysqldump (mysql dump)
cd var/qmail (mail logs, queue and configuration)
cd /var/lib/psa/dumps (physical Plesk backups)
cd opt/psa/tmp/ (Plesk stores temporary files here for backups)

File Permissions

One of the best permissions guide

The values and their equivalent permissions. R is Read, W is Write and X is Execute. There are three sets on a file or folder (e.g. -rwx-w--w- , -rw-r--r-- , -rwxr-xrwx) so you need to put in a value of three (or four in rare occasions) for a file/folder e.g. 644.

0  ---<br>
1  --x
2  -w-
3  -wx
4  r--
5  r-x
6  rw-
7  rwx

The syntax for chmod is (for example) 755 fileorfolder. The -R option can also be used to set the permissions on anything below a folder.
The syntax for chown is chown user:group fileorfolder . To apply the user:group to anything below a folder you need to use the -R option.

Permissions Calculator
Help Guide 1
Help Guide 2
Unix Notation
File protection with chmod

If you use PHP Support as Apache module, it will use the third value of -rw- r-- r-x to permissions. If it is using Fast CGI it will use the user:group for the first two values -rwx rw- --x

If you are using Plesk a good place to check if you are getting Forbidden errors on your website is /var/www/vhosts/yourdomain.com/httpdocs/statistics/logs/error_log . Access your site and tail -f that file. If you get .htaccess pcfg_openfile: unable to check htaccess file, ensure it is readable name your .htaccess file correctly, set your httpdocs to 757 and see this link.

Special permissions

There is also a, g & s which are not widely used as it just needs additional representation at the special/setid permission to the group.

To set a file to -rws--x--x for example use

4 = setuid - s
2 = setgid - s
4 + 2 = S
1 = Sticky Bit
4 + 2 + 1 = T
2511  -r-x--s--x (e.g. /var/qmail/bin/qmail-remote or /var/qmail/bin/qmail-queue)
4655  -rwSr-xr-x
4711  -rws--x--x
4744  -rwsr--r--
4755  -rwsr-xr-x
6411  -r-S--s--x.
6511  -r-s--s--x
6644  -rwSr-Sr--
6666  -rwSrwSrw-
7000  ---S--S--T
7644  -rwSr-Sr-T
7711  -rws--s--t
7744  -rwsr-Sr-T
7755  -rwsr-sr-t

Owner and Group advice for websites

Find command guide

Fix file and folder permissions easily

FreeBSD

  • mysqldump location: /mysql/bin/mysqldump
  • Remove syntax:

- rm -r folderName

  • Generic tunneling interface starts with gif

Check software versions

fetch instead of wget

To download a file.

whereis instead of locate command

To find files/folders.

Full Directory listing

apt-get install tree
yum install tree
tree > tree.txt

If you feel adventurous do cat tree.txt , it will take a while ;)

Find the deepest directory in your server/file structure

find . -type d -printf '%d:%p\n' | sort -n | tail -1

Find and remove specific file types from current directory

cd into the directory
find . -type f -name '*.filetype' -exec rm -v {} \;

Line count a file

wc -l /file/path

Generate random number

Between 1 and 10:

seq 10| shuf | head -1

GeoIP - Block countries accessing website

Ubuntu: apt-cache search geoip ; apt-get install geoip-database libgeoip-dev libgeoip1 python-geoip geoip-bin libapache2-mod-geoip tclgeoip
CentOS: yum list |grep -i geo , yum install GeoIP.x86_64
http://www.webhostingtalk.com/showthread.php?t=959646
http://askmatt.co.uk/blog/2010/05/block-countries-using-apache-mod-geo-ip-list-of-countries-by-fraud-risk

Gunzip and Zip

To gzip a file (.gz) up, do:

gzip file

The above won't work for folders.

Or if zip is installed:

zip -r filename.zip filename

To unzip a .gz file, do:

gunzip file

To extract a .tgz file, do:

tar zxvf fileNameHere.tgz

See http://serverkb.co.uk/wiki/Linux#tar_command for further details.

7zip

https://www.howtoforge.com/tutorial/how-to-install-and-use-7zip-file-archiver-on-ubuntu-linux/

DO NOT USE the 7-zip format for backup purpose on Linux/Unix because:

- 7-zip does not store the owner/group of the file.

On Linux/Unix, in order to backup directories you must use tar:

- to backup a directory  : tar cf - directory | 7za a -si directory.tar.7z

- to restore your backup : 7za x -so directory.tar.7z | tar xf -

If you want to send files and directories (not the owner of file) to others Unix/MacOS/Windows users, you can use the 7-zip format.

Example:

7za a directory.7z directory

Do not use "-r" because this flag does not do what you think.

Do not use directory/* because of ".*" files (example  : "directory/*" does not match "directory/.profile")

https://www.unixtutorial.org/2014/08/7zip-ubuntu/

Hostname guide (rough)

If the server runs Plesk and Virtuozzo, the permanent one needs setting on the hardware or in Virtuozzo.

Run dig -x IP.IP.IP.IP from any Linux server and it will show you the PTR/hostname.

Note: Most servers by default come with a non resolving hostname of localhost, localhost.localdomain or something generic.

The hostname should never be left as the default value on a server sending mail, as it is one of three things mail recipient's mailservers see to determine if mail is spam or not. The other two are reverse DNS and the SMTP banner.

If Plesk throws an error when clicking Websites & Domains tab regarding hostname -f, see this resolution.

How to fix broken packages

Run the following commands below:

dpkg --configure -a
apt-get install -f
#or
apt-get -f install

If the problem still exists, then edit dpkg status file:

gksudo gedit /var/lib/dpkg/status

Find the package in question, and remove everything that has anything to do with it and save the file.

How to install .deb files

dpkg -i filename.deb

.htaccess

HyperText access

inode usage

df --si
df -ih

A lot of the time the cause can be /tmp (/var/lib/php/session/ on Plesk) due to sessions. You may want to delete the files in there.

Investigating high load

If you are getting a high load average in top, these are some of the steps you can take to investigate the issue.

Check which process has the most open of itself:

ps aux | awk '{print $11}' | sort | uniq -c | sort -nk1 | tail -n5

Stop that process, then run the above command a second time. Then start it again and run the command a third time.

Useful software

  • top
  • htop
  • iotop

All these will do the job. Firstly check the CPU wait time, this is shown within top in Cpu(s): 8.0%us, 2.8%sy, 0.0%ni, 40.7%id, 48.3%wa

%wa in

If this is high, check the Status column (S column in top) to see if any are labelled D. The processes blocked on IO are the ones marked as D.

On a Plesk server

Ensure sites are running PHP as Fast CGI Application instead of Apache module so you can see which USER the process is running as. Pressing the letter "c" on your keyboard will show the path and normally the website name.

wget http://serverkb.co.uk/tools/memcpu.sh ; chmod +x memcpu.sh ; ./memcpu.sh > usage.log &
tail -f usage.log

You can alter the PHP Handler on Plesk boxes in the psa database easily by doing:

mysql -uadmin -p`cat /etc/psa/.psa.shadow`;
use psa
select * from hosting\G
select dom_id,www_root,php_handler_type from hosting;
update hosting set php_handler_type="module" where dom_id=x;

If sites run PHP as an Apache module scripts will execute as the Apache user www-data, this can make it difficult to see which site they belong to. This also means scripts run with privileges of the Apache user so if an account is compromised an attacker can get access to all other accounts. Also running as Apache module can make the Apache process CPU report look artificially high. Running PHP as Fast-CGI executes scripts as an FTP user associated with each subscription allowing easier identification of problem scripts and limit the damage of rogue scripts.

CPU reports are not an easy way to determine server health. We'd recommend you look at changes and trends rather than the absolute numbers. Most importantly consider your real world performance.

Linux Container

This install below is for an Ubuntu physical server, I may update this in the future for CentOS, Fedora and others.

https://help.ubuntu.com/12.04/serverguide/lxc.html

If you need to get file off the container, you can just scp it off. If the recipient server is slow, try moving the file to the host machine by doing:

scp -Psshport file root@hostIPaddress:~

Installation of LXC

apt-get install lxc
cat /etc/init/lxc-net.conf | grep USE_LXC_BRIDGE

If true set to false unless you want the containers to NAT to your servers real IP addresses, and to be accessible externally.

Pre-container creation steps

To reduce errors pre-container creation do the following:

dpkg-reconfigure locales
locale-gen en_GB
update-locale LANG=en_GB.UTF-8

Creating/deleting containers

OpenVZ Template list

Check the templates below and pick one:

cd /usr/lib/lxc/templates/ ; ls -lah

Create a container from one of the templates:

lxc-create -t ubuntu -n NameOfTheContainer

If you want to install the fedora package, do apt-get install yum

To delete it just do:

lxc-destroy -n NameOfTheContainer

Start/stop a container

lxc-start -n NameOfTheContainer -d

/etc/init.d/lxc stop

Access the container

The default user is 'ubuntu' with the password 'ubuntu', to enter a container, do:

lxc-console -n NameOfTheContainer
sudo -i

Exit using Ctrl + a, then press q

To re-enter the container, do lxc-console -n NameOfTheContainer and then press enter (you may have to a few times)

Access externally:

iptables -t nat -A PREROUTING -p tcp --dport 2222 -j DNAT --to 10.0.3.61:22

Make sure you remove this rule afterwards and DO NOT reboot your server.

Configuration settings

  • By default you can ping a container from the host, and vice versa, and you can ping the outside world from the container.
  • You can set the hostname just like a normal server, if you want to rename the container.

Default configuration of system files

cd /var/lib/lxc/nameofcontainer/rootfs/etc/sysconfig/network-scripts
vim ifcfg-eth0
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
HOSTNAME=phptester
NM_CONTROLLED=no
TYPE=Ethernet
MTU=
vim /var/lib/lxc/nameofcontainer/config
lxc.network.type=veth
lxc.network.link=lxcbr0
lxc.network.flags=up
lxc.network.hwaddr = MAC Address
lxc.utsname = MT
vim /etc/lxc/lxc.conf
lxc.network.type=veth
lxc.network.link=lxcbr0
lxc.network.flags=up

You can add the below to /etc/network/interfaces

auto br1
iface br1 inet dhcp
    bridge_ports eth0

Fedora

The mirrors/repositories the container uses may be broken by default, don't try to install anything. You'll likely get:

Error: Cannot retrieve repository metadata (repomd.xml) for repository: fedora. Please verify its path and try again

And you likely won't be able to ping anything except the host machine and localhost/127.0.0.1

iptables -t nat -A POSTROUTING -s ContainerIP/24 -j SNAT --to-source PhysicalHostIP
iptables -t nat -A PREROUTING -m tcp -p tcp --dport 10022 -j DNAT -i eth0 --to-destination ContainerIP:80
iptables -t nat -A PREROUTING -m tcp -p tcp --dport 10443 -j DNAT -i eth0 --to-destination ContainerIP:443

libvirt

vi /etc/yum.repos.d/fedora.repo
vi /etc/yum.repos.d/fedora-updates.repo

Uncomment (#) the lines starting with "baseurl"

yum update

OpenSUSE

Version history

http://www.lacerta.be/d7/content/opensuse-lxc-container-inside-ubuntu

List containers

lxc-list

Set passwords

Log in as the root user of the container:

Fedora container:

Username: root
Password: root

Set the root password to something different:

passwd

You will need to do yum install vim when inside the server.

Ubuntu container:

sudo -i
Username: ubuntu
Password: ubuntu

Set the user's password:

passwd ubuntu

Set the root user's password:

passwd

SSH in externally

To route from externally through the host to the container, just do the below iptables rule:

iptables -t nat -A PREROUTING -p tcp --dport 1337 -j DNAT --to 10.0.4.60:22
iptables-save

In the above case we are saying:

- You want to SSH in on port 1337
- The container's eth0 IP address is 10.0.4.60
- Then below we are saying the physical machine has an IP address of 110.111.112.113

Then externally from the server do:

ssh [email protected] -p2222

And bingo! You should be in the container.

passwd

LSB Init Scripts

ls list only directories or files

Directories:

ls -lad */

Files:

ls -la | grep -v ^d

Kill tty session

w
ps aux | grep bash | grep -v grep
ps aux | grep tty | grep -v grep
kill -HUP <processid>

Further info

Maldet

Documentation
Maldet .tar.gz
Install and Configure

cd /root ; wget http://serverkb.co.uk/tools/maldet.sh ; chmod +x maldet.sh ; ./maldet.sh

Always run your scans from chroot environment (if it has one/is possible) and in Screen (screen -S NameIt), and detach by doing CTRL + A, then press D

Scan reports are normally stored in /usr/local/maldetect/sess/ as session. files.

maldet -a -e -l filepath

Manually alter time/date

Ubuntu

date
date mmddtimeyear #as seen below

Cent OS

cd /etc/
ls -lah
rm localtime
ln -s /usr/share/zoneinfo/Europe/London /etc/localtime
date mmddtimeyear
     | |  |   | ---> 2012
     | |  | 24hr --> XX:XX without :
     | | dd -------> day
     | mm ---------> month

/etc/init.d/ntpd restart or start
date

Mount NFS drive

apt-get install nfs-common
cd /media
mount -t nfs HostName:/export/ftpbackup/ServiceName /FolderMount

The example above contains variables, which you will need to substitute with your own values.

HostName: The host name of your backup storage
ServiceName: The name of your server (e.g. ns0000000.ip-123-123-123.net)
FolderMount: The folder where you want to mount the NFS share

e.g.

mount -t nfs ftpback-xxx1-123.ovh.net:/export/ftpbackup/ns123456.ip-XX-XXX-XXX.eu /media/YourNewFolder

Move files into your home directory not owned by your own user

Copy to /var/tmp or /tmp (for small files) and do:

chmod ugo+rw <filename>

PID

Under construction.

Process ID.

To determine the usage of a specific process, do top -p PID

You can use the "c" key to show the file path a PID is being called from.

The maximum number of pids can be obtained from /proc/sys/kernel/pid_max

.profile

Put this in .profile file for on user startup:

echo "" ; df -h | sed -ne '1p;5p' ; echo "rootfs"
echo "" ; free -m
echo "" ; w | head -1 | sed 's/^ *//g'
echo "" ; w | tail -10 | grep -v average ; echo ""

Proxmox

To access via the web go to https://IPaddress:8006

Rough notes:

apt-get install sudo

Proxmox

/var/lib/vz

dump is for backups
images is for OS images
private is for OpenVZ container file systems
template/cache is for OpenVZ templates

http://openvz.org/Download/template/precreated

Create VM creates KVM
Create CT creates OpenVZ container

vzctl enter id
vzctl start/stop id

iptables -t nat -A POSTROUTING -o vmbr1 -j MASQUERADE
restart networking on host and CT

cman_tool: Cannot open connection to cman

pvecm status
pvecm nodes
cman_tool: Cannot open connection to cman, is it running ?
service pve-cluster restart
pvecm delnode NodeName

Force remove an OpenVZ container

vzctl stop 100 ; vzctl destroy ContainerID
cd /var/lib/vz/private
rm ContainerIDfolder -R
cd /var/lib/vz/root
rm ContainerIDfolder -R
cd /etc/pve/nodes/ContainerName/openvz
mv ContainerID.conf ContainerID.bak

Make sure it does not exist in cat /etc/pve/.members

Remount a logical partition/volume

lvdisplay

/dev/mapper/pve-data /var/lib/vz (this will be different in your file system)

Unable to get local IP address

/etc/init.d/pve-cluster restart
service pve-cluster start
Starting pve cluster filesystem : pve-cluster[main] crit: Unable to get local IP address
 (warning).

Make sure in /etc/hosts your domain name resolves to the server and you also have it without the .co.uk/.com etc in the file as so:

ServerIPaddress domain.co.uk domain pvelocalhost
127.0.0.1 localhost localhost.localdomain

Then do:

/etc/init.d/hostname.sh stop
/etc/init.d/hostname.sh start
service pve-cluster start

Transport endpoint is not connected

df -h
df: `/etc/pve': Transport endpoint is not connected
ls -lah /etc/pve
ls: cannot access pve: Transport endpoint is not connected
d?????????   ? ?    ?                      ?            ? pve

Do this:

umount /etc/pve
pvecm status

You will get:

unable to get IP for node 'hostname' - node offline?

The fix is the same as this one, ensure you have the domain without the web extension in /etc/hosts resolving to the servers IP address. Then do:

service pve-cluster start

Recover deleted files

You need to install this software before you delete any files:

apt-get install foremost

Then see this documentation:

https://help.ubuntu.com/community/DataRecovery
http://ddailygirl.wordpress.com/2010/08/17/recovering-files-after-rm-in-linux
http://www.howtoforge.com/recover-deleted-files-with-foremost
http://www.webupd8.org/2009/03/recover-deleted-files-in-ubuntu-debian.html

Remove file starting with dash

rm -- -filename

Remove folder starting with dash

rm -rf -- -folder/

Remove/rename file called tilde

mv '~' newfilename
rm '~'

Restart service or service

Useful for differently named ones, e.g.

[ -f /etc/init.d/mysqld ] && service mysqld restart ; [ -f /etc/init.d/mysql ] && service mysql restart

[ -f /etc/init.d/httpd ] && service httpd restart ; [ -f /etc/init.d/apache2 ] && service apache2 restart

Roughly list file count

This includes nested directories:

find /full/file/path -type f | wc -l

SCP Command - Secure Copy

To secure copy a file from one Linux server to another, use the following syntax form:

scp -P PORT file user@IPAddress:/filepath

For example:

scp -P 22 index.html [email protected]:/

To move a folder, put -r in between the port and the file(s)/folder(s) like so:

scp -P 22 -r testdirectory/ [email protected]:/

If you receive the following error while trying to SCP a file from one server to the other:

stdin: is not a tty

You can solve the issue quickly by doing the following on the destination server:

vi ~/.bashrc
  
if [ $(expr index "$-" i) -eq 0 ]; then
return
fi

Screen

Re-join screen session

screen -r

or

screen -D -r '1234.somescreensession'

A better alternate is tmux.

Manage Multiple sessions in one Terminal
Scrollback (vim /home/.screenrc + defscrollback 1000)
Tips and Tricks

Setting the time

http://geoffhankerson.com/node/112
http://codeghar.wordpress.com/2007/12/06/manage-time-in-ubuntu-through-command-line/

Standard redirection

http://www.xaprb.com/blog/2006/06/06/what-does-devnull-21-mean/

STDIN, STDOUT and STDERR.

0, 1 and 2

1>/dev/null
2>/dev/null

sudo

If you exit out of root access and want to run the last command you entered without authentication, do:

sudo !!

System Logs and Shutdown troubleshooting

If you are having an issue, a reboot should not be performed if the server can be accessed in any way (e.g. locally in the data centre, only if it is a Dedicated server). This is because after a reboot there is little you can find out from the logs as the important logs get cleared on restart.

last reboot
last

Do this to check which files exist:

ls -lh /var/log/syslog ; ls -lh /var/log/kern.log ; ls -lh /var/log/dmesg ; ls -lh /var/log/messages

Then do one or more of these depending on which exist:

grep -i error /var/log/syslog ; grep -i panic /var/log/syslog ; grep -i warning /var/log/syslog
grep -i error /var/log/dmesg ; grep -i panic /var/log/dmesg ; grep -i warning /var/log/dmesg
grep -i error /var/log/kern.log ; grep -i panic /var/log/kern.log ; grep -i warning /var/log/kern.log
grep -i error /var/log/messages ; grep -i panic /var/log/messages ; grep -i warning /var/log/messages

errpt may show an error report on some Unix OS'.

To find .log files, run updatedb and then locate *.log

To restart and on boot do a disk check do (or -rF):

shutdown -Fr now

tar command

To archive and compress a folder/files do:

tar -czvf files.tgz files/
tar -czvf folder.tar.gz folder/
tar cvf mubackup.tar mu/ ; gzip -9 mubackup.tar

#.tgz is same as .tar.gz

c creates the archive (tar), z compresses it into the gzip, v is verbose, f is the file/folder

To extract do:

#for tar
tar xvf file.tar

#for .tgz or tar.gz
tar zxvf file.tar.gz

Guide 1
Guide 2
3 and 4

For bz2 files, use:

tar -xvjpf file

top

The TIME column in top is displayed in minutes:seconds.hundredths

tmux

C-c is CTRL+C
C-m is ENTER

New session

Latest:

tmux new -s session_name

Also possible:

tmux new-session -s session_name

Old:

tmux new-session -n NameTheSession

Detach from a session

CTRL +B then D

Kill session

tmux kill-session -t myname

If this fails and it says:

"session not found"

Do the following:

tmux ls
tmux kill-session -t X

- where X is the number of the session on the left.

List sessions available

tmux ls

Re-attach to session

tmux attach
tmux attach -t ID or NameTheSession

Remotely execute a command

tmux send-keys -t ID "command" ENTER

User ID 99

This is most commonly the user nobody. This can be caused by PHP or done on purpose. cat /etc/passwd | grep 99 ; vim /etc/passwd

User is not in the sudoers file

If you try to sudo into a server or run sudo and get the following message:

"is not in the sudoers file. This incident will be reported."

Do the following command as root and add the username into the file in the same format as the root user:

visudo

Use unusual characters in filenames

If you want to specify a space, lets say the file = /usr/local/etc/testingdatabase.sql

You need to name the file/folder as follows:

mv /usr/local/etc/testingdatabase.sql /usr/local/etc/testing\ database.sql

This will make the file be testing database.sql , on command line this will appear as testing\ database.sql .

A backslash symbol \ needs to be used before an apostrophe ' , bracket (), exclamation/bang ! symbol or question marks ?:

01\ -\ It\'s\ You.txt
#How it appears: 01 - It's You.txt

02\ -\ Boom\!.m3u
#How it appears: 02 - Boom!.m3u

03\ -\ Why\ Wont\ You\ Work\?.sh
#How it appears: 03 - Why Wont You Work?.sh

04\ -\ Musical\ Playlist\ For\ \(VPS\)\ Server.m3u
#How it appears: 04 - Musical Playlist For (VPS) Server.m3u

updatedb (locate command)

This command is used when you cannot locate a file and you get "locate: warning: database /var/lib/slocate/slocate.db' is more than 8 days old". It is advised to run updatedb at least once a month. However if you get the following error when using it:

updatedb: fatal error: load_file: Could not open file: /etc/updatedb.conf: No such file or directory

You need to create or edit this file

vim /etc/updatedb.conf

and put the following inside of it:

PRUNE_BIND_MOUNTS="yes"
PRUNEPATHS="/tmp /var/spool /media"
PRUNEFS="NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs shfs sysfs cifs lustre_lite tmpfs usbfs udf"

Alternatively run the below script after reading this file http://serverkb.co.uk/tools/README.txt:

http://serverkb.co.uk/tools/updatedbscript.sh

To vim a file you locate, see this. vim $(command)

Cannot find an existing file

Ensure that the directory path of the file that does exist is not in the following section in /etc/updatedb.conf:

PRUNEPATHS="/tmp /var/spool /media"

vim

To make a copy the line below where you cursor is, hold CTRL + e. Then put a hash (comment) in front of this line. This is useful for backing up an old value before changing it.

Create backup of file whilst in vim

vim file
:!cp % %-

Press enter and continue to edit

Credit to User MPB

Enter Insert mode

Press the letter i (I)

Enter Replace mode

Press the letter r (R)

E21: Cannot make changes, 'Modifiable' is off

:set modifiable

Search in file for multiple terms

Go to search mode i.e. type '/' and then type \v followed by the words you want to search separated by '|' (pipe).

Example:

/\vword1|word2|word3

Go to search mode and type the words you want to search separated by '\|'.

Example:

/word1\|word2\|word3

The first way puts you in the regular expression mode so that you do not need to put any extra back slashes before every pipe or other delimiters used for searching.

See changes made before exiting

:w !diff % -

Editing a jar/zip file

(zip#Write) sorry, your system doesn't appear to have the zip pgm

1) Check zip is installed

2)

:set modifiable
:set write

3)

E382: Cannot write, 'buftype' option is set

Check if the file has -e on it using:

lsattr filename

If so, you likely cannot edit it.

4)

Alternatively, download the file (e.g. a jar) and use an Archive tool like 7-zip to edit it.

VPS Hints and Tips

To check for the filepath of a command run top and then press c.

If it is a container on a node, there are generally no datacentre / rack level restrictions as the container is virtualised on a node. The only restrictions are what is put in place via the container itself effectively.

To list all open Internet, x.25 (HP-UX), and UNIX domain files, use:

lsof -i -U

WHOIS script

Pipe Viewer, PV info page

apt-get install pv ; cd /var/www/vhosts ; ls -l | awk ' {print $9}' > domainlist ; wget serverkb.co.uk/tools/findregistrar.sh ; chmod +x findregistrar.sh

For CentOS use yum -y install jwhois.x86_64

Edit the file and replace domain registrar with the one you want to find. Then do:

./findregistrar.sh

write error

If you get the following error when doing write user TTYsession or write user Session:

write: write: you have write permission turned off.

Do this to fix the problem:

mesg y

Writing Shell Scripts