Remote Server Management Code Management Network Management Automation System Administration Linux Solaris, Backup, Networking, Security, Mysql, Script, Tips & Tricks
Friday, July 31, 2009
How to Open 7zip Files Under Ubuntu
#sudo apt-get install p7zip
Step 2 : Exectute this command
#p7zip -d filename.7z
Sunday, July 26, 2009
Setup SAMBA server on Ubuntu
#apt-get install samba
Step 2: Configuration
Configuration file : /etc/samba/smb.conf
- Edit below section
Workgroup = EXAMPLE
…………
Security = user
- Create a new section at the bottom of the file /etc/samba/smb.conf
[Samba Share]
Comment= Samba File Server Share
Path=/path to directory
Browsable = yes
Guest ok = yes
Read only = no
Create mask = 0755
Step 3: Create directory and change permissions
#mkdir /path to directoy
#chown -R nobody:nobody /path to directory
Step 4 : Restart Samba service
#/etc/init.d/samba restart
Enjoy!!!!!!!!!!!!
Thursday, July 9, 2009
Open ports using IPTABLES
If you want your machine to respond to requests initiated from elsewhere on the internet you need to open the required ports. You need to know below details first:
1. Service name you want to open up
2. Is it a tcp or udp service?
3. Port number(s) uses by service?
Example:
To enable ssh access to your box from anywhere on for Class A networks, you could use something like
iptables -A allowed -p tcp --dport 22 -s 10.2.0.0/16 -j ACCEPT
iptables -A allowed -p udp --dport 22 -s 10.2.0.0/16 -j ACCEPT
iptables -A allowed -p tcp --dport 22 -s 10.8.0.0/16 -j ACCEPT
iptables -A allowed -p udp --dport 22 -s 10.8.0.0/16 -j ACCEPT
This allows both udp and tcp traffic from either of the two class A networks to access port 22 on your machine.
Work with tcpwrappers hosts.deny and hosts.allow
You can use tcpwrappers to Block user,ip,daemon from outside or your inside newwork. For allow use host.allow file and to block use host.deny file.
Step 1. Use your favorite editor like vi.
Step 2. Type vi /etc/hosts.deny
Step 3. At the bottom line just type “ALL:ALL:deny” to restricted all of daemon process
Step 4. Save it.
Step 5. Open “/etc/hosts.allow” with vi editor
Step 6. At the buttom line “ALL :(some ip that you allow):allow” to allow anything from my IP address
Step 7. Save it.
Thursday, July 2, 2009
Working with tar and tar Encryption
Suppose you have a directory /stuff. To tar everything in stuff to create a “.tar” file.
# tar -cvf example.tar example
It will create example.tar file.
STEP 2 : (Using the tar command to create a “.tar.gz” of /example)
# tar -czf example.tar.gz example
STEP 3 : (List the files in the archive)
# tar -tzf example.tar.gz
or
# tar -tf example.tar
STEP 4 : (ENCRYPTION)
# tar -zcvf – example | openssl des3 -salt -k secretpassword | dd of=example.des3
This will create example.des3…Please don’t forget the password you put in place of secretpassword.
If you want to do this interactively then
# dd if=example.des3 |openssl des3 -d -k secretpassword|tar zxf -
Wednesday, July 1, 2009
Multiple IP address on Single NIC (Redhat/Fedora)
Below i have mentioned for Redhat/Fedora/CentOs system.
STEP 1 : (Initial/First IP address)
#cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
BROADCAST=10.8.0.255
IPADDR=10.8.0.10
NETMASK=255.255.255.0
NETWORK=10.8.0.0
ONBOOT=yes
STEP 2 : (Second IP address)
#cat /etc/sysconfig/network-scripts/ifcfg-eth0:1
DEVICE=eth0:1
BOOTPROTO=static
BROADCAST=10.8.0.255
IPADDR=10.8.0.11
NETMASK=255.255.255.0
NETWORK=10.8.0.0
ONBOOT=yes
View Routing Table and Change your default Gateway
# netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
10.8.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
0.0.0.0 10.8.0.1 0.0.0.0 UG 0 0 0 eth0
Change Your Default Gateway
You will need to update your /etc/sysconfig/network file to reflect the change. This file is used to configure your default gateway
NETWORKING=yes
HOSTNAME=newhost
GATEWAY=10.8.0.2
Use of Netstat Commands
Check tcp connection established on your machine using below command
1: netstat --tcp
% netstat --tcp --numeric
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 nexubuntu:ldap nexubuntu:38885 ESTABLISHED
tcp 0 0 nexubuntu:38885 nexubuntu:ldap ESTABLISHED
tcp 0 0 nexubuntu:ssh 10.8.0.151:50776 ESTABLISHED
tcp 0 0 nexubuntu:microsoft-ds nexubuntu:59000 ESTABLISHED
tcp 0 0 nexubuntu:38875 nexubuntu:ldap ESTABLISHED
tcp 0 0 nexubuntu:ldap nexubuntu:52342 ESTABLISHED
tcp 0 0 nexubuntu:ssh 10.8.0.151:50775 ESTABLISHED
tcp 244 0 nexubuntu:59004 nexubuntu:microsoft-ds ESTABLISHED
tcp 244 0 nexubuntu:59000 nexubuntu:microsoft-ds ESTABLISHED
tcp 0 0 nexubuntu:ldap nexubuntu:38875 ESTABLISHED
tcp 0 0 nexubuntu:microsoft-ds nexubuntu:59004 ESTABLISHED
tcp 0 0 nexubuntu:52343 nexubuntu:ldap TIME_WAIT
tcp 0 0 nexubuntu:52342 nexubuntu:ldap ESTABLISHED
tcp 0 0 nexubuntu:microsoft-ds nexubuntu:58998 ESTABLISHED
tcp 244 0 nexubuntu:58998 nexubuntu:microsoft-ds ESTABLISHED
If you want to see what (TCP) ports your machine is listening on, use netstat --tcp --listening. Another useful flag to add to this is --programs which indicates which process is listening on the specified port.
2: netstat --tcp --listening --programs
# sudo netstat --tcp --listening --programs
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 *:nfs *:* LISTEN -
tcp 0 0 *:ldap *:* LISTEN 2715/slapd
tcp 0 0 *:ftp *:* LISTEN 3302/vsftpd
tcp 0 0 *:ssh *:* LISTEN 4022/sshd
tcp 0 0 nexubuntu:postgresql *:* LISTEN 2680/postgres
tcp 0 0 *:smtp *:* LISTEN 3259/master
tcp 0 0 *:58777 *:* LISTEN 2098/rpc.statd
tcp 0 0 *:microsoft-ds *:* LISTEN 3283/smbd
tcp6 0 0 [::]:ldap [::]:* LISTEN 2715/slapd
tcp6 0 0 [::]:www [::]:* LISTEN 3750/apache2
Uses netstat --route to display the routing table.
3: netstat --route
% netstat --route
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
10.8.0.0 * 255.255.255.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 0 0 0 eth0
default mygateway1.ar7 0.0.0.0 UG 0 0 0 eth0
Uses the --statistics flag to display networking statistics.
Using this flag by itself displays all IP, TCP, UDP, and ICMP connection statistics.
4: netstat --statistics --route
Network configuration for Debian
#/etc/network/interfaces
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet dhcp
For a static IP
#/etc/network/interfaces
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
address 10.8.0.100
netmask 255.255.255.0
gateway 10.8.0.1
broadcast 10.8.0.255
How to add multiple IP addresses manually
# ifconfig
will output all the configured addresses
eth0: inet addr:10.8.0.10 Bcast:10.8.0.255 Mask:255.255.255.0
To add on extra IP addresses execute below command:
# ifconfig eth0:1 10.8.0.12 netmask 255.255.255.0
For additional IP addresses, make sure you increment the 1 in eth0:1
Third ip address would be like this
# ifconfig eth0:2 10.8.0.13 netmask 255.255.255.0
Assigning a virtual IP to a NIC
In Fedora, all information about the network interfaces is kept in the following directories:
- /etc/sysconfig/network-scripts/
- /etc/sysconfig/networking/
My NIC configuration script is /etc/sysconfig/network-scripts/ifcfg-eth0 looks like this:
DEVICE=eth0
BOOTPROTO=static
BROADCAST=192.168.0.255
HWADDR=00:00:00:00:00:00
IPADDR=192.168.0.1
NETMASK=255.255.255.0
NETWORK=192.168.0.0
ONBOOT=yes
TYPE=Ethernet
GATEWAY=192.168.0.254
Make a copy of this in the same directory naming the new file ifcfg-eth0:1
# cp ifcfg-eth0 ifcfg-eth0\:1
Modification in file ifcfg-eth0\:1 is shown in bold
DEVICE=eth0:1
BOOTPROTO=static
BROADCAST=192.168.0.255
HWADDR=00:00:00:00:00:00
IPADDR=192.168.0.101
NETMASK=255.255.255.0
NETWORK=192.168.0.0
ONBOOT=yes
TYPE=Ethernet
GATEWAY=192.168.0.254
So, its IP address will be 192.168.0.101. Save the file and copy it to/etc/sysconfig/networking/devices/:
# cp ifcfg-eth0\:1 /etc/sysconfig/networking/devices/
Also, copy it to your default network profile or whichever profile you use:
# cp ifcfg-eth0\:1 /etc/sysconfig/networking/profiles/default/
Now, bring up the new interface using the ifup script:
# ifup eth0\:1
Running ifconfig, the new interface should be listed. You can also check it by pinging:
# ping 192.168.0.101
Commands related to Process Control
Here "PID" is refer to the process ID, that you can get from command "ps -aux"
#fg PID
It Brings a background or stopped process to the foreground.
#bg PID
It sends the process to the background.
#any command &
Run any command in the background (the symbol "&" means "run the proceeding command in the background").
#batch any command
Run any command when the system load is low. I can logout and the process will keep running.
#at 18:00
Execute a command at a specified time. You will be prompted for the command(s) to run, until you press d.
#kill PID
To shutdown process by force. First determine the PID of the process to kill using ps.
#killall program name
Kill program(s) by name.
#nice program_name
Run program name adjusting its priority. The priority value may be in the range -20 to 19. Only root may specify negative values. Use "top" to display the priorities of the running processes.
#renice -1 PID
(as root) Change the priority of a running process to -1. Normal users can only adjust processes they own, and only up from the current value (make them run slower).
Useful commands for Ubuntu
sudo command - run command as root
sudo su – root shell open
sudo su user – open shell as a user
sudo -k – forget your password sudo
gksudo command – sudo visual dialog (GNOME)
kdesudo command – sudo visual dialog (KDE)
sudo visudo – edit / etc / sudoers
gksudo nautilus – root file manager (GNOME)
kdesudo konqueror – root file manager (KDE)
passwd – change your password
Network Commands
ifconfig – displays information network
iwconfig – displays information from wireless
sudo iwlist scan – scan wireless networks
sudo /etc/init.d/networking restart – reset the network
(file) /etc/network/interfaces – manual configuration
ifup interface – bring online interface
ifdown interface – disable interface
Display Commands
sudo /etc/init.d/gdm restart – reset X (Gnome)
sudo /etc/init.d/kdm restart – reset X (KDE)
(file) /etc/X11/xorg.conf – show Configuration
sudo dpkg-reconfigure - reconfigure xserver-xorg-phigh - reset configuration X
Ctrl+Alt+Bksp – X display reset if frozen
Ctrl+Alt+FN – switch to tty N
Ctrl+Alt+F7 – switch back to X display
Service System Commands.
start service – service to start work (Upstart)
stop service – service to stop working (Upstart)
status service – check if service is running (Upstart)
/etc/init.d/service start – start service (SysV)
/etc/init.d/service stop – stop service (SysV)
/etc/init.d/service status – check service (SysV)
/etc/init.d/service restart – reset service (SysV)
runlevel – get current runlevel
Firewall related commands
ufw enable – turn on the firewall
ufw disable – turn off the firewall
ufw default allow – allow all connections by default
ufw default deny – drop all connections by default
ufw status – current rules and
ufw allow port – to allow traffic on port
ufw deny port – port block
ufw deny from ip – ip block
System Commands.
lsb_release -a – get the version of Ubuntu
uname -r – get kernel version
uname -a – get all the information kernel
Commands for Package Manager.
apt-get update – refresh updates available
apt-get upgrade – update all packages
apt-get dist-upgrade – version update
apt-get install pkg – installing pkg
apt-get remove pkg – uninstall pkg
apt-get autoremove – removing packages obsotletos
apt-get -f install – try to fix packages
dpkg –configure -a – try to fix a broken package
dpkg -i pkg.deb – install file pkg.deb
(file) /etc/apt/sources.list – list of repositories APT
Commands Special Packages.
ubuntu-desktop – Setting the standard Ubuntu
kubuntu-desktop – KDE Desktop
xubuntu-desktop – desktop XFCE
ubuntu-minimal – core earnings Ubuntu
ubuntu-standard – the standard utilities Ubuntu
ubuntu-restricted-extras – not free, but useful
kubuntu-restricted-extras – ditto KDE
xubuntu-restricted-extras – ditto XFCE
build-essential – packages used to compile
linux-image-generic – latest generic kernel image
linux-headers-generic – latest headlines
Applications commands.
nautilus – File Manager (GNOME)
dolphin – File Manager (KDE)
konqueror – Web browser (KDE)
kate – text editor (KDE)
gedit – text editor (GNOME)
Useful 100+ Linux commands
# at 9am March 1
2. Schedule a queue to run after 5 minutes.
# at now +5 minutes
3. Check any jobs pending to run, same as at -l .
# atq
4. Empty out a file.
# cat /dev/null > /path/to/file
5. Change directory, see also pushd and popd.
# cd
6. List run level information for the service type.
# chkconfig --list
7. Change owner recursively.
# chown -R : /path/to/directory
8. Change shell.
# chsh
9. Scan recursively for viruses.
# clamscan -r
10. Compare two files.
# cmp file1 file2
11. Copy keeping the directory structure.
# cp --parent /source/path /destination/path
12. Copy keeping the permissions of the user.
# cp -p
13. Copy recursive.
# cp -r
14. Copy without shell aliasing.
# \cp
15. List crontab for user.
# crontab -u -l
16. Check current date and time.
# date
17. Set current date and time, may need to set the hardware clock to the system time too, `man hwclock`.
# date -s 'Wed May 28 11:35:00 EST 2003'
18. Show disk free in human readable format.
# df -h
19. Configure interface using DHCP protocol.
# dhclient eth0
20. Find context differences between two files.
# diff -c
21. Creating a patch file.
# diff -Naur oldDir/oldFile newDir/newFile > new_patchFile
22. Kernel buffer
# dmesg
23. Show disk used in human readable format.
# du -h /path/to/directory
24. Find files larger than 10MB.
# find /path/to/file -size +10000k
25. Find file permissions with setuids.
find / \( -perm -4000 -o -perm -2000 \) -exec ls -ldb {} \;>> /tmp/suids
26. Search for world writable files and directories.
# find / -perm -002
27. Display information on free and used memory.
# free
28. Grep on word boundaries.
grep -w
29. Count the number of mathces - similar to “wc -l”.
# grep -c
30. Perform timings of device reads for benchmark and comparison purposes.
# hdparm -t /dev/hda1
31. Set the hardware clock to the current system time.
# hwclock --systohc
32. check the ip address
# ifconfig
33. Add an ip address to eth0.
# ifconfig eth0:x xxx.xxx.xxx.xxx
34. Install loadable kernel module. You can also use `modprobe` to do the same.
# insmod
35. Displays information about your system’s CPU and I/O.
# iostat [ interval [ count ] ]
36. List iptables firewall rules in numeric format.
# iptables -L -n
37. HangUP process so it will re-read the config file.
# killall -HUP
38. Install the boot loader and increase verborsity.
# lilo -v -v
39. Query the boot map.
# lilo -q
40. One time boot to the named kernel.
# lilo -R
41. Create symbolic link to the target file or directory.
# ln -s
42. Configure dynamic linker run-time bindings
# ldconfig
43. List the IPs bound via Ensim
# listaliases
44. Quickly search for indexed files. Run `updatedb` to update the indexed database.
# locate
45. List files.
# ls
46. List loaded kernel modules
# lsmod
47. Create the access.db file database map for sendmail.
# makemap hash /etc/mail/access.db < /etc/mail/access
48. Create/Make a new directory.
# mkdir
49. Generate a random 128 character length password.
# mkpasswd -l 128
50. Read in the contents of your mbox (or the specified file).
# mail -f /var/mail/nameOfFile
51. Print the mail queue
# mailq
52.
# mailstat /path/to/procmail/log
53. Description of the hierarchy directory structure of the system
# man hier
54. Check the MD5 message digest.
# md5sum
55. Mount points check.
# mount
56. Provide information about your systems’ processor.
# mpstat [ interval [ count ] ]
57.
# ncftpget -R -u -p
hostname /local_dir /remote_dir
58.
# netstat -a | grep -i listen
59. Will show you who is attached to what port.
# netstat -anpe
60.
# netstat -n
61. See which programs are listening on which port
# netstat -lnp
62. Will show you what local TCP ports are open and what programs are running on them.
# netstat -lntpe
63. Will show you what local UDP ports are open and what programs are running on them.
# netstat -lnupe
64. Run a program with modified scheduling priority. (# range between -20 to +20, negative is higher priority)
# nice -n # [command to nice]
65. Scan network
# nmap -v hostname/ip
66. Patch and keep a backup
# patch -p# -b < patch_file
67.
# ps -ecaux
68. Turn off all quotas for users and groups, verbose mode
# quotaoff -augv
69. Check quota for all users and groups interactively, do quotaoff first.
# quotacheck -augmiv
70. Turn on all quotas for users and groups
# quotaon -augv
71. Add host ip to route on a particular device.
# route add -host xxx.xxx.xxx.xxx dev eth0:x
72.
# rdate
73. Remove file
# rm
74. Remove kernel module
# rmmod
75. Display the routing table in numeric.
# route -n
76.
# rpm
77. Uninstall/erase package.
# rpm -e
78. Erase without dependency check.
# rpm -e --nodeps
79. List out installed rpms by date, latest on top.
# rpm -qa --last | less
80. Rebuild rpm database.
# rpm --rebuilddb
81. Find which package owns the file.
# rpm -qf /path/to/file
# rpm -q --whatprovides /path/to/file
82. Verify package.
# rpm -V
or
# rpm -Vf /path/to/file
83. Locate documentation for the package that owns the file.
# rpm -qdf /path/to/file
84. Query information on package.
# rpm -qip
85. Query files installed by package.
# rpm -qlp
86. Gives list of files that will be installed/overwritten.
# rpm -ql
87. Will show the scripts that will be executed.
# rpm -q --scripts
88. Display system activity information
# sar
89. Print a 0 padded sequence of numbers.
# seq -w 1 10
90. Record eveything printed on your terminal screen.
# script -a
Ctrl+D to exit out. `more ` to view.
91. Check the status of a service.
# service status
92. Restart after shutdown and force fsck (fsck may take a while).
# shutdown -rF now
93. Split a file into pieces with numeric suffixes, so it can be burnt to cds.
# split -d -b 640k big_input_filename.gz piece_file_prefix.gz.
To piece it back you can `cat piece_file_prefix.gz.* > original.gz`
94. Determine if a network service binary is linked againt tcp wrapper, libwrap.a
# strings -f | grep hosts_access
96. how to use tar
# tar -cvzf fileName.tar.gz `find /file/path -mtime -1 ! -type d -print`
97.
# tar -xvzpf fileName.tar.gz /path/to/file.txt
98. How to use tcpdump
# tcpdump -i eth0 dst port 80 | more
99. System process status
# top
100. View the full command line.
# top -c
101. Create empty file of 0 byte
# touch
102. Similar to `which` - shows full path to the command.
# type
103. Check the limit of user
# ulimit -a
104. Check the version of kernel running
# uname -a
105. Update package profile with rhn
# up2date -p
106. Install package via up2date.
# up2date -i
107.
# uptime
108.
# usermod
109. Utility reports virtual memory statistics
# vmstat [second interval] [no. of count]
110. Show who is logged on and what they are doing.
# w
111. Periodically watch output of a command in full screen
# watch ''
112. Run and generate the apache reports using webalizer
# webalizer -c /path/to/webalizer.conf
113. Recursive download of a url, converting links, no parent.
# wget -r -k -np
114. Mirror, convert links, backup original, dynamic to html and output a “logFile”.
# wget -m -k -K -E -o [logFile]
115. Locate the binary, source, and manual page files for a command.
# whereis
116. Shows the full path of command.
# which
117. Show who is logged on.
# who
118. Yum package updates
# yum check-update -- check to see what updates are needed
# yum info -- show basic information about a package
# yum update -- update particular package
119. Control jobs:
# Ctrl-z -- suspend foreground job
# jobs -- list jobs
# bg -- send job to background
# fg -- bring job to foreground
Check your Disk Performance
#hdparm -tT /dev/sda
Output:
/dev/sda:
/dev/sda:
Timing cached reads: 6054 MB in 2.00 seconds = 3028.86 MB/sec
Timing buffered disk reads: 42 MB in 3.09 seconds = 13.60 MB/sec