Linux Commands for Administrator | Linux Admin Commands Reference

linux-commands-administrators

linux-commands-administrators

su: Acquiring Superuser Status
# su  :- Acquire  Superuser after entering password but home directory does not change
# su – l :– Acquire Superuser access after password but home  directory change to ROOT
# su  – henry : – To  Login and Recreate a user environment
# sudo -s root -c “/bin/bash -l”

passwd – Changing any Password
# passwd henry
stat <fileName>

ulimit – Setting Limits on file size
ulimit command impost a restriction on the maximum size of a file that a user is permitted to create
# ulimit 20971510                             This is often placed in /etc/profile. Measured in 512 byte blocks

umask – Please refer user manual

Source
The source command in shell is used to execute commands from a file in the current shell. This is useful to load function or variables stored in another file.
# source filename
# source /path/to/file

locate

which
shows the full path of (shell) commands

od
od command dumps a file in octal, decimal, and other formats.

# od -bc special-chars.txt

chkconfig
chkconfig command is used to setup, view, or change services that are configured to start automatically during the system startup.

The –list option displays all the services with the current startup configuration status.
# chkconfig –list
To view only the services that are configured to be started during system startup, do the following. Please note that this assumes that your system startup level is 3
# chkconfig –list | grep 3:on
To display the current settings for a selected service only, use chkconfig –list followed by the name of the service:
# chkconfig –list service_name

To enable a service in runlevels 2, 3, 4, and 5, type the following at a shell prompt as root.
# chkconfig service_name on
To disable a service in runlevels 2, 3, 4, and 5, type the following at a shell prompt as root.
# chkconfig service_name off

# chkconfig network –level 3 && echo “Network service is configured for level 3”
# chkconfig network –level 1 && echo “Network service is configured for level 1”

To view the startup configuration of a particular service, grep the output of ‘chkconfig –list’ for that service.
# chkconfig –list | grep network

Add a new Service to the Startup
# chkconfig –add iptables

To remove it from the startup list, use the –del option as shown below.
# chkconfig –del ip6tables

How to install services?

http://bitnami.org/article/how-to-install-services-on-linux

service
# service network restart
OR
# /etc/init.d/network restart

To start Linux network service:
# service network start

To stop Linux network service:
# service network stop

date – Setting the system Date
# date MMDDhhmm

groupadd
# groupadd –g 241 dba               The command place entry in /etc/group which can also insert manually(dba:x:241). 241 is GID of dba

useradd
# useradd –u 210 –g dba –c “The RDBMS” –d /home/oracle –s /bin/bash –m username

usermod
# usermod –s /bin/csh username

userdel
# userdel username

shutdown
# shutdown 17:30                         Shutdown at 17:30
# shutdown –r now                     Shutdown immediately and reboot
# shutown –r 17:30                      Shutdown immediately and reboot at 17:30
# shutown –h now                       Shutdown immediately and halt
# shutown –h 17:30                     Shutdown at 17:30
# shutdown –c                               Cancel the shutdown
Reboot / Poweroff / halt

df
The df (disk free) command lets you see if file systems are full.

  • To see how much free space is in all file systems (including mounted ones), use df
  • To see how much free space is in all file systems with names containing “dev”, use df | grep dev
  • To see how much free space is in your home file system, use df /home
  • To see how much free space is in the file system “tmp”, use df /tmp
  • To see if there is enough free space on the machine, check the output from the following

# df /usr,
# df /var
# df -k /tmp /home                                          -k Reports in units of KB
# df –h / /usr                                                       -h reports in MB, GB

du -Disk usage
# du  /home/sales/tml
# du –s /home/*                                               -s options for summary disk usage information for tml directory
# du –sh /home                                                 -h options for reports in MB , GB
# find /home –size +1024 –print                                     Files above 1MB
# find /home –size +1024 –size -4096 – print             Files above 1Mb and below 4MB

mount / unmount

Using file for incremental Backup
# tar –cvf /dev/rct0 find /home –type f –newer .last_time –print
# touch .last_time
# tar –cvf /dev/rct0 /home > homelist date “+%d%b%y”

xrags

xargs vs. exec {}

Remote machine Login: telnet, rlogin
telnet
rlogin

Secure Login with password: ssh
ssh

Remote login without Password: rsh
rsh

File Transfer: ftp, rcp, scp
rcp

scp

ftp
ftp rajesh-servers – How to Login
Directory Commands:
# cd [directory] Changes to the specified directory on the remote system.
# dir Displays a long listing of files on the remote system.
# ls Displays a short listing of the files on the remote system.
# lcd Changes the working directory on the local system.
# pwd Displays the working directory on the remote system.
File Transfer Commands:
# get [filename] Downloads a single file from the remote system.
# put [filename] Uploads a single file to the remote system.
# mget [file…] Downloads multiple files from the remote system.
# mput [file…] Uploads multiple files to the remote system.

