Showing posts with label Mac Command. Show all posts
Showing posts with label Mac Command. Show all posts

Wednesday, April 2, 2008

The Unix Command Line: Administration

The Unix Command Line: Administration

Reference


OS X's BSD/unix command-line

System Administration Commands

By Gordon Davisson

Copyright (c) 2003, Westwind Computing inc.

Index:


Administration Commands:

ps - List the processes running on the system

Examples:
ps
List processes belonging to the current user that are attached to a terminal (not very useful under OS X)
ps -x
List processes belonging to the current user whether or not they're attached to a terminal
ps -ax
List all running processes
ps -aux
List all running processes, with additional information about their resource useage

top - List the top CPU-consuming processes running on the system, along with various other system load statistics. Note: it runs continuously, updating the stats repeatedly, until you quit it with "q".

Examples:
top
Display a list of processes, highest-process-id (i.e. most recent) first, updating once a second
top -us5
Display processes sorted by CPU useage, updating every 5 seconds

kill - Kill (or send other signals to) a process

Examples:
kill 220
Terminate process #220
kill -9 220
Terminate process #220 with extreme prejudice
kill -HUP 220
Send process #220 a hangup signal - by convention, background processes (daemons) treat this as a cue to restart, and reload their configuration information.

su - Set user. Allows you to temporarily become another user (root is the default). It'll ask for that user's password. Use the "exit" command to go back to normal.

Note:
  • You must be a member of the "wheel" group to su to root; under OS X 10.2, nobody is a member of "wheel" so this is effectively forbidden. Use sudo instead.

sudo - Set user and do. Execute a single command as another user (root is the default). It will ask for your password. Access is controlled by a configuration file and can be made quite complex (see the man page). By default, any administrator use sudo to perform any command as any user.

