Linux
From Shiftyjelly
Tips
Apache Tips
User Management
Add an existing user to a group
usermod -gtomcat6 internode -g is the users primary group, files created will be have this group assigned to them
Run Multiple Commands on the command line
ant clean build && cd war && ./runStuff.sh && cd ..
How to create change the default port and username for ssh connections
Add a new file called ~/.ssh/config with the following contents: Host myserver.com hostname myserver.com user bob port 999 To connect just type ssh myserver.com and the username bob and port 999 will be used. Note: This works with both ssh and scp
Copy your public key to the server
If you need to generate your key:
ssh-keygen -t rsa
To copy it:
cat ~/.ssh/id_dsa.pub | ssh user@hostname "cat - >> ~/.ssh/authorized_keys" or ssh user@hostname "echo `cat ~/.ssh/id_dsa.pub` >> ~/.ssh/authorized_keys" Make sure the folders permissions are the following drwx------ .ssh -rw------- authorized_keys You can do this with chmod chmod 700 .ssh chmod 600 authorized_keys
Or if your l33t and want to do it all in one line:
cat ~/.ssh/id_rsa.pub | ssh user@hostname "mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys && cat - >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys && chmod 700 ~/.ssh"
How to change the computers run level
init 5 0 â Halt 1 â Single-user text mode 2 â Not used (user-definable) 3 â Full multi-user text mode 4 â Not used (user-definable) 5 â Full multi-user graphical mode (with an X-based login screen) 6 â Reboot
How to create swap space on Debian
Check if there is any swap already
swapon -s
Create a file for the swap space
dd if=/dev/zero of=/usr/swapfile.1 count=512000 bs=1024
Make sure only root has read/write access
chmod go= /usr/swapfile.1 ls -l /usr/swapfile.1 -rw------- 1 root root 524288000 Oct 6 16:54 /usr/swapfile.1
Assign the swap space
mkswap /usr/swapfile.1
Turn swap on
swapon /usr/swapfile.1
Check the swap is being used
swapon -s
Add to /etc/fstab so swap is enable on boot
vi /etc/fstab
/usr/swapfile.1 swap swap defaults 0 0
How to monitor the disk io usage
apt-get install sysstat
iostat 10
avg-cpu: %user %nice %system %iowait %steal %idle
15.72 0.01 0.63 5.07 0.02 78.54
Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
sda1 6.86 62.23 142.21 835610 1909592
If the %iowait is > 5% it means the cpu is waiting for the disk a little too much!
Commands
apt-get / dpkg
Install a deb file
dpkg -i file.deb
List the installed files in a package
dpkg -L apacheds
Search apt repository
apt-cache search "ldap"
init.d
Restarting a service
invoke-rc.d apache2 restart or /etc/init.d/apache2 restart
List the services runlevels on CentOS
chkconfig --list
Installing a new service on CentOS
chkconfig --level 345 nginx on
Installing a new service on Debian update-rc.d -f nginx defaults
Stopping a service from starting on Debian update-rc.d -f nginx remove
cat
Append one file to another
cat server.policy.ext >> server.policy
Append text to a file inline
cat > jruby.sh << EOF export PATH="\$PATH:\$HOME/Development/jruby/jruby-1.1.6RC1/bin" export JRUBY_HOME="\$HOME/Development/jruby/jruby-1.1.6RC1" EOF
mkdir
Create a directory and make parent directories as needed
mkdir -p ~/svn/test/trunk
grep
Search for files that contain the keyword ausbat. Case insensitive (i), print line numbers (n) and recurse directries (r).
grep -inr "ausbat" *
find
Find files starting with gnome resursively from the root directory. Print the result to the screen and hide errors.
find / -name 'gnome*' -print 2 > /dev/null
Find files with the name '.svn' resursively from the current directory. When the file is found remove them from the file system.
find . -name '.svn' | xargs rm -r
Remove all the startup scripts for openvpn
sudo find -name "S*openvpn*" | xargs sudo rm
ssh
Create a secure connection to mysite.com on port 123 with username bob.
ssh -p 123 bob@mysite.com
Create a secure connection to mysite.com and X11 forward to your machine.
This means you could for example start Firefox on the server and the window would be displayed on your local machine.
ssh -X bob@mysite.com
Map port through using ssh
ssh -p 22 -L 9090:192.168.1.1:8080 -L 4949:192.168.1.1:4848 username@150.101.1.1
scp
Securely copy the web.war local file to the mysite.com remote site into the home directory (~). Copy over port 123 (P).
scp -P 123 web.war mysite.com:~
chmod
Add execute permissions to the test.sh script for the user that owns the file.
chmod u+x test.sh
Add read, write and execute premissions to the removeWindows.sh script for the group that owns this file.
chmod g+rwx removeWindows.sh
Remove execute premissions from the burnDvd.sh script for the other users.
chmod o-x burnDvd.sh
Add read premission to the copyMusic.sh script for all users.
chmod a+r copyMusic.sh chmod +r copyMusic.sh
chown
Change the ownership of file google.tar.gz to the user of âbobâ.
sudo chown bob google.tar.gz
Change the ownership of file google.tar.gz to the user of âbobâ and group of âadminâ.
sudo chown bob:admin google.tar.gz
chgrp
Change the ownership of file google.tar.gz to the group of âadminâ.
sudo chown admin google.tar.gz
tar
Create a tape archive file.
tar -cvf archive.tar files
Extract a tape archive file.
tar -xvf archive.tar
Create a compressed gzip archive file
tar -czvf archive.tar.gz files
Uncompress a compressed gzip archive file
tar -xzvf archive.tar.gz
Create an archive file with both test1.txt and text2.txt.
tar -czvf archive.tar.gz test2.txt test1.txt
Uncompress an archive file into the geek directory
tar -xzvf archive.tar.gz -C geek/
Create a compressed bzip2 file. (bzip2 compresses most files more effectively than more traditional gzip or zip)
tar -cvjf archive.tar.bz2 file-list
Extract a bz2 file
bunzip2 database.sql.bz2
tail
Follow the end of the file. This is useful for monitoring server logs.
tail -f catalina.out
Show this last 200 lines of the file.
tail -n 200 catalina.out
Only monitor the file output for a specific line
tail -f production.log | grep 123456
ln
Creates a symbolic link to the glassfish directory.
ln -s /usr/java/glassfish glassfish
ps
Show all linux processes that the user âbillgatesâ is running (U) and display full format listings (f).
ps -fU billgates
How to tell if apache is already running
ps -ef
How to tell if apache is already running
ps -ef | grep apache2
How to tell if mysql is already running
ps -ef | grep mysql
locate
Show the path to the mysql command.
locate mysql
Disk Usage
Show the size of each directory in the current folder (s), the total of the directory (c) and in human readable form (h).
df -h
Show how much hard disk space you have left in human readable form (h).
du -sch *
Managing Processes
Find out which processes are using up all your CPU.
top
Kill all processes with the name âgaimâ.
killall gaim
Try to kill the process with the ID 12345.
kill 12345
Kill the process with the ID 12345 and donât be friendly about it :)
kill -9 12345
Move the current process in the terminal into the background so that you can type more commands.
[Ctrl]z bg
End the current process in the terminal.
[Ctrl]c
Killing a process using a grep
sudo kill `ps aux | grep jetty | grep -v ' grep ' | awk '{ print $2 }'`
Networking Diagnosis
Display the network configuration including the machineâs IP address.
ifconfig
Control if a network interface is running
sudo ifconfig ath0 down sudo ifconfig ath0 up
Restart your network
sudo /etc/init.d/networking restart
Manually edit your network settings
sudo gedit /etc/network/interfaces
Find out the users currently logged on.
who
netstat
Display the applications running on your machineâs ports. Show all sockets (a), only display tcp connections (t) and the program using the port (p).
netstat -atp
Speed up the results by using the ip address and not trying to determine the server names (n)
netstat -atnp
Display domain and IP information.
nslookup www.yourwebsite.com
List open ports on your machine (Linux)
netstat -atp | grep -i "listen"
List open ports on your machine (Mac OS X)
sudo lsof -i -P | grep -i "listen"
View network activity of any application or user in realtime
lsof -r 2 -p PID -i -a The "-r 2" option puts lsof in repeat mode, with updates every 2 seconds. (Ctrl -c quits) The "-p" option is used to specify the application PID you want to monitor. The "-u' option can be used to keep an eye on a users network activity.
jar
Loop in all the jar files in the current directory and search for the Java class called XmlAccess.
for f in $(ls *.jar) ; do echo $f; jar tvf $f 2>/dev/null | grep -i XmlAccess; done
mount
Automatically mount file systems when the computer boot by editing the /etc/fstab file.
/dev/sdb1 /home/shiftyjelly/media ext3 defaults
Mount an iso image to a folder
sudo mount -o loop -t iso9660 file.iso /mnt/tmp
wget
Copy a website locally.
wget -r -w 1 http://api.rubyonrails.com -r option tells wget to recursively download pages that are linked from the start page. -w 1 option tells wget to wait 1 second between requests so the server isn't hammered.
Advanced copy of a website.
wget -kpNrE -w 1 -U Mozilla --no-proxy http://api.rubyonrails.com -k make links in downloaded HTML point to local files. -p get all images, etc. needed to display HTML page. -N don't re-retrieve files unless newer than local. -r option tells wget to recursively download pages that are linked from the start page. -E save HTML documents with `.html' extension. -w 1 option tells wget to wait 1 second between requests so the server isn't hammered. -U identify as AGENT instead of Wget/VERSION. --no-proxy explicitly turn off proxy.
sed
Replace text in a file
sed -i -e "s/adapter: sqlite3/adapter: jdbcsqlite3/g" config/database.yml sed -i -e "s/^system\.environment=.*$/system\.environment=TEST/g" custom-resources.properties
Replace the default linux apt mirror with the internode mirror
sed -e 's/us\.archive\.ubuntu\.com/mirror\.internode\.on\.net\/pub\/ubuntu/g' source.list
Delete leading whitespace from the start of each line
sed 's/^[ \t]*//' input.txt
curl
Get your out going ip address
curl -s ip.appspot.com
vi
Show line numbers
:set number or :set nu
Commands
G - End of file 1G - First line in the file 122G - Go to line number 122 Ctrl + f - Page forward Ctrl + b - Page back
sendmail
echo "Subject: test" | /usr/lib/sendmail -v me@you.com
If you are having problem with mail on Debian take here are some of my findings Debian Email
Issues
stdin: is not a tty
Edit the /etc/bashrc line with 'mesg y' to if `tty -s ` && `test -x /usr/bin/biff`; then mesg y fi