Generic troubleshooting tips

  • Use tail –f to watch log file in real time, advantage is simple you can spot error or warning message in real time.
    tail –f /path/to/log/file
    Example(s):
    # tail –f /var/log/maillog
  • Use telnet command to see if you get response or not. Sometime you will also see some informative message:
    telnet ip port

Example(s):
# telnet localhost 53
# telnet localhost 25

  • Make sure you can see PID of your service.
    pidof service-name
    cat /var/run/service.pid

    Example(s):
    # pidof sshd
    # cat /var/run/sshd.pid
  • You need to make sure that your DNS server or third party DNS server (ISP) is accessible. This is an important step, as many network services depend upon DNS; especially sendmail/postfix or Squid etc for example. Run dig or nslookup. No timeout should occurred.

    # dig your-domain.com
    # nslookup gw.isp.com
    # more /etc/resolv.conf

Performance /  Memory / CPU / Process check
ps – Read user manual

w username –  Find Out Who Is Logged on And What They Are Doing. w command displays information about the users currently on the machine, and their processes.

top
The top program provides a dynamic real-time view of a running system i.e. actual process activity. By default, it displays the most CPU-intensive tasks running on the server and updates the list every five seconds.

uptime
tell How Long The System Has Been Running

free – Memory Usage
The command free displays the total amount of free and used physical and swap memory in the system, as well as the buffers used by the kernel.
# free –m                                                            Display free memory size in MB
# free -t –m                                                        Displays a line containing the totals memory in MB

vmstat

Linux Performance Monitoring using iostat, mpstat and vmstat

iostat

pmap

mpstat 

# strace & truss – Debug the Execution of a Program in Linux
Trace the Execution of an Executable
# strace ls

Save the Trace Execution to a File Using Option -o
strace -o output.txt ls

Execute Strace on a Running Linux Process Using Option -p
# ps -C firefox-bin To get the process id
# sudo strace -p 1725 -o firefox_trace.txt

Print Relative Time for System Calls Using Option -r
# strace -r ls

Networking troubleshooting tips

Hostname verification or setup tools

  • hostname : To get hostname of server.
  • hostname –s : To get FQDN hostname of server
  • more /etc/sysconfig/network : To setup hostname and networking can enable or disabled.
  • dnsdomainname : List or setup domainname.
  • more /etc/hosts : Make sure at least localhost entry do exist.

Ethernet configuration tools

service network reload|restart|stop|start : To reload (after changed made in ip config file)|restart|stop|start network interface with all properties.

route
The route command allows you to add static routes to the routing tabling.

traceroute (tracert in Windows)
Traceroute displays the routers that are passed through to reach the destination.

dmesg
When the system boots up, it prints number of messages on the screen that displays information about the hardware devices that the kernel detects during boot process.
These messages are available in kernel ring buffer and whenever the new message comes the old message gets overwritten. You could see all those messages after the system bootup using the dmesg command.

ifconfig
The ifconfig command is used to display the local interface configuration (winipcfg for Windows) and to modify the configuration.
ifconfig eth0 up|down : To enable|disable network interface

netstat
The network status command netstat displays status information about the network interfaces on the host machine. Netstat command displays the network related information such as network connections, routing tables, interface statistics.
-a                Show both listening and non-listening sockets
-n                prints the numeric forms of IP address

# netstat -ain                                  Local interface status
# netstat -rn                                    Routing table information
# netstat –an                                  Display Active Internet Connections and domain sockets using netstat
# netstat -tap                                 Display Active Connections with Process ID and Program Name
# netstat –-route                          Display Routing Table
# netstat –statistics –raw         Display RAW network statistics
# netstat –tcp –numeric           List of TCP connection to and from the machine.
netstat -tuplin                                UNIX

iptables

To know which ports are running in the machine.
ping
The ping command allows you to determine that the:

  • TCP/IP stack is configured properly
  • Network interface card is configured properly
  • Default gateway and subnet mask is configured properly
  • Domain name services is configured properly.

# ping ipaddress to check if the ip address in connected with network and if host is alive or dead
# ping 127.0.0.1/localhost – to check TCP/IP services is running properly or not. Checks that your TCP/IP stack is working properly up to the NIC. If this fails, check to see if you have TCP/IP services loaded.
# ping “IP address of default gateway” – Checks that the NIC is working on the local subnet by pinging the local side of the default gateway which is a router. If this fails, check that TCP/IP is bound to the NIC. Then check that the NIC’s IRQ, and base address are set properly both on the card itself and in the operating system’s interface configuration.
# ping “domain name” if DNS name is correctly configured. If it fails, check that the DNS server’s IP address is entered in properly in the TCP/IP configuration.