Examples:
sudo rm /private/var/db/.AppleSetupDone
Become root just long enough to delete one file.
sudo -u george ls ~george/Documents
Become george and list the files in his Documents directory.
sudo -s
Start a root shell (similar to su, except that it asks for your password, rather than the root password, and doesn't requires admin access, not wheel membership).

lsbom - List the contents of an installer's bom (bill of materials) file. This can be used to find out what files an installer will add/replace in your system before running it. It can also be used to find out what files a past install messed with.

Examples:
lsbom /Volumes/Developer\ Tools/Packages/DevTools.pkg/Contents/Resources/DevTools.bom >contents.txt
List the files that will be installed by the "DevTools" package, saving the list in a file named contents.txt.
lsbom /Library/Receipts/Essentials.pkg/Contents/Resources/Essentials.bom | more
List the files that were installed by the "Essentials" package (and pipe it through more to prevent overload).

lsof - List open files on the system. Normally, it only lists files you (or processes you own) have open; if run as root, it lists all open files.

Examples:
lsof
List all files currently open by me and my processes.
sudo lsof
List all files currently open on the entire system.
sudo lsof -i
List all open network connections on the entire system.
sudo lsof "/Volumes/FW Drive"
List all open files on the "FW Drive" volume; useful for figuring out why you can't eject/dismount a disk because something is using it.

ifconfig - Configure network interfaces (e.g. ethernet ports, AirPort cards, etc).

Notes:
  • In general, it's better to adjust the network settings in the Network pane of System Preferences. ifconfig sometimes allows a little more control/information, but changes made this way will almost never "stick" when the computer is rebooted, and may get reset when the network settings change (e.g. if the location changes, a port gets connected or disconnected, etc).
  • Changing the network settings requires root access.
  • ifconfig refers to network ports using rather cryptic identifiers such as:
    en0
    The first ethernet interface (generally, the built-in ethernet port).
    en1, en2, etc
    Additional ethernet interface(s) and/or AirPort wireless network card(s), firewire, etc.
    lo0
    The local loopback pseudo-interface, which your computer uses to talk to itself. Don't worry, this is not a sign of schizophrenia, it's just the way unix systems work...
Examples:
ifconfig -a
List the computer's network ports and their settings.
sudo ifconfig en0 media 100baseTX mediaopt full-duplex
Set the built-in ethernet interface to 100-megabit, full-duplex mode. Note that this may or may not have any effect, depending on whether the driver supports this form of configuration-forcing.
sudo ifconfig en0 alias 10.0.0.150 netmask 255.255.255.0
Attach an additional IP address (aka an alias or subinterface) to the built-in ethernet port. Note that (at least as of OS X 10.2) this is probably better done by simply adding another port configuration in the Network preference pane.

diskutil - Provides various utilities for dealing with Apple's disk format options (HFS+, journaling, RAID, etc). Many options require root access.

Examples:
diskutil
Display a list of diskutil's options.
diskutil info /
List information on the boot volume.
diskutil info /Volumes/Data
List information on a mounted (non-boot) volume named "Data".
diskutil info /dev/disk0s9
List information on partion #9 of disk #0.
sudo diskutil repairDisk /Volumes/Data
Repair the file structure on the volume "Data" (note: this unmounts the volume during repair).
sudo diskutil repairPermissions /
Repair the file permissions on the boot volume.
sudo diskutil enableJournal /
Enable HFS+ journaling on the boot volume.
sudo diskutil disableJournal /
Disable HFS+ journaling on the boot volume.

61. Handy Unix Network Troubleshooting Commands

61. Handy Unix Network Troubleshooting Commands


Introduction to Data Communications
61. Handy Unix Network Troubleshooting Commands

1. Handy Unix Network Troubleshooting Commands

The following network troubleshooting commands will vary slightly in syntax depending on which operating system is used. Some operating systems will provide more options and some less. Please use the following information as a guide and the syntax presented with "a grain of salt".


arp

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. ARP stands for Address Resolution Protocol (ARP).

 arp - a

address resolution protocol
host name (IP address) at (ethernet address)

ping

The ping command is the most versatile network troubleshooting command in Unix. Use it to verify that your TCP/IP network services are operating correctly. 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.

The following is a step by step guide in determining if your network stack is configured properly:

  1. ping 127.0.0.1

    Checks that your TCP/IP stack is working properly up to the network interface card (NIC). If this fails, check to see if you have TCP/IP services loaded.

  2. ping "IP address of default gateway"

    Checks that the network interface card 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.

  3. ping "IP address across the gateway"

    Checks that the default gateway is correctly identified in the TCP/IP configuration and that the proper subnet mask is configured. The IP address selected must not be on the local subnet.

  4. ping "domain name"

    Checks that the domain name services (DNS) is correctly configured in the TCP/IP stack. A domain name is a name like www.yahoo.com. If it fails, check that the DNS server's IP address is entered in properly in the TCP/IP configuration.


netstat

The network status command netstat displays status information about the network interfaces on the host machine and it can display routing table information.

  1. Local interface status

    netstat -ain

    a - all interfaces are displayed
    i - displays configuration
    n - IP addresses (!n - host names)

    example output:

    Name MTU Net/dest address Ierrs Opkts Collis Queue
    le0 1500 (net IP addr) (local IP) . .. ... ....

  2. Routing table information

    netstat -r  (-nr or rn gives ip addresses)

    Routing Table
    Destination Gateway Flags Interface
    (net or host) (IP address) UHGD (name)

    U - up H - host G - gateway D- discovered using ICMP Req


ifconfig

The ifconfig command is used to display the local interface configuration (winipcfg for Windows) and to modify the configuration. Local interfaces can be Ethernet network cards, modems, etc..

ifconfig (interface name) (down/up/nothing) nothing gives status

(interface name): flags - 63 (up, broadcast, notra, Running)
inet (ip address) netmask FFFFFF00 broadcast 128.6.7.255

route

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

route (-n) (add/delete/nothing) (dest IP address/subnet/DEFAULT) (local IP address) (hop)


traceroute (tracert in Windows)

Traceroute displays the routers that are passed through to reach the destination.

traceroute "IP address or domain name"

Tracing route to www.apllejcok.com [192.168.1.64]
over a maximum of 30 hops:

1 116 ms 134 ms 112 ms ts10.dshark.com [192.168.128.20]
2 124 ms 112 ms 114 ms bl1.poufe.com [192.168.130.1]
3 122 ms 118 ms 117 ms fifo.amalag.com [192.168.64.2]
4 130 ms 156 ms 132 ms dfg.apllejcok.com [192.168.1.1]

Introduction to Data Communications
Previous Table of Contents Next

Tuesday, April 1, 2008

Frequently Used Unix Commands

Frequently Used Unix Commands

Frequently Used Unix Commands


U-M Information Technology Central Services * R1159 *November 2003

The Unix operating system supports many commands. To review a more complete definition of any of the commands listed below, type man command at the Unix shell prompt (%), where you have replaced command with the name of a Unix command. For example, type man exit to find out more about the exit command.

NOTE: This document assumes you are using Unix on the ITCS Login Service (host name login.itd.umich.edu). These instructions apply to many other Unix machines; however, you may notice different behavior if you are not using the ITCS Login Service.

TIP: The variable files refers to a list of any number of files separated by white space; in the same way; names refers to a list of file or directory names separated by white space.


NOTE: This document is also available in these formats:


Table of Contents


General

apropos command
Locate commands by keyword lookup.

exit
Terminate your current session, or shell.

man command
Display the Unix manual page describing a given Unix command.

File System Navigation

cd
Return to your home directory.

cd directory
Change directory to make directory your current directory.

file files
Determine file type.

ls
List the contents of the current directory.

ls names
List the contents of the directories; names can name files and/or directories:

ls -l
. . . in a long format, showing permissions, owner, size, and other file info.

ls -a
. . . all files, including "hidden" files (file names that begin with a dot ".").

ls -R
. . . Recursively, for all subdirectories.

ls -t
. . . in time order (when modified, newest to oldest) rather than in name order.

pwd
Display the name of the current directory, or "print working directory."

File/Directory Manipulation

compress files
Reduces the size of a file.

uncompress files
Restores compressed files to their original form.

cp file1 file2
Copy file(s).

cp files directory
Copy file(s) into a directory.

cp -r dir1 dir2
Copy a directory and, recursively, its subdirectories.

mkdir directory
Create, or "make" a directory.

mv file1 file2
Move a file or, if file1 and file2 are in the same directory, rename a file.

mv files directory
Move files into a directory.

mv dir1 dir2
If directory dir2 exists, move dir1 into dir2; otherwise, rename dir1 as dir2.

rm files
Remove (erase) file(s).

rm -r names
Remove files, directories, and recursively, any subdirectories.

rmdir directory
Remove directory (directory must be empty).

Data Manipulation

cat files
Concatenate file(s); you can use cat to display the contents of a file (this is not advisable if the file is a binary file).

grep "pattern" files
Display all lines in the files that match a pattern.

more files
Display contents of files one screen at a time.

sort files
Order the lines in a file or files alphabetically (this command does not alter the file or files -- it merely displays the sorted output to the screen):

sort -r files
. . . in reverse order.

sort -n files
. . . numerically (puts 2 before 10 instead of after).

Networking/Communications

finger user@umich.edu
Displays information about a U-M user from the U-M Online Directory.

ssh hostname
Connect to a remote host using Secure Shell.

telnet hostname
Connect to a remote host using the telnet protocol.

talk user
Initiate a conversation with another user (end conversation with Control-C); talk works only between machines of the same architecture.

Miscellaneous

!!
Repeat last shell command.

!string
Repeat last shell command that began with string (for example, type "!m" to repeat the last command that began with "m").

cal
Display a calendar of the current month.

cal month year
Display a calendar of the given month and year. Note that the year must be fully qualified, for example, "2003" and not "03."

clear
Clears terminal screen.

date
Display the current local date and time.

who
Display a list of users currently logged in.

Additional Resources

Visit ITCS's Information System to obtain ITCS computer documentation and other resources. A list of relevant documents follows.

Idiot's Guide to Basic UNIX Commands - AwkwardTV

Idiot's Guide to Basic UNIX Commands - AwkwardTV

Idiot's Guide to Basic UNIX Commands

From AwkwardTV

Jump to: navigation, search

This is a simple list of commands needed to install applications, plugins or do some other "unixy" stuff on the AppleTV.

Contents

[hide]

[edit] Become root

sudo -s
  • sudo will ask for the current user's password, which is "frontrow".
  • type "exit" to "undo" being root.

[edit] Kill Finder.app

kill $(ps ax | grep [F]inder | cut -c 1-5)

[edit] Mount/unmount a dmg file

[edit] Unmounting

hdiutil detach /Volumes/

If that doesn't work:

hdiutil detach -Force /Volumes/

[edit] Mounting

hdiutil mount someimage.dmg

[edit] Remount your root (/) file system read/write

  • needs to be executed as root user (see above)
mount -rw /

[edit] Move files around

  • might need to have the root file system mounted in read/write mode
mv /file/you/want/to/move /target/location/

[edit] Start shlight

  • needs shlight installed
  • if problems arise, please first add the servername to /etc/hosts (see below)
/usr/sbin/shlight /// /Users/frontrow/Movies/somemountpoint

[edit] Add a host to /etc/hosts

echo -e "\t" >> /etc/hosts

[edit] Get a screen capture of your AppleTV

screencapture -s filename.png

macosxhints.com - Mount .DMG images remotely via the Terminal

macosxhints.com - Mount .DMG images remotely via the Terminal: "Here is a writeup I did last year on how to download disk images, mount them, and install software from the command line. Very useful when you admin a machine only via SSH.


$ cat HeadlessInstall.txt
10 August 2004 Brett Johnson

This describes some of the steps necessary to perform the
installation of Mac OS X packages via the command line.


1) Download the Package"
Download the installation image to a local directory.
I usually download packages to the ~admin/staging directory [a client convention].
Don't try to download it to some target directory like /etc or /usr

