Here are a few important commands you would need to check the disk usage on a linux machine
A simple df (disk filesystem) command can help us get important info on linux file system. -h will make the data more human readable.
$ df -h
More variations of df -h command
Another important command would be du (disk usage) for a particular folder. du -h would give same data in human readable form.
Other useful variation of du command
du- sh *
du -Pshx /* 2>/dev/null
If you want to send an email with attachment through linux server.
echo “This is the message body” | mutt -a “/path/to/file.to.attach” -s “subject of message” — firstname.lastname@example.org
I wanted to check if the service I am trying to access on a server is actually listening on the port I am hitting. I tried to look for a ping variant which could tell me if the port is listening and required service is up and running.
Found nmap command as answer.
nmap -p 80 google.com
Starting Nmap 6.40 ( http://nmap.org ) at 2016-03-02 16:05 IST
Nmap scan report for google.com (126.96.36.199)
Host is up (0.050s latency).
rDNS record for 188.8.131.52: maa03s21-in-f14.1e100.net
PORT STATE SERVICE
80/tcp open http
It checks the port state and also what service is listening at the port.
This command will replace all the occurences for oldstring with newstring after confirming for each existence (c in the end tells vim to ask for confirmation).
More on search replace – http://vim.wikia.com/wiki/Search_and_replace
Some other useful commands are
/ for search
n find next while search (after /)
N find previous while searcg (after /)
:$ last line of file
:0 first line of file
There might be times when you want to control outgoing or incoming traffic from a linux machine. Iptables is answer to that.
To check current settings
sudo iptables -L
To Add a rule
iptables -A OUTPUT -p tcp –dport 8000 -j DROP
Lets get into details
-A: Add the rule
OUTPUT: Type of rule, OUTPUT or INPUT
-p: protocol tcp/ udp
–dport: port number (8000 here)
-j: DROP or ACCEPT
So Above command tell system to not allow any outgoing traffic on port 8000.
iptables -A OUTPUT -p tcp –dport 1935 -s 184.108.40.206 -j ACCEPT
The above rule states to allow outgoing packets on port 1935 to a specific IP.
If we have centos based system
sudo vi /etc/sysconfig/iptables
sudo /etc/init.d/iptables restart
There can be times that a process becomes unresposive and you want to kill it forcefully.
In linux, we will need to first find out the process id
ps command- it gives a list of all processes running.
ps aux- gives all process details with, a = show processes for all users, u = display the process’s user/owner, x = also show processes not attached to a terminal
to fetch only required process- use grep, ps aux| grep ‘name’
Then kill it forcefully- kill -9 PID
Putting it all together.
Lets say I want to kill eclipse
kamalmeet@System:~$ ps aux | grep eclipse
1101 6070 1.3 1.8 46584 5500 ? Sl 12:34 1:38 /usr/bin/java -Dosgi.requiredJavaVersion=1.6 -XX:MaxPermSize=256m -Xms40m -Xmx512m -jar /home/kamalmeet/eclipse//plugins/org.eclipse.equinox.launcher_1.3.0.v20140415-2008.jar
kamalmeet@System:~$ kill -9 6070
There are three type of groups who can access a file in linux.
User: who created the file
Group: Group to which user belongs
Others: Other users (not belonging to above 2 categories).
To add/ modify permissions use following format
chmod u=rwx,g=rw,o=rx abc.txt
or a simpler option
chmod 765 abc.txt
Both of the above means same.
How to interpret these numbers
hence Read+Write+Exceite=4+2+1=7 or Read+ execute=4+1=5 and so on.
How will you set up a job to run every 30 minutes, from midnight till 10 in morning, Monday to Friday?
*/30 0-10 * * 1-5 MyJob
Understanding the syntax
* * * * * *
| | | | | |
| | | | | +– Year (range: 1900-3000)
| | | | +—- Day of the Week (range: 1-7, 1 standing for Monday)
| | | +—— Month of the Year (range: 1-12)
| | +——– Day of the Month (range: 1-31)
| +———- Hour (range: 0-23)
+———— Minute (range: 0-59)
More on Cron Jobs
Two problems- one solution i.e. 10.0.2.2
Sometime back, I faced an issue that I had installed a virtual machine on mu linux machine. I had a server/ website running which I wanted to access from virtual machine. http://localhost/site simply refused to work. A little googling helped with an answer to use http://10.0.2.2/site
Again faced similar issue. Was trying to access local website from within android emulator. Again localhost refused to work. And guess who was the rescuer? yep 10.0.2.2 again 🙂
Creating shortcuts in linux- It can be a pain to write down long linux commands again and again. So here is how you can create quick shortcuts for all reusable commands.
1. Edit bashrc: You will have a “.bashrc” file in your root folder. open it up for editing (emacs .bashrc) and add commands to be added for shortcut at the bottom of this file like
alias connect=’ssh email@example.com’
This will simply create a shortcut for the command ‘ssh firstname.lastname@example.org’ which will get executed by typing connect.
2. Compile bashrc: Before you can actually use your shortcuts, you will need to compile the .bashrc file by
3. Use shortcuts: Just type the shortcut command connect and you will see that it actually executes the command for which ‘connect’ is an alias.