# arp   – To see the IP to MAC address translation table
# arp -a  -Use this command to see the IP to MAC address translation table if you are having problems connecting to other network hosts. It is a dynamic cache which updates every 120 seconds.

# more /etc/modules.conf : To see your network card configuration alias for eth0 exists or not.
# lsmod : To list loaded modules (read as drivers), here you need to see that eth0 module is loaded or not, if not loaded then use insmod to insert (load) driver.
# dhclient : Dynamic Host Configuration Protocol Client, run this if your Ethernet card is not getting ip from DHCP box on startup; this command does by default shows useful information.

To see if service blocked because of access control
# iptables –n –L : To list all iptable rules; useful to see if firewall blocks service or not.
# service iptables stop|start : To start|stop iptables
# more /etc/xinetd.conf

sar
sar is an excellent monitoring tool that displays performance data of pretty much every resource of the system including CPU, memory, IO, paging, networking, interrupts etc., Sar Collects, Reports (displays) and Saves the performance data.

# sar –u | more         Display CPU Statistics using Sar Command
# sar –d | more         Display Disk IO Statistics using sar command
# sar -n DEV | more Display networking Statistics using sar command
# sar -n SOCK |more Display networking Statistics using sar command
# sar -n DEV -f /var/log/sa/sa24 | more   To display the network counters from the 24th
# sar 4 5                                             You can also display real time usage using sar

lsof
lsof stands for “ls open files”, which will list all the open files in the system. The open files include network connection, devices and directories. The output of the lsof command will have the following columns:

COMMAND process name.
PID process ID
USER Username
FD file descriptor
TYPE node type of the file
DEVICE device number
SIZE file size
NODE node number
NAME full path of the file name.

# lsof | more              View all open files of the system
# lsof | wc -l                How many files are open in the system at any given point
# lsof –u ramesh       View open files by a specific user. A system administrator can use this command to get some idea on what users are executing on the system.
# lsof /bin/vi               it displays all users who are currently using vi

Some external Tools
iptraf – Real-time Network Statistics
tcpdump  – Detailed Network Traffic Analysis
Nagios – Server And Network Monitoring
Cacti – Web-based Monitoring Tool
KDE System Guard – Real-time Systems Reporting and Graphing
Gnome System Monitor – Real-time Systems Reporting and Graphing
nmap – scan your server for open ports.
lsof – list open files, network connections and much more.
ntop web based tool – ntop is the best tool to see network usage in a way similar to what top command does for processes i.e. it is network traffic monitoring software. You can see network status, protocol wise distribution of traffic for UDP, TCP, DNS, HTTP and other protocols.
Conky – Another good monitoring tool for the X Window System. It is highly configurable and is able to monitor many system variables including the status of the CPU, memory, swap space, disk storage,
temperatures, processes, network interfaces, battery power, system messages, e-mail inboxes etc.
GKrellM – It can be used to monitor the status of CPUs, main memory, hard disks, network interfaces, local and remote mailboxes, and many other things.
vnstat – vnStat is a console-based network traffic monitor. It keeps a log of hourly, daily and monthly network traffic for the selected interface(s).
htop – htop is an enhanced version of top, the interactive process viewer, which can display the list of processes in a tree form.
mtr – mtr combines the functionality of the traceroute and ping programs in a single network diagnostic tool.

yum
Rsync
rpm – Refer Cheat Book

Some Well known System services
QUICK DNS Flush                             /etc/rc.d/init.d/nscd restart
/Proc file system – Various Kernel Statistics
# cat /proc/cpuinfo
# cat /proc/meminfo
# cat /proc/zoneinfo
# cat /proc/mounts

Questions
What is MASK in output of ifconfig
Understand the output of ifconfig in linux such as RX packets and TX packets
Set ip address using commands line

Change Host name in RHEL
Changes should be made in the following files.
/etc/hosts
/etc/sysconfig/network

For finding your java process
jps -v      For finding your java process.
jstack PID  To Get stack of threads inside jvm

If application running under different user than yourself
sudo jps -v
sudo jstack 6172

QUICK DNS Flush
/etc/rc.d/init.d/nscd restart
/etc/rc.d/init.d/nscd restart
Reference
http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch04_:_Simple_Network_Troubleshooting
http://www.cyberciti.biz/tips/linux-security.html
http://www.cyberciti.biz/tips/list-of-linux-troubleshooting-commandstools-part-1.html
http://www.princeton.edu/~unix/Solaris/troubleshoot/index.html
http://www.techbooksforfree.com/intro_to_data_com/page252.html
http://www.scmgalaxy.com/unix-linux/linux-performance-monitoring-using-iostat-mpstat-and-vmstat.html

http://www.tecmint.com/20-advanced-commands-for-linux-experts/