13.2. Using the Command-Line InterfaceIn order to fully and efficiently manage your ESX Server, you need to become very familiar with the command-line interface of the ESX Server Service Console. Although the MUI can be used to perform the majority of the administrative tasks for your ESX Server, several tasks can be carried out only from the command line. This section walks you through the most common task you must perform at the Service Console. Specifically, you'll learn the following:
13.2.1. Connecting to the Service ConsoleIn order to use the command-line interface, you must connect to the Service Console with either an SSH client or work at the physical Service Console and use the virtual terminals that are available by using the Alt + F2 through Alt + F6 keys. 13.2.1.1. Virtual TerminalWorking from the physical console, you can access the virtual terminals by pressing Alt + F2 through ALT + F6. Alt + F1 brings you back to the Service Console Status screen. Thus, you have five virtual terminals to work with at the physical console. 13.2.1.2. Remote Access CardIt's highly recommended that you include a remote access card in your ESX Server, which will give you the ability to perform remote management. Some of the most common vendor solutions allow for remote management of your server with virtual power, virtual CD-ROM, and virtual floppy capabilities. Some of these solutions are listed in the next paragraph. Once connected, you can use the Alt + F2 through Alt + F6 commands to access virtual terminals as if you where at the physical server. Most will run through a web browser. The main reason this option is recommended is because you'll have the ability to control virtual power and still maintain access to the console even if someone configured the Service Console incorrectly. Major vendor remote access solutions include the following:
13.2.1.3. SSH ClientIf you're working from a Linux workstation, you can connect to the Service Console remotely using an SSH client. For Windows-based clients, you can download the PuTTY ssh client for free. 13.2.1.4. TelnetIf you have set your ESX Server security to Medium or Custom to allow telnet, you'll also be able to use a standard telnet client to connect to the Service Console (it's not very secure; therefore, it's not recommended). To attach to the Service Console remotely from a Windows workstation, you can use the PuTTY SSH client shown in Figure 13.1 Specify the IP or host name and you'll get the window shown in Figure 13.2 Figure 13-1. The PuTTY Configuration ScreenFigure 13.2 shows the SSH login screen for your ESX Server Service Console. Figure 13-2. The SSH Login ScreenOnce you've successfully logged in to the Service Console, you can use the following command to validate who you're logged in as, check group membership, or view another user account and their group memberships. To view your own user account information, type
To view user account information for another user, type
This can be useful if you want to validate that a user account is a member of a specific group (see Figure 13.3). Figure 13-3. Output from the ID CommandAs a best practice, you should always log in with an account that does not have root privileges. If you require root access to perform administrative tasks, you can use the following command:
This prompts you for the password of the root user account and logs you in as the root user, thus you'll get the root user's environment. Make sure you include the hyphen or you'll get errors when trying to execute certain commands. When you're finished using root privileges, simply type logout and you'll be returned to your normal non-root user environment. When you've completed your command-line session and want to log out, type
This logs you out and you exit the SSH client window. 13.2.1.5. Man PagesIf you require more detailed documentation for a command, you can view its man page (also called a manual page). To view the man pages for a command, type
To search all man pages for a specific keyword, you can do so, but you'll first need to build an index by typing the following:
This allows you to search all man pages by keyword, as in the following command example:
Figure 13.4 shows the output from the mank command. Figure 13-4. Output from the mank CommandThe command output in Figure 13.4 shows the results for the man page keyword search and the associated command and man page references. 13.2.2. Command-Line ShortcutsObviously, using the command line may not be as intuitive as a GUI for some individuals, but there are a few key shortcuts you can use at the command line to alleviate some of the pain. This section covers a few of the shortcuts you can use at the command line. When typing commands or file names, you can use the Tab key to auto complete the text of the command. You'll need to type enough characters to make the filename or command unique. If you don't type enough characters to be unique, you can press the Tab key twice to see the available choices based on your current characters. For example, in Figure 13.5, the command cd /vm was used, and then the Tab key was pressed twice, which then showed that there are several directories starting with the characters vm in the root directory. This allows you to complete the command and switch to the directory you wish to. If you wanted to switch to the vmimages directory, all you'd need to do is add cd /vmi and press the Tab key and it would complete the rest of the directory name for you. Figure 13-5. Output of the cd /vm Command13.2.2.1. Command HistoryIf you want to display a list of previously executed commands, type
Figure 13.6 shows the output of history 20, which displays the last 20 commands executed. Figure 13-6. Output from the history CommandIf you want to execute a particular command in the list, you would type
Let's say you wanted to execute the ls /etc command, which has event number 1088 shown in Figure 13.6, you'd type !1088 at the command line and it would rerun that command. Obviously, this technique is more helpful for a much longer command that was executed previously. You can also cycle through the command history by pressing the Up-arrow key to go backward through previous commands at the command prompt. 13.2.3. Basic Commands for Directories and FilesThis section covers commands used to move around the directory structure, as well as those used to copy files, move files, delete files, rename files, and make new directories on the Service Console. If you need to determine your current directory, type
The output of the pwd command is shown in Figure 13.7 Figure 13-7. Output of the pwd Command13.2.3.1. Changing DirectoriesIf you need to change to a different directory using the absolute path name, (from the root) type
For example, this might manifest itself as cd /vmimages. To change to a directory relative to your parent directory, type
(This will go up one directory and switch to a directory under your parent.) To change to your parent directory, type
To automatically go to your home directory, type
To list the contents of a directory, type
The output of the ls command is shown in Figure 13.8 Figure 13-8. Output of the ls CommandTo list the contents of a directory in long format, type
The output is shown in Figure 13.9 This output offers more detail for the directory list, such as the permissions for the owner, group, and other. The username of the owner and the affiliated group for the file or directory are also displayed. In Figure 13.9, the owner is root and the affiliated group is also root. Figure 13-9. Output of the ls/ Command13.2.3.2. Moving and Copying FilesTo copy a file from one directory to another, type
In the following example, let's assume your VMFS volume has a label of local and you want to copy a virtual disk file to a backup directory. To make a copy of a vmdk file and copy it to another directory, type
To move or rename a file, type
So, for example, if you want to rename a file, type
If you want to move a group of files from one directory to another, type
Note: Before moving any virtual machine configuration files, make sure the machine is powered off and you un-register the VM first. If not, you'll have a lot of fun trying to power it back on (we speak from experience). You can un-register the VM in the MUI and re-register it once you've moved all the files to the new location by clicking the .vmx file in the MUI Manage Files applet. When you click the .vmx file in the MUI, you'll be prompted to register the virtual machine. You can also un-register and register VMs using the vmware-cmd command. This is covered later in this chapter. To delete file(s), type
If you need to delete a directory (be extremely careful using this command), type
If you need to create a new directory, type
13.2.4. Finding Files and Searching Their ContentYou'll find it necessary to search for specific files or search for strings and patterns within files on your ESX Server at some point in time. To do so, use the find and grep command or a combination of both. The find command is used to search for files, while grep is used to search the contents of the files for text strings or patterns. Here are some examples of what you can do with both commands. If you need to search a file for a specific string, type
If you want to find out whether a specific user account exists on your server, type
Figure 13.10 shows the grep command being used to find the user account for vmadmin1 in the /etc/passwd file. In this example, the grep command found a string match for vmadmin1 and printed the line containing the value to the screen. Figure 13-10. Finding a String Match with the grep CommandNote: The /etc/passwd file contains a list of all user accounts on your ESX Server. It also displays the path to the home directory and default shell. If you want to find a list of all the virtual switches that exist on your server, type
This example shows all lines in the /etc/vmware/netmap.conf file that have the string ".name". The /etc/vmware/netmap.conf file contains a list of all the virtual switches and their assigned network devices, as shown in Figure 13.11 Figure 13-11. The /etc/vmware/netmap.conf FileYou can also use the cat command to print the contents of a text file to the screen. Also, remember that you can use the cat command with the |more or |less command if the contents of the file are larger than the screen. Figure 13.12 shows the contents of the /etc/vmware/netmap.conf file using the cat command. Figure 13-12. Using the cat Command to View the Contents of /etc/vmware/netmap.confBecause the MUI does not give you the option to search for files, you'll need to use the command-line find command to search for files on your ESX Server. Where grep is used to search for strings or patterns within files, find is used to find files that match certain criteria. Figure 13.13 shows the output of the find command when searching the entire directory for files with the .vmx extension. This is useful for locating all virtual machine configuration files whether they are registered or not and to find out if configuration files are being created in the correct locations. Table 13.1 lists the options available for the find command.
Figure 13-13. Output of the find CommandNote: As an FYI, the file /etc/vmware/vm-list also contains a dynamically created list of registered virtual machines on ESX Server. You can use the grep and find commands together to find out information quickly about the configuration of your virtual machines. For example, what if you need to find out which virtual machines are using a specific virtual switch on your ESX Server. You could manually browse all the VMs in the MUI to find out which ones are using the virtual switch or you could use the following command at the Service Console to find out which virtual machines are using a virtual switch named Network01:
The output appears in Figure 13.14 This command shows the path to the .vmx file and the line within the file that shows which virtual interface is using the virtual switch. Figure 13-14. Output of the find and grep Commands13.2.4.1. Searching Virtual Machine Configuration Files for Important InformationIf you cat out the contents of a virtual machine .vmx configuration file, you can find some useful strings to search for. Figure 13.15 shows the contents of a virtual machine configuration file. A few of the strings have been highlighted that can be useful for finding specific information about your virtual machines. Figure 13-15. Contents of a Virtual Machine Configuration FileThe following are some of the commands that can be used to find useful information about your virtual machines. Hopefully, you'll get the hang of it. If you want to find all machines that are set to autostart, type
Incidentally, the backslash (\) is used before "True" because the quote is part of the string being searched for. In this case, the \ lets the command know that the character following the \ is part of the string pattern to search for. If you want to find all Windows 2003 Standard VMs, type
Figure 13.16 shows the output of find ./ -type f -name "*.vmx" -exec egrep -H -i "WinNetStandard" {} \;. Table 13.2 contains additional values you can use to query to your server to determine which operating systems are running as virtual machines on your ESX Server. Figure 13-16. Output of the find and egrep Commands Searching All .vmx Files for the Pattern "WinNetStandard"
13.2.5. Editing Text FilesYou may find it necessary to edit text files on your server in order to change configurations or create scripts. The wimpy and inefficient way to edit a text file would be to create them in Windows and run a DOS-to-UNIX file conversion (just to avoid any issues with file formats) and then copy them to the Service Console via an scp/winscp client. If you want to feel more manly/womanly go ahead and learn vi or nano and edit the files on the Service Console directly. It will be worth it in the long run. You'll generally need to edit configuration files that specify network settings such as host name, IP address, gateway address, or that are used for creating and editing scripts. The two text editors available by default are vi and nano. Each can be used to edit text files but have different capabilities. You should try both editors to see which one fits your needs. 13.2.5.1. Editing with viTo launch vi, type vi at the command prompt. The vi editor has three modes of operation: command mode, edit mode, and ex mode. We'll cover only the most basic functions of vi for editing files. Typing vi at the command prompt gives you the options shown in Figure 13.17 Figure 13-17. Options for viIf you need to edit an existing file, type
If you want to create a new blank file to edit, type
Whenever you open a new or existing file for editing, you are in command mode. To actually start editing text, you'll need to enter edit mode. This can be accomplished by typing i or pressing Insert on your keyboard. This allows you to add text or edit existing text in your file. Once you're done editing your file, you can get back to command mode by pressing the Esc key on your keyboard. If you'd like to save the file and exit, you'll need to enter ex mode. You can do this by typing :wq (see Figure 13.18). If you'd like to simply exit the file without saving, just type q!; otherwise, you'll be prompted to save if changes have been made to the file. If no edits have been made to the file, simply typing :q will be sufficient. Figure 13-18. Entering Ex Mode13.2.5.2. Editing with NanoThe other text editor you have available is nano. Make sure when you edit configuration files with nano that you use nanow. This controls the way nano wraps long lines and can result in syntax errors if not specified when editing configuration files. The nano editing screen is shown in Figure 13.19 To give you an example from the screen in Figure 13.19, use Ctrl + x to exit the editor or Ctrl + k to cut text. The keyboard functions available for editing your file are shown in Table 13.3. Figure 13-19. The Nano Editing Screen
In nano, when you want to copy text, it copies to the whole line by default from the start of the cursor to the end of the line. In Windows, you have the ability to press the Shift key and use the arrow keys to highlight specific text you want to block for copying and pasting. In nano, place the cursor at the starting point of the text you want to copy and then press Ctrl + 6 to set the mark. From there, use the arrow keys to highlight the text you want to copy. The example in Figure 13.20 shows what it will look like in the nano editor. Press Ctrl + 6 again to UNset the mark. Once you've marked your text, you can use Ctrl + K to cut the text and then move the cursor to the point you want to paste the text and then press Ctrl + U to uncut (paste) the text. Figure 13-20. Highlighting Text to Copy in the Nano Editor13.2.6. Managing Users and GroupsYou can create users and groups from the ESX Server MUI, but you may find it necessary to create or modify users and groups from the command line. One example is the integration of ESX Server authentication with Active Directory. If you create the user account in the MUI, it forces you to create a password which is stored locally and causes ESX to authenticate locally versus going to Active Directory to perform Kerberos authentication. The adduser program allows you to create user accounts without specifying the password (not blank, just no local password), which forces the authentication mechanism to look for alternate authentication sources if you have configured the appropriate Pluggable Authentication Module (PAM). To create a new user account, type
The following's an example for adduser:
Figure 13.21 shows the options available for the adduser program. Figure 13-21. Options for the adduser ProgramTo delete a user from your server, type
To add new groups to your ESX Server, type
To modify existing group memberships, type
13.2.7. Managing Directory and File Ownership and PermissionsYou may need to change the permissions on a file or directory to allow other groups access to the file or directory. One example of this would be to allow members of a group the ability to manage virtual machines. You would need to grant permissions on the directory and VM configuration files for them to have access. To change the permissions settings on a file or directory use the chmod command. 13.2.7.1. Changing Permissions on a File or DirectoryFor permissions, you have three different entities you can assign permissions to. Permissions can be set for the user who owns the file, the permissions for the group that is affiliated with the file and other (that is, everyone else). The options are listed as follows:
You also have three different permission attributes you can assign to a file or directory and they can be assigned to the owner, group, or other. The permissions are r=read, w=write, and x=execute. Use the chmod command to set permissions on files and directories. Each entity is identified as follows: Let's say you wish to grant the owner of the file execute permissions on the file and you want to remove the read permission for other. To change the permissions on a file, type
Use either u, g, or o to specify who you are changing permissions for and then you use + or - along with r, w, or x to add or remove that specific permission. In another example, we could add the execute permission for the owner of the file, the group, and remove the read permission for other. This is shown in Figure 13.22 To set the permissions specified in the previous paragraph, type
Figure 13-22. Permissions Changes on a FileThe permissions are now changed for the file vmwareiscool. Originally, the permissions wererw-rw-r--. After the command chmod g+x,u+x,o-r vmwareiscool, the permissions arerwxrwx---, as shown in Figure 13.22 Note: If you decide to create a shell script (similar to a batch file in DOS/Windows), you'll need to set the execute permissions in order for it to run the commands within the file. 13.2.7.2. Changing File or Directory Owner or Group AffiliationTo change the owner on a file or directory, use the chown command. To change the group affiliation, use the chgrp command. Figure 13.23 shows a directory created by the root user, who is the owner by default, while the group root is the default group affiliated with the directory. Figure 13-23. Directory Created by a Root UserTo change the owner of the directory and all files and subdirectories within it, type
You can see an example in Figure 13.24, which shows that the ownership of the testdir has been changed to the user vcadmin. Figure 13-24. Changing the Ownership of testdirNow, the user vmadmin is the owner of the testdir directory as well as all files and directories below it. To change the group affiliation of a file or directory, type
See Figure 13.25 for the results of the command. Figure 13-25. Changing the Group Affiliation of a File or DirectoryNow, the group affiliation is changed from root to vmadmin, as reflected in Figure 13.25 13.2.8. Archiving and Compressing Files and DirectoriesThe following utilities are used to archive files, to compress files as well as extract files from archives, and to uncompress zipped files. If you need to create an archive containing a group of files, type
Figure 13.26 shows an example of archiving all vm configuration files stored under the /home/vmware directory to a directory named /home/archives. You end up with a file named vm_configs.tar in the /home/archives directory. Figure 13-26. Archiving vm Configuration FilesIf you need to see the contents of a tar file, type
If you need to compress a file using gzip or bzip2, type
The following command archives the vm_configs.tar file created in the previous example:
You then end up with a file named vm_configs.tar.gz. You can also use the bzip2 tool to compress files by typing the following:
This results in a file named vm_configs.tar.bz2. To uncompress files, use gunzip or bunzip2, depending on which utility was used to compress the file. This will be evident from the file extension. Files compressed with gzip will have a .gz extension, whereas a file compressed with bzip2 will have a .bz2 extension. If you need to uncompress a file compressed with gzip, type
If you need to uncompress a file compressed with bzip2, type
If you need to extract archived files that were compressed with gzip, type
If you need to extract archived files that were compressed with bzip2, type
In you need to extract files from an archive, type
The preceding command extracts the archive into your current directory. You may want to create a new directory or specify the path to extract the archive to in the command line. If you want to specify a different directory to extract the archive to, type
This command extracts the archive into the /targetdirectory specified on the command line. The C option is needed to specify a directory name other than the current directory. To list the contents of files that are compressed with either gzip or bzip2, use the following commands: For an archive compressed with gzip, type
For an archive compressed with bzip2, type
13.2.9. Mounting ISO Images, CD-ROMs, and Virtual Disks in the Service ConsoleThe Service Console gives you the ability to mount several different devices as well as ISO images and virtual disks so you can access those file systems. This section covers the commands used to mount these file systems so you can gain access to the files. To mount a CD-ROM on your ESX Server Service Console, type
To unmount a CD-ROM, type
To mount a virtual disk in the Service Console, type
|