I use 'curl' to fetch the package from the net. I usually Ctrl-click
or right-click the package link in Safari (running locally) and select
'Copy link to Clipboard'. This copies the URL to the package to the
clipboard. I then paste the URL into the curl command in a ssh login
window to this server. Note that curl writes its output to stdout,
so be sure to redirect it to a file. For example

% curl http://www2.entropy.ch/download/Entropy-PHP-4.3.6-4.dmg > Entropy-PHP-4.3.6-4.dmg

This DOESN'T work for things on SourceForge, where the links are actually
javascript that goes to a mirror. In that case, download it locally,
then use sftp (or Fugu) to move it to this server.



2) If it is a compressed archive, decompress it using the appropriate tool.
For instance suppose you have a .tar.gz file:

% ls
xyzzy.pkg.tar.gz

% gunzip xyzzy.pkg.tar.gz
% tar -xvf xyzzy.pkg.tar



3) If it is a disk image file (.dmg) mount it using hdid. By default,
the disk images are mounted under the /Volumes directory. I see no
reason to do otherwise.

% hdid Entropy-PHP-4.3.6-4.dmg
% ls /Volumes
Backup HD Entropy PHP 4.3.6-4 Server HD
% cd "/Volumes/Entropy PHP 4.3.6-4"
% ls
Documentation.html php-4.3.6.pkg


4) Run the command line version of the Mac OS X installer. By default
the installer produces terse output suitable for parsing by scripts
doing installations. We will make the output verbose [human readable].
The installer takes the package name as well as the target Volume.
The installer is /usr/sbin/installer , which should be on the path.
The installer must be run as root, so be prepared to sudo...

% sudo /usr/sbin/installer -verbose -pkg php-4.3.6.pkg -target /



5) Don't forget to unmount the .dmg disk image if you mounted one.
Here we execute the 'mount' command to determine which virtual
disk device our dmg is mounted as (in this case it is /dev/disk2s2).
Then we use 'hdiutil detach' to unmount it.

% mount
/dev/disk0s3 on / (local, journaled)
/dev/disk1s3 on /Volumes/Backup HD (local)
automount -nsl [336] on /Network (automounted)
/dev/disk2s2 on /Volumes/Entropy PHP 4.3.6-4 (local, nodev, nosuid, read-only, mounted by admin)

% hdiutil detach /dev/disk2s2 -force
"disk2" unmounted.
"disk2" ejected.